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
57static 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.  */
89static unsigned int marked_pr_dependency = 0;
90
91#endif /* OBJ_ELF */
92
93enum arm_float_abi
94{
95  ARM_FLOAT_ABI_HARD,
96  ARM_FLOAT_ABI_SOFTFP,
97  ARM_FLOAT_ABI_SOFT
98};
99
100/* Types of processor to assemble for.	*/
101#ifndef CPU_DEFAULT
102#if defined __XSCALE__
103#define CPU_DEFAULT	ARM_ARCH_XSCALE
104#else
105#if defined __thumb__
106#define CPU_DEFAULT	ARM_ARCH_V5T
107#endif
108#endif
109#endif
110
111#ifndef FPU_DEFAULT
112# ifdef TE_LINUX
113#  define FPU_DEFAULT FPU_ARCH_FPA
114# elif defined (TE_NetBSD) || defined (TE_OpenBSD)
115#  ifdef OBJ_ELF
116#   define FPU_DEFAULT FPU_ARCH_VFP	/* Soft-float, but VFP order.  */
117#  else
118    /* Legacy a.out format.  */
119#   define FPU_DEFAULT FPU_ARCH_FPA	/* Soft-float, but FPA order.  */
120#  endif
121# elif defined (TE_VXWORKS)
122#  define FPU_DEFAULT FPU_ARCH_VFP	/* Soft-float, VFP order.  */
123# else
124   /* For backwards compatibility, default to FPA.  */
125#  define FPU_DEFAULT FPU_ARCH_FPA
126# endif
127#endif /* ifndef FPU_DEFAULT */
128
129#define streq(a, b)	      (strcmp (a, b) == 0)
130
131static arm_feature_set cpu_variant;
132static arm_feature_set arm_arch_used;
133static arm_feature_set thumb_arch_used;
134
135/* Flags stored in private area of BFD structure.  */
136static int uses_apcs_26	     = FALSE;
137static int atpcs	     = FALSE;
138static int support_interwork = FALSE;
139static int uses_apcs_float   = FALSE;
140static int pic_code	     = FALSE;
141
142/* Variables that we set while parsing command-line options.  Once all
143   options have been read we re-process these values to set the real
144   assembly flags.  */
145static const arm_feature_set *legacy_cpu = NULL;
146static const arm_feature_set *legacy_fpu = NULL;
147
148static const arm_feature_set *mcpu_cpu_opt = NULL;
149static const arm_feature_set *mcpu_fpu_opt = NULL;
150static const arm_feature_set *march_cpu_opt = NULL;
151static const arm_feature_set *march_fpu_opt = NULL;
152static const arm_feature_set *mfpu_opt = NULL;
153
154/* Constants for known architecture features.  */
155static const arm_feature_set fpu_default = FPU_DEFAULT;
156static const arm_feature_set fpu_arch_vfp_v1 = FPU_ARCH_VFP_V1;
157static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
158static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
159static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
160static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
161static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
162
163#ifdef CPU_DEFAULT
164static const arm_feature_set cpu_default = CPU_DEFAULT;
165#endif
166
167static const arm_feature_set arm_ext_v1 = ARM_FEATURE (ARM_EXT_V1, 0);
168static const arm_feature_set arm_ext_v2 = ARM_FEATURE (ARM_EXT_V1, 0);
169static const arm_feature_set arm_ext_v2s = ARM_FEATURE (ARM_EXT_V2S, 0);
170static const arm_feature_set arm_ext_v3 = ARM_FEATURE (ARM_EXT_V3, 0);
171static const arm_feature_set arm_ext_v3m = ARM_FEATURE (ARM_EXT_V3M, 0);
172static const arm_feature_set arm_ext_v4 = ARM_FEATURE (ARM_EXT_V4, 0);
173static const arm_feature_set arm_ext_v4t = ARM_FEATURE (ARM_EXT_V4T, 0);
174static const arm_feature_set arm_ext_v5 = ARM_FEATURE (ARM_EXT_V5, 0);
175static const arm_feature_set arm_ext_v4t_5 =
176  ARM_FEATURE (ARM_EXT_V4T | ARM_EXT_V5, 0);
177static const arm_feature_set arm_ext_v5t = ARM_FEATURE (ARM_EXT_V5T, 0);
178static const arm_feature_set arm_ext_v5e = ARM_FEATURE (ARM_EXT_V5E, 0);
179static const arm_feature_set arm_ext_v5exp = ARM_FEATURE (ARM_EXT_V5ExP, 0);
180static const arm_feature_set arm_ext_v5j = ARM_FEATURE (ARM_EXT_V5J, 0);
181static const arm_feature_set arm_ext_v6 = ARM_FEATURE (ARM_EXT_V6, 0);
182static const arm_feature_set arm_ext_v6k = ARM_FEATURE (ARM_EXT_V6K, 0);
183static const arm_feature_set arm_ext_v6z = ARM_FEATURE (ARM_EXT_V6Z, 0);
184static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE (ARM_EXT_V6T2, 0);
185static const arm_feature_set arm_ext_v6_notm = ARM_FEATURE (ARM_EXT_V6_NOTM, 0);
186static const arm_feature_set arm_ext_div = ARM_FEATURE (ARM_EXT_DIV, 0);
187static const arm_feature_set arm_ext_v7 = ARM_FEATURE (ARM_EXT_V7, 0);
188static const arm_feature_set arm_ext_v7a = ARM_FEATURE (ARM_EXT_V7A, 0);
189static const arm_feature_set arm_ext_v7r = ARM_FEATURE (ARM_EXT_V7R, 0);
190static const arm_feature_set arm_ext_v7m = ARM_FEATURE (ARM_EXT_V7M, 0);
191
192static const arm_feature_set arm_arch_any = ARM_ANY;
193static const arm_feature_set arm_arch_full = ARM_FEATURE (-1, -1);
194static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
195static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
196
197static const arm_feature_set arm_cext_iwmmxt =
198  ARM_FEATURE (0, ARM_CEXT_IWMMXT);
199static const arm_feature_set arm_cext_xscale =
200  ARM_FEATURE (0, ARM_CEXT_XSCALE);
201static const arm_feature_set arm_cext_maverick =
202  ARM_FEATURE (0, ARM_CEXT_MAVERICK);
203static const arm_feature_set fpu_fpa_ext_v1 = ARM_FEATURE (0, FPU_FPA_EXT_V1);
204static const arm_feature_set fpu_fpa_ext_v2 = ARM_FEATURE (0, FPU_FPA_EXT_V2);
205static const arm_feature_set fpu_vfp_ext_v1xd =
206  ARM_FEATURE (0, FPU_VFP_EXT_V1xD);
207static const arm_feature_set fpu_vfp_ext_v1 = ARM_FEATURE (0, FPU_VFP_EXT_V1);
208static const arm_feature_set fpu_vfp_ext_v2 = ARM_FEATURE (0, FPU_VFP_EXT_V2);
209
210static int mfloat_abi_opt = -1;
211/* Record user cpu selection for object attributes.  */
212static arm_feature_set selected_cpu = ARM_ARCH_NONE;
213/* Must be long enough to hold any of the names in arm_cpus.  */
214static char selected_cpu_name[16];
215#ifdef OBJ_ELF
216# ifdef EABI_DEFAULT
217static int meabi_flags = EABI_DEFAULT;
218# else
219static int meabi_flags = EF_ARM_EABI_UNKNOWN;
220# endif
221#endif
222
223#ifdef OBJ_ELF
224/* Pre-defined "_GLOBAL_OFFSET_TABLE_"	*/
225symbolS * GOT_symbol;
226#endif
227
228/* 0: assemble for ARM,
229   1: assemble for Thumb,
230   2: assemble for Thumb even though target CPU does not support thumb
231      instructions.  */
232static int thumb_mode = 0;
233
234/* If unified_syntax is true, we are processing the new unified
235   ARM/Thumb syntax.  Important differences from the old ARM mode:
236
237     - Immediate operands do not require a # prefix.
238     - Conditional affixes always appear at the end of the
239       instruction.  (For backward compatibility, those instructions
240       that formerly had them in the middle, continue to accept them
241       there.)
242     - The IT instruction may appear, and if it does is validated
243       against subsequent conditional affixes.  It does not generate
244       machine code.
245
246   Important differences from the old Thumb mode:
247
248     - Immediate operands do not require a # prefix.
249     - Most of the V6T2 instructions are only available in unified mode.
250     - The .N and .W suffixes are recognized and honored (it is an error
251       if they cannot be honored).
252     - All instructions set the flags if and only if they have an 's' affix.
253     - Conditional affixes may be used.  They are validated against
254       preceding IT instructions.  Unlike ARM mode, you cannot use a
255       conditional affix except in the scope of an IT instruction.  */
256
257static bfd_boolean unified_syntax = FALSE;
258
259struct arm_it
260{
261  const char *	error;
262  unsigned long instruction;
263  int		size;
264  int		size_req;
265  int		cond;
266  /* Set to the opcode if the instruction needs relaxation.
267     Zero if the instruction is not relaxed.  */
268  unsigned long	relax;
269  struct
270  {
271    bfd_reloc_code_real_type type;
272    expressionS		     exp;
273    int			     pc_rel;
274  } reloc;
275
276  struct
277  {
278    unsigned reg;
279    signed int imm;
280    unsigned present	: 1;  /* Operand present.  */
281    unsigned isreg	: 1;  /* Operand was a register.  */
282    unsigned immisreg	: 1;  /* .imm field is a second register.  */
283    unsigned hasreloc	: 1;  /* Operand has relocation suffix.  */
284    unsigned writeback	: 1;  /* Operand has trailing !  */
285    unsigned preind	: 1;  /* Preindexed address.  */
286    unsigned postind	: 1;  /* Postindexed address.  */
287    unsigned negative	: 1;  /* Index register was negated.  */
288    unsigned shifted	: 1;  /* Shift applied to operation.  */
289    unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
290  } operands[6];
291};
292
293static struct arm_it inst;
294
295#define NUM_FLOAT_VALS 8
296
297const char * fp_const[] =
298{
299  "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
300};
301
302/* Number of littlenums required to hold an extended precision number.	*/
303#define MAX_LITTLENUMS 6
304
305LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
306
307#define FAIL	(-1)
308#define SUCCESS (0)
309
310#define SUFF_S 1
311#define SUFF_D 2
312#define SUFF_E 3
313#define SUFF_P 4
314
315#define CP_T_X	 0x00008000
316#define CP_T_Y	 0x00400000
317
318#define CONDS_BIT	 0x00100000
319#define LOAD_BIT	 0x00100000
320
321#define DOUBLE_LOAD_FLAG 0x00000001
322
323struct asm_cond
324{
325  const char *	template;
326  unsigned long value;
327};
328
329#define COND_ALWAYS 0xE
330
331struct asm_psr
332{
333  const char *template;
334  unsigned long field;
335};
336
337struct asm_barrier_opt
338{
339  const char *template;
340  unsigned long value;
341};
342
343/* The bit that distinguishes CPSR and SPSR.  */
344#define SPSR_BIT   (1 << 22)
345
346/* The individual PSR flag bits.  */
347#define PSR_c	(1 << 16)
348#define PSR_x	(1 << 17)
349#define PSR_s	(1 << 18)
350#define PSR_f	(1 << 19)
351
352struct reloc_entry
353{
354  char *name;
355  bfd_reloc_code_real_type reloc;
356};
357
358enum vfp_sp_reg_pos
359{
360  VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn
361};
362
363enum vfp_ldstm_type
364{
365  VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
366};
367
368/* ARM register categories.  This includes coprocessor numbers and various
369   architecture extensions' registers.	*/
370enum arm_reg_type
371{
372  REG_TYPE_RN,
373  REG_TYPE_CP,
374  REG_TYPE_CN,
375  REG_TYPE_FN,
376  REG_TYPE_VFS,
377  REG_TYPE_VFD,
378  REG_TYPE_VFC,
379  REG_TYPE_MVF,
380  REG_TYPE_MVD,
381  REG_TYPE_MVFX,
382  REG_TYPE_MVDX,
383  REG_TYPE_MVAX,
384  REG_TYPE_DSPSC,
385  REG_TYPE_MMXWR,
386  REG_TYPE_MMXWC,
387  REG_TYPE_MMXWCG,
388  REG_TYPE_XSCALE,
389};
390
391/* Structure for a hash table entry for a register.  */
392struct reg_entry
393{
394  const char   *name;
395  unsigned char number;
396  unsigned char type;
397  unsigned char builtin;
398};
399
400/* Diagnostics used when we don't get a register of the expected type.	*/
401const char *const reg_expected_msgs[] =
402{
403  N_("ARM register expected"),
404  N_("bad or missing co-processor number"),
405  N_("co-processor register expected"),
406  N_("FPA register expected"),
407  N_("VFP single precision register expected"),
408  N_("VFP double precision register expected"),
409  N_("VFP system register expected"),
410  N_("Maverick MVF register expected"),
411  N_("Maverick MVD register expected"),
412  N_("Maverick MVFX register expected"),
413  N_("Maverick MVDX register expected"),
414  N_("Maverick MVAX register expected"),
415  N_("Maverick DSPSC register expected"),
416  N_("iWMMXt data register expected"),
417  N_("iWMMXt control register expected"),
418  N_("iWMMXt scalar register expected"),
419  N_("XScale accumulator register expected"),
420};
421
422/* Some well known registers that we refer to directly elsewhere.  */
423#define REG_SP	13
424#define REG_LR	14
425#define REG_PC	15
426
427/* ARM instructions take 4bytes in the object file, Thumb instructions
428   take 2:  */
429#define INSN_SIZE	4
430
431struct asm_opcode
432{
433  /* Basic string to match.  */
434  const char *template;
435
436  /* Parameters to instruction.	 */
437  unsigned char operands[8];
438
439  /* Conditional tag - see opcode_lookup.  */
440  unsigned int tag : 4;
441
442  /* Basic instruction code.  */
443  unsigned int avalue : 28;
444
445  /* Thumb-format instruction code.  */
446  unsigned int tvalue;
447
448  /* Which architecture variant provides this instruction.  */
449  const arm_feature_set *avariant;
450  const arm_feature_set *tvariant;
451
452  /* Function to call to encode instruction in ARM format.  */
453  void (* aencode) (void);
454
455  /* Function to call to encode instruction in Thumb format.  */
456  void (* tencode) (void);
457};
458
459/* Defines for various bits that we will want to toggle.  */
460#define INST_IMMEDIATE	0x02000000
461#define OFFSET_REG	0x02000000
462#define HWOFFSET_IMM	0x00400000
463#define SHIFT_BY_REG	0x00000010
464#define PRE_INDEX	0x01000000
465#define INDEX_UP	0x00800000
466#define WRITE_BACK	0x00200000
467#define LDM_TYPE_2_OR_3	0x00400000
468
469#define LITERAL_MASK	0xf000f000
470#define OPCODE_MASK	0xfe1fffff
471#define V4_STR_BIT	0x00000020
472
473#define DATA_OP_SHIFT	21
474
475#define T2_OPCODE_MASK	0xfe1fffff
476#define T2_DATA_OP_SHIFT 21
477
478/* Codes to distinguish the arithmetic instructions.  */
479#define OPCODE_AND	0
480#define OPCODE_EOR	1
481#define OPCODE_SUB	2
482#define OPCODE_RSB	3
483#define OPCODE_ADD	4
484#define OPCODE_ADC	5
485#define OPCODE_SBC	6
486#define OPCODE_RSC	7
487#define OPCODE_TST	8
488#define OPCODE_TEQ	9
489#define OPCODE_CMP	10
490#define OPCODE_CMN	11
491#define OPCODE_ORR	12
492#define OPCODE_MOV	13
493#define OPCODE_BIC	14
494#define OPCODE_MVN	15
495
496#define T2_OPCODE_AND	0
497#define T2_OPCODE_BIC	1
498#define T2_OPCODE_ORR	2
499#define T2_OPCODE_ORN	3
500#define T2_OPCODE_EOR	4
501#define T2_OPCODE_ADD	8
502#define T2_OPCODE_ADC	10
503#define T2_OPCODE_SBC	11
504#define T2_OPCODE_SUB	13
505#define T2_OPCODE_RSB	14
506
507#define T_OPCODE_MUL 0x4340
508#define T_OPCODE_TST 0x4200
509#define T_OPCODE_CMN 0x42c0
510#define T_OPCODE_NEG 0x4240
511#define T_OPCODE_MVN 0x43c0
512
513#define T_OPCODE_ADD_R3	0x1800
514#define T_OPCODE_SUB_R3 0x1a00
515#define T_OPCODE_ADD_HI 0x4400
516#define T_OPCODE_ADD_ST 0xb000
517#define T_OPCODE_SUB_ST 0xb080
518#define T_OPCODE_ADD_SP 0xa800
519#define T_OPCODE_ADD_PC 0xa000
520#define T_OPCODE_ADD_I8 0x3000
521#define T_OPCODE_SUB_I8 0x3800
522#define T_OPCODE_ADD_I3 0x1c00
523#define T_OPCODE_SUB_I3 0x1e00
524
525#define T_OPCODE_ASR_R	0x4100
526#define T_OPCODE_LSL_R	0x4080
527#define T_OPCODE_LSR_R	0x40c0
528#define T_OPCODE_ROR_R	0x41c0
529#define T_OPCODE_ASR_I	0x1000
530#define T_OPCODE_LSL_I	0x0000
531#define T_OPCODE_LSR_I	0x0800
532
533#define T_OPCODE_MOV_I8	0x2000
534#define T_OPCODE_CMP_I8 0x2800
535#define T_OPCODE_CMP_LR 0x4280
536#define T_OPCODE_MOV_HR 0x4600
537#define T_OPCODE_CMP_HR 0x4500
538
539#define T_OPCODE_LDR_PC 0x4800
540#define T_OPCODE_LDR_SP 0x9800
541#define T_OPCODE_STR_SP 0x9000
542#define T_OPCODE_LDR_IW 0x6800
543#define T_OPCODE_STR_IW 0x6000
544#define T_OPCODE_LDR_IH 0x8800
545#define T_OPCODE_STR_IH 0x8000
546#define T_OPCODE_LDR_IB 0x7800
547#define T_OPCODE_STR_IB 0x7000
548#define T_OPCODE_LDR_RW 0x5800
549#define T_OPCODE_STR_RW 0x5000
550#define T_OPCODE_LDR_RH 0x5a00
551#define T_OPCODE_STR_RH 0x5200
552#define T_OPCODE_LDR_RB 0x5c00
553#define T_OPCODE_STR_RB 0x5400
554
555#define T_OPCODE_PUSH	0xb400
556#define T_OPCODE_POP	0xbc00
557
558#define T_OPCODE_BRANCH 0xe000
559
560#define THUMB_SIZE	2	/* Size of thumb instruction.  */
561#define THUMB_PP_PC_LR 0x0100
562#define THUMB_LOAD_BIT 0x0800
563#define THUMB2_LOAD_BIT 0x00100000
564
565#define BAD_ARGS	_("bad arguments to instruction")
566#define BAD_PC		_("r15 not allowed here")
567#define BAD_COND	_("instruction cannot be conditional")
568#define BAD_OVERLAP	_("registers may not be the same")
569#define BAD_HIREG	_("lo register required")
570#define BAD_THUMB32	_("instruction not supported in Thumb16 mode")
571#define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
572#define BAD_BRANCH	_("branch must be last instruction in IT block")
573#define BAD_NOT_IT	_("instruction not allowed in IT block")
574
575static struct hash_control *arm_ops_hsh;
576static struct hash_control *arm_cond_hsh;
577static struct hash_control *arm_shift_hsh;
578static struct hash_control *arm_psr_hsh;
579static struct hash_control *arm_v7m_psr_hsh;
580static struct hash_control *arm_reg_hsh;
581static struct hash_control *arm_reloc_hsh;
582static struct hash_control *arm_barrier_opt_hsh;
583
584/* Stuff needed to resolve the label ambiguity
585   As:
586     ...
587     label:   <insn>
588   may differ from:
589     ...
590     label:
591	      <insn>
592*/
593
594symbolS *  last_label_seen;
595static int label_is_thumb_function_name = FALSE;
596
597/* Literal pool structure.  Held on a per-section
598   and per-sub-section basis.  */
599
600#define MAX_LITERAL_POOL_SIZE 1024
601typedef struct literal_pool
602{
603  expressionS	 literals [MAX_LITERAL_POOL_SIZE];
604  unsigned int	 next_free_entry;
605  unsigned int	 id;
606  symbolS *	 symbol;
607  segT		 section;
608  subsegT	 sub_section;
609  struct literal_pool * next;
610} literal_pool;
611
612/* Pointer to a linked list of literal pools.  */
613literal_pool * list_of_pools = NULL;
614
615/* State variables for IT block handling.  */
616static bfd_boolean current_it_mask = 0;
617static int current_cc;
618
619
620/* Pure syntax.	 */
621
622/* This array holds the chars that always start a comment.  If the
623   pre-processor is disabled, these aren't very useful.	 */
624const char comment_chars[] = "@";
625
626/* This array holds the chars that only start a comment at the beginning of
627   a line.  If the line seems to have the form '# 123 filename'
628   .line and .file directives will appear in the pre-processed output.	*/
629/* Note that input_file.c hand checks for '#' at the beginning of the
630   first line of the input file.  This is because the compiler outputs
631   #NO_APP at the beginning of its output.  */
632/* Also note that comments like this one will always work.  */
633const char line_comment_chars[] = "#";
634
635const char line_separator_chars[] = ";";
636
637/* Chars that can be used to separate mant
638   from exp in floating point numbers.	*/
639const char EXP_CHARS[] = "eE";
640
641/* Chars that mean this number is a floating point constant.  */
642/* As in 0f12.456  */
643/* or	 0d1.2345e12  */
644
645const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
646
647/* Prefix characters that indicate the start of an immediate
648   value.  */
649#define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
650
651/* Separator character handling.  */
652
653#define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
654
655static inline int
656skip_past_char (char ** str, char c)
657{
658  if (**str == c)
659    {
660      (*str)++;
661      return SUCCESS;
662    }
663  else
664    return FAIL;
665}
666#define skip_past_comma(str) skip_past_char (str, ',')
667
668/* Arithmetic expressions (possibly involving symbols).	 */
669
670/* Return TRUE if anything in the expression is a bignum.  */
671
672static int
673walk_no_bignums (symbolS * sp)
674{
675  if (symbol_get_value_expression (sp)->X_op == O_big)
676    return 1;
677
678  if (symbol_get_value_expression (sp)->X_add_symbol)
679    {
680      return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
681	      || (symbol_get_value_expression (sp)->X_op_symbol
682		  && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
683    }
684
685  return 0;
686}
687
688static int in_my_get_expression = 0;
689
690/* Third argument to my_get_expression.	 */
691#define GE_NO_PREFIX 0
692#define GE_IMM_PREFIX 1
693#define GE_OPT_PREFIX 2
694
695static int
696my_get_expression (expressionS * ep, char ** str, int prefix_mode)
697{
698  char * save_in;
699  segT	 seg;
700
701  /* In unified syntax, all prefixes are optional.  */
702  if (unified_syntax)
703    prefix_mode = GE_OPT_PREFIX;
704
705  switch (prefix_mode)
706    {
707    case GE_NO_PREFIX: break;
708    case GE_IMM_PREFIX:
709      if (!is_immediate_prefix (**str))
710	{
711	  inst.error = _("immediate expression requires a # prefix");
712	  return FAIL;
713	}
714      (*str)++;
715      break;
716    case GE_OPT_PREFIX:
717      if (is_immediate_prefix (**str))
718	(*str)++;
719      break;
720    default: abort ();
721    }
722
723  memset (ep, 0, sizeof (expressionS));
724
725  save_in = input_line_pointer;
726  input_line_pointer = *str;
727  in_my_get_expression = 1;
728  seg = expression (ep);
729  in_my_get_expression = 0;
730
731  if (ep->X_op == O_illegal)
732    {
733      /* We found a bad expression in md_operand().  */
734      *str = input_line_pointer;
735      input_line_pointer = save_in;
736      if (inst.error == NULL)
737	inst.error = _("bad expression");
738      return 1;
739    }
740
741#ifdef OBJ_AOUT
742  if (seg != absolute_section
743      && seg != text_section
744      && seg != data_section
745      && seg != bss_section
746      && seg != undefined_section)
747    {
748      inst.error = _("bad segment");
749      *str = input_line_pointer;
750      input_line_pointer = save_in;
751      return 1;
752    }
753#endif
754
755  /* Get rid of any bignums now, so that we don't generate an error for which
756     we can't establish a line number later on.	 Big numbers are never valid
757     in instructions, which is where this routine is always called.  */
758  if (ep->X_op == O_big
759      || (ep->X_add_symbol
760	  && (walk_no_bignums (ep->X_add_symbol)
761	      || (ep->X_op_symbol
762		  && walk_no_bignums (ep->X_op_symbol)))))
763    {
764      inst.error = _("invalid constant");
765      *str = input_line_pointer;
766      input_line_pointer = save_in;
767      return 1;
768    }
769
770  *str = input_line_pointer;
771  input_line_pointer = save_in;
772  return 0;
773}
774
775/* Turn a string in input_line_pointer into a floating point constant
776   of type TYPE, and store the appropriate bytes in *LITP.  The number
777   of LITTLENUMS emitted is stored in *SIZEP.  An error message is
778   returned, or NULL on OK.
779
780   Note that fp constants aren't represent in the normal way on the ARM.
781   In big endian mode, things are as expected.	However, in little endian
782   mode fp constants are big-endian word-wise, and little-endian byte-wise
783   within the words.  For example, (double) 1.1 in big endian mode is
784   the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
785   the byte sequence 99 99 f1 3f 9a 99 99 99.
786
787   ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
788
789char *
790md_atof (int type, char * litP, int * sizeP)
791{
792  int prec;
793  LITTLENUM_TYPE words[MAX_LITTLENUMS];
794  char *t;
795  int i;
796
797  switch (type)
798    {
799    case 'f':
800    case 'F':
801    case 's':
802    case 'S':
803      prec = 2;
804      break;
805
806    case 'd':
807    case 'D':
808    case 'r':
809    case 'R':
810      prec = 4;
811      break;
812
813    case 'x':
814    case 'X':
815      prec = 6;
816      break;
817
818    case 'p':
819    case 'P':
820      prec = 6;
821      break;
822
823    default:
824      *sizeP = 0;
825      return _("bad call to MD_ATOF()");
826    }
827
828  t = atof_ieee (input_line_pointer, type, words);
829  if (t)
830    input_line_pointer = t;
831  *sizeP = prec * 2;
832
833  if (target_big_endian)
834    {
835      for (i = 0; i < prec; i++)
836	{
837	  md_number_to_chars (litP, (valueT) words[i], 2);
838	  litP += 2;
839	}
840    }
841  else
842    {
843      if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
844	for (i = prec - 1; i >= 0; i--)
845	  {
846	    md_number_to_chars (litP, (valueT) words[i], 2);
847	    litP += 2;
848	  }
849      else
850	/* For a 4 byte float the order of elements in `words' is 1 0.
851	   For an 8 byte float the order is 1 0 3 2.  */
852	for (i = 0; i < prec; i += 2)
853	  {
854	    md_number_to_chars (litP, (valueT) words[i + 1], 2);
855	    md_number_to_chars (litP + 2, (valueT) words[i], 2);
856	    litP += 4;
857	  }
858    }
859
860  return 0;
861}
862
863/* We handle all bad expressions here, so that we can report the faulty
864   instruction in the error message.  */
865void
866md_operand (expressionS * expr)
867{
868  if (in_my_get_expression)
869    expr->X_op = O_illegal;
870}
871
872/* Immediate values.  */
873
874/* Generic immediate-value read function for use in directives.
875   Accepts anything that 'expression' can fold to a constant.
876   *val receives the number.  */
877#ifdef OBJ_ELF
878static int
879immediate_for_directive (int *val)
880{
881  expressionS exp;
882  exp.X_op = O_illegal;
883
884  if (is_immediate_prefix (*input_line_pointer))
885    {
886      input_line_pointer++;
887      expression (&exp);
888    }
889
890  if (exp.X_op != O_constant)
891    {
892      as_bad (_("expected #constant"));
893      ignore_rest_of_line ();
894      return FAIL;
895    }
896  *val = exp.X_add_number;
897  return SUCCESS;
898}
899#endif
900
901/* Register parsing.  */
902
903/* Generic register parser.  CCP points to what should be the
904   beginning of a register name.  If it is indeed a valid register
905   name, advance CCP over it and return the reg_entry structure;
906   otherwise return NULL.  Does not issue diagnostics.	*/
907
908static struct reg_entry *
909arm_reg_parse_multi (char **ccp)
910{
911  char *start = *ccp;
912  char *p;
913  struct reg_entry *reg;
914
915#ifdef REGISTER_PREFIX
916  if (*start != REGISTER_PREFIX)
917    return NULL;
918  start++;
919#endif
920#ifdef OPTIONAL_REGISTER_PREFIX
921  if (*start == OPTIONAL_REGISTER_PREFIX)
922    start++;
923#endif
924
925  p = start;
926  if (!ISALPHA (*p) || !is_name_beginner (*p))
927    return NULL;
928
929  do
930    p++;
931  while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
932
933  reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
934
935  if (!reg)
936    return NULL;
937
938  *ccp = p;
939  return reg;
940}
941
942/* As above, but the register must be of type TYPE, and the return
943   value is the register number or FAIL.  */
944
945static int
946arm_reg_parse (char **ccp, enum arm_reg_type type)
947{
948  char *start = *ccp;
949  struct reg_entry *reg = arm_reg_parse_multi (ccp);
950
951  if (reg && reg->type == type)
952    return reg->number;
953
954  /* Alternative syntaxes are accepted for a few register classes.  */
955  switch (type)
956    {
957    case REG_TYPE_MVF:
958    case REG_TYPE_MVD:
959    case REG_TYPE_MVFX:
960    case REG_TYPE_MVDX:
961      /* Generic coprocessor register names are allowed for these.  */
962      if (reg && reg->type == REG_TYPE_CN)
963	return reg->number;
964      break;
965
966    case REG_TYPE_CP:
967      /* For backward compatibility, a bare number is valid here.  */
968      {
969	unsigned long processor = strtoul (start, ccp, 10);
970	if (*ccp != start && processor <= 15)
971	  return processor;
972      }
973
974    case REG_TYPE_MMXWC:
975      /* WC includes WCG.  ??? I'm not sure this is true for all
976	 instructions that take WC registers.  */
977      if (reg && reg->type == REG_TYPE_MMXWCG)
978	return reg->number;
979      break;
980
981    default:
982      break;
983    }
984
985  *ccp = start;
986  return FAIL;
987}
988
989/* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
990static long
991parse_reg_list (char ** strp)
992{
993  char * str = * strp;
994  long	 range = 0;
995  int	 another_range;
996
997  /* We come back here if we get ranges concatenated by '+' or '|'.  */
998  do
999    {
1000      another_range = 0;
1001
1002      if (*str == '{')
1003	{
1004	  int in_range = 0;
1005	  int cur_reg = -1;
1006
1007	  str++;
1008	  do
1009	    {
1010	      int reg;
1011
1012	      if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1013		{
1014		  inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
1015		  return FAIL;
1016		}
1017
1018	      if (in_range)
1019		{
1020		  int i;
1021
1022		  if (reg <= cur_reg)
1023		    {
1024		      inst.error = _("bad range in register list");
1025		      return FAIL;
1026		    }
1027
1028		  for (i = cur_reg + 1; i < reg; i++)
1029		    {
1030		      if (range & (1 << i))
1031			as_tsktsk
1032			  (_("Warning: duplicated register (r%d) in register list"),
1033			   i);
1034		      else
1035			range |= 1 << i;
1036		    }
1037		  in_range = 0;
1038		}
1039
1040	      if (range & (1 << reg))
1041		as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1042			   reg);
1043	      else if (reg <= cur_reg)
1044		as_tsktsk (_("Warning: register range not in ascending order"));
1045
1046	      range |= 1 << reg;
1047	      cur_reg = reg;
1048	    }
1049	  while (skip_past_comma (&str) != FAIL
1050		 || (in_range = 1, *str++ == '-'));
1051	  str--;
1052
1053	  if (*str++ != '}')
1054	    {
1055	      inst.error = _("missing `}'");
1056	      return FAIL;
1057	    }
1058	}
1059      else
1060	{
1061	  expressionS expr;
1062
1063	  if (my_get_expression (&expr, &str, GE_NO_PREFIX))
1064	    return FAIL;
1065
1066	  if (expr.X_op == O_constant)
1067	    {
1068	      if (expr.X_add_number
1069		  != (expr.X_add_number & 0x0000ffff))
1070		{
1071		  inst.error = _("invalid register mask");
1072		  return FAIL;
1073		}
1074
1075	      if ((range & expr.X_add_number) != 0)
1076		{
1077		  int regno = range & expr.X_add_number;
1078
1079		  regno &= -regno;
1080		  regno = (1 << regno) - 1;
1081		  as_tsktsk
1082		    (_("Warning: duplicated register (r%d) in register list"),
1083		     regno);
1084		}
1085
1086	      range |= expr.X_add_number;
1087	    }
1088	  else
1089	    {
1090	      if (inst.reloc.type != 0)
1091		{
1092		  inst.error = _("expression too complex");
1093		  return FAIL;
1094		}
1095
1096	      memcpy (&inst.reloc.exp, &expr, sizeof (expressionS));
1097	      inst.reloc.type = BFD_RELOC_ARM_MULTI;
1098	      inst.reloc.pc_rel = 0;
1099	    }
1100	}
1101
1102      if (*str == '|' || *str == '+')
1103	{
1104	  str++;
1105	  another_range = 1;
1106	}
1107    }
1108  while (another_range);
1109
1110  *strp = str;
1111  return range;
1112}
1113
1114/* Parse a VFP register list.  If the string is invalid return FAIL.
1115   Otherwise return the number of registers, and set PBASE to the first
1116   register.  Double precision registers are matched if DP is nonzero.	*/
1117
1118static int
1119parse_vfp_reg_list (char **str, unsigned int *pbase, int dp)
1120{
1121  int base_reg;
1122  int new_base;
1123  int regtype;
1124  int max_regs;
1125  int count = 0;
1126  int warned = 0;
1127  unsigned long mask = 0;
1128  int i;
1129
1130  if (**str != '{')
1131    return FAIL;
1132
1133  (*str)++;
1134
1135  if (dp)
1136    {
1137      regtype = REG_TYPE_VFD;
1138      max_regs = 16;
1139    }
1140  else
1141    {
1142      regtype = REG_TYPE_VFS;
1143      max_regs = 32;
1144    }
1145
1146  base_reg = max_regs;
1147
1148  do
1149    {
1150      new_base = arm_reg_parse (str, regtype);
1151      if (new_base == FAIL)
1152	{
1153	  inst.error = gettext (reg_expected_msgs[regtype]);
1154	  return FAIL;
1155	}
1156
1157      if (new_base < base_reg)
1158	base_reg = new_base;
1159
1160      if (mask & (1 << new_base))
1161	{
1162	  inst.error = _("invalid register list");
1163	  return FAIL;
1164	}
1165
1166      if ((mask >> new_base) != 0 && ! warned)
1167	{
1168	  as_tsktsk (_("register list not in ascending order"));
1169	  warned = 1;
1170	}
1171
1172      mask |= 1 << new_base;
1173      count++;
1174
1175      if (**str == '-') /* We have the start of a range expression */
1176	{
1177	  int high_range;
1178
1179	  (*str)++;
1180
1181	  if ((high_range = arm_reg_parse (str, regtype)) == FAIL)
1182	    {
1183	      inst.error = gettext (reg_expected_msgs[regtype]);
1184	      return FAIL;
1185	    }
1186
1187	  if (high_range <= new_base)
1188	    {
1189	      inst.error = _("register range not in ascending order");
1190	      return FAIL;
1191	    }
1192
1193	  for (new_base++; new_base <= high_range; new_base++)
1194	    {
1195	      if (mask & (1 << new_base))
1196		{
1197		  inst.error = _("invalid register list");
1198		  return FAIL;
1199		}
1200
1201	      mask |= 1 << new_base;
1202	      count++;
1203	    }
1204	}
1205    }
1206  while (skip_past_comma (str) != FAIL);
1207
1208  (*str)++;
1209
1210  /* Sanity check -- should have raised a parse error above.  */
1211  if (count == 0 || count > max_regs)
1212    abort ();
1213
1214  *pbase = base_reg;
1215
1216  /* Final test -- the registers must be consecutive.  */
1217  mask >>= base_reg;
1218  for (i = 0; i < count; i++)
1219    {
1220      if ((mask & (1u << i)) == 0)
1221	{
1222	  inst.error = _("non-contiguous register range");
1223	  return FAIL;
1224	}
1225    }
1226
1227  return count;
1228}
1229
1230/* Parse an explicit relocation suffix on an expression.  This is
1231   either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
1232   arm_reloc_hsh contains no entries, so this function can only
1233   succeed if there is no () after the word.  Returns -1 on error,
1234   BFD_RELOC_UNUSED if there wasn't any suffix.	 */
1235static int
1236parse_reloc (char **str)
1237{
1238  struct reloc_entry *r;
1239  char *p, *q;
1240
1241  if (**str != '(')
1242    return BFD_RELOC_UNUSED;
1243
1244  p = *str + 1;
1245  q = p;
1246
1247  while (*q && *q != ')' && *q != ',')
1248    q++;
1249  if (*q != ')')
1250    return -1;
1251
1252  if ((r = hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
1253    return -1;
1254
1255  *str = q + 1;
1256  return r->reloc;
1257}
1258
1259/* Directives: register aliases.  */
1260
1261static void
1262insert_reg_alias (char *str, int number, int type)
1263{
1264  struct reg_entry *new;
1265  const char *name;
1266
1267  if ((new = hash_find (arm_reg_hsh, str)) != 0)
1268    {
1269      if (new->builtin)
1270	as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
1271
1272      /* Only warn about a redefinition if it's not defined as the
1273	 same register.	 */
1274      else if (new->number != number || new->type != type)
1275	as_warn (_("ignoring redefinition of register alias '%s'"), str);
1276
1277      return;
1278    }
1279
1280  name = xstrdup (str);
1281  new = xmalloc (sizeof (struct reg_entry));
1282
1283  new->name = name;
1284  new->number = number;
1285  new->type = type;
1286  new->builtin = FALSE;
1287
1288  if (hash_insert (arm_reg_hsh, name, (PTR) new))
1289    abort ();
1290}
1291
1292/* Look for the .req directive.	 This is of the form:
1293
1294	new_register_name .req existing_register_name
1295
1296   If we find one, or if it looks sufficiently like one that we want to
1297   handle any error here, return non-zero.  Otherwise return zero.  */
1298
1299static int
1300create_register_alias (char * newname, char *p)
1301{
1302  struct reg_entry *old;
1303  char *oldname, *nbuf;
1304  size_t nlen;
1305
1306  /* The input scrubber ensures that whitespace after the mnemonic is
1307     collapsed to single spaces.  */
1308  oldname = p;
1309  if (strncmp (oldname, " .req ", 6) != 0)
1310    return 0;
1311
1312  oldname += 6;
1313  if (*oldname == '\0')
1314    return 0;
1315
1316  old = hash_find (arm_reg_hsh, oldname);
1317  if (!old)
1318    {
1319      as_warn (_("unknown register '%s' -- .req ignored"), oldname);
1320      return 1;
1321    }
1322
1323  /* If TC_CASE_SENSITIVE is defined, then newname already points to
1324     the desired alias name, and p points to its end.  If not, then
1325     the desired alias name is in the global original_case_string.  */
1326#ifdef TC_CASE_SENSITIVE
1327  nlen = p - newname;
1328#else
1329  newname = original_case_string;
1330  nlen = strlen (newname);
1331#endif
1332
1333  nbuf = alloca (nlen + 1);
1334  memcpy (nbuf, newname, nlen);
1335  nbuf[nlen] = '\0';
1336
1337  /* Create aliases under the new name as stated; an all-lowercase
1338     version of the new name; and an all-uppercase version of the new
1339     name.  */
1340  insert_reg_alias (nbuf, old->number, old->type);
1341
1342  for (p = nbuf; *p; p++)
1343    *p = TOUPPER (*p);
1344
1345  if (strncmp (nbuf, newname, nlen))
1346    insert_reg_alias (nbuf, old->number, old->type);
1347
1348  for (p = nbuf; *p; p++)
1349    *p = TOLOWER (*p);
1350
1351  if (strncmp (nbuf, newname, nlen))
1352    insert_reg_alias (nbuf, old->number, old->type);
1353
1354  return 1;
1355}
1356
1357/* Should never be called, as .req goes between the alias and the
1358   register name, not at the beginning of the line.  */
1359static void
1360s_req (int a ATTRIBUTE_UNUSED)
1361{
1362  as_bad (_("invalid syntax for .req directive"));
1363}
1364
1365/* The .unreq directive deletes an alias which was previously defined
1366   by .req.  For example:
1367
1368       my_alias .req r11
1369       .unreq my_alias	  */
1370
1371static void
1372s_unreq (int a ATTRIBUTE_UNUSED)
1373{
1374  char * name;
1375  char saved_char;
1376
1377  name = input_line_pointer;
1378
1379  while (*input_line_pointer != 0
1380	 && *input_line_pointer != ' '
1381	 && *input_line_pointer != '\n')
1382    ++input_line_pointer;
1383
1384  saved_char = *input_line_pointer;
1385  *input_line_pointer = 0;
1386
1387  if (!*name)
1388    as_bad (_("invalid syntax for .unreq directive"));
1389  else
1390    {
1391      struct reg_entry *reg = hash_find (arm_reg_hsh, name);
1392
1393      if (!reg)
1394	as_bad (_("unknown register alias '%s'"), name);
1395      else if (reg->builtin)
1396	as_warn (_("ignoring attempt to undefine built-in register '%s'"),
1397		 name);
1398      else
1399	{
1400	  hash_delete (arm_reg_hsh, name);
1401	  free ((char *) reg->name);
1402	  free (reg);
1403	}
1404    }
1405
1406  *input_line_pointer = saved_char;
1407  demand_empty_rest_of_line ();
1408}
1409
1410/* Directives: Instruction set selection.  */
1411
1412#ifdef OBJ_ELF
1413/* This code is to handle mapping symbols as defined in the ARM ELF spec.
1414   (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
1415   Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
1416   and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
1417
1418static enum mstate mapstate = MAP_UNDEFINED;
1419
1420static void
1421mapping_state (enum mstate state)
1422{
1423  symbolS * symbolP;
1424  const char * symname;
1425  int type;
1426
1427  if (mapstate == state)
1428    /* The mapping symbol has already been emitted.
1429       There is nothing else to do.  */
1430    return;
1431
1432  mapstate = state;
1433
1434  switch (state)
1435    {
1436    case MAP_DATA:
1437      symname = "$d";
1438      type = BSF_NO_FLAGS;
1439      break;
1440    case MAP_ARM:
1441      symname = "$a";
1442      type = BSF_NO_FLAGS;
1443      break;
1444    case MAP_THUMB:
1445      symname = "$t";
1446      type = BSF_NO_FLAGS;
1447      break;
1448    case MAP_UNDEFINED:
1449      return;
1450    default:
1451      abort ();
1452    }
1453
1454  seg_info (now_seg)->tc_segment_info_data.mapstate = state;
1455
1456  symbolP = symbol_new (symname, now_seg, (valueT) frag_now_fix (), frag_now);
1457  symbol_table_insert (symbolP);
1458  symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
1459
1460  switch (state)
1461    {
1462    case MAP_ARM:
1463      THUMB_SET_FUNC (symbolP, 0);
1464      ARM_SET_THUMB (symbolP, 0);
1465      ARM_SET_INTERWORK (symbolP, support_interwork);
1466      break;
1467
1468    case MAP_THUMB:
1469      THUMB_SET_FUNC (symbolP, 1);
1470      ARM_SET_THUMB (symbolP, 1);
1471      ARM_SET_INTERWORK (symbolP, support_interwork);
1472      break;
1473
1474    case MAP_DATA:
1475    default:
1476      return;
1477    }
1478}
1479#else
1480#define mapping_state(x) /* nothing */
1481#endif
1482
1483/* Find the real, Thumb encoded start of a Thumb function.  */
1484
1485static symbolS *
1486find_real_start (symbolS * symbolP)
1487{
1488  char *       real_start;
1489  const char * name = S_GET_NAME (symbolP);
1490  symbolS *    new_target;
1491
1492  /* This definition must agree with the one in gcc/config/arm/thumb.c.	 */
1493#define STUB_NAME ".real_start_of"
1494
1495  if (name == NULL)
1496    abort ();
1497
1498  /* The compiler may generate BL instructions to local labels because
1499     it needs to perform a branch to a far away location. These labels
1500     do not have a corresponding ".real_start_of" label.  We check
1501     both for S_IS_LOCAL and for a leading dot, to give a way to bypass
1502     the ".real_start_of" convention for nonlocal branches.  */
1503  if (S_IS_LOCAL (symbolP) || name[0] == '.')
1504    return symbolP;
1505
1506  real_start = ACONCAT ((STUB_NAME, name, NULL));
1507  new_target = symbol_find (real_start);
1508
1509  if (new_target == NULL)
1510    {
1511      as_warn ("Failed to find real start of function: %s\n", name);
1512      new_target = symbolP;
1513    }
1514
1515  return new_target;
1516}
1517
1518static void
1519opcode_select (int width)
1520{
1521  switch (width)
1522    {
1523    case 16:
1524      if (! thumb_mode)
1525	{
1526	  if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
1527	    as_bad (_("selected processor does not support THUMB opcodes"));
1528
1529	  thumb_mode = 1;
1530	  /* No need to force the alignment, since we will have been
1531	     coming from ARM mode, which is word-aligned.  */
1532	  record_alignment (now_seg, 1);
1533	}
1534      mapping_state (MAP_THUMB);
1535      break;
1536
1537    case 32:
1538      if (thumb_mode)
1539	{
1540	  if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
1541	    as_bad (_("selected processor does not support ARM opcodes"));
1542
1543	  thumb_mode = 0;
1544
1545	  if (!need_pass_2)
1546	    frag_align (2, 0, 0);
1547
1548	  record_alignment (now_seg, 1);
1549	}
1550      mapping_state (MAP_ARM);
1551      break;
1552
1553    default:
1554      as_bad (_("invalid instruction size selected (%d)"), width);
1555    }
1556}
1557
1558static void
1559s_arm (int ignore ATTRIBUTE_UNUSED)
1560{
1561  opcode_select (32);
1562  demand_empty_rest_of_line ();
1563}
1564
1565static void
1566s_thumb (int ignore ATTRIBUTE_UNUSED)
1567{
1568  opcode_select (16);
1569  demand_empty_rest_of_line ();
1570}
1571
1572static void
1573s_code (int unused ATTRIBUTE_UNUSED)
1574{
1575  int temp;
1576
1577  temp = get_absolute_expression ();
1578  switch (temp)
1579    {
1580    case 16:
1581    case 32:
1582      opcode_select (temp);
1583      break;
1584
1585    default:
1586      as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
1587    }
1588}
1589
1590static void
1591s_force_thumb (int ignore ATTRIBUTE_UNUSED)
1592{
1593  /* If we are not already in thumb mode go into it, EVEN if
1594     the target processor does not support thumb instructions.
1595     This is used by gcc/config/arm/lib1funcs.asm for example
1596     to compile interworking support functions even if the
1597     target processor should not support interworking.	*/
1598  if (! thumb_mode)
1599    {
1600      thumb_mode = 2;
1601      record_alignment (now_seg, 1);
1602    }
1603
1604  demand_empty_rest_of_line ();
1605}
1606
1607static void
1608s_thumb_func (int ignore ATTRIBUTE_UNUSED)
1609{
1610  s_thumb (0);
1611
1612  /* The following label is the name/address of the start of a Thumb function.
1613     We need to know this for the interworking support.	 */
1614  label_is_thumb_function_name = TRUE;
1615}
1616
1617/* Perform a .set directive, but also mark the alias as
1618   being a thumb function.  */
1619
1620static void
1621s_thumb_set (int equiv)
1622{
1623  /* XXX the following is a duplicate of the code for s_set() in read.c
1624     We cannot just call that code as we need to get at the symbol that
1625     is created.  */
1626  char *    name;
1627  char	    delim;
1628  char *    end_name;
1629  symbolS * symbolP;
1630
1631  /* Especial apologies for the random logic:
1632     This just grew, and could be parsed much more simply!
1633     Dean - in haste.  */
1634  name	    = input_line_pointer;
1635  delim	    = get_symbol_end ();
1636  end_name  = input_line_pointer;
1637  *end_name = delim;
1638
1639  if (*input_line_pointer != ',')
1640    {
1641      *end_name = 0;
1642      as_bad (_("expected comma after name \"%s\""), name);
1643      *end_name = delim;
1644      ignore_rest_of_line ();
1645      return;
1646    }
1647
1648  input_line_pointer++;
1649  *end_name = 0;
1650
1651  if (name[0] == '.' && name[1] == '\0')
1652    {
1653      /* XXX - this should not happen to .thumb_set.  */
1654      abort ();
1655    }
1656
1657  if ((symbolP = symbol_find (name)) == NULL
1658      && (symbolP = md_undefined_symbol (name)) == NULL)
1659    {
1660#ifndef NO_LISTING
1661      /* When doing symbol listings, play games with dummy fragments living
1662	 outside the normal fragment chain to record the file and line info
1663	 for this symbol.  */
1664      if (listing & LISTING_SYMBOLS)
1665	{
1666	  extern struct list_info_struct * listing_tail;
1667	  fragS * dummy_frag = xmalloc (sizeof (fragS));
1668
1669	  memset (dummy_frag, 0, sizeof (fragS));
1670	  dummy_frag->fr_type = rs_fill;
1671	  dummy_frag->line = listing_tail;
1672	  symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
1673	  dummy_frag->fr_symbol = symbolP;
1674	}
1675      else
1676#endif
1677	symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
1678
1679#ifdef OBJ_COFF
1680      /* "set" symbols are local unless otherwise specified.  */
1681      SF_SET_LOCAL (symbolP);
1682#endif /* OBJ_COFF  */
1683    }				/* Make a new symbol.  */
1684
1685  symbol_table_insert (symbolP);
1686
1687  * end_name = delim;
1688
1689  if (equiv
1690      && S_IS_DEFINED (symbolP)
1691      && S_GET_SEGMENT (symbolP) != reg_section)
1692    as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
1693
1694  pseudo_set (symbolP);
1695
1696  demand_empty_rest_of_line ();
1697
1698  /* XXX Now we come to the Thumb specific bit of code.	 */
1699
1700  THUMB_SET_FUNC (symbolP, 1);
1701  ARM_SET_THUMB (symbolP, 1);
1702#if defined OBJ_ELF || defined OBJ_COFF
1703  ARM_SET_INTERWORK (symbolP, support_interwork);
1704#endif
1705}
1706
1707/* Directives: Mode selection.  */
1708
1709/* .syntax [unified|divided] - choose the new unified syntax
1710   (same for Arm and Thumb encoding, modulo slight differences in what
1711   can be represented) or the old divergent syntax for each mode.  */
1712static void
1713s_syntax (int unused ATTRIBUTE_UNUSED)
1714{
1715  char *name, delim;
1716
1717  name = input_line_pointer;
1718  delim = get_symbol_end ();
1719
1720  if (!strcasecmp (name, "unified"))
1721    unified_syntax = TRUE;
1722  else if (!strcasecmp (name, "divided"))
1723    unified_syntax = FALSE;
1724  else
1725    {
1726      as_bad (_("unrecognized syntax mode \"%s\""), name);
1727      return;
1728    }
1729  *input_line_pointer = delim;
1730  demand_empty_rest_of_line ();
1731}
1732
1733static void
1734s_inst(int unused ATTRIBUTE_UNUSED)
1735{
1736	expressionS exp;
1737
1738	if (thumb_mode) {
1739		as_bad(".inst not implemented for Thumb mode");
1740		ignore_rest_of_line();
1741		return;
1742	}
1743
1744	if (is_it_end_of_statement()) {
1745		demand_empty_rest_of_line();
1746		return;
1747	}
1748
1749	do {
1750		expression(&exp);
1751
1752		if (exp.X_op != O_constant)
1753			as_bad("constant expression required");
1754		else
1755			emit_expr(&exp, 4);
1756
1757	} while (*input_line_pointer++ == ',');
1758
1759	/* Put terminator back into stream. */
1760	input_line_pointer--;
1761	demand_empty_rest_of_line();
1762}
1763
1764/* Directives: sectioning and alignment.  */
1765
1766/* Same as s_align_ptwo but align 0 => align 2.	 */
1767
1768static void
1769s_align (int unused ATTRIBUTE_UNUSED)
1770{
1771  int temp;
1772  long temp_fill;
1773  long max_alignment = 15;
1774
1775  temp = get_absolute_expression ();
1776  if (temp > max_alignment)
1777    as_bad (_("alignment too large: %d assumed"), temp = max_alignment);
1778  else if (temp < 0)
1779    {
1780      as_bad (_("alignment negative. 0 assumed."));
1781      temp = 0;
1782    }
1783
1784  if (*input_line_pointer == ',')
1785    {
1786      input_line_pointer++;
1787      temp_fill = get_absolute_expression ();
1788    }
1789  else
1790    temp_fill = 0;
1791
1792  if (!temp)
1793    temp = 2;
1794
1795  /* Only make a frag if we HAVE to.  */
1796  if (temp && !need_pass_2)
1797    frag_align (temp, (int) temp_fill, 0);
1798  demand_empty_rest_of_line ();
1799
1800  record_alignment (now_seg, temp);
1801}
1802
1803static void
1804s_bss (int ignore ATTRIBUTE_UNUSED)
1805{
1806  /* We don't support putting frags in the BSS segment, we fake it by
1807     marking in_bss, then looking at s_skip for clues.	*/
1808  subseg_set (bss_section, 0);
1809  demand_empty_rest_of_line ();
1810  mapping_state (MAP_DATA);
1811}
1812
1813static void
1814s_even (int ignore ATTRIBUTE_UNUSED)
1815{
1816  /* Never make frag if expect extra pass.  */
1817  if (!need_pass_2)
1818    frag_align (1, 0, 0);
1819
1820  record_alignment (now_seg, 1);
1821
1822  demand_empty_rest_of_line ();
1823}
1824
1825/* Directives: Literal pools.  */
1826
1827static literal_pool *
1828find_literal_pool (void)
1829{
1830  literal_pool * pool;
1831
1832  for (pool = list_of_pools; pool != NULL; pool = pool->next)
1833    {
1834      if (pool->section == now_seg
1835	  && pool->sub_section == now_subseg)
1836	break;
1837    }
1838
1839  return pool;
1840}
1841
1842static literal_pool *
1843find_or_make_literal_pool (void)
1844{
1845  /* Next literal pool ID number.  */
1846  static unsigned int latest_pool_num = 1;
1847  literal_pool *      pool;
1848
1849  pool = find_literal_pool ();
1850
1851  if (pool == NULL)
1852    {
1853      /* Create a new pool.  */
1854      pool = xmalloc (sizeof (* pool));
1855      if (! pool)
1856	return NULL;
1857
1858      pool->next_free_entry = 0;
1859      pool->section	    = now_seg;
1860      pool->sub_section	    = now_subseg;
1861      pool->next	    = list_of_pools;
1862      pool->symbol	    = NULL;
1863
1864      /* Add it to the list.  */
1865      list_of_pools = pool;
1866    }
1867
1868  /* New pools, and emptied pools, will have a NULL symbol.  */
1869  if (pool->symbol == NULL)
1870    {
1871      pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
1872				    (valueT) 0, &zero_address_frag);
1873      pool->id = latest_pool_num ++;
1874    }
1875
1876  /* Done.  */
1877  return pool;
1878}
1879
1880/* Add the literal in the global 'inst'
1881   structure to the relevent literal pool.  */
1882
1883static int
1884add_to_lit_pool (void)
1885{
1886  literal_pool * pool;
1887  unsigned int entry;
1888
1889  pool = find_or_make_literal_pool ();
1890
1891  /* Check if this literal value is already in the pool.  */
1892  for (entry = 0; entry < pool->next_free_entry; entry ++)
1893    {
1894      if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
1895	  && (inst.reloc.exp.X_op == O_constant)
1896	  && (pool->literals[entry].X_add_number
1897	      == inst.reloc.exp.X_add_number)
1898	  && (pool->literals[entry].X_unsigned
1899	      == inst.reloc.exp.X_unsigned))
1900	break;
1901
1902      if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
1903	  && (inst.reloc.exp.X_op == O_symbol)
1904	  && (pool->literals[entry].X_add_number
1905	      == inst.reloc.exp.X_add_number)
1906	  && (pool->literals[entry].X_add_symbol
1907	      == inst.reloc.exp.X_add_symbol)
1908	  && (pool->literals[entry].X_op_symbol
1909	      == inst.reloc.exp.X_op_symbol))
1910	break;
1911    }
1912
1913  /* Do we need to create a new entry?	*/
1914  if (entry == pool->next_free_entry)
1915    {
1916      if (entry >= MAX_LITERAL_POOL_SIZE)
1917	{
1918	  inst.error = _("literal pool overflow");
1919	  return FAIL;
1920	}
1921
1922      pool->literals[entry] = inst.reloc.exp;
1923      pool->next_free_entry += 1;
1924    }
1925
1926  inst.reloc.exp.X_op	      = O_symbol;
1927  inst.reloc.exp.X_add_number = ((int) entry) * 4;
1928  inst.reloc.exp.X_add_symbol = pool->symbol;
1929
1930  return SUCCESS;
1931}
1932
1933/* Can't use symbol_new here, so have to create a symbol and then at
1934   a later date assign it a value. Thats what these functions do.  */
1935
1936static void
1937symbol_locate (symbolS *    symbolP,
1938	       const char * name,	/* It is copied, the caller can modify.	 */
1939	       segT	    segment,	/* Segment identifier (SEG_<something>).  */
1940	       valueT	    valu,	/* Symbol value.  */
1941	       fragS *	    frag)	/* Associated fragment.	 */
1942{
1943  unsigned int name_length;
1944  char * preserved_copy_of_name;
1945
1946  name_length = strlen (name) + 1;   /* +1 for \0.  */
1947  obstack_grow (&notes, name, name_length);
1948  preserved_copy_of_name = obstack_finish (&notes);
1949
1950#ifdef tc_canonicalize_symbol_name
1951  preserved_copy_of_name =
1952    tc_canonicalize_symbol_name (preserved_copy_of_name);
1953#endif
1954
1955  S_SET_NAME (symbolP, preserved_copy_of_name);
1956
1957  S_SET_SEGMENT (symbolP, segment);
1958  S_SET_VALUE (symbolP, valu);
1959  symbol_clear_list_pointers (symbolP);
1960
1961  symbol_set_frag (symbolP, frag);
1962
1963  /* Link to end of symbol chain.  */
1964  {
1965    extern int symbol_table_frozen;
1966
1967    if (symbol_table_frozen)
1968      abort ();
1969  }
1970
1971  symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
1972
1973  obj_symbol_new_hook (symbolP);
1974
1975#ifdef tc_symbol_new_hook
1976  tc_symbol_new_hook (symbolP);
1977#endif
1978
1979#ifdef DEBUG_SYMS
1980  verify_symbol_chain (symbol_rootP, symbol_lastP);
1981#endif /* DEBUG_SYMS  */
1982}
1983
1984
1985static void
1986s_ltorg (int ignored ATTRIBUTE_UNUSED)
1987{
1988  unsigned int entry;
1989  literal_pool * pool;
1990  char sym_name[20];
1991
1992  pool = find_literal_pool ();
1993  if (pool == NULL
1994      || pool->symbol == NULL
1995      || pool->next_free_entry == 0)
1996    return;
1997
1998  mapping_state (MAP_DATA);
1999
2000  /* Align pool as you have word accesses.
2001     Only make a frag if we have to.  */
2002  if (!need_pass_2)
2003    frag_align (2, 0, 0);
2004
2005  record_alignment (now_seg, 2);
2006
2007  sprintf (sym_name, "$$lit_\002%x", pool->id);
2008
2009  symbol_locate (pool->symbol, sym_name, now_seg,
2010		 (valueT) frag_now_fix (), frag_now);
2011  symbol_table_insert (pool->symbol);
2012
2013  ARM_SET_THUMB (pool->symbol, thumb_mode);
2014
2015#if defined OBJ_COFF || defined OBJ_ELF
2016  ARM_SET_INTERWORK (pool->symbol, support_interwork);
2017#endif
2018
2019  for (entry = 0; entry < pool->next_free_entry; entry ++)
2020    /* First output the expression in the instruction to the pool.  */
2021    emit_expr (&(pool->literals[entry]), 4); /* .word  */
2022
2023  /* Mark the pool as empty.  */
2024  pool->next_free_entry = 0;
2025  pool->symbol = NULL;
2026}
2027
2028#ifdef OBJ_ELF
2029/* Forward declarations for functions below, in the MD interface
2030   section.  */
2031static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
2032static valueT create_unwind_entry (int);
2033static void start_unwind_section (const segT, int);
2034static void add_unwind_opcode (valueT, int);
2035static void flush_pending_unwind (void);
2036
2037/* Directives: Data.  */
2038
2039static void
2040s_arm_elf_cons (int nbytes)
2041{
2042  expressionS exp;
2043
2044#ifdef md_flush_pending_output
2045  md_flush_pending_output ();
2046#endif
2047
2048  if (is_it_end_of_statement ())
2049    {
2050      demand_empty_rest_of_line ();
2051      return;
2052    }
2053
2054#ifdef md_cons_align
2055  md_cons_align (nbytes);
2056#endif
2057
2058  mapping_state (MAP_DATA);
2059  do
2060    {
2061      int reloc;
2062      char *base = input_line_pointer;
2063
2064      expression (& exp);
2065
2066      if (exp.X_op != O_symbol)
2067	emit_expr (&exp, (unsigned int) nbytes);
2068      else
2069	{
2070	  char *before_reloc = input_line_pointer;
2071	  reloc = parse_reloc (&input_line_pointer);
2072	  if (reloc == -1)
2073	    {
2074	      as_bad (_("unrecognized relocation suffix"));
2075	      ignore_rest_of_line ();
2076	      return;
2077	    }
2078	  else if (reloc == BFD_RELOC_UNUSED)
2079	    emit_expr (&exp, (unsigned int) nbytes);
2080	  else
2081	    {
2082	      reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, reloc);
2083	      int size = bfd_get_reloc_size (howto);
2084
2085	      if (reloc == BFD_RELOC_ARM_PLT32)
2086		{
2087		  as_bad (_("(plt) is only valid on branch targets"));
2088		  reloc = BFD_RELOC_UNUSED;
2089		  size = 0;
2090		}
2091
2092	      if (size > nbytes)
2093		as_bad (_("%s relocations do not fit in %d bytes"),
2094			howto->name, nbytes);
2095	      else
2096		{
2097		  /* We've parsed an expression stopping at O_symbol.
2098		     But there may be more expression left now that we
2099		     have parsed the relocation marker.  Parse it again.
2100		     XXX Surely there is a cleaner way to do this.  */
2101		  char *p = input_line_pointer;
2102		  int offset;
2103		  char *save_buf = alloca (input_line_pointer - base);
2104		  memcpy (save_buf, base, input_line_pointer - base);
2105		  memmove (base + (input_line_pointer - before_reloc),
2106			   base, before_reloc - base);
2107
2108		  input_line_pointer = base + (input_line_pointer-before_reloc);
2109		  expression (&exp);
2110		  memcpy (base, save_buf, p - base);
2111
2112		  offset = nbytes - size;
2113		  p = frag_more ((int) nbytes);
2114		  fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
2115			       size, &exp, 0, reloc);
2116		}
2117	    }
2118	}
2119    }
2120  while (*input_line_pointer++ == ',');
2121
2122  /* Put terminator back into stream.  */
2123  input_line_pointer --;
2124  demand_empty_rest_of_line ();
2125}
2126
2127
2128/* Parse a .rel31 directive.  */
2129
2130static void
2131s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
2132{
2133  expressionS exp;
2134  char *p;
2135  valueT highbit;
2136
2137  highbit = 0;
2138  if (*input_line_pointer == '1')
2139    highbit = 0x80000000;
2140  else if (*input_line_pointer != '0')
2141    as_bad (_("expected 0 or 1"));
2142
2143  input_line_pointer++;
2144  if (*input_line_pointer != ',')
2145    as_bad (_("missing comma"));
2146  input_line_pointer++;
2147
2148#ifdef md_flush_pending_output
2149  md_flush_pending_output ();
2150#endif
2151
2152#ifdef md_cons_align
2153  md_cons_align (4);
2154#endif
2155
2156  mapping_state (MAP_DATA);
2157
2158  expression (&exp);
2159
2160  p = frag_more (4);
2161  md_number_to_chars (p, highbit, 4);
2162  fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
2163	       BFD_RELOC_ARM_PREL31);
2164
2165  demand_empty_rest_of_line ();
2166}
2167
2168/* Directives: AEABI stack-unwind tables.  */
2169
2170/* Parse an unwind_fnstart directive.  Simply records the current location.  */
2171
2172static void
2173s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
2174{
2175  demand_empty_rest_of_line ();
2176  /* Mark the start of the function.  */
2177  unwind.proc_start = expr_build_dot ();
2178
2179  /* Reset the rest of the unwind info.	 */
2180  unwind.opcode_count = 0;
2181  unwind.table_entry = NULL;
2182  unwind.personality_routine = NULL;
2183  unwind.personality_index = -1;
2184  unwind.frame_size = 0;
2185  unwind.fp_offset = 0;
2186  unwind.fp_reg = 13;
2187  unwind.fp_used = 0;
2188  unwind.sp_restored = 0;
2189}
2190
2191
2192/* Parse a handlerdata directive.  Creates the exception handling table entry
2193   for the function.  */
2194
2195static void
2196s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
2197{
2198  demand_empty_rest_of_line ();
2199  if (unwind.table_entry)
2200    as_bad (_("dupicate .handlerdata directive"));
2201
2202  create_unwind_entry (1);
2203}
2204
2205/* Parse an unwind_fnend directive.  Generates the index table entry.  */
2206
2207static void
2208s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
2209{
2210  long where;
2211  char *ptr;
2212  valueT val;
2213
2214  demand_empty_rest_of_line ();
2215
2216  /* Add eh table entry.  */
2217  if (unwind.table_entry == NULL)
2218    val = create_unwind_entry (0);
2219  else
2220    val = 0;
2221
2222  /* Add index table entry.  This is two words.	 */
2223  start_unwind_section (unwind.saved_seg, 1);
2224  frag_align (2, 0, 0);
2225  record_alignment (now_seg, 2);
2226
2227  ptr = frag_more (8);
2228  where = frag_now_fix () - 8;
2229
2230  /* Self relative offset of the function start.  */
2231  fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
2232	   BFD_RELOC_ARM_PREL31);
2233
2234  /* Indicate dependency on EHABI-defined personality routines to the
2235     linker, if it hasn't been done already.  */
2236  if (unwind.personality_index >= 0 && unwind.personality_index < 3
2237      && !(marked_pr_dependency & (1 << unwind.personality_index)))
2238    {
2239      static const char *const name[] = {
2240	"__aeabi_unwind_cpp_pr0",
2241	"__aeabi_unwind_cpp_pr1",
2242	"__aeabi_unwind_cpp_pr2"
2243      };
2244      symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
2245      fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
2246      marked_pr_dependency |= 1 << unwind.personality_index;
2247      seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
2248	= marked_pr_dependency;
2249    }
2250
2251  if (val)
2252    /* Inline exception table entry.  */
2253    md_number_to_chars (ptr + 4, val, 4);
2254  else
2255    /* Self relative offset of the table entry.	 */
2256    fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
2257	     BFD_RELOC_ARM_PREL31);
2258
2259  /* Restore the original section.  */
2260  subseg_set (unwind.saved_seg, unwind.saved_subseg);
2261}
2262
2263
2264/* Parse an unwind_cantunwind directive.  */
2265
2266static void
2267s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
2268{
2269  demand_empty_rest_of_line ();
2270  if (unwind.personality_routine || unwind.personality_index != -1)
2271    as_bad (_("personality routine specified for cantunwind frame"));
2272
2273  unwind.personality_index = -2;
2274}
2275
2276
2277/* Parse a personalityindex directive.	*/
2278
2279static void
2280s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
2281{
2282  expressionS exp;
2283
2284  if (unwind.personality_routine || unwind.personality_index != -1)
2285    as_bad (_("duplicate .personalityindex directive"));
2286
2287  expression (&exp);
2288
2289  if (exp.X_op != O_constant
2290      || exp.X_add_number < 0 || exp.X_add_number > 15)
2291    {
2292      as_bad (_("bad personality routine number"));
2293      ignore_rest_of_line ();
2294      return;
2295    }
2296
2297  unwind.personality_index = exp.X_add_number;
2298
2299  demand_empty_rest_of_line ();
2300}
2301
2302
2303/* Parse a personality directive.  */
2304
2305static void
2306s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
2307{
2308  char *name, *p, c;
2309
2310  if (unwind.personality_routine || unwind.personality_index != -1)
2311    as_bad (_("duplicate .personality directive"));
2312
2313  name = input_line_pointer;
2314  c = get_symbol_end ();
2315  p = input_line_pointer;
2316  unwind.personality_routine = symbol_find_or_make (name);
2317  *p = c;
2318  demand_empty_rest_of_line ();
2319}
2320
2321
2322/* Parse a directive saving core registers.  */
2323
2324static void
2325s_arm_unwind_save_core (void)
2326{
2327  valueT op;
2328  long range;
2329  int n;
2330
2331  range = parse_reg_list (&input_line_pointer);
2332  if (range == FAIL)
2333    {
2334      as_bad (_("expected register list"));
2335      ignore_rest_of_line ();
2336      return;
2337    }
2338
2339  demand_empty_rest_of_line ();
2340
2341  /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
2342     into .unwind_save {..., sp...}.  We aren't bothered about the value of
2343     ip because it is clobbered by calls.  */
2344  if (unwind.sp_restored && unwind.fp_reg == 12
2345      && (range & 0x3000) == 0x1000)
2346    {
2347      unwind.opcode_count--;
2348      unwind.sp_restored = 0;
2349      range = (range | 0x2000) & ~0x1000;
2350      unwind.pending_offset = 0;
2351    }
2352
2353  /* Pop r4-r15.  */
2354  if (range & 0xfff0)
2355    {
2356      /* See if we can use the short opcodes.  These pop a block of up to 8
2357	 registers starting with r4, plus maybe r14.  */
2358      for (n = 0; n < 8; n++)
2359	{
2360	  /* Break at the first non-saved register.	 */
2361	  if ((range & (1 << (n + 4))) == 0)
2362	    break;
2363	}
2364      /* See if there are any other bits set.  */
2365      if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
2366	{
2367	  /* Use the long form.  */
2368	  op = 0x8000 | ((range >> 4) & 0xfff);
2369	  add_unwind_opcode (op, 2);
2370	}
2371      else
2372	{
2373	  /* Use the short form.  */
2374	  if (range & 0x4000)
2375	    op = 0xa8; /* Pop r14.	*/
2376	  else
2377	    op = 0xa0; /* Do not pop r14.  */
2378	  op |= (n - 1);
2379	  add_unwind_opcode (op, 1);
2380	}
2381    }
2382
2383  /* Pop r0-r3.	 */
2384  if (range & 0xf)
2385    {
2386      op = 0xb100 | (range & 0xf);
2387      add_unwind_opcode (op, 2);
2388    }
2389
2390  /* Record the number of bytes pushed.	 */
2391  for (n = 0; n < 16; n++)
2392    {
2393      if (range & (1 << n))
2394	unwind.frame_size += 4;
2395    }
2396}
2397
2398
2399/* Parse a directive saving FPA registers.  */
2400
2401static void
2402s_arm_unwind_save_fpa (int reg)
2403{
2404  expressionS exp;
2405  int num_regs;
2406  valueT op;
2407
2408  /* Get Number of registers to transfer.  */
2409  if (skip_past_comma (&input_line_pointer) != FAIL)
2410    expression (&exp);
2411  else
2412    exp.X_op = O_illegal;
2413
2414  if (exp.X_op != O_constant)
2415    {
2416      as_bad (_("expected , <constant>"));
2417      ignore_rest_of_line ();
2418      return;
2419    }
2420
2421  num_regs = exp.X_add_number;
2422
2423  if (num_regs < 1 || num_regs > 4)
2424    {
2425      as_bad (_("number of registers must be in the range [1:4]"));
2426      ignore_rest_of_line ();
2427      return;
2428    }
2429
2430  demand_empty_rest_of_line ();
2431
2432  if (reg == 4)
2433    {
2434      /* Short form.  */
2435      op = 0xb4 | (num_regs - 1);
2436      add_unwind_opcode (op, 1);
2437    }
2438  else
2439    {
2440      /* Long form.  */
2441      op = 0xc800 | (reg << 4) | (num_regs - 1);
2442      add_unwind_opcode (op, 2);
2443    }
2444  unwind.frame_size += num_regs * 12;
2445}
2446
2447
2448/* Parse a directive saving VFP registers.  */
2449
2450static void
2451s_arm_unwind_save_vfp (void)
2452{
2453  int count;
2454  unsigned int reg;
2455  valueT op;
2456
2457  count = parse_vfp_reg_list (&input_line_pointer, &reg, 1);
2458  if (count == FAIL)
2459    {
2460      as_bad (_("expected register list"));
2461      ignore_rest_of_line ();
2462      return;
2463    }
2464
2465  demand_empty_rest_of_line ();
2466
2467  if (reg == 8)
2468    {
2469      /* Short form.  */
2470      op = 0xb8 | (count - 1);
2471      add_unwind_opcode (op, 1);
2472    }
2473  else
2474    {
2475      /* Long form.  */
2476      op = 0xb300 | (reg << 4) | (count - 1);
2477      add_unwind_opcode (op, 2);
2478    }
2479  unwind.frame_size += count * 8 + 4;
2480}
2481
2482
2483/* Parse a directive saving iWMMXt data registers.  */
2484
2485static void
2486s_arm_unwind_save_mmxwr (void)
2487{
2488  int reg;
2489  int hi_reg;
2490  int i;
2491  unsigned mask = 0;
2492  valueT op;
2493
2494  if (*input_line_pointer == '{')
2495    input_line_pointer++;
2496
2497  do
2498    {
2499      reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
2500
2501      if (reg == FAIL)
2502	{
2503	  as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
2504	  goto error;
2505	}
2506
2507      if (mask >> reg)
2508	as_tsktsk (_("register list not in ascending order"));
2509      mask |= 1 << reg;
2510
2511      if (*input_line_pointer == '-')
2512	{
2513	  input_line_pointer++;
2514	  hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
2515	  if (hi_reg == FAIL)
2516	    {
2517	      as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
2518	      goto error;
2519	    }
2520	  else if (reg >= hi_reg)
2521	    {
2522	      as_bad (_("bad register range"));
2523	      goto error;
2524	    }
2525	  for (; reg < hi_reg; reg++)
2526	    mask |= 1 << reg;
2527	}
2528    }
2529  while (skip_past_comma (&input_line_pointer) != FAIL);
2530
2531  if (*input_line_pointer == '}')
2532    input_line_pointer++;
2533
2534  demand_empty_rest_of_line ();
2535
2536  /* Generate any deferred opcodes becuuse we're going to be looking at
2537     the list.	*/
2538  flush_pending_unwind ();
2539
2540  for (i = 0; i < 16; i++)
2541    {
2542      if (mask & (1 << i))
2543	unwind.frame_size += 8;
2544    }
2545
2546  /* Attempt to combine with a previous opcode.	 We do this because gcc
2547     likes to output separate unwind directives for a single block of
2548     registers.	 */
2549  if (unwind.opcode_count > 0)
2550    {
2551      i = unwind.opcodes[unwind.opcode_count - 1];
2552      if ((i & 0xf8) == 0xc0)
2553	{
2554	  i &= 7;
2555	  /* Only merge if the blocks are contiguous.  */
2556	  if (i < 6)
2557	    {
2558	      if ((mask & 0xfe00) == (1 << 9))
2559		{
2560		  mask |= ((1 << (i + 11)) - 1) & 0xfc00;
2561		  unwind.opcode_count--;
2562		}
2563	    }
2564	  else if (i == 6 && unwind.opcode_count >= 2)
2565	    {
2566	      i = unwind.opcodes[unwind.opcode_count - 2];
2567	      reg = i >> 4;
2568	      i &= 0xf;
2569
2570	      op = 0xffff << (reg - 1);
2571	      if (reg > 0
2572		  || ((mask & op) == (1u << (reg - 1))))
2573		{
2574		  op = (1 << (reg + i + 1)) - 1;
2575		  op &= ~((1 << reg) - 1);
2576		  mask |= op;
2577		  unwind.opcode_count -= 2;
2578		}
2579	    }
2580	}
2581    }
2582
2583  hi_reg = 15;
2584  /* We want to generate opcodes in the order the registers have been
2585     saved, ie. descending order.  */
2586  for (reg = 15; reg >= -1; reg--)
2587    {
2588      /* Save registers in blocks.  */
2589      if (reg < 0
2590	  || !(mask & (1 << reg)))
2591	{
2592	  /* We found an unsaved reg.  Generate opcodes to save the
2593	     preceeding block.	*/
2594	  if (reg != hi_reg)
2595	    {
2596	      if (reg == 9)
2597		{
2598		  /* Short form.  */
2599		  op = 0xc0 | (hi_reg - 10);
2600		  add_unwind_opcode (op, 1);
2601		}
2602	      else
2603		{
2604		  /* Long form.	 */
2605		  op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
2606		  add_unwind_opcode (op, 2);
2607		}
2608	    }
2609	  hi_reg = reg - 1;
2610	}
2611    }
2612
2613  return;
2614error:
2615  ignore_rest_of_line ();
2616}
2617
2618static void
2619s_arm_unwind_save_mmxwcg (void)
2620{
2621  int reg;
2622  int hi_reg;
2623  unsigned mask = 0;
2624  valueT op;
2625
2626  if (*input_line_pointer == '{')
2627    input_line_pointer++;
2628
2629  do
2630    {
2631      reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
2632
2633      if (reg == FAIL)
2634	{
2635	  as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
2636	  goto error;
2637	}
2638
2639      reg -= 8;
2640      if (mask >> reg)
2641	as_tsktsk (_("register list not in ascending order"));
2642      mask |= 1 << reg;
2643
2644      if (*input_line_pointer == '-')
2645	{
2646	  input_line_pointer++;
2647	  hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
2648	  if (hi_reg == FAIL)
2649	    {
2650	      as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
2651	      goto error;
2652	    }
2653	  else if (reg >= hi_reg)
2654	    {
2655	      as_bad (_("bad register range"));
2656	      goto error;
2657	    }
2658	  for (; reg < hi_reg; reg++)
2659	    mask |= 1 << reg;
2660	}
2661    }
2662  while (skip_past_comma (&input_line_pointer) != FAIL);
2663
2664  if (*input_line_pointer == '}')
2665    input_line_pointer++;
2666
2667  demand_empty_rest_of_line ();
2668
2669  /* Generate any deferred opcodes becuuse we're going to be looking at
2670     the list.	*/
2671  flush_pending_unwind ();
2672
2673  for (reg = 0; reg < 16; reg++)
2674    {
2675      if (mask & (1 << reg))
2676	unwind.frame_size += 4;
2677    }
2678  op = 0xc700 | mask;
2679  add_unwind_opcode (op, 2);
2680  return;
2681error:
2682  ignore_rest_of_line ();
2683}
2684
2685
2686/* Parse an unwind_save directive.  */
2687
2688static void
2689s_arm_unwind_save (int ignored ATTRIBUTE_UNUSED)
2690{
2691  char *peek;
2692  struct reg_entry *reg;
2693  bfd_boolean had_brace = FALSE;
2694
2695  /* Figure out what sort of save we have.  */
2696  peek = input_line_pointer;
2697
2698  if (*peek == '{')
2699    {
2700      had_brace = TRUE;
2701      peek++;
2702    }
2703
2704  reg = arm_reg_parse_multi (&peek);
2705
2706  if (!reg)
2707    {
2708      as_bad (_("register expected"));
2709      ignore_rest_of_line ();
2710      return;
2711    }
2712
2713  switch (reg->type)
2714    {
2715    case REG_TYPE_FN:
2716      if (had_brace)
2717	{
2718	  as_bad (_("FPA .unwind_save does not take a register list"));
2719	  ignore_rest_of_line ();
2720	  return;
2721	}
2722      s_arm_unwind_save_fpa (reg->number);
2723      return;
2724
2725    case REG_TYPE_RN:	  s_arm_unwind_save_core ();   return;
2726    case REG_TYPE_VFD:	   s_arm_unwind_save_vfp ();	return;
2727    case REG_TYPE_MMXWR:  s_arm_unwind_save_mmxwr ();  return;
2728    case REG_TYPE_MMXWCG: s_arm_unwind_save_mmxwcg (); return;
2729
2730    default:
2731      as_bad (_(".unwind_save does not support this kind of register"));
2732      ignore_rest_of_line ();
2733    }
2734}
2735
2736
2737/* Parse an unwind_movsp directive.  */
2738
2739static void
2740s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
2741{
2742  int reg;
2743  valueT op;
2744
2745  reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
2746  if (reg == FAIL)
2747    {
2748      as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
2749      ignore_rest_of_line ();
2750      return;
2751    }
2752  demand_empty_rest_of_line ();
2753
2754  if (reg == REG_SP || reg == REG_PC)
2755    {
2756      as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
2757      return;
2758    }
2759
2760  if (unwind.fp_reg != REG_SP)
2761    as_bad (_("unexpected .unwind_movsp directive"));
2762
2763  /* Generate opcode to restore the value.  */
2764  op = 0x90 | reg;
2765  add_unwind_opcode (op, 1);
2766
2767  /* Record the information for later.	*/
2768  unwind.fp_reg = reg;
2769  unwind.fp_offset = unwind.frame_size;
2770  unwind.sp_restored = 1;
2771}
2772
2773/* Parse an unwind_pad directive.  */
2774
2775static void
2776s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
2777{
2778  int offset;
2779
2780  if (immediate_for_directive (&offset) == FAIL)
2781    return;
2782
2783  if (offset & 3)
2784    {
2785      as_bad (_("stack increment must be multiple of 4"));
2786      ignore_rest_of_line ();
2787      return;
2788    }
2789
2790  /* Don't generate any opcodes, just record the details for later.  */
2791  unwind.frame_size += offset;
2792  unwind.pending_offset += offset;
2793
2794  demand_empty_rest_of_line ();
2795}
2796
2797/* Parse an unwind_setfp directive.  */
2798
2799static void
2800s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
2801{
2802  int sp_reg;
2803  int fp_reg;
2804  int offset;
2805
2806  fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
2807  if (skip_past_comma (&input_line_pointer) == FAIL)
2808    sp_reg = FAIL;
2809  else
2810    sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
2811
2812  if (fp_reg == FAIL || sp_reg == FAIL)
2813    {
2814      as_bad (_("expected <reg>, <reg>"));
2815      ignore_rest_of_line ();
2816      return;
2817    }
2818
2819  /* Optional constant.	 */
2820  if (skip_past_comma (&input_line_pointer) != FAIL)
2821    {
2822      if (immediate_for_directive (&offset) == FAIL)
2823	return;
2824    }
2825  else
2826    offset = 0;
2827
2828  demand_empty_rest_of_line ();
2829
2830  if (sp_reg != 13 && sp_reg != unwind.fp_reg)
2831    {
2832      as_bad (_("register must be either sp or set by a previous"
2833		"unwind_movsp directive"));
2834      return;
2835    }
2836
2837  /* Don't generate any opcodes, just record the information for later.	 */
2838  unwind.fp_reg = fp_reg;
2839  unwind.fp_used = 1;
2840  if (sp_reg == 13)
2841    unwind.fp_offset = unwind.frame_size - offset;
2842  else
2843    unwind.fp_offset -= offset;
2844}
2845
2846/* Parse an unwind_raw directive.  */
2847
2848static void
2849s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
2850{
2851  expressionS exp;
2852  /* This is an arbitary limit.	 */
2853  unsigned char op[16];
2854  int count;
2855
2856  expression (&exp);
2857  if (exp.X_op == O_constant
2858      && skip_past_comma (&input_line_pointer) != FAIL)
2859    {
2860      unwind.frame_size += exp.X_add_number;
2861      expression (&exp);
2862    }
2863  else
2864    exp.X_op = O_illegal;
2865
2866  if (exp.X_op != O_constant)
2867    {
2868      as_bad (_("expected <offset>, <opcode>"));
2869      ignore_rest_of_line ();
2870      return;
2871    }
2872
2873  count = 0;
2874
2875  /* Parse the opcode.	*/
2876  for (;;)
2877    {
2878      if (count >= 16)
2879	{
2880	  as_bad (_("unwind opcode too long"));
2881	  ignore_rest_of_line ();
2882	}
2883      if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
2884	{
2885	  as_bad (_("invalid unwind opcode"));
2886	  ignore_rest_of_line ();
2887	  return;
2888	}
2889      op[count++] = exp.X_add_number;
2890
2891      /* Parse the next byte.  */
2892      if (skip_past_comma (&input_line_pointer) == FAIL)
2893	break;
2894
2895      expression (&exp);
2896    }
2897
2898  /* Add the opcode bytes in reverse order.  */
2899  while (count--)
2900    add_unwind_opcode (op[count], 1);
2901
2902  demand_empty_rest_of_line ();
2903}
2904
2905
2906/* Parse a .eabi_attribute directive.  */
2907
2908static void
2909s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
2910{
2911  expressionS exp;
2912  bfd_boolean is_string;
2913  int tag;
2914  unsigned int i = 0;
2915  char *s = NULL;
2916  char saved_char;
2917
2918  expression (& exp);
2919  if (exp.X_op != O_constant)
2920    goto bad;
2921
2922  tag = exp.X_add_number;
2923  if (tag == 4 || tag == 5 || tag == 32 || (tag > 32 && (tag & 1) != 0))
2924    is_string = 1;
2925  else
2926    is_string = 0;
2927
2928  if (skip_past_comma (&input_line_pointer) == FAIL)
2929    goto bad;
2930  if (tag == 32 || !is_string)
2931    {
2932      expression (& exp);
2933      if (exp.X_op != O_constant)
2934	{
2935	  as_bad (_("expected numeric constant"));
2936	  ignore_rest_of_line ();
2937	  return;
2938	}
2939      i = exp.X_add_number;
2940    }
2941  if (tag == Tag_compatibility
2942      && skip_past_comma (&input_line_pointer) == FAIL)
2943    {
2944      as_bad (_("expected comma"));
2945      ignore_rest_of_line ();
2946      return;
2947    }
2948  if (is_string)
2949    {
2950      skip_whitespace(input_line_pointer);
2951      if (*input_line_pointer != '"')
2952	goto bad_string;
2953      input_line_pointer++;
2954      s = input_line_pointer;
2955      while (*input_line_pointer && *input_line_pointer != '"')
2956	input_line_pointer++;
2957      if (*input_line_pointer != '"')
2958	goto bad_string;
2959      saved_char = *input_line_pointer;
2960      *input_line_pointer = 0;
2961    }
2962  else
2963    {
2964      s = NULL;
2965      saved_char = 0;
2966    }
2967
2968  if (tag == Tag_compatibility)
2969    elf32_arm_add_eabi_attr_compat (stdoutput, i, s);
2970  else if (is_string)
2971    elf32_arm_add_eabi_attr_string (stdoutput, tag, s);
2972  else
2973    elf32_arm_add_eabi_attr_int (stdoutput, tag, i);
2974
2975  if (s)
2976    {
2977      *input_line_pointer = saved_char;
2978      input_line_pointer++;
2979    }
2980  demand_empty_rest_of_line ();
2981  return;
2982bad_string:
2983  as_bad (_("bad string constant"));
2984  ignore_rest_of_line ();
2985  return;
2986bad:
2987  as_bad (_("expected <tag> , <value>"));
2988  ignore_rest_of_line ();
2989}
2990
2991static void s_arm_arch (int);
2992static void s_arm_cpu (int);
2993static void s_arm_fpu (int);
2994#endif /* OBJ_ELF */
2995
2996/* This table describes all the machine specific pseudo-ops the assembler
2997   has to support.  The fields are:
2998     pseudo-op name without dot
2999     function to call to execute this pseudo-op
3000     Integer arg to pass to the function.  */
3001
3002const pseudo_typeS md_pseudo_table[] =
3003{
3004  /* Never called because '.req' does not start a line.	 */
3005  { "req",	   s_req,	  0 },
3006  { "unreq",	   s_unreq,	  0 },
3007  { "bss",	   s_bss,	  0 },
3008  { "align",	   s_align,	  0 },
3009  { "arm",	   s_arm,	  0 },
3010  { "thumb",	   s_thumb,	  0 },
3011  { "code",	   s_code,	  0 },
3012  { "force_thumb", s_force_thumb, 0 },
3013  { "thumb_func",  s_thumb_func,  0 },
3014  { "thumb_set",   s_thumb_set,	  0 },
3015  { "even",	   s_even,	  0 },
3016  { "ltorg",	   s_ltorg,	  0 },
3017  { "pool",	   s_ltorg,	  0 },
3018  { "syntax",	   s_syntax,	  0 },
3019  { "inst",        s_inst,        0 },
3020#ifdef OBJ_ELF
3021  { "word",	   s_arm_elf_cons, 4 },
3022  { "long",	   s_arm_elf_cons, 4 },
3023  { "rel31",	   s_arm_rel31,	  0 },
3024  { "fnstart",		s_arm_unwind_fnstart,	0 },
3025  { "fnend",		s_arm_unwind_fnend,	0 },
3026  { "cantunwind",	s_arm_unwind_cantunwind, 0 },
3027  { "personality",	s_arm_unwind_personality, 0 },
3028  { "personalityindex",	s_arm_unwind_personalityindex, 0 },
3029  { "handlerdata",	s_arm_unwind_handlerdata, 0 },
3030  { "save",		s_arm_unwind_save,	0 },
3031  { "movsp",		s_arm_unwind_movsp,	0 },
3032  { "pad",		s_arm_unwind_pad,	0 },
3033  { "setfp",		s_arm_unwind_setfp,	0 },
3034  { "unwind_raw",	s_arm_unwind_raw,	0 },
3035  { "cpu",		s_arm_cpu,		0 },
3036  { "arch",		s_arm_arch,		0 },
3037  { "fpu",		s_arm_fpu,		0 },
3038  { "eabi_attribute",	s_arm_eabi_attribute,	0 },
3039#else
3040  { "word",	   cons, 4},
3041#endif
3042  { "extend",	   float_cons, 'x' },
3043  { "ldouble",	   float_cons, 'x' },
3044  { "packed",	   float_cons, 'p' },
3045  { 0, 0, 0 }
3046};
3047
3048/* Parser functions used exclusively in instruction operands.  */
3049
3050/* Generic immediate-value read function for use in insn parsing.
3051   STR points to the beginning of the immediate (the leading #);
3052   VAL receives the value; if the value is outside [MIN, MAX]
3053   issue an error.  PREFIX_OPT is true if the immediate prefix is
3054   optional.  */
3055
3056static int
3057parse_immediate (char **str, int *val, int min, int max,
3058		 bfd_boolean prefix_opt)
3059{
3060  expressionS exp;
3061  my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
3062  if (exp.X_op != O_constant)
3063    {
3064      inst.error = _("constant expression required");
3065      return FAIL;
3066    }
3067
3068  if (exp.X_add_number < min || exp.X_add_number > max)
3069    {
3070      inst.error = _("immediate value out of range");
3071      return FAIL;
3072    }
3073
3074  *val = exp.X_add_number;
3075  return SUCCESS;
3076}
3077
3078/* Returns the pseudo-register number of an FPA immediate constant,
3079   or FAIL if there isn't a valid constant here.  */
3080
3081static int
3082parse_fpa_immediate (char ** str)
3083{
3084  LITTLENUM_TYPE words[MAX_LITTLENUMS];
3085  char *	 save_in;
3086  expressionS	 exp;
3087  int		 i;
3088  int		 j;
3089
3090  /* First try and match exact strings, this is to guarantee
3091     that some formats will work even for cross assembly.  */
3092
3093  for (i = 0; fp_const[i]; i++)
3094    {
3095      if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
3096	{
3097	  char *start = *str;
3098
3099	  *str += strlen (fp_const[i]);
3100	  if (is_end_of_line[(unsigned char) **str])
3101	    return i + 8;
3102	  *str = start;
3103	}
3104    }
3105
3106  /* Just because we didn't get a match doesn't mean that the constant
3107     isn't valid, just that it is in a format that we don't
3108     automatically recognize.  Try parsing it with the standard
3109     expression routines.  */
3110
3111  memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
3112
3113  /* Look for a raw floating point number.  */
3114  if ((save_in = atof_ieee (*str, 'x', words)) != NULL
3115      && is_end_of_line[(unsigned char) *save_in])
3116    {
3117      for (i = 0; i < NUM_FLOAT_VALS; i++)
3118	{
3119	  for (j = 0; j < MAX_LITTLENUMS; j++)
3120	    {
3121	      if (words[j] != fp_values[i][j])
3122		break;
3123	    }
3124
3125	  if (j == MAX_LITTLENUMS)
3126	    {
3127	      *str = save_in;
3128	      return i + 8;
3129	    }
3130	}
3131    }
3132
3133  /* Try and parse a more complex expression, this will probably fail
3134     unless the code uses a floating point prefix (eg "0f").  */
3135  save_in = input_line_pointer;
3136  input_line_pointer = *str;
3137  if (expression (&exp) == absolute_section
3138      && exp.X_op == O_big
3139      && exp.X_add_number < 0)
3140    {
3141      /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
3142	 Ditto for 15.	*/
3143      if (gen_to_words (words, 5, (long) 15) == 0)
3144	{
3145	  for (i = 0; i < NUM_FLOAT_VALS; i++)
3146	    {
3147	      for (j = 0; j < MAX_LITTLENUMS; j++)
3148		{
3149		  if (words[j] != fp_values[i][j])
3150		    break;
3151		}
3152
3153	      if (j == MAX_LITTLENUMS)
3154		{
3155		  *str = input_line_pointer;
3156		  input_line_pointer = save_in;
3157		  return i + 8;
3158		}
3159	    }
3160	}
3161    }
3162
3163  *str = input_line_pointer;
3164  input_line_pointer = save_in;
3165  inst.error = _("invalid FPA immediate expression");
3166  return FAIL;
3167}
3168
3169/* Shift operands.  */
3170enum shift_kind
3171{
3172  SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
3173};
3174
3175struct asm_shift_name
3176{
3177  const char	  *name;
3178  enum shift_kind  kind;
3179};
3180
3181/* Third argument to parse_shift.  */
3182enum parse_shift_mode
3183{
3184  NO_SHIFT_RESTRICT,		/* Any kind of shift is accepted.  */
3185  SHIFT_IMMEDIATE,		/* Shift operand must be an immediate.	*/
3186  SHIFT_LSL_OR_ASR_IMMEDIATE,	/* Shift must be LSL or ASR immediate.	*/
3187  SHIFT_ASR_IMMEDIATE,		/* Shift must be ASR immediate.	 */
3188  SHIFT_LSL_IMMEDIATE,		/* Shift must be LSL immediate.	 */
3189};
3190
3191/* Parse a <shift> specifier on an ARM data processing instruction.
3192   This has three forms:
3193
3194     (LSL|LSR|ASL|ASR|ROR) Rs
3195     (LSL|LSR|ASL|ASR|ROR) #imm
3196     RRX
3197
3198   Note that ASL is assimilated to LSL in the instruction encoding, and
3199   RRX to ROR #0 (which cannot be written as such).  */
3200
3201static int
3202parse_shift (char **str, int i, enum parse_shift_mode mode)
3203{
3204  const struct asm_shift_name *shift_name;
3205  enum shift_kind shift;
3206  char *s = *str;
3207  char *p = s;
3208  int reg;
3209
3210  for (p = *str; ISALPHA (*p); p++)
3211    ;
3212
3213  if (p == *str)
3214    {
3215      inst.error = _("shift expression expected");
3216      return FAIL;
3217    }
3218
3219  shift_name = hash_find_n (arm_shift_hsh, *str, p - *str);
3220
3221  if (shift_name == NULL)
3222    {
3223      inst.error = _("shift expression expected");
3224      return FAIL;
3225    }
3226
3227  shift = shift_name->kind;
3228
3229  switch (mode)
3230    {
3231    case NO_SHIFT_RESTRICT:
3232    case SHIFT_IMMEDIATE:   break;
3233
3234    case SHIFT_LSL_OR_ASR_IMMEDIATE:
3235      if (shift != SHIFT_LSL && shift != SHIFT_ASR)
3236	{
3237	  inst.error = _("'LSL' or 'ASR' required");
3238	  return FAIL;
3239	}
3240      break;
3241
3242    case SHIFT_LSL_IMMEDIATE:
3243      if (shift != SHIFT_LSL)
3244	{
3245	  inst.error = _("'LSL' required");
3246	  return FAIL;
3247	}
3248      break;
3249
3250    case SHIFT_ASR_IMMEDIATE:
3251      if (shift != SHIFT_ASR)
3252	{
3253	  inst.error = _("'ASR' required");
3254	  return FAIL;
3255	}
3256      break;
3257
3258    default: abort ();
3259    }
3260
3261  if (shift != SHIFT_RRX)
3262    {
3263      /* Whitespace can appear here if the next thing is a bare digit.	*/
3264      skip_whitespace (p);
3265
3266      if (mode == NO_SHIFT_RESTRICT
3267	  && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
3268	{
3269	  inst.operands[i].imm = reg;
3270	  inst.operands[i].immisreg = 1;
3271	}
3272      else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3273	return FAIL;
3274    }
3275  inst.operands[i].shift_kind = shift;
3276  inst.operands[i].shifted = 1;
3277  *str = p;
3278  return SUCCESS;
3279}
3280
3281/* Parse a <shifter_operand> for an ARM data processing instruction:
3282
3283      #<immediate>
3284      #<immediate>, <rotate>
3285      <Rm>
3286      <Rm>, <shift>
3287
3288   where <shift> is defined by parse_shift above, and <rotate> is a
3289   multiple of 2 between 0 and 30.  Validation of immediate operands
3290   is deferred to md_apply_fix.  */
3291
3292static int
3293parse_shifter_operand (char **str, int i)
3294{
3295  int value;
3296  expressionS expr;
3297
3298  if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
3299    {
3300      inst.operands[i].reg = value;
3301      inst.operands[i].isreg = 1;
3302
3303      /* parse_shift will override this if appropriate */
3304      inst.reloc.exp.X_op = O_constant;
3305      inst.reloc.exp.X_add_number = 0;
3306
3307      if (skip_past_comma (str) == FAIL)
3308	return SUCCESS;
3309
3310      /* Shift operation on register.  */
3311      return parse_shift (str, i, NO_SHIFT_RESTRICT);
3312    }
3313
3314  if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
3315    return FAIL;
3316
3317  if (skip_past_comma (str) == SUCCESS)
3318    {
3319      /* #x, y -- ie explicit rotation by Y.  */
3320      if (my_get_expression (&expr, str, GE_NO_PREFIX))
3321	return FAIL;
3322
3323      if (expr.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
3324	{
3325	  inst.error = _("constant expression expected");
3326	  return FAIL;
3327	}
3328
3329      value = expr.X_add_number;
3330      if (value < 0 || value > 30 || value % 2 != 0)
3331	{
3332	  inst.error = _("invalid rotation");
3333	  return FAIL;
3334	}
3335      if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
3336	{
3337	  inst.error = _("invalid constant");
3338	  return FAIL;
3339	}
3340
3341      /* Convert to decoded value.  md_apply_fix will put it back.  */
3342      inst.reloc.exp.X_add_number
3343	= (((inst.reloc.exp.X_add_number << (32 - value))
3344	    | (inst.reloc.exp.X_add_number >> value)) & 0xffffffff);
3345    }
3346
3347  inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
3348  inst.reloc.pc_rel = 0;
3349  return SUCCESS;
3350}
3351
3352/* Parse all forms of an ARM address expression.  Information is written
3353   to inst.operands[i] and/or inst.reloc.
3354
3355   Preindexed addressing (.preind=1):
3356
3357   [Rn, #offset]       .reg=Rn .reloc.exp=offset
3358   [Rn, +/-Rm]	       .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3359   [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3360		       .shift_kind=shift .reloc.exp=shift_imm
3361
3362   These three may have a trailing ! which causes .writeback to be set also.
3363
3364   Postindexed addressing (.postind=1, .writeback=1):
3365
3366   [Rn], #offset       .reg=Rn .reloc.exp=offset
3367   [Rn], +/-Rm	       .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3368   [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
3369		       .shift_kind=shift .reloc.exp=shift_imm
3370
3371   Unindexed addressing (.preind=0, .postind=0):
3372
3373   [Rn], {option}      .reg=Rn .imm=option .immisreg=0
3374
3375   Other:
3376
3377   [Rn]{!}	       shorthand for [Rn,#0]{!}
3378   =immediate	       .isreg=0 .reloc.exp=immediate
3379   label	       .reg=PC .reloc.pc_rel=1 .reloc.exp=label
3380
3381  It is the caller's responsibility to check for addressing modes not
3382  supported by the instruction, and to set inst.reloc.type.  */
3383
3384static int
3385parse_address (char **str, int i)
3386{
3387  char *p = *str;
3388  int reg;
3389
3390  if (skip_past_char (&p, '[') == FAIL)
3391    {
3392      if (skip_past_char (&p, '=') == FAIL)
3393	{
3394	  /* bare address - translate to PC-relative offset */
3395	  inst.reloc.pc_rel = 1;
3396	  inst.operands[i].reg = REG_PC;
3397	  inst.operands[i].isreg = 1;
3398	  inst.operands[i].preind = 1;
3399	}
3400      /* else a load-constant pseudo op, no special treatment needed here */
3401
3402      if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
3403	return FAIL;
3404
3405      *str = p;
3406      return SUCCESS;
3407    }
3408
3409  if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
3410    {
3411      inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3412      return FAIL;
3413    }
3414  inst.operands[i].reg = reg;
3415  inst.operands[i].isreg = 1;
3416
3417  if (skip_past_comma (&p) == SUCCESS)
3418    {
3419      inst.operands[i].preind = 1;
3420
3421      if (*p == '+') p++;
3422      else if (*p == '-') p++, inst.operands[i].negative = 1;
3423
3424      if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
3425	{
3426	  inst.operands[i].imm = reg;
3427	  inst.operands[i].immisreg = 1;
3428
3429	  if (skip_past_comma (&p) == SUCCESS)
3430	    if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
3431	      return FAIL;
3432	}
3433      else
3434	{
3435	  if (inst.operands[i].negative)
3436	    {
3437	      inst.operands[i].negative = 0;
3438	      p--;
3439	    }
3440	  if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3441	    return FAIL;
3442	}
3443    }
3444
3445  if (skip_past_char (&p, ']') == FAIL)
3446    {
3447      inst.error = _("']' expected");
3448      return FAIL;
3449    }
3450
3451  if (skip_past_char (&p, '!') == SUCCESS)
3452    inst.operands[i].writeback = 1;
3453
3454  else if (skip_past_comma (&p) == SUCCESS)
3455    {
3456      if (skip_past_char (&p, '{') == SUCCESS)
3457	{
3458	  /* [Rn], {expr} - unindexed, with option */
3459	  if (parse_immediate (&p, &inst.operands[i].imm,
3460			       0, 255, TRUE) == FAIL)
3461	    return FAIL;
3462
3463	  if (skip_past_char (&p, '}') == FAIL)
3464	    {
3465	      inst.error = _("'}' expected at end of 'option' field");
3466	      return FAIL;
3467	    }
3468	  if (inst.operands[i].preind)
3469	    {
3470	      inst.error = _("cannot combine index with option");
3471	      return FAIL;
3472	    }
3473	  *str = p;
3474	  return SUCCESS;
3475	}
3476      else
3477	{
3478	  inst.operands[i].postind = 1;
3479	  inst.operands[i].writeback = 1;
3480
3481	  if (inst.operands[i].preind)
3482	    {
3483	      inst.error = _("cannot combine pre- and post-indexing");
3484	      return FAIL;
3485	    }
3486
3487	  if (*p == '+') p++;
3488	  else if (*p == '-') p++, inst.operands[i].negative = 1;
3489
3490	  if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
3491	    {
3492	      inst.operands[i].imm = reg;
3493	      inst.operands[i].immisreg = 1;
3494
3495	      if (skip_past_comma (&p) == SUCCESS)
3496		if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
3497		  return FAIL;
3498	    }
3499	  else
3500	    {
3501	      if (inst.operands[i].negative)
3502		{
3503		  inst.operands[i].negative = 0;
3504		  p--;
3505		}
3506	      if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
3507		return FAIL;
3508	    }
3509	}
3510    }
3511
3512  /* If at this point neither .preind nor .postind is set, we have a
3513     bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
3514  if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
3515    {
3516      inst.operands[i].preind = 1;
3517      inst.reloc.exp.X_op = O_constant;
3518      inst.reloc.exp.X_add_number = 0;
3519    }
3520  *str = p;
3521  return SUCCESS;
3522}
3523
3524/* Parse an operand for a MOVW or MOVT instruction.  */
3525static int
3526parse_half (char **str)
3527{
3528  char * p;
3529
3530  p = *str;
3531  skip_past_char (&p, '#');
3532  if (strncasecmp (p, ":lower16:", 9) == 0)
3533    inst.reloc.type = BFD_RELOC_ARM_MOVW;
3534  else if (strncasecmp (p, ":upper16:", 9) == 0)
3535    inst.reloc.type = BFD_RELOC_ARM_MOVT;
3536
3537  if (inst.reloc.type != BFD_RELOC_UNUSED)
3538    {
3539      p += 9;
3540      skip_whitespace(p);
3541    }
3542
3543  if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
3544    return FAIL;
3545
3546  if (inst.reloc.type == BFD_RELOC_UNUSED)
3547    {
3548      if (inst.reloc.exp.X_op != O_constant)
3549	{
3550	  inst.error = _("constant expression expected");
3551	  return FAIL;
3552	}
3553      if (inst.reloc.exp.X_add_number < 0
3554	  || inst.reloc.exp.X_add_number > 0xffff)
3555	{
3556	  inst.error = _("immediate value out of range");
3557	  return FAIL;
3558	}
3559    }
3560  *str = p;
3561  return SUCCESS;
3562}
3563
3564/* Miscellaneous. */
3565
3566/* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
3567   or a bitmask suitable to be or-ed into the ARM msr instruction.  */
3568static int
3569parse_psr (char **str)
3570{
3571  char *p;
3572  unsigned long psr_field;
3573  const struct asm_psr *psr;
3574  char *start;
3575
3576  /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
3577     feature for ease of use and backwards compatibility.  */
3578  p = *str;
3579  if (strncasecmp (p, "SPSR", 4) == 0)
3580    psr_field = SPSR_BIT;
3581  else if (strncasecmp (p, "CPSR", 4) == 0)
3582    psr_field = 0;
3583  else
3584    {
3585      start = p;
3586      do
3587	p++;
3588      while (ISALNUM (*p) || *p == '_');
3589
3590      psr = hash_find_n (arm_v7m_psr_hsh, start, p - start);
3591      if (!psr)
3592	return FAIL;
3593
3594      *str = p;
3595      return psr->field;
3596    }
3597
3598  p += 4;
3599  if (*p == '_')
3600    {
3601      /* A suffix follows.  */
3602      p++;
3603      start = p;
3604
3605      do
3606	p++;
3607      while (ISALNUM (*p) || *p == '_');
3608
3609      psr = hash_find_n (arm_psr_hsh, start, p - start);
3610      if (!psr)
3611	goto error;
3612
3613      psr_field |= psr->field;
3614    }
3615  else
3616    {
3617      if (ISALNUM (*p))
3618	goto error;    /* Garbage after "[CS]PSR".  */
3619
3620      psr_field |= (PSR_c | PSR_f);
3621    }
3622  *str = p;
3623  return psr_field;
3624
3625 error:
3626  inst.error = _("flag for {c}psr instruction expected");
3627  return FAIL;
3628}
3629
3630/* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
3631   value suitable for splatting into the AIF field of the instruction.	*/
3632
3633static int
3634parse_cps_flags (char **str)
3635{
3636  int val = 0;
3637  int saw_a_flag = 0;
3638  char *s = *str;
3639
3640  for (;;)
3641    switch (*s++)
3642      {
3643      case '\0': case ',':
3644	goto done;
3645
3646      case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
3647      case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
3648      case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
3649
3650      default:
3651	inst.error = _("unrecognized CPS flag");
3652	return FAIL;
3653      }
3654
3655 done:
3656  if (saw_a_flag == 0)
3657    {
3658      inst.error = _("missing CPS flags");
3659      return FAIL;
3660    }
3661
3662  *str = s - 1;
3663  return val;
3664}
3665
3666/* Parse an endian specifier ("BE" or "LE", case insensitive);
3667   returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
3668
3669static int
3670parse_endian_specifier (char **str)
3671{
3672  int little_endian;
3673  char *s = *str;
3674
3675  if (strncasecmp (s, "BE", 2))
3676    little_endian = 0;
3677  else if (strncasecmp (s, "LE", 2))
3678    little_endian = 1;
3679  else
3680    {
3681      inst.error = _("valid endian specifiers are be or le");
3682      return FAIL;
3683    }
3684
3685  if (ISALNUM (s[2]) || s[2] == '_')
3686    {
3687      inst.error = _("valid endian specifiers are be or le");
3688      return FAIL;
3689    }
3690
3691  *str = s + 2;
3692  return little_endian;
3693}
3694
3695/* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
3696   value suitable for poking into the rotate field of an sxt or sxta
3697   instruction, or FAIL on error.  */
3698
3699static int
3700parse_ror (char **str)
3701{
3702  int rot;
3703  char *s = *str;
3704
3705  if (strncasecmp (s, "ROR", 3) == 0)
3706    s += 3;
3707  else
3708    {
3709      inst.error = _("missing rotation field after comma");
3710      return FAIL;
3711    }
3712
3713  if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
3714    return FAIL;
3715
3716  switch (rot)
3717    {
3718    case  0: *str = s; return 0x0;
3719    case  8: *str = s; return 0x1;
3720    case 16: *str = s; return 0x2;
3721    case 24: *str = s; return 0x3;
3722
3723    default:
3724      inst.error = _("rotation can only be 0, 8, 16, or 24");
3725      return FAIL;
3726    }
3727}
3728
3729/* Parse a conditional code (from conds[] below).  The value returned is in the
3730   range 0 .. 14, or FAIL.  */
3731static int
3732parse_cond (char **str)
3733{
3734  char *p, *q;
3735  const struct asm_cond *c;
3736
3737  p = q = *str;
3738  while (ISALPHA (*q))
3739    q++;
3740
3741  c = hash_find_n (arm_cond_hsh, p, q - p);
3742  if (!c)
3743    {
3744      inst.error = _("condition required");
3745      return FAIL;
3746    }
3747
3748  *str = q;
3749  return c->value;
3750}
3751
3752/* Parse an option for a barrier instruction.  Returns the encoding for the
3753   option, or FAIL.  */
3754static int
3755parse_barrier (char **str)
3756{
3757  char *p, *q;
3758  const struct asm_barrier_opt *o;
3759
3760  p = q = *str;
3761  while (ISALPHA (*q))
3762    q++;
3763
3764  o = hash_find_n (arm_barrier_opt_hsh, p, q - p);
3765  if (!o)
3766    return FAIL;
3767
3768  *str = q;
3769  return o->value;
3770}
3771
3772/* Parse the operands of a table branch instruction.  Similar to a memory
3773   operand.  */
3774static int
3775parse_tb (char **str)
3776{
3777  char * p = *str;
3778  int reg;
3779
3780  if (skip_past_char (&p, '[') == FAIL)
3781    {
3782      inst.error = _("'[' expected");
3783      return FAIL;
3784    }
3785
3786  if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
3787    {
3788      inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3789      return FAIL;
3790    }
3791  inst.operands[0].reg = reg;
3792
3793  if (skip_past_comma (&p) == FAIL)
3794    {
3795      inst.error = _("',' expected");
3796      return FAIL;
3797    }
3798
3799  if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
3800    {
3801      inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
3802      return FAIL;
3803    }
3804  inst.operands[0].imm = reg;
3805
3806  if (skip_past_comma (&p) == SUCCESS)
3807    {
3808      if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
3809	return FAIL;
3810      if (inst.reloc.exp.X_add_number != 1)
3811	{
3812	  inst.error = _("invalid shift");
3813	  return FAIL;
3814	}
3815      inst.operands[0].shifted = 1;
3816    }
3817
3818  if (skip_past_char (&p, ']') == FAIL)
3819    {
3820      inst.error = _("']' expected");
3821      return FAIL;
3822    }
3823  *str = p;
3824  return SUCCESS;
3825}
3826
3827/* Matcher codes for parse_operands.  */
3828enum operand_parse_code
3829{
3830  OP_stop,	/* end of line */
3831
3832  OP_RR,	/* ARM register */
3833  OP_RRnpc,	/* ARM register, not r15 */
3834  OP_RRnpcb,	/* ARM register, not r15, in square brackets */
3835  OP_RRw,	/* ARM register, not r15, optional trailing ! */
3836  OP_RCP,	/* Coprocessor number */
3837  OP_RCN,	/* Coprocessor register */
3838  OP_RF,	/* FPA register */
3839  OP_RVS,	/* VFP single precision register */
3840  OP_RVD,	/* VFP double precision register */
3841  OP_RVC,	/* VFP control register */
3842  OP_RMF,	/* Maverick F register */
3843  OP_RMD,	/* Maverick D register */
3844  OP_RMFX,	/* Maverick FX register */
3845  OP_RMDX,	/* Maverick DX register */
3846  OP_RMAX,	/* Maverick AX register */
3847  OP_RMDS,	/* Maverick DSPSC register */
3848  OP_RIWR,	/* iWMMXt wR register */
3849  OP_RIWC,	/* iWMMXt wC register */
3850  OP_RIWG,	/* iWMMXt wCG register */
3851  OP_RXA,	/* XScale accumulator register */
3852
3853  OP_REGLST,	/* ARM register list */
3854  OP_VRSLST,	/* VFP single-precision register list */
3855  OP_VRDLST,	/* VFP double-precision register list */
3856
3857  OP_I7,	/* immediate value 0 .. 7 */
3858  OP_I15,	/*		   0 .. 15 */
3859  OP_I16,	/*		   1 .. 16 */
3860  OP_I31,	/*		   0 .. 31 */
3861  OP_I31w,	/*		   0 .. 31, optional trailing ! */
3862  OP_I32,	/*		   1 .. 32 */
3863  OP_I63s,	/*		 -64 .. 63 */
3864  OP_I255,	/*		   0 .. 255 */
3865
3866  OP_I4b,	/* immediate, prefix optional, 1 .. 4 */
3867  OP_I7b,	/*			       0 .. 7 */
3868  OP_I15b,	/*			       0 .. 15 */
3869  OP_I31b,	/*			       0 .. 31 */
3870
3871  OP_SH,	/* shifter operand */
3872  OP_ADDR,	/* Memory address expression (any mode) */
3873  OP_EXP,	/* arbitrary expression */
3874  OP_EXPi,	/* same, with optional immediate prefix */
3875  OP_EXPr,	/* same, with optional relocation suffix */
3876  OP_HALF,	/* 0 .. 65535 or low/high reloc.  */
3877
3878  OP_CPSF,	/* CPS flags */
3879  OP_ENDI,	/* Endianness specifier */
3880  OP_PSR,	/* CPSR/SPSR mask for msr */
3881  OP_COND,	/* conditional code */
3882  OP_TB,	/* Table branch.  */
3883
3884  OP_RRnpc_I0,	/* ARM register or literal 0 */
3885  OP_RR_EXr,	/* ARM register or expression with opt. reloc suff. */
3886  OP_RR_EXi,	/* ARM register or expression with imm prefix */
3887  OP_RF_IF,	/* FPA register or immediate */
3888  OP_RIWR_RIWC, /* iWMMXt R or C reg */
3889
3890  /* Optional operands.	 */
3891  OP_oI7b,	 /* immediate, prefix optional, 0 .. 7 */
3892  OP_oI31b,	 /*				0 .. 31 */
3893  OP_oIffffb,	 /*				0 .. 65535 */
3894  OP_oI255c,	 /*	  curly-brace enclosed, 0 .. 255 */
3895
3896  OP_oRR,	 /* ARM register */
3897  OP_oRRnpc,	 /* ARM register, not the PC */
3898  OP_oSHll,	 /* LSL immediate */
3899  OP_oSHar,	 /* ASR immediate */
3900  OP_oSHllar,	 /* LSL or ASR immediate */
3901  OP_oROR,	 /* ROR 0/8/16/24 */
3902  OP_oBARRIER,	 /* Option argument for a barrier instruction.  */
3903
3904  OP_FIRST_OPTIONAL = OP_oI7b
3905};
3906
3907/* Generic instruction operand parser.	This does no encoding and no
3908   semantic validation; it merely squirrels values away in the inst
3909   structure.  Returns SUCCESS or FAIL depending on whether the
3910   specified grammar matched.  */
3911static int
3912parse_operands (char *str, const unsigned char *pattern)
3913{
3914  unsigned const char *upat = pattern;
3915  char *backtrack_pos = 0;
3916  const char *backtrack_error = 0;
3917  int i, val, backtrack_index = 0;
3918
3919#define po_char_or_fail(chr) do {		\
3920  if (skip_past_char (&str, chr) == FAIL)	\
3921    goto bad_args;				\
3922} while (0)
3923
3924#define po_reg_or_fail(regtype) do {			\
3925  val = arm_reg_parse (&str, regtype);			\
3926  if (val == FAIL)					\
3927    {							\
3928      inst.error = _(reg_expected_msgs[regtype]);	\
3929      goto failure;					\
3930    }							\
3931  inst.operands[i].reg = val;				\
3932  inst.operands[i].isreg = 1;				\
3933} while (0)
3934
3935#define po_reg_or_goto(regtype, label) do {	\
3936  val = arm_reg_parse (&str, regtype);		\
3937  if (val == FAIL)				\
3938    goto label;					\
3939						\
3940  inst.operands[i].reg = val;			\
3941  inst.operands[i].isreg = 1;			\
3942} while (0)
3943
3944#define po_imm_or_fail(min, max, popt) do {			\
3945  if (parse_immediate (&str, &val, min, max, popt) == FAIL)	\
3946    goto failure;						\
3947  inst.operands[i].imm = val;					\
3948} while (0)
3949
3950#define po_misc_or_fail(expr) do {		\
3951  if (expr)					\
3952    goto failure;				\
3953} while (0)
3954
3955  skip_whitespace (str);
3956
3957  for (i = 0; upat[i] != OP_stop; i++)
3958    {
3959      if (upat[i] >= OP_FIRST_OPTIONAL)
3960	{
3961	  /* Remember where we are in case we need to backtrack.  */
3962	  assert (!backtrack_pos);
3963	  backtrack_pos = str;
3964	  backtrack_error = inst.error;
3965	  backtrack_index = i;
3966	}
3967
3968      if (i > 0)
3969	po_char_or_fail (',');
3970
3971      switch (upat[i])
3972	{
3973	  /* Registers */
3974	case OP_oRRnpc:
3975	case OP_RRnpc:
3976	case OP_oRR:
3977	case OP_RR:    po_reg_or_fail (REG_TYPE_RN);	  break;
3978	case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);	  break;
3979	case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);	  break;
3980	case OP_RF:    po_reg_or_fail (REG_TYPE_FN);	  break;
3981	case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);	  break;
3982	case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);	  break;
3983	case OP_RVC:   po_reg_or_fail (REG_TYPE_VFC);	  break;
3984	case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);	  break;
3985	case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);	  break;
3986	case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);	  break;
3987	case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);	  break;
3988	case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);	  break;
3989	case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);	  break;
3990	case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);	  break;
3991	case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);	  break;
3992	case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
3993	case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
3994
3995	case OP_RRnpcb:
3996	  po_char_or_fail ('[');
3997	  po_reg_or_fail  (REG_TYPE_RN);
3998	  po_char_or_fail (']');
3999	  break;
4000
4001	case OP_RRw:
4002	  po_reg_or_fail (REG_TYPE_RN);
4003	  if (skip_past_char (&str, '!') == SUCCESS)
4004	    inst.operands[i].writeback = 1;
4005	  break;
4006
4007	  /* Immediates */
4008	case OP_I7:	 po_imm_or_fail (  0,	   7, FALSE);	break;
4009	case OP_I15:	 po_imm_or_fail (  0,	  15, FALSE);	break;
4010	case OP_I16:	 po_imm_or_fail (  1,	  16, FALSE);	break;
4011	case OP_I31:	 po_imm_or_fail (  0,	  31, FALSE);	break;
4012	case OP_I32:	 po_imm_or_fail (  1,	  32, FALSE);	break;
4013	case OP_I63s:	 po_imm_or_fail (-64,	  63, FALSE);	break;
4014	case OP_I255:	 po_imm_or_fail (  0,	 255, FALSE);	break;
4015
4016	case OP_I4b:	 po_imm_or_fail (  1,	   4, TRUE);	break;
4017	case OP_oI7b:
4018	case OP_I7b:	 po_imm_or_fail (  0,	   7, TRUE);	break;
4019	case OP_I15b:	 po_imm_or_fail (  0,	  15, TRUE);	break;
4020	case OP_oI31b:
4021	case OP_I31b:	 po_imm_or_fail (  0,	  31, TRUE);	break;
4022	case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);	break;
4023
4024	  /* Immediate variants */
4025	case OP_oI255c:
4026	  po_char_or_fail ('{');
4027	  po_imm_or_fail (0, 255, TRUE);
4028	  po_char_or_fail ('}');
4029	  break;
4030
4031	case OP_I31w:
4032	  /* The expression parser chokes on a trailing !, so we have
4033	     to find it first and zap it.  */
4034	  {
4035	    char *s = str;
4036	    while (*s && *s != ',')
4037	      s++;
4038	    if (s[-1] == '!')
4039	      {
4040		s[-1] = '\0';
4041		inst.operands[i].writeback = 1;
4042	      }
4043	    po_imm_or_fail (0, 31, TRUE);
4044	    if (str == s - 1)
4045	      str = s;
4046	  }
4047	  break;
4048
4049	  /* Expressions */
4050	case OP_EXPi:	EXPi:
4051	  po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
4052					      GE_OPT_PREFIX));
4053	  break;
4054
4055	case OP_EXP:
4056	  po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
4057					      GE_NO_PREFIX));
4058	  break;
4059
4060	case OP_EXPr:	EXPr:
4061	  po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
4062					      GE_NO_PREFIX));
4063	  if (inst.reloc.exp.X_op == O_symbol)
4064	    {
4065	      val = parse_reloc (&str);
4066	      if (val == -1)
4067		{
4068		  inst.error = _("unrecognized relocation suffix");
4069		  goto failure;
4070		}
4071	      else if (val != BFD_RELOC_UNUSED)
4072		{
4073		  inst.operands[i].imm = val;
4074		  inst.operands[i].hasreloc = 1;
4075		}
4076	    }
4077	  break;
4078
4079	  /* Operand for MOVW or MOVT.  */
4080	case OP_HALF:
4081	  po_misc_or_fail (parse_half (&str));
4082	  break;
4083
4084	  /* Register or expression */
4085	case OP_RR_EXr:	  po_reg_or_goto (REG_TYPE_RN, EXPr); break;
4086	case OP_RR_EXi:	  po_reg_or_goto (REG_TYPE_RN, EXPi); break;
4087
4088	  /* Register or immediate */
4089	case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
4090	I0:		  po_imm_or_fail (0, 0, FALSE);	      break;
4091
4092	case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
4093	IF:
4094	  if (!is_immediate_prefix (*str))
4095	    goto bad_args;
4096	  str++;
4097	  val = parse_fpa_immediate (&str);
4098	  if (val == FAIL)
4099	    goto failure;
4100	  /* FPA immediates are encoded as registers 8-15.
4101	     parse_fpa_immediate has already applied the offset.  */
4102	  inst.operands[i].reg = val;
4103	  inst.operands[i].isreg = 1;
4104	  break;
4105
4106	  /* Two kinds of register */
4107	case OP_RIWR_RIWC:
4108	  {
4109	    struct reg_entry *rege = arm_reg_parse_multi (&str);
4110	    if (rege->type != REG_TYPE_MMXWR
4111		&& rege->type != REG_TYPE_MMXWC
4112		&& rege->type != REG_TYPE_MMXWCG)
4113	      {
4114		inst.error = _("iWMMXt data or control register expected");
4115		goto failure;
4116	      }
4117	    inst.operands[i].reg = rege->number;
4118	    inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
4119	  }
4120	  break;
4121
4122	  /* Misc */
4123	case OP_CPSF:	 val = parse_cps_flags (&str);		break;
4124	case OP_ENDI:	 val = parse_endian_specifier (&str);	break;
4125	case OP_oROR:	 val = parse_ror (&str);		break;
4126	case OP_PSR:	 val = parse_psr (&str);		break;
4127	case OP_COND:	 val = parse_cond (&str);		break;
4128	case OP_oBARRIER:val = parse_barrier (&str);		break;
4129
4130	case OP_TB:
4131	  po_misc_or_fail (parse_tb (&str));
4132	  break;
4133
4134	  /* Register lists */
4135	case OP_REGLST:
4136	  val = parse_reg_list (&str);
4137	  if (*str == '^')
4138	    {
4139	      inst.operands[1].writeback = 1;
4140	      str++;
4141	    }
4142	  break;
4143
4144	case OP_VRSLST:
4145	  val = parse_vfp_reg_list (&str, &inst.operands[i].reg, 0);
4146	  break;
4147
4148	case OP_VRDLST:
4149	  val = parse_vfp_reg_list (&str, &inst.operands[i].reg, 1);
4150	  break;
4151
4152	  /* Addressing modes */
4153	case OP_ADDR:
4154	  po_misc_or_fail (parse_address (&str, i));
4155	  break;
4156
4157	case OP_SH:
4158	  po_misc_or_fail (parse_shifter_operand (&str, i));
4159	  break;
4160
4161	case OP_oSHll:
4162	  po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
4163	  break;
4164
4165	case OP_oSHar:
4166	  po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
4167	  break;
4168
4169	case OP_oSHllar:
4170	  po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
4171	  break;
4172
4173	default:
4174	  as_fatal ("unhandled operand code %d", upat[i]);
4175	}
4176
4177      /* Various value-based sanity checks and shared operations.  We
4178	 do not signal immediate failures for the register constraints;
4179	 this allows a syntax error to take precedence.	 */
4180      switch (upat[i])
4181	{
4182	case OP_oRRnpc:
4183	case OP_RRnpc:
4184	case OP_RRnpcb:
4185	case OP_RRw:
4186	case OP_RRnpc_I0:
4187	  if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
4188	    inst.error = BAD_PC;
4189	  break;
4190
4191	case OP_CPSF:
4192	case OP_ENDI:
4193	case OP_oROR:
4194	case OP_PSR:
4195	case OP_COND:
4196	case OP_oBARRIER:
4197	case OP_REGLST:
4198	case OP_VRSLST:
4199	case OP_VRDLST:
4200	  if (val == FAIL)
4201	    goto failure;
4202	  inst.operands[i].imm = val;
4203	  break;
4204
4205	default:
4206	  break;
4207	}
4208
4209      /* If we get here, this operand was successfully parsed.	*/
4210      inst.operands[i].present = 1;
4211      continue;
4212
4213    bad_args:
4214      inst.error = BAD_ARGS;
4215
4216    failure:
4217      if (!backtrack_pos)
4218	{
4219	  /* The parse routine should already have set inst.error, but set a
4220	     defaut here just in case.  */
4221	  if (!inst.error)
4222	    inst.error = _("syntax error");
4223	  return FAIL;
4224	}
4225
4226      /* Do not backtrack over a trailing optional argument that
4227	 absorbed some text.  We will only fail again, with the
4228	 'garbage following instruction' error message, which is
4229	 probably less helpful than the current one.  */
4230      if (backtrack_index == i && backtrack_pos != str
4231	  && upat[i+1] == OP_stop)
4232	{
4233	  if (!inst.error)
4234	    inst.error = _("syntax error");
4235	  return FAIL;
4236	}
4237
4238      /* Try again, skipping the optional argument at backtrack_pos.  */
4239      str = backtrack_pos;
4240      inst.error = backtrack_error;
4241      inst.operands[backtrack_index].present = 0;
4242      i = backtrack_index;
4243      backtrack_pos = 0;
4244    }
4245
4246  /* Check that we have parsed all the arguments.  */
4247  if (*str != '\0' && !inst.error)
4248    inst.error = _("garbage following instruction");
4249
4250  return inst.error ? FAIL : SUCCESS;
4251}
4252
4253#undef po_char_or_fail
4254#undef po_reg_or_fail
4255#undef po_reg_or_goto
4256#undef po_imm_or_fail
4257
4258/* Shorthand macro for instruction encoding functions issuing errors.  */
4259#define constraint(expr, err) do {		\
4260  if (expr)					\
4261    {						\
4262      inst.error = err;				\
4263      return;					\
4264    }						\
4265} while (0)
4266
4267/* Functions for operand encoding.  ARM, then Thumb.  */
4268
4269#define rotate_left(v, n) (v << (n % 32) | v >> ((32 - n) % 32))
4270
4271/* If VAL can be encoded in the immediate field of an ARM instruction,
4272   return the encoded form.  Otherwise, return FAIL.  */
4273
4274static unsigned int
4275encode_arm_immediate (unsigned int val)
4276{
4277  unsigned int a, i;
4278
4279  for (i = 0; i < 32; i += 2)
4280    if ((a = rotate_left (val, i)) <= 0xff)
4281      return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
4282
4283  return FAIL;
4284}
4285
4286/* If VAL can be encoded in the immediate field of a Thumb32 instruction,
4287   return the encoded form.  Otherwise, return FAIL.  */
4288static unsigned int
4289encode_thumb32_immediate (unsigned int val)
4290{
4291  unsigned int a, i;
4292
4293  if (val <= 0xff)
4294    return val;
4295
4296  for (i = 1; i <= 24; i++)
4297    {
4298      a = val >> i;
4299      if ((val & ~(0xff << i)) == 0)
4300	return ((val >> i) & 0x7f) | ((32 - i) << 7);
4301    }
4302
4303  a = val & 0xff;
4304  if (val == ((a << 16) | a))
4305    return 0x100 | a;
4306  if (val == ((a << 24) | (a << 16) | (a << 8) | a))
4307    return 0x300 | a;
4308
4309  a = val & 0xff00;
4310  if (val == ((a << 16) | a))
4311    return 0x200 | (a >> 8);
4312
4313  return FAIL;
4314}
4315/* Encode a VFP SP register number into inst.instruction.  */
4316
4317static void
4318encode_arm_vfp_sp_reg (int reg, enum vfp_sp_reg_pos pos)
4319{
4320  switch (pos)
4321    {
4322    case VFP_REG_Sd:
4323      inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
4324      break;
4325
4326    case VFP_REG_Sn:
4327      inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
4328      break;
4329
4330    case VFP_REG_Sm:
4331      inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
4332      break;
4333
4334    default:
4335      abort ();
4336    }
4337}
4338
4339/* Encode a <shift> in an ARM-format instruction.  The immediate,
4340   if any, is handled by md_apply_fix.	 */
4341static void
4342encode_arm_shift (int i)
4343{
4344  if (inst.operands[i].shift_kind == SHIFT_RRX)
4345    inst.instruction |= SHIFT_ROR << 5;
4346  else
4347    {
4348      inst.instruction |= inst.operands[i].shift_kind << 5;
4349      if (inst.operands[i].immisreg)
4350	{
4351	  inst.instruction |= SHIFT_BY_REG;
4352	  inst.instruction |= inst.operands[i].imm << 8;
4353	}
4354      else
4355	inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
4356    }
4357}
4358
4359static void
4360encode_arm_shifter_operand (int i)
4361{
4362  if (inst.operands[i].isreg)
4363    {
4364      inst.instruction |= inst.operands[i].reg;
4365      encode_arm_shift (i);
4366    }
4367  else
4368    inst.instruction |= INST_IMMEDIATE;
4369}
4370
4371/* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
4372static void
4373encode_arm_addr_mode_common (int i, bfd_boolean is_t)
4374{
4375  assert (inst.operands[i].isreg);
4376  inst.instruction |= inst.operands[i].reg << 16;
4377
4378  if (inst.operands[i].preind)
4379    {
4380      if (is_t)
4381	{
4382	  inst.error = _("instruction does not accept preindexed addressing");
4383	  return;
4384	}
4385      inst.instruction |= PRE_INDEX;
4386      if (inst.operands[i].writeback)
4387	inst.instruction |= WRITE_BACK;
4388
4389    }
4390  else if (inst.operands[i].postind)
4391    {
4392      assert (inst.operands[i].writeback);
4393      if (is_t)
4394	inst.instruction |= WRITE_BACK;
4395    }
4396  else /* unindexed - only for coprocessor */
4397    {
4398      inst.error = _("instruction does not accept unindexed addressing");
4399      return;
4400    }
4401
4402  if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
4403      && (((inst.instruction & 0x000f0000) >> 16)
4404	  == ((inst.instruction & 0x0000f000) >> 12)))
4405    as_warn ((inst.instruction & LOAD_BIT)
4406	     ? _("destination register same as write-back base")
4407	     : _("source register same as write-back base"));
4408}
4409
4410/* inst.operands[i] was set up by parse_address.  Encode it into an
4411   ARM-format mode 2 load or store instruction.	 If is_t is true,
4412   reject forms that cannot be used with a T instruction (i.e. not
4413   post-indexed).  */
4414static void
4415encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
4416{
4417  encode_arm_addr_mode_common (i, is_t);
4418
4419  if (inst.operands[i].immisreg)
4420    {
4421      inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
4422      inst.instruction |= inst.operands[i].imm;
4423      if (!inst.operands[i].negative)
4424	inst.instruction |= INDEX_UP;
4425      if (inst.operands[i].shifted)
4426	{
4427	  if (inst.operands[i].shift_kind == SHIFT_RRX)
4428	    inst.instruction |= SHIFT_ROR << 5;
4429	  else
4430	    {
4431	      inst.instruction |= inst.operands[i].shift_kind << 5;
4432	      inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
4433	    }
4434	}
4435    }
4436  else /* immediate offset in inst.reloc */
4437    {
4438      if (inst.reloc.type == BFD_RELOC_UNUSED)
4439	inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
4440    }
4441}
4442
4443/* inst.operands[i] was set up by parse_address.  Encode it into an
4444   ARM-format mode 3 load or store instruction.	 Reject forms that
4445   cannot be used with such instructions.  If is_t is true, reject
4446   forms that cannot be used with a T instruction (i.e. not
4447   post-indexed).  */
4448static void
4449encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
4450{
4451  if (inst.operands[i].immisreg && inst.operands[i].shifted)
4452    {
4453      inst.error = _("instruction does not accept scaled register index");
4454      return;
4455    }
4456
4457  encode_arm_addr_mode_common (i, is_t);
4458
4459  if (inst.operands[i].immisreg)
4460    {
4461      inst.instruction |= inst.operands[i].imm;
4462      if (!inst.operands[i].negative)
4463	inst.instruction |= INDEX_UP;
4464    }
4465  else /* immediate offset in inst.reloc */
4466    {
4467      inst.instruction |= HWOFFSET_IMM;
4468      if (inst.reloc.type == BFD_RELOC_UNUSED)
4469	inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
4470    }
4471}
4472
4473/* inst.operands[i] was set up by parse_address.  Encode it into an
4474   ARM-format instruction.  Reject all forms which cannot be encoded
4475   into a coprocessor load/store instruction.  If wb_ok is false,
4476   reject use of writeback; if unind_ok is false, reject use of
4477   unindexed addressing.  If reloc_override is not 0, use it instead
4478   of BFD_ARM_CP_OFF_IMM.  */
4479
4480static int
4481encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
4482{
4483  inst.instruction |= inst.operands[i].reg << 16;
4484
4485  assert (!(inst.operands[i].preind && inst.operands[i].postind));
4486
4487  if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
4488    {
4489      assert (!inst.operands[i].writeback);
4490      if (!unind_ok)
4491	{
4492	  inst.error = _("instruction does not support unindexed addressing");
4493	  return FAIL;
4494	}
4495      inst.instruction |= inst.operands[i].imm;
4496      inst.instruction |= INDEX_UP;
4497      return SUCCESS;
4498    }
4499
4500  if (inst.operands[i].preind)
4501    inst.instruction |= PRE_INDEX;
4502
4503  if (inst.operands[i].writeback)
4504    {
4505      if (inst.operands[i].reg == REG_PC)
4506	{
4507	  inst.error = _("pc may not be used with write-back");
4508	  return FAIL;
4509	}
4510      if (!wb_ok)
4511	{
4512	  inst.error = _("instruction does not support writeback");
4513	  return FAIL;
4514	}
4515      inst.instruction |= WRITE_BACK;
4516    }
4517
4518  if (reloc_override)
4519    inst.reloc.type = reloc_override;
4520  else if (thumb_mode)
4521    inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
4522  else
4523    inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
4524  return SUCCESS;
4525}
4526
4527/* inst.reloc.exp describes an "=expr" load pseudo-operation.
4528   Determine whether it can be performed with a move instruction; if
4529   it can, convert inst.instruction to that move instruction and
4530   return 1; if it can't, convert inst.instruction to a literal-pool
4531   load and return 0.  If this is not a valid thing to do in the
4532   current context, set inst.error and return 1.
4533
4534   inst.operands[i] describes the destination register.	 */
4535
4536static int
4537move_or_literal_pool (int i, bfd_boolean thumb_p, bfd_boolean mode_3)
4538{
4539  unsigned long tbit;
4540
4541  if (thumb_p)
4542    tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
4543  else
4544    tbit = LOAD_BIT;
4545
4546  if ((inst.instruction & tbit) == 0)
4547    {
4548      inst.error = _("invalid pseudo operation");
4549      return 1;
4550    }
4551  if (inst.reloc.exp.X_op != O_constant && inst.reloc.exp.X_op != O_symbol)
4552    {
4553      inst.error = _("constant expression expected");
4554      return 1;
4555    }
4556  if (inst.reloc.exp.X_op == O_constant)
4557    {
4558      if (thumb_p)
4559	{
4560	  if (!unified_syntax && (inst.reloc.exp.X_add_number & ~0xFF) == 0)
4561	    {
4562	      /* This can be done with a mov(1) instruction.  */
4563	      inst.instruction	= T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
4564	      inst.instruction |= inst.reloc.exp.X_add_number;
4565	      return 1;
4566	    }
4567	}
4568      else
4569	{
4570	  int value = encode_arm_immediate (inst.reloc.exp.X_add_number);
4571	  if (value != FAIL)
4572	    {
4573	      /* This can be done with a mov instruction.  */
4574	      inst.instruction &= LITERAL_MASK;
4575	      inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
4576	      inst.instruction |= value & 0xfff;
4577	      return 1;
4578	    }
4579
4580	  value = encode_arm_immediate (~inst.reloc.exp.X_add_number);
4581	  if (value != FAIL)
4582	    {
4583	      /* This can be done with a mvn instruction.  */
4584	      inst.instruction &= LITERAL_MASK;
4585	      inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
4586	      inst.instruction |= value & 0xfff;
4587	      return 1;
4588	    }
4589	}
4590    }
4591
4592  if (add_to_lit_pool () == FAIL)
4593    {
4594      inst.error = _("literal pool insertion failed");
4595      return 1;
4596    }
4597  inst.operands[1].reg = REG_PC;
4598  inst.operands[1].isreg = 1;
4599  inst.operands[1].preind = 1;
4600  inst.reloc.pc_rel = 1;
4601  inst.reloc.type = (thumb_p
4602		     ? BFD_RELOC_ARM_THUMB_OFFSET
4603		     : (mode_3
4604			? BFD_RELOC_ARM_HWLITERAL
4605			: BFD_RELOC_ARM_LITERAL));
4606  return 0;
4607}
4608
4609/* Functions for instruction encoding, sorted by subarchitecture.
4610   First some generics; their names are taken from the conventional
4611   bit positions for register arguments in ARM format instructions.  */
4612
4613static void
4614do_noargs (void)
4615{
4616}
4617
4618static void
4619do_rd (void)
4620{
4621  inst.instruction |= inst.operands[0].reg << 12;
4622}
4623
4624static void
4625do_rd_rm (void)
4626{
4627  inst.instruction |= inst.operands[0].reg << 12;
4628  inst.instruction |= inst.operands[1].reg;
4629}
4630
4631static void
4632do_rd_rn (void)
4633{
4634  inst.instruction |= inst.operands[0].reg << 12;
4635  inst.instruction |= inst.operands[1].reg << 16;
4636}
4637
4638static void
4639do_rn_rd (void)
4640{
4641  inst.instruction |= inst.operands[0].reg << 16;
4642  inst.instruction |= inst.operands[1].reg << 12;
4643}
4644
4645static void
4646do_rd_rm_rn (void)
4647{
4648  unsigned Rn = inst.operands[2].reg;
4649  /* Enforce resutrictions on SWP instruction.  */
4650  if ((inst.instruction & 0x0fbfffff) == 0x01000090)
4651    constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
4652		_("Rn must not overlap other operands"));
4653  inst.instruction |= inst.operands[0].reg << 12;
4654  inst.instruction |= inst.operands[1].reg;
4655  inst.instruction |= Rn << 16;
4656}
4657
4658static void
4659do_rd_rn_rm (void)
4660{
4661  inst.instruction |= inst.operands[0].reg << 12;
4662  inst.instruction |= inst.operands[1].reg << 16;
4663  inst.instruction |= inst.operands[2].reg;
4664}
4665
4666static void
4667do_rm_rd_rn (void)
4668{
4669  inst.instruction |= inst.operands[0].reg;
4670  inst.instruction |= inst.operands[1].reg << 12;
4671  inst.instruction |= inst.operands[2].reg << 16;
4672}
4673
4674static void
4675do_imm0 (void)
4676{
4677  inst.instruction |= inst.operands[0].imm;
4678}
4679
4680static void
4681do_rd_cpaddr (void)
4682{
4683  inst.instruction |= inst.operands[0].reg << 12;
4684  encode_arm_cp_address (1, TRUE, TRUE, 0);
4685}
4686
4687/* ARM instructions, in alphabetical order by function name (except
4688   that wrapper functions appear immediately after the function they
4689   wrap).  */
4690
4691/* This is a pseudo-op of the form "adr rd, label" to be converted
4692   into a relative address of the form "add rd, pc, #label-.-8".  */
4693
4694static void
4695do_adr (void)
4696{
4697  inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
4698
4699  /* Frag hacking will turn this into a sub instruction if the offset turns
4700     out to be negative.  */
4701  inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
4702  inst.reloc.pc_rel = 1;
4703  inst.reloc.exp.X_add_number -= 8;
4704}
4705
4706/* This is a pseudo-op of the form "adrl rd, label" to be converted
4707   into a relative address of the form:
4708   add rd, pc, #low(label-.-8)"
4709   add rd, rd, #high(label-.-8)"  */
4710
4711static void
4712do_adrl (void)
4713{
4714  inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
4715
4716  /* Frag hacking will turn this into a sub instruction if the offset turns
4717     out to be negative.  */
4718  inst.reloc.type	       = BFD_RELOC_ARM_ADRL_IMMEDIATE;
4719  inst.reloc.pc_rel	       = 1;
4720  inst.size		       = INSN_SIZE * 2;
4721  inst.reloc.exp.X_add_number -= 8;
4722}
4723
4724static void
4725do_arit (void)
4726{
4727  if (!inst.operands[1].present)
4728    inst.operands[1].reg = inst.operands[0].reg;
4729  inst.instruction |= inst.operands[0].reg << 12;
4730  inst.instruction |= inst.operands[1].reg << 16;
4731  encode_arm_shifter_operand (2);
4732}
4733
4734static void
4735do_barrier (void)
4736{
4737  if (inst.operands[0].present)
4738    {
4739      constraint ((inst.instruction & 0xf0) != 0x40
4740		  && (inst.instruction & 0xf0) != 0x50
4741		  && inst.operands[0].imm != 0xf,
4742		  "bad barrier type");
4743      inst.instruction |= inst.operands[0].imm;
4744    }
4745  else
4746    inst.instruction |= 0xf;
4747}
4748
4749static void
4750do_bfc (void)
4751{
4752  unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
4753  constraint (msb > 32, _("bit-field extends past end of register"));
4754  /* The instruction encoding stores the LSB and MSB,
4755     not the LSB and width.  */
4756  inst.instruction |= inst.operands[0].reg << 12;
4757  inst.instruction |= inst.operands[1].imm << 7;
4758  inst.instruction |= (msb - 1) << 16;
4759}
4760
4761static void
4762do_bfi (void)
4763{
4764  unsigned int msb;
4765
4766  /* #0 in second position is alternative syntax for bfc, which is
4767     the same instruction but with REG_PC in the Rm field.  */
4768  if (!inst.operands[1].isreg)
4769    inst.operands[1].reg = REG_PC;
4770
4771  msb = inst.operands[2].imm + inst.operands[3].imm;
4772  constraint (msb > 32, _("bit-field extends past end of register"));
4773  /* The instruction encoding stores the LSB and MSB,
4774     not the LSB and width.  */
4775  inst.instruction |= inst.operands[0].reg << 12;
4776  inst.instruction |= inst.operands[1].reg;
4777  inst.instruction |= inst.operands[2].imm << 7;
4778  inst.instruction |= (msb - 1) << 16;
4779}
4780
4781static void
4782do_bfx (void)
4783{
4784  constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
4785	      _("bit-field extends past end of register"));
4786  inst.instruction |= inst.operands[0].reg << 12;
4787  inst.instruction |= inst.operands[1].reg;
4788  inst.instruction |= inst.operands[2].imm << 7;
4789  inst.instruction |= (inst.operands[3].imm - 1) << 16;
4790}
4791
4792/* ARM V5 breakpoint instruction (argument parse)
4793     BKPT <16 bit unsigned immediate>
4794     Instruction is not conditional.
4795	The bit pattern given in insns[] has the COND_ALWAYS condition,
4796	and it is an error if the caller tried to override that.  */
4797
4798static void
4799do_bkpt (void)
4800{
4801  /* Top 12 of 16 bits to bits 19:8.  */
4802  inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
4803
4804  /* Bottom 4 of 16 bits to bits 3:0.  */
4805  inst.instruction |= inst.operands[0].imm & 0xf;
4806}
4807
4808static void
4809encode_branch (int default_reloc)
4810{
4811  if (inst.operands[0].hasreloc)
4812    {
4813      constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32,
4814		  _("the only suffix valid here is '(plt)'"));
4815      inst.reloc.type	= BFD_RELOC_ARM_PLT32;
4816    }
4817  else
4818    {
4819      inst.reloc.type = default_reloc;
4820    }
4821  inst.reloc.pc_rel = 1;
4822}
4823
4824static void
4825do_branch (void)
4826{
4827#ifdef OBJ_ELF
4828  if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
4829    encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
4830  else
4831#endif
4832    encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
4833}
4834
4835static void
4836do_bl (void)
4837{
4838#ifdef OBJ_ELF
4839  if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
4840    {
4841      if (inst.cond == COND_ALWAYS)
4842	encode_branch (BFD_RELOC_ARM_PCREL_CALL);
4843      else
4844	encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
4845    }
4846  else
4847#endif
4848    encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
4849}
4850
4851/* ARM V5 branch-link-exchange instruction (argument parse)
4852     BLX <target_addr>		ie BLX(1)
4853     BLX{<condition>} <Rm>	ie BLX(2)
4854   Unfortunately, there are two different opcodes for this mnemonic.
4855   So, the insns[].value is not used, and the code here zaps values
4856	into inst.instruction.
4857   Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
4858
4859static void
4860do_blx (void)
4861{
4862  if (inst.operands[0].isreg)
4863    {
4864      /* Arg is a register; the opcode provided by insns[] is correct.
4865	 It is not illegal to do "blx pc", just useless.  */
4866      if (inst.operands[0].reg == REG_PC)
4867	as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
4868
4869      inst.instruction |= inst.operands[0].reg;
4870    }
4871  else
4872    {
4873      /* Arg is an address; this instruction cannot be executed
4874	 conditionally, and the opcode must be adjusted.  */
4875      constraint (inst.cond != COND_ALWAYS, BAD_COND);
4876      inst.instruction = 0xfa000000;
4877#ifdef OBJ_ELF
4878      if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
4879	encode_branch (BFD_RELOC_ARM_PCREL_CALL);
4880      else
4881#endif
4882	encode_branch (BFD_RELOC_ARM_PCREL_BLX);
4883    }
4884}
4885
4886static void
4887do_bx (void)
4888{
4889  if (inst.operands[0].reg == REG_PC)
4890    as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
4891
4892  inst.instruction |= inst.operands[0].reg;
4893}
4894
4895
4896/* ARM v5TEJ.  Jump to Jazelle code.  */
4897
4898static void
4899do_bxj (void)
4900{
4901  if (inst.operands[0].reg == REG_PC)
4902    as_tsktsk (_("use of r15 in bxj is not really useful"));
4903
4904  inst.instruction |= inst.operands[0].reg;
4905}
4906
4907/* Co-processor data operation:
4908      CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
4909      CDP2	<coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}	 */
4910static void
4911do_cdp (void)
4912{
4913  inst.instruction |= inst.operands[0].reg << 8;
4914  inst.instruction |= inst.operands[1].imm << 20;
4915  inst.instruction |= inst.operands[2].reg << 12;
4916  inst.instruction |= inst.operands[3].reg << 16;
4917  inst.instruction |= inst.operands[4].reg;
4918  inst.instruction |= inst.operands[5].imm << 5;
4919}
4920
4921static void
4922do_cmp (void)
4923{
4924  inst.instruction |= inst.operands[0].reg << 16;
4925  encode_arm_shifter_operand (1);
4926}
4927
4928/* Transfer between coprocessor and ARM registers.
4929   MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
4930   MRC2
4931   MCR{cond}
4932   MCR2
4933
4934   No special properties.  */
4935
4936static void
4937do_co_reg (void)
4938{
4939  inst.instruction |= inst.operands[0].reg << 8;
4940  inst.instruction |= inst.operands[1].imm << 21;
4941  inst.instruction |= inst.operands[2].reg << 12;
4942  inst.instruction |= inst.operands[3].reg << 16;
4943  inst.instruction |= inst.operands[4].reg;
4944  inst.instruction |= inst.operands[5].imm << 5;
4945}
4946
4947/* Transfer between coprocessor register and pair of ARM registers.
4948   MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
4949   MCRR2
4950   MRRC{cond}
4951   MRRC2
4952
4953   Two XScale instructions are special cases of these:
4954
4955     MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
4956     MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
4957
4958   Result unpredicatable if Rd or Rn is R15.  */
4959
4960static void
4961do_co_reg2c (void)
4962{
4963  inst.instruction |= inst.operands[0].reg << 8;
4964  inst.instruction |= inst.operands[1].imm << 4;
4965  inst.instruction |= inst.operands[2].reg << 12;
4966  inst.instruction |= inst.operands[3].reg << 16;
4967  inst.instruction |= inst.operands[4].reg;
4968}
4969
4970static void
4971do_cpsi (void)
4972{
4973  inst.instruction |= inst.operands[0].imm << 6;
4974  inst.instruction |= inst.operands[1].imm;
4975}
4976
4977static void
4978do_dbg (void)
4979{
4980  inst.instruction |= inst.operands[0].imm;
4981}
4982
4983static void
4984do_it (void)
4985{
4986  /* There is no IT instruction in ARM mode.  We
4987     process it but do not generate code for it.  */
4988  inst.size = 0;
4989}
4990
4991static void
4992do_ldmstm (void)
4993{
4994  int base_reg = inst.operands[0].reg;
4995  int range = inst.operands[1].imm;
4996
4997  inst.instruction |= base_reg << 16;
4998  inst.instruction |= range;
4999
5000  if (inst.operands[1].writeback)
5001    inst.instruction |= LDM_TYPE_2_OR_3;
5002
5003  if (inst.operands[0].writeback)
5004    {
5005      inst.instruction |= WRITE_BACK;
5006      /* Check for unpredictable uses of writeback.  */
5007      if (inst.instruction & LOAD_BIT)
5008	{
5009	  /* Not allowed in LDM type 2.	 */
5010	  if ((inst.instruction & LDM_TYPE_2_OR_3)
5011	      && ((range & (1 << REG_PC)) == 0))
5012	    as_warn (_("writeback of base register is UNPREDICTABLE"));
5013	  /* Only allowed if base reg not in list for other types.  */
5014	  else if (range & (1 << base_reg))
5015	    as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
5016	}
5017      else /* STM.  */
5018	{
5019	  /* Not allowed for type 2.  */
5020	  if (inst.instruction & LDM_TYPE_2_OR_3)
5021	    as_warn (_("writeback of base register is UNPREDICTABLE"));
5022	  /* Only allowed if base reg not in list, or first in list.  */
5023	  else if ((range & (1 << base_reg))
5024		   && (range & ((1 << base_reg) - 1)))
5025	    as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
5026	}
5027    }
5028}
5029
5030/* ARMv5TE load-consecutive (argument parse)
5031   Mode is like LDRH.
5032
5033     LDRccD R, mode
5034     STRccD R, mode.  */
5035
5036static void
5037do_ldrd (void)
5038{
5039  constraint (inst.operands[0].reg % 2 != 0,
5040	      _("first destination register must be even"));
5041  constraint (inst.operands[1].present
5042	      && inst.operands[1].reg != inst.operands[0].reg + 1,
5043	      _("can only load two consecutive registers"));
5044  constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
5045  constraint (!inst.operands[2].isreg, _("'[' expected"));
5046
5047  if (!inst.operands[1].present)
5048    inst.operands[1].reg = inst.operands[0].reg + 1;
5049
5050  if (inst.instruction & LOAD_BIT)
5051    {
5052      /* encode_arm_addr_mode_3 will diagnose overlap between the base
5053	 register and the first register written; we have to diagnose
5054	 overlap between the base and the second register written here.	 */
5055
5056      if (inst.operands[2].reg == inst.operands[1].reg
5057	  && (inst.operands[2].writeback || inst.operands[2].postind))
5058	as_warn (_("base register written back, and overlaps "
5059		   "second destination register"));
5060
5061      /* For an index-register load, the index register must not overlap the
5062	 destination (even if not write-back).	*/
5063      else if (inst.operands[2].immisreg
5064	       && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
5065		   || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
5066	as_warn (_("index register overlaps destination register"));
5067    }
5068
5069  inst.instruction |= inst.operands[0].reg << 12;
5070  encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
5071}
5072
5073static void
5074do_ldrex (void)
5075{
5076  constraint (!inst.operands[1].isreg || !inst.operands[1].preind
5077	      || inst.operands[1].postind || inst.operands[1].writeback
5078	      || inst.operands[1].immisreg || inst.operands[1].shifted
5079	      || inst.operands[1].negative
5080	      /* This can arise if the programmer has written
5081		   strex rN, rM, foo
5082		 or if they have mistakenly used a register name as the last
5083		 operand,  eg:
5084		   strex rN, rM, rX
5085		 It is very difficult to distinguish between these two cases
5086		 because "rX" might actually be a label. ie the register
5087		 name has been occluded by a symbol of the same name. So we
5088		 just generate a general 'bad addressing mode' type error
5089		 message and leave it up to the programmer to discover the
5090		 true cause and fix their mistake.  */
5091	      || (inst.operands[1].reg == REG_PC),
5092	      BAD_ADDR_MODE);
5093
5094  constraint (inst.reloc.exp.X_op != O_constant
5095	      || inst.reloc.exp.X_add_number != 0,
5096	      _("offset must be zero in ARM encoding"));
5097
5098  inst.instruction |= inst.operands[0].reg << 12;
5099  inst.instruction |= inst.operands[1].reg << 16;
5100  inst.reloc.type = BFD_RELOC_UNUSED;
5101}
5102
5103static void
5104do_ldrexd (void)
5105{
5106  constraint (inst.operands[0].reg % 2 != 0,
5107	      _("even register required"));
5108  constraint (inst.operands[1].present
5109	      && inst.operands[1].reg != inst.operands[0].reg + 1,
5110	      _("can only load two consecutive registers"));
5111  /* If op 1 were present and equal to PC, this function wouldn't
5112     have been called in the first place.  */
5113  constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
5114
5115  inst.instruction |= inst.operands[0].reg << 12;
5116  inst.instruction |= inst.operands[2].reg << 16;
5117}
5118
5119static void
5120do_ldst (void)
5121{
5122  inst.instruction |= inst.operands[0].reg << 12;
5123  if (!inst.operands[1].isreg)
5124    if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/FALSE))
5125      return;
5126  encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
5127}
5128
5129static void
5130do_ldstt (void)
5131{
5132  /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
5133     reject [Rn,...].  */
5134  if (inst.operands[1].preind)
5135    {
5136      constraint (inst.reloc.exp.X_op != O_constant ||
5137		  inst.reloc.exp.X_add_number != 0,
5138		  _("this instruction requires a post-indexed address"));
5139
5140      inst.operands[1].preind = 0;
5141      inst.operands[1].postind = 1;
5142      inst.operands[1].writeback = 1;
5143    }
5144  inst.instruction |= inst.operands[0].reg << 12;
5145  encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
5146}
5147
5148/* Halfword and signed-byte load/store operations.  */
5149
5150static void
5151do_ldstv4 (void)
5152{
5153  inst.instruction |= inst.operands[0].reg << 12;
5154  if (!inst.operands[1].isreg)
5155    if (move_or_literal_pool (0, /*thumb_p=*/FALSE, /*mode_3=*/TRUE))
5156      return;
5157  encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
5158}
5159
5160static void
5161do_ldsttv4 (void)
5162{
5163  /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
5164     reject [Rn,...].  */
5165  if (inst.operands[1].preind)
5166    {
5167      constraint (inst.reloc.exp.X_op != O_constant ||
5168		  inst.reloc.exp.X_add_number != 0,
5169		  _("this instruction requires a post-indexed address"));
5170
5171      inst.operands[1].preind = 0;
5172      inst.operands[1].postind = 1;
5173      inst.operands[1].writeback = 1;
5174    }
5175  inst.instruction |= inst.operands[0].reg << 12;
5176  encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
5177}
5178
5179/* Co-processor register load/store.
5180   Format: <LDC|STC>{cond}[L] CP#,CRd,<address>	 */
5181static void
5182do_lstc (void)
5183{
5184  inst.instruction |= inst.operands[0].reg << 8;
5185  inst.instruction |= inst.operands[1].reg << 12;
5186  encode_arm_cp_address (2, TRUE, TRUE, 0);
5187}
5188
5189static void
5190do_mlas (void)
5191{
5192  /* This restriction does not apply to mls (nor to mla in v6, but
5193     that's hard to detect at present).	 */
5194  if (inst.operands[0].reg == inst.operands[1].reg
5195      && !(inst.instruction & 0x00400000))
5196    as_tsktsk (_("rd and rm should be different in mla"));
5197
5198  inst.instruction |= inst.operands[0].reg << 16;
5199  inst.instruction |= inst.operands[1].reg;
5200  inst.instruction |= inst.operands[2].reg << 8;
5201  inst.instruction |= inst.operands[3].reg << 12;
5202
5203}
5204
5205static void
5206do_mov (void)
5207{
5208  inst.instruction |= inst.operands[0].reg << 12;
5209  encode_arm_shifter_operand (1);
5210}
5211
5212/* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.	 */
5213static void
5214do_mov16 (void)
5215{
5216  bfd_vma imm;
5217  bfd_boolean top;
5218
5219  top = (inst.instruction & 0x00400000) != 0;
5220  constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
5221	      _(":lower16: not allowed this instruction"));
5222  constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
5223	      _(":upper16: not allowed instruction"));
5224  inst.instruction |= inst.operands[0].reg << 12;
5225  if (inst.reloc.type == BFD_RELOC_UNUSED)
5226    {
5227      imm = inst.reloc.exp.X_add_number;
5228      /* The value is in two pieces: 0:11, 16:19.  */
5229      inst.instruction |= (imm & 0x00000fff);
5230      inst.instruction |= (imm & 0x0000f000) << 4;
5231    }
5232}
5233
5234static void
5235do_mrs (void)
5236{
5237  /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
5238  constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
5239	      != (PSR_c|PSR_f),
5240	      _("'CPSR' or 'SPSR' expected"));
5241  inst.instruction |= inst.operands[0].reg << 12;
5242  inst.instruction |= (inst.operands[1].imm & SPSR_BIT);
5243}
5244
5245/* Two possible forms:
5246      "{C|S}PSR_<field>, Rm",
5247      "{C|S}PSR_f, #expression".  */
5248
5249static void
5250do_msr (void)
5251{
5252  inst.instruction |= inst.operands[0].imm;
5253  if (inst.operands[1].isreg)
5254    inst.instruction |= inst.operands[1].reg;
5255  else
5256    {
5257      inst.instruction |= INST_IMMEDIATE;
5258      inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5259      inst.reloc.pc_rel = 0;
5260    }
5261}
5262
5263static void
5264do_mul (void)
5265{
5266  if (!inst.operands[2].present)
5267    inst.operands[2].reg = inst.operands[0].reg;
5268  inst.instruction |= inst.operands[0].reg << 16;
5269  inst.instruction |= inst.operands[1].reg;
5270  inst.instruction |= inst.operands[2].reg << 8;
5271
5272  if (inst.operands[0].reg == inst.operands[1].reg)
5273    as_tsktsk (_("rd and rm should be different in mul"));
5274}
5275
5276/* Long Multiply Parser
5277   UMULL RdLo, RdHi, Rm, Rs
5278   SMULL RdLo, RdHi, Rm, Rs
5279   UMLAL RdLo, RdHi, Rm, Rs
5280   SMLAL RdLo, RdHi, Rm, Rs.  */
5281
5282static void
5283do_mull (void)
5284{
5285  inst.instruction |= inst.operands[0].reg << 12;
5286  inst.instruction |= inst.operands[1].reg << 16;
5287  inst.instruction |= inst.operands[2].reg;
5288  inst.instruction |= inst.operands[3].reg << 8;
5289
5290  /* rdhi, rdlo and rm must all be different.  */
5291  if (inst.operands[0].reg == inst.operands[1].reg
5292      || inst.operands[0].reg == inst.operands[2].reg
5293      || inst.operands[1].reg == inst.operands[2].reg)
5294    as_tsktsk (_("rdhi, rdlo and rm must all be different"));
5295}
5296
5297static void
5298do_nop (void)
5299{
5300  if (inst.operands[0].present)
5301    {
5302      /* Architectural NOP hints are CPSR sets with no bits selected.  */
5303      inst.instruction &= 0xf0000000;
5304      inst.instruction |= 0x0320f000 + inst.operands[0].imm;
5305    }
5306}
5307
5308/* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
5309   PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
5310   Condition defaults to COND_ALWAYS.
5311   Error if Rd, Rn or Rm are R15.  */
5312
5313static void
5314do_pkhbt (void)
5315{
5316  inst.instruction |= inst.operands[0].reg << 12;
5317  inst.instruction |= inst.operands[1].reg << 16;
5318  inst.instruction |= inst.operands[2].reg;
5319  if (inst.operands[3].present)
5320    encode_arm_shift (3);
5321}
5322
5323/* ARM V6 PKHTB (Argument Parse).  */
5324
5325static void
5326do_pkhtb (void)
5327{
5328  if (!inst.operands[3].present)
5329    {
5330      /* If the shift specifier is omitted, turn the instruction
5331	 into pkhbt rd, rm, rn. */
5332      inst.instruction &= 0xfff00010;
5333      inst.instruction |= inst.operands[0].reg << 12;
5334      inst.instruction |= inst.operands[1].reg;
5335      inst.instruction |= inst.operands[2].reg << 16;
5336    }
5337  else
5338    {
5339      inst.instruction |= inst.operands[0].reg << 12;
5340      inst.instruction |= inst.operands[1].reg << 16;
5341      inst.instruction |= inst.operands[2].reg;
5342      encode_arm_shift (3);
5343    }
5344}
5345
5346/* ARMv5TE: Preload-Cache
5347
5348    PLD <addr_mode>
5349
5350  Syntactically, like LDR with B=1, W=0, L=1.  */
5351
5352static void
5353do_pld (void)
5354{
5355  constraint (!inst.operands[0].isreg,
5356	      _("'[' expected after PLD mnemonic"));
5357  constraint (inst.operands[0].postind,
5358	      _("post-indexed expression used in preload instruction"));
5359  constraint (inst.operands[0].writeback,
5360	      _("writeback used in preload instruction"));
5361  constraint (!inst.operands[0].preind,
5362	      _("unindexed addressing used in preload instruction"));
5363  encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
5364}
5365
5366/* ARMv7: PLI <addr_mode>  */
5367static void
5368do_pli (void)
5369{
5370  constraint (!inst.operands[0].isreg,
5371	      _("'[' expected after PLI mnemonic"));
5372  constraint (inst.operands[0].postind,
5373	      _("post-indexed expression used in preload instruction"));
5374  constraint (inst.operands[0].writeback,
5375	      _("writeback used in preload instruction"));
5376  constraint (!inst.operands[0].preind,
5377	      _("unindexed addressing used in preload instruction"));
5378  encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
5379  inst.instruction &= ~PRE_INDEX;
5380}
5381
5382static void
5383do_push_pop (void)
5384{
5385  inst.operands[1] = inst.operands[0];
5386  memset (&inst.operands[0], 0, sizeof inst.operands[0]);
5387  inst.operands[0].isreg = 1;
5388  inst.operands[0].writeback = 1;
5389  inst.operands[0].reg = REG_SP;
5390  do_ldmstm ();
5391}
5392
5393/* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
5394   word at the specified address and the following word
5395   respectively.
5396   Unconditionally executed.
5397   Error if Rn is R15.	*/
5398
5399static void
5400do_rfe (void)
5401{
5402  inst.instruction |= inst.operands[0].reg << 16;
5403  if (inst.operands[0].writeback)
5404    inst.instruction |= WRITE_BACK;
5405}
5406
5407/* ARM V6 ssat (argument parse).  */
5408
5409static void
5410do_ssat (void)
5411{
5412  inst.instruction |= inst.operands[0].reg << 12;
5413  inst.instruction |= (inst.operands[1].imm - 1) << 16;
5414  inst.instruction |= inst.operands[2].reg;
5415
5416  if (inst.operands[3].present)
5417    encode_arm_shift (3);
5418}
5419
5420/* ARM V6 usat (argument parse).  */
5421
5422static void
5423do_usat (void)
5424{
5425  inst.instruction |= inst.operands[0].reg << 12;
5426  inst.instruction |= inst.operands[1].imm << 16;
5427  inst.instruction |= inst.operands[2].reg;
5428
5429  if (inst.operands[3].present)
5430    encode_arm_shift (3);
5431}
5432
5433/* ARM V6 ssat16 (argument parse).  */
5434
5435static void
5436do_ssat16 (void)
5437{
5438  inst.instruction |= inst.operands[0].reg << 12;
5439  inst.instruction |= ((inst.operands[1].imm - 1) << 16);
5440  inst.instruction |= inst.operands[2].reg;
5441}
5442
5443static void
5444do_usat16 (void)
5445{
5446  inst.instruction |= inst.operands[0].reg << 12;
5447  inst.instruction |= inst.operands[1].imm << 16;
5448  inst.instruction |= inst.operands[2].reg;
5449}
5450
5451/* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
5452   preserving the other bits.
5453
5454   setend <endian_specifier>, where <endian_specifier> is either
5455   BE or LE.  */
5456
5457static void
5458do_setend (void)
5459{
5460  if (inst.operands[0].imm)
5461    inst.instruction |= 0x200;
5462}
5463
5464static void
5465do_shift (void)
5466{
5467  unsigned int Rm = (inst.operands[1].present
5468		     ? inst.operands[1].reg
5469		     : inst.operands[0].reg);
5470
5471  inst.instruction |= inst.operands[0].reg << 12;
5472  inst.instruction |= Rm;
5473  if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
5474    {
5475      inst.instruction |= inst.operands[2].reg << 8;
5476      inst.instruction |= SHIFT_BY_REG;
5477    }
5478  else
5479    inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
5480}
5481
5482static void
5483do_smc (void)
5484{
5485  inst.reloc.type = BFD_RELOC_ARM_SMC;
5486  inst.reloc.pc_rel = 0;
5487}
5488
5489static void
5490do_swi (void)
5491{
5492  inst.reloc.type = BFD_RELOC_ARM_SWI;
5493  inst.reloc.pc_rel = 0;
5494}
5495
5496/* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
5497   SMLAxy{cond} Rd,Rm,Rs,Rn
5498   SMLAWy{cond} Rd,Rm,Rs,Rn
5499   Error if any register is R15.  */
5500
5501static void
5502do_smla (void)
5503{
5504  inst.instruction |= inst.operands[0].reg << 16;
5505  inst.instruction |= inst.operands[1].reg;
5506  inst.instruction |= inst.operands[2].reg << 8;
5507  inst.instruction |= inst.operands[3].reg << 12;
5508}
5509
5510/* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
5511   SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
5512   Error if any register is R15.
5513   Warning if Rdlo == Rdhi.  */
5514
5515static void
5516do_smlal (void)
5517{
5518  inst.instruction |= inst.operands[0].reg << 12;
5519  inst.instruction |= inst.operands[1].reg << 16;
5520  inst.instruction |= inst.operands[2].reg;
5521  inst.instruction |= inst.operands[3].reg << 8;
5522
5523  if (inst.operands[0].reg == inst.operands[1].reg)
5524    as_tsktsk (_("rdhi and rdlo must be different"));
5525}
5526
5527/* ARM V5E (El Segundo) signed-multiply (argument parse)
5528   SMULxy{cond} Rd,Rm,Rs
5529   Error if any register is R15.  */
5530
5531static void
5532do_smul (void)
5533{
5534  inst.instruction |= inst.operands[0].reg << 16;
5535  inst.instruction |= inst.operands[1].reg;
5536  inst.instruction |= inst.operands[2].reg << 8;
5537}
5538
5539/* ARM V6 srs (argument parse).	 */
5540
5541static void
5542do_srs (void)
5543{
5544  inst.instruction |= inst.operands[0].imm;
5545  if (inst.operands[0].writeback)
5546    inst.instruction |= WRITE_BACK;
5547}
5548
5549/* ARM V6 strex (argument parse).  */
5550
5551static void
5552do_strex (void)
5553{
5554  constraint (!inst.operands[2].isreg || !inst.operands[2].preind
5555	      || inst.operands[2].postind || inst.operands[2].writeback
5556	      || inst.operands[2].immisreg || inst.operands[2].shifted
5557	      || inst.operands[2].negative
5558	      /* See comment in do_ldrex().  */
5559	      || (inst.operands[2].reg == REG_PC),
5560	      BAD_ADDR_MODE);
5561
5562  constraint (inst.operands[0].reg == inst.operands[1].reg
5563	      || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
5564
5565  constraint (inst.reloc.exp.X_op != O_constant
5566	      || inst.reloc.exp.X_add_number != 0,
5567	      _("offset must be zero in ARM encoding"));
5568
5569  inst.instruction |= inst.operands[0].reg << 12;
5570  inst.instruction |= inst.operands[1].reg;
5571  inst.instruction |= inst.operands[2].reg << 16;
5572  inst.reloc.type = BFD_RELOC_UNUSED;
5573}
5574
5575static void
5576do_strexd (void)
5577{
5578  constraint (inst.operands[1].reg % 2 != 0,
5579	      _("even register required"));
5580  constraint (inst.operands[2].present
5581	      && inst.operands[2].reg != inst.operands[1].reg + 1,
5582	      _("can only store two consecutive registers"));
5583  /* If op 2 were present and equal to PC, this function wouldn't
5584     have been called in the first place.  */
5585  constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
5586
5587  constraint (inst.operands[0].reg == inst.operands[1].reg
5588	      || inst.operands[0].reg == inst.operands[1].reg + 1
5589	      || inst.operands[0].reg == inst.operands[3].reg,
5590	      BAD_OVERLAP);
5591
5592  inst.instruction |= inst.operands[0].reg << 12;
5593  inst.instruction |= inst.operands[1].reg;
5594  inst.instruction |= inst.operands[3].reg << 16;
5595}
5596
5597/* ARM V6 SXTAH extracts a 16-bit value from a register, sign
5598   extends it to 32-bits, and adds the result to a value in another
5599   register.  You can specify a rotation by 0, 8, 16, or 24 bits
5600   before extracting the 16-bit value.
5601   SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
5602   Condition defaults to COND_ALWAYS.
5603   Error if any register uses R15.  */
5604
5605static void
5606do_sxtah (void)
5607{
5608  inst.instruction |= inst.operands[0].reg << 12;
5609  inst.instruction |= inst.operands[1].reg << 16;
5610  inst.instruction |= inst.operands[2].reg;
5611  inst.instruction |= inst.operands[3].imm << 10;
5612}
5613
5614/* ARM V6 SXTH.
5615
5616   SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
5617   Condition defaults to COND_ALWAYS.
5618   Error if any register uses R15.  */
5619
5620static void
5621do_sxth (void)
5622{
5623  inst.instruction |= inst.operands[0].reg << 12;
5624  inst.instruction |= inst.operands[1].reg;
5625  inst.instruction |= inst.operands[2].imm << 10;
5626}
5627
5628/* VFP instructions.  In a logical order: SP variant first, monad
5629   before dyad, arithmetic then move then load/store.  */
5630
5631static void
5632do_vfp_sp_monadic (void)
5633{
5634  encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5635  encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sm);
5636}
5637
5638static void
5639do_vfp_sp_dyadic (void)
5640{
5641  encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5642  encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sn);
5643  encode_arm_vfp_sp_reg (inst.operands[2].reg, VFP_REG_Sm);
5644}
5645
5646static void
5647do_vfp_sp_compare_z (void)
5648{
5649  encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5650}
5651
5652static void
5653do_vfp_dp_sp_cvt (void)
5654{
5655  inst.instruction |= inst.operands[0].reg << 12;
5656  encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sm);
5657}
5658
5659static void
5660do_vfp_sp_dp_cvt (void)
5661{
5662  encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5663  inst.instruction |= inst.operands[1].reg;
5664}
5665
5666static void
5667do_vfp_reg_from_sp (void)
5668{
5669  inst.instruction |= inst.operands[0].reg << 12;
5670  encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sn);
5671}
5672
5673static void
5674do_vfp_reg2_from_sp2 (void)
5675{
5676  constraint (inst.operands[2].imm != 2,
5677	      _("only two consecutive VFP SP registers allowed here"));
5678  inst.instruction |= inst.operands[0].reg << 12;
5679  inst.instruction |= inst.operands[1].reg << 16;
5680  encode_arm_vfp_sp_reg (inst.operands[2].reg, VFP_REG_Sm);
5681}
5682
5683static void
5684do_vfp_sp_from_reg (void)
5685{
5686  encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sn);
5687  inst.instruction |= inst.operands[1].reg << 12;
5688}
5689
5690static void
5691do_vfp_sp2_from_reg2 (void)
5692{
5693  constraint (inst.operands[0].imm != 2,
5694	      _("only two consecutive VFP SP registers allowed here"));
5695  encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sm);
5696  inst.instruction |= inst.operands[1].reg << 12;
5697  inst.instruction |= inst.operands[2].reg << 16;
5698}
5699
5700static void
5701do_vfp_sp_ldst (void)
5702{
5703  encode_arm_vfp_sp_reg (inst.operands[0].reg, VFP_REG_Sd);
5704  encode_arm_cp_address (1, FALSE, TRUE, 0);
5705}
5706
5707static void
5708do_vfp_dp_ldst (void)
5709{
5710  inst.instruction |= inst.operands[0].reg << 12;
5711  encode_arm_cp_address (1, FALSE, TRUE, 0);
5712}
5713
5714
5715static void
5716vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
5717{
5718  if (inst.operands[0].writeback)
5719    inst.instruction |= WRITE_BACK;
5720  else
5721    constraint (ldstm_type != VFP_LDSTMIA,
5722		_("this addressing mode requires base-register writeback"));
5723  inst.instruction |= inst.operands[0].reg << 16;
5724  encode_arm_vfp_sp_reg (inst.operands[1].reg, VFP_REG_Sd);
5725  inst.instruction |= inst.operands[1].imm;
5726}
5727
5728static void
5729vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
5730{
5731  int count;
5732
5733  if (inst.operands[0].writeback)
5734    inst.instruction |= WRITE_BACK;
5735  else
5736    constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
5737		_("this addressing mode requires base-register writeback"));
5738
5739  inst.instruction |= inst.operands[0].reg << 16;
5740  inst.instruction |= inst.operands[1].reg << 12;
5741
5742  count = inst.operands[1].imm << 1;
5743  if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
5744    count += 1;
5745
5746  inst.instruction |= count;
5747}
5748
5749static void
5750do_vfp_sp_ldstmia (void)
5751{
5752  vfp_sp_ldstm (VFP_LDSTMIA);
5753}
5754
5755static void
5756do_vfp_sp_ldstmdb (void)
5757{
5758  vfp_sp_ldstm (VFP_LDSTMDB);
5759}
5760
5761static void
5762do_vfp_dp_ldstmia (void)
5763{
5764  vfp_dp_ldstm (VFP_LDSTMIA);
5765}
5766
5767static void
5768do_vfp_dp_ldstmdb (void)
5769{
5770  vfp_dp_ldstm (VFP_LDSTMDB);
5771}
5772
5773static void
5774do_vfp_xp_ldstmia (void)
5775{
5776  vfp_dp_ldstm (VFP_LDSTMIAX);
5777}
5778
5779static void
5780do_vfp_xp_ldstmdb (void)
5781{
5782  vfp_dp_ldstm (VFP_LDSTMDBX);
5783}
5784
5785/* FPA instructions.  Also in a logical order.	*/
5786
5787static void
5788do_fpa_cmp (void)
5789{
5790  inst.instruction |= inst.operands[0].reg << 16;
5791  inst.instruction |= inst.operands[1].reg;
5792}
5793
5794static void
5795do_fpa_ldmstm (void)
5796{
5797  inst.instruction |= inst.operands[0].reg << 12;
5798  switch (inst.operands[1].imm)
5799    {
5800    case 1: inst.instruction |= CP_T_X;		 break;
5801    case 2: inst.instruction |= CP_T_Y;		 break;
5802    case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
5803    case 4:					 break;
5804    default: abort ();
5805    }
5806
5807  if (inst.instruction & (PRE_INDEX | INDEX_UP))
5808    {
5809      /* The instruction specified "ea" or "fd", so we can only accept
5810	 [Rn]{!}.  The instruction does not really support stacking or
5811	 unstacking, so we have to emulate these by setting appropriate
5812	 bits and offsets.  */
5813      constraint (inst.reloc.exp.X_op != O_constant
5814		  || inst.reloc.exp.X_add_number != 0,
5815		  _("this instruction does not support indexing"));
5816
5817      if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
5818	inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
5819
5820      if (!(inst.instruction & INDEX_UP))
5821	inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
5822
5823      if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
5824	{
5825	  inst.operands[2].preind = 0;
5826	  inst.operands[2].postind = 1;
5827	}
5828    }
5829
5830  encode_arm_cp_address (2, TRUE, TRUE, 0);
5831}
5832
5833/* iWMMXt instructions: strictly in alphabetical order.	 */
5834
5835static void
5836do_iwmmxt_tandorc (void)
5837{
5838  constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
5839}
5840
5841static void
5842do_iwmmxt_textrc (void)
5843{
5844  inst.instruction |= inst.operands[0].reg << 12;
5845  inst.instruction |= inst.operands[1].imm;
5846}
5847
5848static void
5849do_iwmmxt_textrm (void)
5850{
5851  inst.instruction |= inst.operands[0].reg << 12;
5852  inst.instruction |= inst.operands[1].reg << 16;
5853  inst.instruction |= inst.operands[2].imm;
5854}
5855
5856static void
5857do_iwmmxt_tinsr (void)
5858{
5859  inst.instruction |= inst.operands[0].reg << 16;
5860  inst.instruction |= inst.operands[1].reg << 12;
5861  inst.instruction |= inst.operands[2].imm;
5862}
5863
5864static void
5865do_iwmmxt_tmia (void)
5866{
5867  inst.instruction |= inst.operands[0].reg << 5;
5868  inst.instruction |= inst.operands[1].reg;
5869  inst.instruction |= inst.operands[2].reg << 12;
5870}
5871
5872static void
5873do_iwmmxt_waligni (void)
5874{
5875  inst.instruction |= inst.operands[0].reg << 12;
5876  inst.instruction |= inst.operands[1].reg << 16;
5877  inst.instruction |= inst.operands[2].reg;
5878  inst.instruction |= inst.operands[3].imm << 20;
5879}
5880
5881static void
5882do_iwmmxt_wmov (void)
5883{
5884  /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
5885  inst.instruction |= inst.operands[0].reg << 12;
5886  inst.instruction |= inst.operands[1].reg << 16;
5887  inst.instruction |= inst.operands[1].reg;
5888}
5889
5890static void
5891do_iwmmxt_wldstbh (void)
5892{
5893  int reloc;
5894  inst.instruction |= inst.operands[0].reg << 12;
5895  if (thumb_mode)
5896    reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
5897  else
5898    reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
5899  encode_arm_cp_address (1, TRUE, FALSE, reloc);
5900}
5901
5902static void
5903do_iwmmxt_wldstw (void)
5904{
5905  /* RIWR_RIWC clears .isreg for a control register.  */
5906  if (!inst.operands[0].isreg)
5907    {
5908      constraint (inst.cond != COND_ALWAYS, BAD_COND);
5909      inst.instruction |= 0xf0000000;
5910    }
5911
5912  inst.instruction |= inst.operands[0].reg << 12;
5913  encode_arm_cp_address (1, TRUE, TRUE, 0);
5914}
5915
5916static void
5917do_iwmmxt_wldstd (void)
5918{
5919  inst.instruction |= inst.operands[0].reg << 12;
5920  encode_arm_cp_address (1, TRUE, FALSE, 0);
5921}
5922
5923static void
5924do_iwmmxt_wshufh (void)
5925{
5926  inst.instruction |= inst.operands[0].reg << 12;
5927  inst.instruction |= inst.operands[1].reg << 16;
5928  inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
5929  inst.instruction |= (inst.operands[2].imm & 0x0f);
5930}
5931
5932static void
5933do_iwmmxt_wzero (void)
5934{
5935  /* WZERO reg is an alias for WANDN reg, reg, reg.  */
5936  inst.instruction |= inst.operands[0].reg;
5937  inst.instruction |= inst.operands[0].reg << 12;
5938  inst.instruction |= inst.operands[0].reg << 16;
5939}
5940
5941/* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
5942   operations first, then control, shift, and load/store.  */
5943
5944/* Insns like "foo X,Y,Z".  */
5945
5946static void
5947do_mav_triple (void)
5948{
5949  inst.instruction |= inst.operands[0].reg << 16;
5950  inst.instruction |= inst.operands[1].reg;
5951  inst.instruction |= inst.operands[2].reg << 12;
5952}
5953
5954/* Insns like "foo W,X,Y,Z".
5955    where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
5956
5957static void
5958do_mav_quad (void)
5959{
5960  inst.instruction |= inst.operands[0].reg << 5;
5961  inst.instruction |= inst.operands[1].reg << 12;
5962  inst.instruction |= inst.operands[2].reg << 16;
5963  inst.instruction |= inst.operands[3].reg;
5964}
5965
5966/* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
5967static void
5968do_mav_dspsc (void)
5969{
5970  inst.instruction |= inst.operands[1].reg << 12;
5971}
5972
5973/* Maverick shift immediate instructions.
5974   cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
5975   cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
5976
5977static void
5978do_mav_shift (void)
5979{
5980  int imm = inst.operands[2].imm;
5981
5982  inst.instruction |= inst.operands[0].reg << 12;
5983  inst.instruction |= inst.operands[1].reg << 16;
5984
5985  /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
5986     Bits 5-7 of the insn should have bits 4-6 of the immediate.
5987     Bit 4 should be 0.	 */
5988  imm = (imm & 0xf) | ((imm & 0x70) << 1);
5989
5990  inst.instruction |= imm;
5991}
5992
5993/* XScale instructions.	 Also sorted arithmetic before move.  */
5994
5995/* Xscale multiply-accumulate (argument parse)
5996     MIAcc   acc0,Rm,Rs
5997     MIAPHcc acc0,Rm,Rs
5998     MIAxycc acc0,Rm,Rs.  */
5999
6000static void
6001do_xsc_mia (void)
6002{
6003  inst.instruction |= inst.operands[1].reg;
6004  inst.instruction |= inst.operands[2].reg << 12;
6005}
6006
6007/* Xscale move-accumulator-register (argument parse)
6008
6009     MARcc   acc0,RdLo,RdHi.  */
6010
6011static void
6012do_xsc_mar (void)
6013{
6014  inst.instruction |= inst.operands[1].reg << 12;
6015  inst.instruction |= inst.operands[2].reg << 16;
6016}
6017
6018/* Xscale move-register-accumulator (argument parse)
6019
6020     MRAcc   RdLo,RdHi,acc0.  */
6021
6022static void
6023do_xsc_mra (void)
6024{
6025  constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
6026  inst.instruction |= inst.operands[0].reg << 12;
6027  inst.instruction |= inst.operands[1].reg << 16;
6028}
6029
6030/* Encoding functions relevant only to Thumb.  */
6031
6032/* inst.operands[i] is a shifted-register operand; encode
6033   it into inst.instruction in the format used by Thumb32.  */
6034
6035static void
6036encode_thumb32_shifted_operand (int i)
6037{
6038  unsigned int value = inst.reloc.exp.X_add_number;
6039  unsigned int shift = inst.operands[i].shift_kind;
6040
6041  constraint (inst.operands[i].immisreg,
6042	      _("shift by register not allowed in thumb mode"));
6043  inst.instruction |= inst.operands[i].reg;
6044  if (shift == SHIFT_RRX)
6045    inst.instruction |= SHIFT_ROR << 4;
6046  else
6047    {
6048      constraint (inst.reloc.exp.X_op != O_constant,
6049		  _("expression too complex"));
6050
6051      constraint (value > 32
6052		  || (value == 32 && (shift == SHIFT_LSL
6053				      || shift == SHIFT_ROR)),
6054		  _("shift expression is too large"));
6055
6056      if (value == 0)
6057	shift = SHIFT_LSL;
6058      else if (value == 32)
6059	value = 0;
6060
6061      inst.instruction |= shift << 4;
6062      inst.instruction |= (value & 0x1c) << 10;
6063      inst.instruction |= (value & 0x03) << 6;
6064    }
6065}
6066
6067
6068/* inst.operands[i] was set up by parse_address.  Encode it into a
6069   Thumb32 format load or store instruction.  Reject forms that cannot
6070   be used with such instructions.  If is_t is true, reject forms that
6071   cannot be used with a T instruction; if is_d is true, reject forms
6072   that cannot be used with a D instruction.  */
6073
6074static void
6075encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
6076{
6077  bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
6078
6079  constraint (!inst.operands[i].isreg,
6080	      _("Instruction does not support =N addresses"));
6081
6082  inst.instruction |= inst.operands[i].reg << 16;
6083  if (inst.operands[i].immisreg)
6084    {
6085      constraint (is_pc, _("cannot use register index with PC-relative addressing"));
6086      constraint (is_t || is_d, _("cannot use register index with this instruction"));
6087      constraint (inst.operands[i].negative,
6088		  _("Thumb does not support negative register indexing"));
6089      constraint (inst.operands[i].postind,
6090		  _("Thumb does not support register post-indexing"));
6091      constraint (inst.operands[i].writeback,
6092		  _("Thumb does not support register indexing with writeback"));
6093      constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
6094		  _("Thumb supports only LSL in shifted register indexing"));
6095
6096      inst.instruction |= inst.operands[i].imm;
6097      if (inst.operands[i].shifted)
6098	{
6099	  constraint (inst.reloc.exp.X_op != O_constant,
6100		      _("expression too complex"));
6101	  constraint (inst.reloc.exp.X_add_number < 0
6102		      || inst.reloc.exp.X_add_number > 3,
6103		      _("shift out of range"));
6104	  inst.instruction |= inst.reloc.exp.X_add_number << 4;
6105	}
6106      inst.reloc.type = BFD_RELOC_UNUSED;
6107    }
6108  else if (inst.operands[i].preind)
6109    {
6110      constraint (is_pc && inst.operands[i].writeback,
6111		  _("cannot use writeback with PC-relative addressing"));
6112      constraint (is_t && inst.operands[i].writeback,
6113		  _("cannot use writeback with this instruction"));
6114
6115      if (is_d)
6116	{
6117	  inst.instruction |= 0x01000000;
6118	  if (inst.operands[i].writeback)
6119	    inst.instruction |= 0x00200000;
6120	}
6121      else
6122	{
6123	  inst.instruction |= 0x00000c00;
6124	  if (inst.operands[i].writeback)
6125	    inst.instruction |= 0x00000100;
6126	}
6127      inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
6128    }
6129  else if (inst.operands[i].postind)
6130    {
6131      assert (inst.operands[i].writeback);
6132      constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
6133      constraint (is_t, _("cannot use post-indexing with this instruction"));
6134
6135      if (is_d)
6136	inst.instruction |= 0x00200000;
6137      else
6138	inst.instruction |= 0x00000900;
6139      inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
6140    }
6141  else /* unindexed - only for coprocessor */
6142    inst.error = _("instruction does not accept unindexed addressing");
6143}
6144
6145/* Table of Thumb instructions which exist in both 16- and 32-bit
6146   encodings (the latter only in post-V6T2 cores).  The index is the
6147   value used in the insns table below.  When there is more than one
6148   possible 16-bit encoding for the instruction, this table always
6149   holds variant (1).
6150   Also contains several pseudo-instructions used during relaxation.  */
6151#define T16_32_TAB				\
6152  X(adc,   4140, eb400000),			\
6153  X(adcs,  4140, eb500000),			\
6154  X(add,   1c00, eb000000),			\
6155  X(adds,  1c00, eb100000),			\
6156  X(addi,  0000, f1000000),			\
6157  X(addis, 0000, f1100000),			\
6158  X(add_pc,000f, f20f0000),			\
6159  X(add_sp,000d, f10d0000),			\
6160  X(adr,   000f, f20f0000),			\
6161  X(and,   4000, ea000000),			\
6162  X(ands,  4000, ea100000),			\
6163  X(asr,   1000, fa40f000),			\
6164  X(asrs,  1000, fa50f000),			\
6165  X(b,     e000, f000b000),			\
6166  X(bcond, d000, f0008000),			\
6167  X(bic,   4380, ea200000),			\
6168  X(bics,  4380, ea300000),			\
6169  X(cmn,   42c0, eb100f00),			\
6170  X(cmp,   2800, ebb00f00),			\
6171  X(cpsie, b660, f3af8400),			\
6172  X(cpsid, b670, f3af8600),			\
6173  X(cpy,   4600, ea4f0000),			\
6174  X(dec_sp,80dd, f1bd0d00),			\
6175  X(eor,   4040, ea800000),			\
6176  X(eors,  4040, ea900000),			\
6177  X(inc_sp,00dd, f10d0d00),			\
6178  X(ldmia, c800, e8900000),			\
6179  X(ldr,   6800, f8500000),			\
6180  X(ldrb,  7800, f8100000),			\
6181  X(ldrh,  8800, f8300000),			\
6182  X(ldrsb, 5600, f9100000),			\
6183  X(ldrsh, 5e00, f9300000),			\
6184  X(ldr_pc,4800, f85f0000),			\
6185  X(ldr_pc2,4800, f85f0000),			\
6186  X(ldr_sp,9800, f85d0000),			\
6187  X(lsl,   0000, fa00f000),			\
6188  X(lsls,  0000, fa10f000),			\
6189  X(lsr,   0800, fa20f000),			\
6190  X(lsrs,  0800, fa30f000),			\
6191  X(mov,   2000, ea4f0000),			\
6192  X(movs,  2000, ea5f0000),			\
6193  X(mul,   4340, fb00f000),                     \
6194  X(muls,  4340, ffffffff), /* no 32b muls */	\
6195  X(mvn,   43c0, ea6f0000),			\
6196  X(mvns,  43c0, ea7f0000),			\
6197  X(neg,   4240, f1c00000), /* rsb #0 */	\
6198  X(negs,  4240, f1d00000), /* rsbs #0 */	\
6199  X(orr,   4300, ea400000),			\
6200  X(orrs,  4300, ea500000),			\
6201  X(pop,   bc00, e8bd0000), /* ldmia sp!,... */	\
6202  X(push,  b400, e92d0000), /* stmdb sp!,... */	\
6203  X(rev,   ba00, fa90f080),			\
6204  X(rev16, ba40, fa90f090),			\
6205  X(revsh, bac0, fa90f0b0),			\
6206  X(ror,   41c0, fa60f000),			\
6207  X(rors,  41c0, fa70f000),			\
6208  X(sbc,   4180, eb600000),			\
6209  X(sbcs,  4180, eb700000),			\
6210  X(stmia, c000, e8800000),			\
6211  X(str,   6000, f8400000),			\
6212  X(strb,  7000, f8000000),			\
6213  X(strh,  8000, f8200000),			\
6214  X(str_sp,9000, f84d0000),			\
6215  X(sub,   1e00, eba00000),			\
6216  X(subs,  1e00, ebb00000),			\
6217  X(subi,  8000, f1a00000),			\
6218  X(subis, 8000, f1b00000),			\
6219  X(sxtb,  b240, fa4ff080),			\
6220  X(sxth,  b200, fa0ff080),			\
6221  X(tst,   4200, ea100f00),			\
6222  X(uxtb,  b2c0, fa5ff080),			\
6223  X(uxth,  b280, fa1ff080),			\
6224  X(nop,   bf00, f3af8000),			\
6225  X(yield, bf10, f3af8001),			\
6226  X(wfe,   bf20, f3af8002),			\
6227  X(wfi,   bf30, f3af8003),			\
6228  X(sev,   bf40, f3af9004), /* typo, 8004? */
6229
6230/* To catch errors in encoding functions, the codes are all offset by
6231   0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
6232   as 16-bit instructions.  */
6233#define X(a,b,c) T_MNEM_##a
6234enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
6235#undef X
6236
6237#define X(a,b,c) 0x##b
6238static const unsigned short thumb_op16[] = { T16_32_TAB };
6239#define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
6240#undef X
6241
6242#define X(a,b,c) 0x##c
6243static const unsigned int thumb_op32[] = { T16_32_TAB };
6244#define THUMB_OP32(n) (thumb_op32[(n) - (T16_32_OFFSET + 1)])
6245#define THUMB_SETS_FLAGS(n) (THUMB_OP32 (n) & 0x00100000)
6246#undef X
6247#undef T16_32_TAB
6248
6249/* Thumb instruction encoders, in alphabetical order.  */
6250
6251/* ADDW or SUBW.  */
6252static void
6253do_t_add_sub_w (void)
6254{
6255  int Rd, Rn;
6256
6257  Rd = inst.operands[0].reg;
6258  Rn = inst.operands[1].reg;
6259
6260  constraint (Rd == 15, _("PC not allowed as destination"));
6261  inst.instruction |= (Rn << 16) | (Rd << 8);
6262  inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
6263}
6264
6265/* Parse an add or subtract instruction.  We get here with inst.instruction
6266   equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
6267
6268static void
6269do_t_add_sub (void)
6270{
6271  int Rd, Rs, Rn;
6272
6273  Rd = inst.operands[0].reg;
6274  Rs = (inst.operands[1].present
6275	? inst.operands[1].reg    /* Rd, Rs, foo */
6276	: inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
6277
6278  if (unified_syntax)
6279    {
6280      bfd_boolean flags;
6281      bfd_boolean narrow;
6282      int opcode;
6283
6284      flags = (inst.instruction == T_MNEM_adds
6285	       || inst.instruction == T_MNEM_subs);
6286      if (flags)
6287	narrow = (current_it_mask == 0);
6288      else
6289	narrow = (current_it_mask != 0);
6290      if (!inst.operands[2].isreg)
6291	{
6292	  opcode = 0;
6293	  if (inst.size_req != 4)
6294	    {
6295	      int add;
6296
6297	      add = (inst.instruction == T_MNEM_add
6298		     || inst.instruction == T_MNEM_adds);
6299	      /* Attempt to use a narrow opcode, with relaxation if
6300	         appropriate.  */
6301	      if (Rd == REG_SP && Rs == REG_SP && !flags)
6302		opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
6303	      else if (Rd <= 7 && Rs == REG_SP && add && !flags)
6304		opcode = T_MNEM_add_sp;
6305	      else if (Rd <= 7 && Rs == REG_PC && add && !flags)
6306		opcode = T_MNEM_add_pc;
6307	      else if (Rd <= 7 && Rs <= 7 && narrow)
6308		{
6309		  if (flags)
6310		    opcode = add ? T_MNEM_addis : T_MNEM_subis;
6311		  else
6312		    opcode = add ? T_MNEM_addi : T_MNEM_subi;
6313		}
6314	      if (opcode)
6315		{
6316		  inst.instruction = THUMB_OP16(opcode);
6317		  inst.instruction |= (Rd << 4) | Rs;
6318		  inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
6319		  if (inst.size_req != 2)
6320		    inst.relax = opcode;
6321		}
6322	      else
6323		constraint (inst.size_req == 2, BAD_HIREG);
6324	    }
6325	  if (inst.size_req == 4
6326	      || (inst.size_req != 2 && !opcode))
6327	    {
6328	      /* ??? Convert large immediates to addw/subw.  */
6329	      inst.instruction = THUMB_OP32 (inst.instruction);
6330	      inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
6331	      inst.instruction |= inst.operands[0].reg << 8;
6332	      inst.instruction |= inst.operands[1].reg << 16;
6333	      inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
6334	    }
6335	}
6336      else
6337	{
6338	  Rn = inst.operands[2].reg;
6339	  /* See if we can do this with a 16-bit instruction.  */
6340	  if (!inst.operands[2].shifted && inst.size_req != 4)
6341	    {
6342	      if (Rd > 7 || Rs > 7 || Rn > 7)
6343		narrow = FALSE;
6344
6345	      if (narrow)
6346		{
6347		  inst.instruction = ((inst.instruction == T_MNEM_adds
6348				       || inst.instruction == T_MNEM_add)
6349				      ? T_OPCODE_ADD_R3
6350				      : T_OPCODE_SUB_R3);
6351		  inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
6352		  return;
6353		}
6354
6355	      if (inst.instruction == T_MNEM_add)
6356		{
6357		  if (Rd == Rs)
6358		    {
6359		      inst.instruction = T_OPCODE_ADD_HI;
6360		      inst.instruction |= (Rd & 8) << 4;
6361		      inst.instruction |= (Rd & 7);
6362		      inst.instruction |= Rn << 3;
6363		      return;
6364		    }
6365		  /* ... because addition is commutative! */
6366		  else if (Rd == Rn)
6367		    {
6368		      inst.instruction = T_OPCODE_ADD_HI;
6369		      inst.instruction |= (Rd & 8) << 4;
6370		      inst.instruction |= (Rd & 7);
6371		      inst.instruction |= Rs << 3;
6372		      return;
6373		    }
6374		}
6375	    }
6376	  /* If we get here, it can't be done in 16 bits.  */
6377	  constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
6378		      _("shift must be constant"));
6379	  inst.instruction = THUMB_OP32 (inst.instruction);
6380	  inst.instruction |= Rd << 8;
6381	  inst.instruction |= Rs << 16;
6382	  encode_thumb32_shifted_operand (2);
6383	}
6384    }
6385  else
6386    {
6387      constraint (inst.instruction == T_MNEM_adds
6388		  || inst.instruction == T_MNEM_subs,
6389		  BAD_THUMB32);
6390
6391      if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
6392	{
6393	  constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
6394		      || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
6395		      BAD_HIREG);
6396
6397	  inst.instruction = (inst.instruction == T_MNEM_add
6398			      ? 0x0000 : 0x8000);
6399	  inst.instruction |= (Rd << 4) | Rs;
6400	  inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
6401	  return;
6402	}
6403
6404      Rn = inst.operands[2].reg;
6405      constraint (inst.operands[2].shifted, _("unshifted register required"));
6406
6407      /* We now have Rd, Rs, and Rn set to registers.  */
6408      if (Rd > 7 || Rs > 7 || Rn > 7)
6409	{
6410	  /* Can't do this for SUB.	 */
6411	  constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
6412	  inst.instruction = T_OPCODE_ADD_HI;
6413	  inst.instruction |= (Rd & 8) << 4;
6414	  inst.instruction |= (Rd & 7);
6415	  if (Rs == Rd)
6416	    inst.instruction |= Rn << 3;
6417	  else if (Rn == Rd)
6418	    inst.instruction |= Rs << 3;
6419	  else
6420	    constraint (1, _("dest must overlap one source register"));
6421	}
6422      else
6423	{
6424	  inst.instruction = (inst.instruction == T_MNEM_add
6425			      ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
6426	  inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
6427	}
6428    }
6429}
6430
6431static void
6432do_t_adr (void)
6433{
6434  if (unified_syntax && inst.size_req == 0 && inst.operands[0].reg <= 7)
6435    {
6436      /* Defer to section relaxation.  */
6437      inst.relax = inst.instruction;
6438      inst.instruction = THUMB_OP16 (inst.instruction);
6439      inst.instruction |= inst.operands[0].reg << 4;
6440    }
6441  else if (unified_syntax && inst.size_req != 2)
6442    {
6443      /* Generate a 32-bit opcode.  */
6444      inst.instruction = THUMB_OP32 (inst.instruction);
6445      inst.instruction |= inst.operands[0].reg << 8;
6446      inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
6447      inst.reloc.pc_rel = 1;
6448    }
6449  else
6450    {
6451      /* Generate a 16-bit opcode.  */
6452      inst.instruction = THUMB_OP16 (inst.instruction);
6453      inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
6454      inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
6455      inst.reloc.pc_rel = 1;
6456
6457      inst.instruction |= inst.operands[0].reg << 4;
6458    }
6459}
6460
6461/* Arithmetic instructions for which there is just one 16-bit
6462   instruction encoding, and it allows only two low registers.
6463   For maximal compatibility with ARM syntax, we allow three register
6464   operands even when Thumb-32 instructions are not available, as long
6465   as the first two are identical.  For instance, both "sbc r0,r1" and
6466   "sbc r0,r0,r1" are allowed.  */
6467static void
6468do_t_arit3 (void)
6469{
6470  int Rd, Rs, Rn;
6471
6472  Rd = inst.operands[0].reg;
6473  Rs = (inst.operands[1].present
6474	? inst.operands[1].reg    /* Rd, Rs, foo */
6475	: inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
6476  Rn = inst.operands[2].reg;
6477
6478  if (unified_syntax)
6479    {
6480      if (!inst.operands[2].isreg)
6481	{
6482	  /* For an immediate, we always generate a 32-bit opcode;
6483	     section relaxation will shrink it later if possible.  */
6484	  inst.instruction = THUMB_OP32 (inst.instruction);
6485	  inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
6486	  inst.instruction |= Rd << 8;
6487	  inst.instruction |= Rs << 16;
6488	  inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
6489	}
6490      else
6491	{
6492	  bfd_boolean narrow;
6493
6494	  /* See if we can do this with a 16-bit instruction.  */
6495	  if (THUMB_SETS_FLAGS (inst.instruction))
6496	    narrow = current_it_mask == 0;
6497	  else
6498	    narrow = current_it_mask != 0;
6499
6500	  if (Rd > 7 || Rn > 7 || Rs > 7)
6501	    narrow = FALSE;
6502	  if (inst.operands[2].shifted)
6503	    narrow = FALSE;
6504	  if (inst.size_req == 4)
6505	    narrow = FALSE;
6506
6507	  if (narrow
6508	      && Rd == Rs)
6509	    {
6510	      inst.instruction = THUMB_OP16 (inst.instruction);
6511	      inst.instruction |= Rd;
6512	      inst.instruction |= Rn << 3;
6513	      return;
6514	    }
6515
6516	  /* If we get here, it can't be done in 16 bits.  */
6517	  constraint (inst.operands[2].shifted
6518		      && inst.operands[2].immisreg,
6519		      _("shift must be constant"));
6520	  inst.instruction = THUMB_OP32 (inst.instruction);
6521	  inst.instruction |= Rd << 8;
6522	  inst.instruction |= Rs << 16;
6523	  encode_thumb32_shifted_operand (2);
6524	}
6525    }
6526  else
6527    {
6528      /* On its face this is a lie - the instruction does set the
6529	 flags.  However, the only supported mnemonic in this mode
6530	 says it doesn't.  */
6531      constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
6532
6533      constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
6534		  _("unshifted register required"));
6535      constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
6536      constraint (Rd != Rs,
6537		  _("dest and source1 must be the same register"));
6538
6539      inst.instruction = THUMB_OP16 (inst.instruction);
6540      inst.instruction |= Rd;
6541      inst.instruction |= Rn << 3;
6542    }
6543}
6544
6545/* Similarly, but for instructions where the arithmetic operation is
6546   commutative, so we can allow either of them to be different from
6547   the destination operand in a 16-bit instruction.  For instance, all
6548   three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
6549   accepted.  */
6550static void
6551do_t_arit3c (void)
6552{
6553  int Rd, Rs, Rn;
6554
6555  Rd = inst.operands[0].reg;
6556  Rs = (inst.operands[1].present
6557	? inst.operands[1].reg    /* Rd, Rs, foo */
6558	: inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
6559  Rn = inst.operands[2].reg;
6560
6561  if (unified_syntax)
6562    {
6563      if (!inst.operands[2].isreg)
6564	{
6565	  /* For an immediate, we always generate a 32-bit opcode;
6566	     section relaxation will shrink it later if possible.  */
6567	  inst.instruction = THUMB_OP32 (inst.instruction);
6568	  inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
6569	  inst.instruction |= Rd << 8;
6570	  inst.instruction |= Rs << 16;
6571	  inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
6572	}
6573      else
6574	{
6575	  bfd_boolean narrow;
6576
6577	  /* See if we can do this with a 16-bit instruction.  */
6578	  if (THUMB_SETS_FLAGS (inst.instruction))
6579	    narrow = current_it_mask == 0;
6580	  else
6581	    narrow = current_it_mask != 0;
6582
6583	  if (Rd > 7 || Rn > 7 || Rs > 7)
6584	    narrow = FALSE;
6585	  if (inst.operands[2].shifted)
6586	    narrow = FALSE;
6587	  if (inst.size_req == 4)
6588	    narrow = FALSE;
6589
6590	  if (narrow)
6591	    {
6592	      if (Rd == Rs)
6593		{
6594		  inst.instruction = THUMB_OP16 (inst.instruction);
6595		  inst.instruction |= Rd;
6596		  inst.instruction |= Rn << 3;
6597		  return;
6598		}
6599	      if (Rd == Rn)
6600		{
6601		  inst.instruction = THUMB_OP16 (inst.instruction);
6602		  inst.instruction |= Rd;
6603		  inst.instruction |= Rs << 3;
6604		  return;
6605		}
6606	    }
6607
6608	  /* If we get here, it can't be done in 16 bits.  */
6609	  constraint (inst.operands[2].shifted
6610		      && inst.operands[2].immisreg,
6611		      _("shift must be constant"));
6612	  inst.instruction = THUMB_OP32 (inst.instruction);
6613	  inst.instruction |= Rd << 8;
6614	  inst.instruction |= Rs << 16;
6615	  encode_thumb32_shifted_operand (2);
6616	}
6617    }
6618  else
6619    {
6620      /* On its face this is a lie - the instruction does set the
6621	 flags.  However, the only supported mnemonic in this mode
6622	 says it doesn't.  */
6623      constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
6624
6625      constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
6626		  _("unshifted register required"));
6627      constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
6628
6629      inst.instruction = THUMB_OP16 (inst.instruction);
6630      inst.instruction |= Rd;
6631
6632      if (Rd == Rs)
6633	inst.instruction |= Rn << 3;
6634      else if (Rd == Rn)
6635	inst.instruction |= Rs << 3;
6636      else
6637	constraint (1, _("dest must overlap one source register"));
6638    }
6639}
6640
6641static void
6642do_t_barrier (void)
6643{
6644  if (inst.operands[0].present)
6645    {
6646      constraint ((inst.instruction & 0xf0) != 0x40
6647		  && (inst.instruction & 0xf0) != 0x50
6648		  && inst.operands[0].imm != 0xf,
6649		  "bad barrier type");
6650      inst.instruction |= inst.operands[0].imm;
6651    }
6652  else
6653    inst.instruction |= 0xf;
6654}
6655
6656static void
6657do_t_bfc (void)
6658{
6659  unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
6660  constraint (msb > 32, _("bit-field extends past end of register"));
6661  /* The instruction encoding stores the LSB and MSB,
6662     not the LSB and width.  */
6663  inst.instruction |= inst.operands[0].reg << 8;
6664  inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
6665  inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
6666  inst.instruction |= msb - 1;
6667}
6668
6669static void
6670do_t_bfi (void)
6671{
6672  unsigned int msb;
6673
6674  /* #0 in second position is alternative syntax for bfc, which is
6675     the same instruction but with REG_PC in the Rm field.  */
6676  if (!inst.operands[1].isreg)
6677    inst.operands[1].reg = REG_PC;
6678
6679  msb = inst.operands[2].imm + inst.operands[3].imm;
6680  constraint (msb > 32, _("bit-field extends past end of register"));
6681  /* The instruction encoding stores the LSB and MSB,
6682     not the LSB and width.  */
6683  inst.instruction |= inst.operands[0].reg << 8;
6684  inst.instruction |= inst.operands[1].reg << 16;
6685  inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
6686  inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
6687  inst.instruction |= msb - 1;
6688}
6689
6690static void
6691do_t_bfx (void)
6692{
6693  constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
6694	      _("bit-field extends past end of register"));
6695  inst.instruction |= inst.operands[0].reg << 8;
6696  inst.instruction |= inst.operands[1].reg << 16;
6697  inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
6698  inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
6699  inst.instruction |= inst.operands[3].imm - 1;
6700}
6701
6702/* ARM V5 Thumb BLX (argument parse)
6703	BLX <target_addr>	which is BLX(1)
6704	BLX <Rm>		which is BLX(2)
6705   Unfortunately, there are two different opcodes for this mnemonic.
6706   So, the insns[].value is not used, and the code here zaps values
6707	into inst.instruction.
6708
6709   ??? How to take advantage of the additional two bits of displacement
6710   available in Thumb32 mode?  Need new relocation?  */
6711
6712static void
6713do_t_blx (void)
6714{
6715  constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
6716  if (inst.operands[0].isreg)
6717    /* We have a register, so this is BLX(2).  */
6718    inst.instruction |= inst.operands[0].reg << 3;
6719  else
6720    {
6721      /* No register.  This must be BLX(1).  */
6722      inst.instruction = 0xf000e800;
6723#ifdef OBJ_ELF
6724      if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
6725	inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
6726      else
6727#endif
6728	inst.reloc.type = BFD_RELOC_THUMB_PCREL_BLX;
6729      inst.reloc.pc_rel = 1;
6730    }
6731}
6732
6733static void
6734do_t_branch (void)
6735{
6736  int opcode;
6737  int cond;
6738
6739  if (current_it_mask)
6740    {
6741      /* Conditional branches inside IT blocks are encoded as unconditional
6742         branches.  */
6743      cond = COND_ALWAYS;
6744      /* A branch must be the last instruction in an IT block.  */
6745      constraint (current_it_mask != 0x10, BAD_BRANCH);
6746    }
6747  else
6748    cond = inst.cond;
6749
6750  if (cond != COND_ALWAYS)
6751    opcode = T_MNEM_bcond;
6752  else
6753    opcode = inst.instruction;
6754
6755  if (unified_syntax && inst.size_req == 4)
6756    {
6757      inst.instruction = THUMB_OP32(opcode);
6758      if (cond == COND_ALWAYS)
6759	inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH25;
6760      else
6761	{
6762	  assert (cond != 0xF);
6763	  inst.instruction |= cond << 22;
6764	  inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH20;
6765	}
6766    }
6767  else
6768    {
6769      inst.instruction = THUMB_OP16(opcode);
6770      if (cond == COND_ALWAYS)
6771	inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH12;
6772      else
6773	{
6774	  inst.instruction |= cond << 8;
6775	  inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH9;
6776	}
6777      /* Allow section relaxation.  */
6778      if (unified_syntax && inst.size_req != 2)
6779	inst.relax = opcode;
6780    }
6781
6782  inst.reloc.pc_rel = 1;
6783}
6784
6785static void
6786do_t_bkpt (void)
6787{
6788  constraint (inst.cond != COND_ALWAYS,
6789	      _("instruction is always unconditional"));
6790  if (inst.operands[0].present)
6791    {
6792      constraint (inst.operands[0].imm > 255,
6793		  _("immediate value out of range"));
6794      inst.instruction |= inst.operands[0].imm;
6795    }
6796}
6797
6798static void
6799do_t_branch23 (void)
6800{
6801  constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
6802  inst.reloc.type   = BFD_RELOC_THUMB_PCREL_BRANCH23;
6803  inst.reloc.pc_rel = 1;
6804
6805  /* If the destination of the branch is a defined symbol which does not have
6806     the THUMB_FUNC attribute, then we must be calling a function which has
6807     the (interfacearm) attribute.  We look for the Thumb entry point to that
6808     function and change the branch to refer to that function instead.	*/
6809  if (	 inst.reloc.exp.X_op == O_symbol
6810      && inst.reloc.exp.X_add_symbol != NULL
6811      && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
6812      && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
6813    inst.reloc.exp.X_add_symbol =
6814      find_real_start (inst.reloc.exp.X_add_symbol);
6815}
6816
6817static void
6818do_t_bx (void)
6819{
6820  constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
6821  inst.instruction |= inst.operands[0].reg << 3;
6822  /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.	 The reloc
6823     should cause the alignment to be checked once it is known.	 This is
6824     because BX PC only works if the instruction is word aligned.  */
6825}
6826
6827static void
6828do_t_bxj (void)
6829{
6830  constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
6831  if (inst.operands[0].reg == REG_PC)
6832    as_tsktsk (_("use of r15 in bxj is not really useful"));
6833
6834  inst.instruction |= inst.operands[0].reg << 16;
6835}
6836
6837static void
6838do_t_clz (void)
6839{
6840  inst.instruction |= inst.operands[0].reg << 8;
6841  inst.instruction |= inst.operands[1].reg << 16;
6842  inst.instruction |= inst.operands[1].reg;
6843}
6844
6845static void
6846do_t_cps (void)
6847{
6848  constraint (current_it_mask, BAD_NOT_IT);
6849  inst.instruction |= inst.operands[0].imm;
6850}
6851
6852static void
6853do_t_cpsi (void)
6854{
6855  constraint (current_it_mask, BAD_NOT_IT);
6856  if (unified_syntax
6857      && (inst.operands[1].present || inst.size_req == 4)
6858      && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
6859    {
6860      unsigned int imod = (inst.instruction & 0x0030) >> 4;
6861      inst.instruction = 0xf3af8000;
6862      inst.instruction |= imod << 9;
6863      inst.instruction |= inst.operands[0].imm << 5;
6864      if (inst.operands[1].present)
6865	inst.instruction |= 0x100 | inst.operands[1].imm;
6866    }
6867  else
6868    {
6869      constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
6870		  && (inst.operands[0].imm & 4),
6871		  _("selected processor does not support 'A' form "
6872		    "of this instruction"));
6873      constraint (inst.operands[1].present || inst.size_req == 4,
6874		  _("Thumb does not support the 2-argument "
6875		    "form of this instruction"));
6876      inst.instruction |= inst.operands[0].imm;
6877    }
6878}
6879
6880/* THUMB CPY instruction (argument parse).  */
6881
6882static void
6883do_t_cpy (void)
6884{
6885  if (inst.size_req == 4)
6886    {
6887      inst.instruction = THUMB_OP32 (T_MNEM_mov);
6888      inst.instruction |= inst.operands[0].reg << 8;
6889      inst.instruction |= inst.operands[1].reg;
6890    }
6891  else
6892    {
6893      inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
6894      inst.instruction |= (inst.operands[0].reg & 0x7);
6895      inst.instruction |= inst.operands[1].reg << 3;
6896    }
6897}
6898
6899static void
6900do_t_czb (void)
6901{
6902  constraint (current_it_mask, BAD_NOT_IT);
6903  constraint (inst.operands[0].reg > 7, BAD_HIREG);
6904  inst.instruction |= inst.operands[0].reg;
6905  inst.reloc.pc_rel = 1;
6906  inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
6907}
6908
6909static void
6910do_t_dbg (void)
6911{
6912  inst.instruction |= inst.operands[0].imm;
6913}
6914
6915static void
6916do_t_div (void)
6917{
6918  if (!inst.operands[1].present)
6919    inst.operands[1].reg = inst.operands[0].reg;
6920  inst.instruction |= inst.operands[0].reg << 8;
6921  inst.instruction |= inst.operands[1].reg << 16;
6922  inst.instruction |= inst.operands[2].reg;
6923}
6924
6925static void
6926do_t_hint (void)
6927{
6928  if (unified_syntax && inst.size_req == 4)
6929    inst.instruction = THUMB_OP32 (inst.instruction);
6930  else
6931    inst.instruction = THUMB_OP16 (inst.instruction);
6932}
6933
6934static void
6935do_t_it (void)
6936{
6937  unsigned int cond = inst.operands[0].imm;
6938
6939  constraint (current_it_mask, BAD_NOT_IT);
6940  current_it_mask = (inst.instruction & 0xf) | 0x10;
6941  current_cc = cond;
6942
6943  /* If the condition is a negative condition, invert the mask.  */
6944  if ((cond & 0x1) == 0x0)
6945    {
6946      unsigned int mask = inst.instruction & 0x000f;
6947
6948      if ((mask & 0x7) == 0)
6949	/* no conversion needed */;
6950      else if ((mask & 0x3) == 0)
6951	mask ^= 0x8;
6952      else if ((mask & 0x1) == 0)
6953	mask ^= 0xC;
6954      else
6955	mask ^= 0xE;
6956
6957      inst.instruction &= 0xfff0;
6958      inst.instruction |= mask;
6959    }
6960
6961  inst.instruction |= cond << 4;
6962}
6963
6964static void
6965do_t_ldmstm (void)
6966{
6967  /* This really doesn't seem worth it.  */
6968  constraint (inst.reloc.type != BFD_RELOC_UNUSED,
6969	      _("expression too complex"));
6970  constraint (inst.operands[1].writeback,
6971	      _("Thumb load/store multiple does not support {reglist}^"));
6972
6973  if (unified_syntax)
6974    {
6975      /* See if we can use a 16-bit instruction.  */
6976      if (inst.instruction < 0xffff /* not ldmdb/stmdb */
6977	  && inst.size_req != 4
6978	  && inst.operands[0].reg <= 7
6979	  && !(inst.operands[1].imm & ~0xff)
6980	  && (inst.instruction == T_MNEM_stmia
6981	      ? inst.operands[0].writeback
6982	      : (inst.operands[0].writeback
6983		 == !(inst.operands[1].imm & (1 << inst.operands[0].reg)))))
6984	{
6985	  if (inst.instruction == T_MNEM_stmia
6986	      && (inst.operands[1].imm & (1 << inst.operands[0].reg))
6987	      && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
6988	    as_warn (_("value stored for r%d is UNPREDICTABLE"),
6989		     inst.operands[0].reg);
6990
6991	  inst.instruction = THUMB_OP16 (inst.instruction);
6992	  inst.instruction |= inst.operands[0].reg << 8;
6993	  inst.instruction |= inst.operands[1].imm;
6994	}
6995      else
6996	{
6997	  if (inst.operands[1].imm & (1 << 13))
6998	    as_warn (_("SP should not be in register list"));
6999	  if (inst.instruction == T_MNEM_stmia)
7000	    {
7001	      if (inst.operands[1].imm & (1 << 15))
7002		as_warn (_("PC should not be in register list"));
7003	      if (inst.operands[1].imm & (1 << inst.operands[0].reg))
7004		as_warn (_("value stored for r%d is UNPREDICTABLE"),
7005			 inst.operands[0].reg);
7006	    }
7007	  else
7008	    {
7009	      if (inst.operands[1].imm & (1 << 14)
7010		  && inst.operands[1].imm & (1 << 15))
7011		as_warn (_("LR and PC should not both be in register list"));
7012	      if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
7013		  && inst.operands[0].writeback)
7014		as_warn (_("base register should not be in register list "
7015			   "when written back"));
7016	    }
7017	  if (inst.instruction < 0xffff)
7018	    inst.instruction = THUMB_OP32 (inst.instruction);
7019	  inst.instruction |= inst.operands[0].reg << 16;
7020	  inst.instruction |= inst.operands[1].imm;
7021	  if (inst.operands[0].writeback)
7022	    inst.instruction |= WRITE_BACK;
7023	}
7024    }
7025  else
7026    {
7027      constraint (inst.operands[0].reg > 7
7028		  || (inst.operands[1].imm & ~0xff), BAD_HIREG);
7029      if (inst.instruction == T_MNEM_stmia)
7030	{
7031	  if (!inst.operands[0].writeback)
7032	    as_warn (_("this instruction will write back the base register"));
7033	  if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
7034	      && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
7035	    as_warn (_("value stored for r%d is UNPREDICTABLE"),
7036		     inst.operands[0].reg);
7037	}
7038      else
7039	{
7040	  if (!inst.operands[0].writeback
7041	      && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
7042	    as_warn (_("this instruction will write back the base register"));
7043	  else if (inst.operands[0].writeback
7044		   && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
7045	    as_warn (_("this instruction will not write back the base register"));
7046	}
7047
7048      inst.instruction = THUMB_OP16 (inst.instruction);
7049      inst.instruction |= inst.operands[0].reg << 8;
7050      inst.instruction |= inst.operands[1].imm;
7051    }
7052}
7053
7054static void
7055do_t_ldrex (void)
7056{
7057  constraint (!inst.operands[1].isreg || !inst.operands[1].preind
7058	      || inst.operands[1].postind || inst.operands[1].writeback
7059	      || inst.operands[1].immisreg || inst.operands[1].shifted
7060	      || inst.operands[1].negative,
7061	      BAD_ADDR_MODE);
7062
7063  inst.instruction |= inst.operands[0].reg << 12;
7064  inst.instruction |= inst.operands[1].reg << 16;
7065  inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
7066}
7067
7068static void
7069do_t_ldrexd (void)
7070{
7071  if (!inst.operands[1].present)
7072    {
7073      constraint (inst.operands[0].reg == REG_LR,
7074		  _("r14 not allowed as first register "
7075		    "when second register is omitted"));
7076      inst.operands[1].reg = inst.operands[0].reg + 1;
7077    }
7078  constraint (inst.operands[0].reg == inst.operands[1].reg,
7079	      BAD_OVERLAP);
7080
7081  inst.instruction |= inst.operands[0].reg << 12;
7082  inst.instruction |= inst.operands[1].reg << 8;
7083  inst.instruction |= inst.operands[2].reg << 16;
7084}
7085
7086static void
7087do_t_ldst (void)
7088{
7089  unsigned long opcode;
7090  int Rn;
7091
7092  opcode = inst.instruction;
7093  if (unified_syntax)
7094    {
7095      if (!inst.operands[1].isreg)
7096	{
7097	  if (opcode <= 0xffff)
7098	    inst.instruction = THUMB_OP32 (opcode);
7099	  if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
7100	    return;
7101	}
7102      if (inst.operands[1].isreg
7103	  && !inst.operands[1].writeback
7104	  && !inst.operands[1].shifted && !inst.operands[1].postind
7105	  && !inst.operands[1].negative && inst.operands[0].reg <= 7
7106	  && opcode <= 0xffff
7107	  && inst.size_req != 4)
7108	{
7109	  /* Insn may have a 16-bit form.  */
7110	  Rn = inst.operands[1].reg;
7111	  if (inst.operands[1].immisreg)
7112	    {
7113	      inst.instruction = THUMB_OP16 (opcode);
7114	      /* [Rn, Ri] */
7115	      if (Rn <= 7 && inst.operands[1].imm <= 7)
7116		goto op16;
7117	    }
7118	  else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
7119		    && opcode != T_MNEM_ldrsb)
7120		   || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
7121		   || (Rn == REG_SP && opcode == T_MNEM_str))
7122	    {
7123	      /* [Rn, #const] */
7124	      if (Rn > 7)
7125		{
7126		  if (Rn == REG_PC)
7127		    {
7128		      if (inst.reloc.pc_rel)
7129			opcode = T_MNEM_ldr_pc2;
7130		      else
7131			opcode = T_MNEM_ldr_pc;
7132		    }
7133		  else
7134		    {
7135		      if (opcode == T_MNEM_ldr)
7136			opcode = T_MNEM_ldr_sp;
7137		      else
7138			opcode = T_MNEM_str_sp;
7139		    }
7140		  inst.instruction = inst.operands[0].reg << 8;
7141		}
7142	      else
7143		{
7144		  inst.instruction = inst.operands[0].reg;
7145		  inst.instruction |= inst.operands[1].reg << 3;
7146		}
7147	      inst.instruction |= THUMB_OP16 (opcode);
7148	      if (inst.size_req == 2)
7149		inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
7150	      else
7151		inst.relax = opcode;
7152	      return;
7153	    }
7154	}
7155      /* Definitely a 32-bit variant.  */
7156      inst.instruction = THUMB_OP32 (opcode);
7157      inst.instruction |= inst.operands[0].reg << 12;
7158      encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
7159      return;
7160    }
7161
7162  constraint (inst.operands[0].reg > 7, BAD_HIREG);
7163
7164  if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
7165    {
7166      /* Only [Rn,Rm] is acceptable.  */
7167      constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
7168      constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
7169		  || inst.operands[1].postind || inst.operands[1].shifted
7170		  || inst.operands[1].negative,
7171		  _("Thumb does not support this addressing mode"));
7172      inst.instruction = THUMB_OP16 (inst.instruction);
7173      goto op16;
7174    }
7175
7176  inst.instruction = THUMB_OP16 (inst.instruction);
7177  if (!inst.operands[1].isreg)
7178    if (move_or_literal_pool (0, /*thumb_p=*/TRUE, /*mode_3=*/FALSE))
7179      return;
7180
7181  constraint (!inst.operands[1].preind
7182	      || inst.operands[1].shifted
7183	      || inst.operands[1].writeback,
7184	      _("Thumb does not support this addressing mode"));
7185  if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
7186    {
7187      constraint (inst.instruction & 0x0600,
7188		  _("byte or halfword not valid for base register"));
7189      constraint (inst.operands[1].reg == REG_PC
7190		  && !(inst.instruction & THUMB_LOAD_BIT),
7191		  _("r15 based store not allowed"));
7192      constraint (inst.operands[1].immisreg,
7193		  _("invalid base register for register offset"));
7194
7195      if (inst.operands[1].reg == REG_PC)
7196	inst.instruction = T_OPCODE_LDR_PC;
7197      else if (inst.instruction & THUMB_LOAD_BIT)
7198	inst.instruction = T_OPCODE_LDR_SP;
7199      else
7200	inst.instruction = T_OPCODE_STR_SP;
7201
7202      inst.instruction |= inst.operands[0].reg << 8;
7203      inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
7204      return;
7205    }
7206
7207  constraint (inst.operands[1].reg > 7, BAD_HIREG);
7208  if (!inst.operands[1].immisreg)
7209    {
7210      /* Immediate offset.  */
7211      inst.instruction |= inst.operands[0].reg;
7212      inst.instruction |= inst.operands[1].reg << 3;
7213      inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
7214      return;
7215    }
7216
7217  /* Register offset.  */
7218  constraint (inst.operands[1].imm > 7, BAD_HIREG);
7219  constraint (inst.operands[1].negative,
7220	      _("Thumb does not support this addressing mode"));
7221
7222 op16:
7223  switch (inst.instruction)
7224    {
7225    case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
7226    case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
7227    case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
7228    case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
7229    case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
7230    case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
7231    case 0x5600 /* ldrsb */:
7232    case 0x5e00 /* ldrsh */: break;
7233    default: abort ();
7234    }
7235
7236  inst.instruction |= inst.operands[0].reg;
7237  inst.instruction |= inst.operands[1].reg << 3;
7238  inst.instruction |= inst.operands[1].imm << 6;
7239}
7240
7241static void
7242do_t_ldstd (void)
7243{
7244  if (!inst.operands[1].present)
7245    {
7246      inst.operands[1].reg = inst.operands[0].reg + 1;
7247      constraint (inst.operands[0].reg == REG_LR,
7248		  _("r14 not allowed here"));
7249    }
7250  inst.instruction |= inst.operands[0].reg << 12;
7251  inst.instruction |= inst.operands[1].reg << 8;
7252  encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
7253
7254}
7255
7256static void
7257do_t_ldstt (void)
7258{
7259  inst.instruction |= inst.operands[0].reg << 12;
7260  encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
7261}
7262
7263static void
7264do_t_mla (void)
7265{
7266  inst.instruction |= inst.operands[0].reg << 8;
7267  inst.instruction |= inst.operands[1].reg << 16;
7268  inst.instruction |= inst.operands[2].reg;
7269  inst.instruction |= inst.operands[3].reg << 12;
7270}
7271
7272static void
7273do_t_mlal (void)
7274{
7275  inst.instruction |= inst.operands[0].reg << 12;
7276  inst.instruction |= inst.operands[1].reg << 8;
7277  inst.instruction |= inst.operands[2].reg << 16;
7278  inst.instruction |= inst.operands[3].reg;
7279}
7280
7281static void
7282do_t_mov_cmp (void)
7283{
7284  if (unified_syntax)
7285    {
7286      int r0off = (inst.instruction == T_MNEM_mov
7287		   || inst.instruction == T_MNEM_movs) ? 8 : 16;
7288      unsigned long opcode;
7289      bfd_boolean narrow;
7290      bfd_boolean low_regs;
7291
7292      low_regs = (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7);
7293      opcode = inst.instruction;
7294      if (current_it_mask)
7295	narrow = opcode != T_MNEM_movs;
7296      else
7297	narrow = opcode != T_MNEM_movs || low_regs;
7298      if (inst.size_req == 4
7299	  || inst.operands[1].shifted)
7300	narrow = FALSE;
7301
7302      if (!inst.operands[1].isreg)
7303	{
7304	  /* Immediate operand.  */
7305	  if (current_it_mask == 0 && opcode == T_MNEM_mov)
7306	    narrow = 0;
7307	  if (low_regs && narrow)
7308	    {
7309	      inst.instruction = THUMB_OP16 (opcode);
7310	      inst.instruction |= inst.operands[0].reg << 8;
7311	      if (inst.size_req == 2)
7312		inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
7313	      else
7314		inst.relax = opcode;
7315	    }
7316	  else
7317	    {
7318	      inst.instruction = THUMB_OP32 (inst.instruction);
7319	      inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7320	      inst.instruction |= inst.operands[0].reg << r0off;
7321	      inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7322	    }
7323	}
7324      else if (!narrow)
7325	{
7326	  inst.instruction = THUMB_OP32 (inst.instruction);
7327	  inst.instruction |= inst.operands[0].reg << r0off;
7328	  encode_thumb32_shifted_operand (1);
7329	}
7330      else
7331	switch (inst.instruction)
7332	  {
7333	  case T_MNEM_mov:
7334	    inst.instruction = T_OPCODE_MOV_HR;
7335	    inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
7336	    inst.instruction |= (inst.operands[0].reg & 0x7);
7337	    inst.instruction |= inst.operands[1].reg << 3;
7338	    break;
7339
7340	  case T_MNEM_movs:
7341	    /* We know we have low registers at this point.
7342	       Generate ADD Rd, Rs, #0.  */
7343	    inst.instruction = T_OPCODE_ADD_I3;
7344	    inst.instruction |= inst.operands[0].reg;
7345	    inst.instruction |= inst.operands[1].reg << 3;
7346	    break;
7347
7348	  case T_MNEM_cmp:
7349	    if (low_regs)
7350	      {
7351		inst.instruction = T_OPCODE_CMP_LR;
7352		inst.instruction |= inst.operands[0].reg;
7353		inst.instruction |= inst.operands[1].reg << 3;
7354	      }
7355	    else
7356	      {
7357		inst.instruction = T_OPCODE_CMP_HR;
7358		inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
7359		inst.instruction |= (inst.operands[0].reg & 0x7);
7360		inst.instruction |= inst.operands[1].reg << 3;
7361	      }
7362	    break;
7363	  }
7364      return;
7365    }
7366
7367  inst.instruction = THUMB_OP16 (inst.instruction);
7368  if (inst.operands[1].isreg)
7369    {
7370      if (inst.operands[0].reg < 8 && inst.operands[1].reg < 8)
7371	{
7372	  /* A move of two lowregs is encoded as ADD Rd, Rs, #0
7373	     since a MOV instruction produces unpredictable results.  */
7374	  if (inst.instruction == T_OPCODE_MOV_I8)
7375	    inst.instruction = T_OPCODE_ADD_I3;
7376	  else
7377	    inst.instruction = T_OPCODE_CMP_LR;
7378
7379	  inst.instruction |= inst.operands[0].reg;
7380	  inst.instruction |= inst.operands[1].reg << 3;
7381	}
7382      else
7383	{
7384	  if (inst.instruction == T_OPCODE_MOV_I8)
7385	    inst.instruction = T_OPCODE_MOV_HR;
7386	  else
7387	    inst.instruction = T_OPCODE_CMP_HR;
7388	  do_t_cpy ();
7389	}
7390    }
7391  else
7392    {
7393      constraint (inst.operands[0].reg > 7,
7394		  _("only lo regs allowed with immediate"));
7395      inst.instruction |= inst.operands[0].reg << 8;
7396      inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
7397    }
7398}
7399
7400static void
7401do_t_mov16 (void)
7402{
7403  bfd_vma imm;
7404  bfd_boolean top;
7405
7406  top = (inst.instruction & 0x00800000) != 0;
7407  if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
7408    {
7409      constraint (top, _(":lower16: not allowed this instruction"));
7410      inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
7411    }
7412  else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
7413    {
7414      constraint (!top, _(":upper16: not allowed this instruction"));
7415      inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
7416    }
7417
7418  inst.instruction |= inst.operands[0].reg << 8;
7419  if (inst.reloc.type == BFD_RELOC_UNUSED)
7420    {
7421      imm = inst.reloc.exp.X_add_number;
7422      inst.instruction |= (imm & 0xf000) << 4;
7423      inst.instruction |= (imm & 0x0800) << 15;
7424      inst.instruction |= (imm & 0x0700) << 4;
7425      inst.instruction |= (imm & 0x00ff);
7426    }
7427}
7428
7429static void
7430do_t_mvn_tst (void)
7431{
7432  if (unified_syntax)
7433    {
7434      int r0off = (inst.instruction == T_MNEM_mvn
7435		   || inst.instruction == T_MNEM_mvns) ? 8 : 16;
7436      bfd_boolean narrow;
7437
7438      if (inst.size_req == 4
7439	  || inst.instruction > 0xffff
7440	  || inst.operands[1].shifted
7441	  || inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
7442	narrow = FALSE;
7443      else if (inst.instruction == T_MNEM_cmn)
7444	narrow = TRUE;
7445      else if (THUMB_SETS_FLAGS (inst.instruction))
7446	narrow = (current_it_mask == 0);
7447      else
7448	narrow = (current_it_mask != 0);
7449
7450      if (!inst.operands[1].isreg)
7451	{
7452	  /* For an immediate, we always generate a 32-bit opcode;
7453	     section relaxation will shrink it later if possible.  */
7454	  if (inst.instruction < 0xffff)
7455	    inst.instruction = THUMB_OP32 (inst.instruction);
7456	  inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7457	  inst.instruction |= inst.operands[0].reg << r0off;
7458	  inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7459	}
7460      else
7461	{
7462	  /* See if we can do this with a 16-bit instruction.  */
7463	  if (narrow)
7464	    {
7465	      inst.instruction = THUMB_OP16 (inst.instruction);
7466	      inst.instruction |= inst.operands[0].reg;
7467	      inst.instruction |= inst.operands[1].reg << 3;
7468	    }
7469	  else
7470	    {
7471	      constraint (inst.operands[1].shifted
7472			  && inst.operands[1].immisreg,
7473			  _("shift must be constant"));
7474	      if (inst.instruction < 0xffff)
7475		inst.instruction = THUMB_OP32 (inst.instruction);
7476	      inst.instruction |= inst.operands[0].reg << r0off;
7477	      encode_thumb32_shifted_operand (1);
7478	    }
7479	}
7480    }
7481  else
7482    {
7483      constraint (inst.instruction > 0xffff
7484		  || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
7485      constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
7486		  _("unshifted register required"));
7487      constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
7488		  BAD_HIREG);
7489
7490      inst.instruction = THUMB_OP16 (inst.instruction);
7491      inst.instruction |= inst.operands[0].reg;
7492      inst.instruction |= inst.operands[1].reg << 3;
7493    }
7494}
7495
7496static void
7497do_t_mrs (void)
7498{
7499  int flags;
7500  flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
7501  if (flags == 0)
7502    {
7503      constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7m),
7504		  _("selected processor does not support "
7505		    "requested special purpose register"));
7506    }
7507  else
7508    {
7509      constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1),
7510		  _("selected processor does not support "
7511		    "requested special purpose register %x"));
7512      /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
7513      constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
7514		  _("'CPSR' or 'SPSR' expected"));
7515    }
7516
7517  inst.instruction |= inst.operands[0].reg << 8;
7518  inst.instruction |= (flags & SPSR_BIT) >> 2;
7519  inst.instruction |= inst.operands[1].imm & 0xff;
7520}
7521
7522static void
7523do_t_msr (void)
7524{
7525  int flags;
7526
7527  constraint (!inst.operands[1].isreg,
7528	      _("Thumb encoding does not support an immediate here"));
7529  flags = inst.operands[0].imm;
7530  if (flags & ~0xff)
7531    {
7532      constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1),
7533		  _("selected processor does not support "
7534		    "requested special purpose register"));
7535    }
7536  else
7537    {
7538      constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7m),
7539		  _("selected processor does not support "
7540		    "requested special purpose register"));
7541      flags |= PSR_f;
7542    }
7543  inst.instruction |= (flags & SPSR_BIT) >> 2;
7544  inst.instruction |= (flags & ~SPSR_BIT) >> 8;
7545  inst.instruction |= (flags & 0xff);
7546  inst.instruction |= inst.operands[1].reg << 16;
7547}
7548
7549static void
7550do_t_mul (void)
7551{
7552  if (!inst.operands[2].present)
7553    inst.operands[2].reg = inst.operands[0].reg;
7554
7555  /* There is no 32-bit MULS and no 16-bit MUL. */
7556  if (unified_syntax && inst.instruction == T_MNEM_mul)
7557    {
7558      inst.instruction = THUMB_OP32 (inst.instruction);
7559      inst.instruction |= inst.operands[0].reg << 8;
7560      inst.instruction |= inst.operands[1].reg << 16;
7561      inst.instruction |= inst.operands[2].reg << 0;
7562    }
7563  else
7564    {
7565      constraint (!unified_syntax
7566		  && inst.instruction == T_MNEM_muls, BAD_THUMB32);
7567      constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
7568		  BAD_HIREG);
7569
7570      inst.instruction = THUMB_OP16 (inst.instruction);
7571      inst.instruction |= inst.operands[0].reg;
7572
7573      if (inst.operands[0].reg == inst.operands[1].reg)
7574	inst.instruction |= inst.operands[2].reg << 3;
7575      else if (inst.operands[0].reg == inst.operands[2].reg)
7576	inst.instruction |= inst.operands[1].reg << 3;
7577      else
7578	constraint (1, _("dest must overlap one source register"));
7579    }
7580}
7581
7582static void
7583do_t_mull (void)
7584{
7585  inst.instruction |= inst.operands[0].reg << 12;
7586  inst.instruction |= inst.operands[1].reg << 8;
7587  inst.instruction |= inst.operands[2].reg << 16;
7588  inst.instruction |= inst.operands[3].reg;
7589
7590  if (inst.operands[0].reg == inst.operands[1].reg)
7591    as_tsktsk (_("rdhi and rdlo must be different"));
7592}
7593
7594static void
7595do_t_nop (void)
7596{
7597  if (unified_syntax)
7598    {
7599      if (inst.size_req == 4 || inst.operands[0].imm > 15)
7600	{
7601	  inst.instruction = THUMB_OP32 (inst.instruction);
7602	  inst.instruction |= inst.operands[0].imm;
7603	}
7604      else
7605	{
7606	  inst.instruction = THUMB_OP16 (inst.instruction);
7607	  inst.instruction |= inst.operands[0].imm << 4;
7608	}
7609    }
7610  else
7611    {
7612      constraint (inst.operands[0].present,
7613		  _("Thumb does not support NOP with hints"));
7614      inst.instruction = 0x46c0;
7615    }
7616}
7617
7618static void
7619do_t_neg (void)
7620{
7621  if (unified_syntax)
7622    {
7623      bfd_boolean narrow;
7624
7625      if (THUMB_SETS_FLAGS (inst.instruction))
7626	narrow = (current_it_mask == 0);
7627      else
7628	narrow = (current_it_mask != 0);
7629      if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
7630	narrow = FALSE;
7631      if (inst.size_req == 4)
7632	narrow = FALSE;
7633
7634      if (!narrow)
7635	{
7636	  inst.instruction = THUMB_OP32 (inst.instruction);
7637	  inst.instruction |= inst.operands[0].reg << 8;
7638	  inst.instruction |= inst.operands[1].reg << 16;
7639	}
7640      else
7641	{
7642	  inst.instruction = THUMB_OP16 (inst.instruction);
7643	  inst.instruction |= inst.operands[0].reg;
7644	  inst.instruction |= inst.operands[1].reg << 3;
7645	}
7646    }
7647  else
7648    {
7649      constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
7650		  BAD_HIREG);
7651      constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
7652
7653      inst.instruction = THUMB_OP16 (inst.instruction);
7654      inst.instruction |= inst.operands[0].reg;
7655      inst.instruction |= inst.operands[1].reg << 3;
7656    }
7657}
7658
7659static void
7660do_t_pkhbt (void)
7661{
7662  inst.instruction |= inst.operands[0].reg << 8;
7663  inst.instruction |= inst.operands[1].reg << 16;
7664  inst.instruction |= inst.operands[2].reg;
7665  if (inst.operands[3].present)
7666    {
7667      unsigned int val = inst.reloc.exp.X_add_number;
7668      constraint (inst.reloc.exp.X_op != O_constant,
7669		  _("expression too complex"));
7670      inst.instruction |= (val & 0x1c) << 10;
7671      inst.instruction |= (val & 0x03) << 6;
7672    }
7673}
7674
7675static void
7676do_t_pkhtb (void)
7677{
7678  if (!inst.operands[3].present)
7679    inst.instruction &= ~0x00000020;
7680  do_t_pkhbt ();
7681}
7682
7683static void
7684do_t_pld (void)
7685{
7686  encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
7687}
7688
7689static void
7690do_t_push_pop (void)
7691{
7692  unsigned mask;
7693
7694  constraint (inst.operands[0].writeback,
7695	      _("push/pop do not support {reglist}^"));
7696  constraint (inst.reloc.type != BFD_RELOC_UNUSED,
7697	      _("expression too complex"));
7698
7699  mask = inst.operands[0].imm;
7700  if ((mask & ~0xff) == 0)
7701    inst.instruction = THUMB_OP16 (inst.instruction);
7702  else if ((inst.instruction == T_MNEM_push
7703	    && (mask & ~0xff) == 1 << REG_LR)
7704	   || (inst.instruction == T_MNEM_pop
7705	       && (mask & ~0xff) == 1 << REG_PC))
7706    {
7707      inst.instruction = THUMB_OP16 (inst.instruction);
7708      inst.instruction |= THUMB_PP_PC_LR;
7709      mask &= 0xff;
7710    }
7711  else if (unified_syntax)
7712    {
7713      if (mask & (1 << 13))
7714	inst.error =  _("SP not allowed in register list");
7715      if (inst.instruction == T_MNEM_push)
7716	{
7717	  if (mask & (1 << 15))
7718	    inst.error = _("PC not allowed in register list");
7719	}
7720      else
7721	{
7722	  if (mask & (1 << 14)
7723	      && mask & (1 << 15))
7724	    inst.error = _("LR and PC should not both be in register list");
7725	}
7726      if ((mask & (mask - 1)) == 0)
7727	{
7728	  /* Single register push/pop implemented as str/ldr.  */
7729	  if (inst.instruction == T_MNEM_push)
7730	    inst.instruction = 0xf84d0d04; /* str reg, [sp, #-4]! */
7731	  else
7732	    inst.instruction = 0xf85d0b04; /* ldr reg, [sp], #4 */
7733	  mask = ffs(mask) - 1;
7734	  mask <<= 12;
7735	}
7736      else
7737	inst.instruction = THUMB_OP32 (inst.instruction);
7738    }
7739  else
7740    {
7741      inst.error = _("invalid register list to push/pop instruction");
7742      return;
7743    }
7744
7745  inst.instruction |= mask;
7746}
7747
7748static void
7749do_t_rbit (void)
7750{
7751  inst.instruction |= inst.operands[0].reg << 8;
7752  inst.instruction |= inst.operands[1].reg << 16;
7753}
7754
7755static void
7756do_t_rd_rm (void)
7757{
7758  inst.instruction |= inst.operands[0].reg << 8;
7759  inst.instruction |= inst.operands[1].reg;
7760}
7761
7762static void
7763do_t_rev (void)
7764{
7765  if (inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
7766      && inst.size_req != 4)
7767    {
7768      inst.instruction = THUMB_OP16 (inst.instruction);
7769      inst.instruction |= inst.operands[0].reg;
7770      inst.instruction |= inst.operands[1].reg << 3;
7771    }
7772  else if (unified_syntax)
7773    {
7774      inst.instruction = THUMB_OP32 (inst.instruction);
7775      inst.instruction |= inst.operands[0].reg << 8;
7776      inst.instruction |= inst.operands[1].reg << 16;
7777      inst.instruction |= inst.operands[1].reg;
7778    }
7779  else
7780    inst.error = BAD_HIREG;
7781}
7782
7783static void
7784do_t_rsb (void)
7785{
7786  int Rd, Rs;
7787
7788  Rd = inst.operands[0].reg;
7789  Rs = (inst.operands[1].present
7790	? inst.operands[1].reg    /* Rd, Rs, foo */
7791	: inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
7792
7793  inst.instruction |= Rd << 8;
7794  inst.instruction |= Rs << 16;
7795  if (!inst.operands[2].isreg)
7796    {
7797      inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
7798      inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
7799    }
7800  else
7801    encode_thumb32_shifted_operand (2);
7802}
7803
7804static void
7805do_t_setend (void)
7806{
7807  constraint (current_it_mask, BAD_NOT_IT);
7808  if (inst.operands[0].imm)
7809    inst.instruction |= 0x8;
7810}
7811
7812static void
7813do_t_shift (void)
7814{
7815  if (!inst.operands[1].present)
7816    inst.operands[1].reg = inst.operands[0].reg;
7817
7818  if (unified_syntax)
7819    {
7820      bfd_boolean narrow;
7821      int shift_kind;
7822
7823      switch (inst.instruction)
7824	{
7825	case T_MNEM_asr:
7826	case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
7827	case T_MNEM_lsl:
7828	case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
7829	case T_MNEM_lsr:
7830	case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
7831	case T_MNEM_ror:
7832	case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
7833	default: abort ();
7834	}
7835
7836      if (THUMB_SETS_FLAGS (inst.instruction))
7837	narrow = (current_it_mask == 0);
7838      else
7839	narrow = (current_it_mask != 0);
7840      if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
7841	narrow = FALSE;
7842      if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
7843	narrow = FALSE;
7844      if (inst.operands[2].isreg
7845	  && (inst.operands[1].reg != inst.operands[0].reg
7846	      || inst.operands[2].reg > 7))
7847	narrow = FALSE;
7848      if (inst.size_req == 4)
7849	narrow = FALSE;
7850
7851      if (!narrow)
7852	{
7853	  if (inst.operands[2].isreg)
7854	    {
7855	      inst.instruction = THUMB_OP32 (inst.instruction);
7856	      inst.instruction |= inst.operands[0].reg << 8;
7857	      inst.instruction |= inst.operands[1].reg << 16;
7858	      inst.instruction |= inst.operands[2].reg;
7859	    }
7860	  else
7861	    {
7862	      inst.operands[1].shifted = 1;
7863	      inst.operands[1].shift_kind = shift_kind;
7864	      inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
7865					     ? T_MNEM_movs : T_MNEM_mov);
7866	      inst.instruction |= inst.operands[0].reg << 8;
7867	      encode_thumb32_shifted_operand (1);
7868	      /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
7869	      inst.reloc.type = BFD_RELOC_UNUSED;
7870	    }
7871	}
7872      else
7873	{
7874	  if (inst.operands[2].isreg)
7875	    {
7876	      switch (shift_kind)
7877		{
7878		case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
7879		case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
7880		case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
7881		case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
7882		default: abort ();
7883		}
7884
7885	      inst.instruction |= inst.operands[0].reg;
7886	      inst.instruction |= inst.operands[2].reg << 3;
7887	    }
7888	  else
7889	    {
7890	      switch (shift_kind)
7891		{
7892		case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
7893		case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
7894		case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
7895		default: abort ();
7896		}
7897	      inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
7898	      inst.instruction |= inst.operands[0].reg;
7899	      inst.instruction |= inst.operands[1].reg << 3;
7900	    }
7901	}
7902    }
7903  else
7904    {
7905      constraint (inst.operands[0].reg > 7
7906		  || inst.operands[1].reg > 7, BAD_HIREG);
7907      constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
7908
7909      if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
7910	{
7911	  constraint (inst.operands[2].reg > 7, BAD_HIREG);
7912	  constraint (inst.operands[0].reg != inst.operands[1].reg,
7913		      _("source1 and dest must be same register"));
7914
7915	  switch (inst.instruction)
7916	    {
7917	    case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
7918	    case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
7919	    case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
7920	    case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
7921	    default: abort ();
7922	    }
7923
7924	  inst.instruction |= inst.operands[0].reg;
7925	  inst.instruction |= inst.operands[2].reg << 3;
7926	}
7927      else
7928	{
7929	  switch (inst.instruction)
7930	    {
7931	    case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
7932	    case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
7933	    case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
7934	    case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
7935	    default: abort ();
7936	    }
7937	  inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
7938	  inst.instruction |= inst.operands[0].reg;
7939	  inst.instruction |= inst.operands[1].reg << 3;
7940	}
7941    }
7942}
7943
7944static void
7945do_t_simd (void)
7946{
7947  inst.instruction |= inst.operands[0].reg << 8;
7948  inst.instruction |= inst.operands[1].reg << 16;
7949  inst.instruction |= inst.operands[2].reg;
7950}
7951
7952static void
7953do_t_smc (void)
7954{
7955  unsigned int value = inst.reloc.exp.X_add_number;
7956  constraint (inst.reloc.exp.X_op != O_constant,
7957	      _("expression too complex"));
7958  inst.reloc.type = BFD_RELOC_UNUSED;
7959  inst.instruction |= (value & 0xf000) >> 12;
7960  inst.instruction |= (value & 0x0ff0);
7961  inst.instruction |= (value & 0x000f) << 16;
7962}
7963
7964static void
7965do_t_ssat (void)
7966{
7967  inst.instruction |= inst.operands[0].reg << 8;
7968  inst.instruction |= inst.operands[1].imm - 1;
7969  inst.instruction |= inst.operands[2].reg << 16;
7970
7971  if (inst.operands[3].present)
7972    {
7973      constraint (inst.reloc.exp.X_op != O_constant,
7974		  _("expression too complex"));
7975
7976      if (inst.reloc.exp.X_add_number != 0)
7977	{
7978	  if (inst.operands[3].shift_kind == SHIFT_ASR)
7979	    inst.instruction |= 0x00200000;  /* sh bit */
7980	  inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
7981	  inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
7982	}
7983      inst.reloc.type = BFD_RELOC_UNUSED;
7984    }
7985}
7986
7987static void
7988do_t_ssat16 (void)
7989{
7990  inst.instruction |= inst.operands[0].reg << 8;
7991  inst.instruction |= inst.operands[1].imm - 1;
7992  inst.instruction |= inst.operands[2].reg << 16;
7993}
7994
7995static void
7996do_t_strex (void)
7997{
7998  constraint (!inst.operands[2].isreg || !inst.operands[2].preind
7999	      || inst.operands[2].postind || inst.operands[2].writeback
8000	      || inst.operands[2].immisreg || inst.operands[2].shifted
8001	      || inst.operands[2].negative,
8002	      BAD_ADDR_MODE);
8003
8004  inst.instruction |= inst.operands[0].reg << 8;
8005  inst.instruction |= inst.operands[1].reg << 12;
8006  inst.instruction |= inst.operands[2].reg << 16;
8007  inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
8008}
8009
8010static void
8011do_t_strexd (void)
8012{
8013  if (!inst.operands[2].present)
8014    inst.operands[2].reg = inst.operands[1].reg + 1;
8015
8016  constraint (inst.operands[0].reg == inst.operands[1].reg
8017	      || inst.operands[0].reg == inst.operands[2].reg
8018	      || inst.operands[0].reg == inst.operands[3].reg
8019	      || inst.operands[1].reg == inst.operands[2].reg,
8020	      BAD_OVERLAP);
8021
8022  inst.instruction |= inst.operands[0].reg;
8023  inst.instruction |= inst.operands[1].reg << 12;
8024  inst.instruction |= inst.operands[2].reg << 8;
8025  inst.instruction |= inst.operands[3].reg << 16;
8026}
8027
8028static void
8029do_t_sxtah (void)
8030{
8031  inst.instruction |= inst.operands[0].reg << 8;
8032  inst.instruction |= inst.operands[1].reg << 16;
8033  inst.instruction |= inst.operands[2].reg;
8034  inst.instruction |= inst.operands[3].imm << 4;
8035}
8036
8037static void
8038do_t_sxth (void)
8039{
8040  if (inst.instruction <= 0xffff && inst.size_req != 4
8041      && inst.operands[0].reg <= 7 && inst.operands[1].reg <= 7
8042      && (!inst.operands[2].present || inst.operands[2].imm == 0))
8043    {
8044      inst.instruction = THUMB_OP16 (inst.instruction);
8045      inst.instruction |= inst.operands[0].reg;
8046      inst.instruction |= inst.operands[1].reg << 3;
8047    }
8048  else if (unified_syntax)
8049    {
8050      if (inst.instruction <= 0xffff)
8051	inst.instruction = THUMB_OP32 (inst.instruction);
8052      inst.instruction |= inst.operands[0].reg << 8;
8053      inst.instruction |= inst.operands[1].reg;
8054      inst.instruction |= inst.operands[2].imm << 4;
8055    }
8056  else
8057    {
8058      constraint (inst.operands[2].present && inst.operands[2].imm != 0,
8059		  _("Thumb encoding does not support rotation"));
8060      constraint (1, BAD_HIREG);
8061    }
8062}
8063
8064static void
8065do_t_swi (void)
8066{
8067  inst.reloc.type = BFD_RELOC_ARM_SWI;
8068}
8069
8070static void
8071do_t_tb (void)
8072{
8073  int half;
8074
8075  half = (inst.instruction & 0x10) != 0;
8076  constraint (current_it_mask && current_it_mask != 0x10, BAD_BRANCH);
8077  constraint (inst.operands[0].immisreg,
8078	      _("instruction requires register index"));
8079  constraint (inst.operands[0].imm == 15,
8080	      _("PC is not a valid index register"));
8081  constraint (!half && inst.operands[0].shifted,
8082	      _("instruction does not allow shifted index"));
8083  inst.instruction |= (inst.operands[0].reg << 16) | inst.operands[0].imm;
8084}
8085
8086static void
8087do_t_usat (void)
8088{
8089  inst.instruction |= inst.operands[0].reg << 8;
8090  inst.instruction |= inst.operands[1].imm;
8091  inst.instruction |= inst.operands[2].reg << 16;
8092
8093  if (inst.operands[3].present)
8094    {
8095      constraint (inst.reloc.exp.X_op != O_constant,
8096		  _("expression too complex"));
8097      if (inst.reloc.exp.X_add_number != 0)
8098	{
8099	  if (inst.operands[3].shift_kind == SHIFT_ASR)
8100	    inst.instruction |= 0x00200000;  /* sh bit */
8101
8102	  inst.instruction |= (inst.reloc.exp.X_add_number & 0x1c) << 10;
8103	  inst.instruction |= (inst.reloc.exp.X_add_number & 0x03) << 6;
8104	}
8105      inst.reloc.type = BFD_RELOC_UNUSED;
8106    }
8107}
8108
8109static void
8110do_t_usat16 (void)
8111{
8112  inst.instruction |= inst.operands[0].reg << 8;
8113  inst.instruction |= inst.operands[1].imm;
8114  inst.instruction |= inst.operands[2].reg << 16;
8115}
8116
8117/* Overall per-instruction processing.	*/
8118
8119/* We need to be able to fix up arbitrary expressions in some statements.
8120   This is so that we can handle symbols that are an arbitrary distance from
8121   the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
8122   which returns part of an address in a form which will be valid for
8123   a data instruction.	We do this by pushing the expression into a symbol
8124   in the expr_section, and creating a fix for that.  */
8125
8126static void
8127fix_new_arm (fragS *	   frag,
8128	     int	   where,
8129	     short int	   size,
8130	     expressionS * exp,
8131	     int	   pc_rel,
8132	     int	   reloc)
8133{
8134  fixS *	   new_fix;
8135
8136  switch (exp->X_op)
8137    {
8138    case O_constant:
8139    case O_symbol:
8140    case O_add:
8141    case O_subtract:
8142      new_fix = fix_new_exp (frag, where, size, exp, pc_rel, reloc);
8143      break;
8144
8145    default:
8146      new_fix = fix_new (frag, where, size, make_expr_symbol (exp), 0,
8147			 pc_rel, reloc);
8148      break;
8149    }
8150
8151  /* Mark whether the fix is to a THUMB instruction, or an ARM
8152     instruction.  */
8153  new_fix->tc_fix_data = thumb_mode;
8154}
8155
8156/* Create a frg for an instruction requiring relaxation.  */
8157static void
8158output_relax_insn (void)
8159{
8160  char * to;
8161  symbolS *sym;
8162  int offset;
8163
8164#ifdef OBJ_ELF
8165  /* The size of the instruction is unknown, so tie the debug info to the
8166     start of the instruction.  */
8167  dwarf2_emit_insn (0);
8168#endif
8169
8170  switch (inst.reloc.exp.X_op)
8171    {
8172    case O_symbol:
8173      sym = inst.reloc.exp.X_add_symbol;
8174      offset = inst.reloc.exp.X_add_number;
8175      break;
8176    case O_constant:
8177      sym = NULL;
8178      offset = inst.reloc.exp.X_add_number;
8179      break;
8180    default:
8181      sym = make_expr_symbol (&inst.reloc.exp);
8182      offset = 0;
8183      break;
8184  }
8185  to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
8186		 inst.relax, sym, offset, NULL/*offset, opcode*/);
8187  md_number_to_chars (to, inst.instruction, THUMB_SIZE);
8188}
8189
8190/* Write a 32-bit thumb instruction to buf.  */
8191static void
8192put_thumb32_insn (char * buf, unsigned long insn)
8193{
8194  md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
8195  md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
8196}
8197
8198static void
8199output_inst (const char * str)
8200{
8201  char * to = NULL;
8202
8203  if (inst.error)
8204    {
8205      as_bad ("%s -- `%s'", inst.error, str);
8206      return;
8207    }
8208  if (inst.relax) {
8209      output_relax_insn();
8210      return;
8211  }
8212  if (inst.size == 0)
8213    return;
8214
8215  to = frag_more (inst.size);
8216
8217  if (thumb_mode && (inst.size > THUMB_SIZE))
8218    {
8219      assert (inst.size == (2 * THUMB_SIZE));
8220      put_thumb32_insn (to, inst.instruction);
8221    }
8222  else if (inst.size > INSN_SIZE)
8223    {
8224      assert (inst.size == (2 * INSN_SIZE));
8225      md_number_to_chars (to, inst.instruction, INSN_SIZE);
8226      md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
8227    }
8228  else
8229    md_number_to_chars (to, inst.instruction, inst.size);
8230
8231  if (inst.reloc.type != BFD_RELOC_UNUSED)
8232    fix_new_arm (frag_now, to - frag_now->fr_literal,
8233		 inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
8234		 inst.reloc.type);
8235
8236#ifdef OBJ_ELF
8237  dwarf2_emit_insn (inst.size);
8238#endif
8239}
8240
8241/* Tag values used in struct asm_opcode's tag field.  */
8242enum opcode_tag
8243{
8244  OT_unconditional,	/* Instruction cannot be conditionalized.
8245			   The ARM condition field is still 0xE.  */
8246  OT_unconditionalF,	/* Instruction cannot be conditionalized
8247			   and carries 0xF in its ARM condition field.  */
8248  OT_csuffix,		/* Instruction takes a conditional suffix.  */
8249  OT_cinfix3,		/* Instruction takes a conditional infix,
8250			   beginning at character index 3.  (In
8251			   unified mode, it becomes a suffix.)  */
8252  OT_cinfix3_legacy,	/* Legacy instruction takes a conditional infix at
8253			   character index 3, even in unified mode.  Used for
8254			   legacy instructions where suffix and infix forms
8255			   may be ambiguous.  */
8256  OT_csuf_or_in3,	/* Instruction takes either a conditional
8257			   suffix or an infix at character index 3.  */
8258  OT_odd_infix_unc,	/* This is the unconditional variant of an
8259			   instruction that takes a conditional infix
8260			   at an unusual position.  In unified mode,
8261			   this variant will accept a suffix.  */
8262  OT_odd_infix_0	/* Values greater than or equal to OT_odd_infix_0
8263			   are the conditional variants of instructions that
8264			   take conditional infixes in unusual positions.
8265			   The infix appears at character index
8266			   (tag - OT_odd_infix_0).  These are not accepted
8267			   in unified mode.  */
8268};
8269
8270/* Subroutine of md_assemble, responsible for looking up the primary
8271   opcode from the mnemonic the user wrote.  STR points to the
8272   beginning of the mnemonic.
8273
8274   This is not simply a hash table lookup, because of conditional
8275   variants.  Most instructions have conditional variants, which are
8276   expressed with a _conditional affix_ to the mnemonic.  If we were
8277   to encode each conditional variant as a literal string in the opcode
8278   table, it would have approximately 20,000 entries.
8279
8280   Most mnemonics take this affix as a suffix, and in unified syntax,
8281   'most' is upgraded to 'all'.  However, in the divided syntax, some
8282   instructions take the affix as an infix, notably the s-variants of
8283   the arithmetic instructions.  Of those instructions, all but six
8284   have the infix appear after the third character of the mnemonic.
8285
8286   Accordingly, the algorithm for looking up primary opcodes given
8287   an identifier is:
8288
8289   1. Look up the identifier in the opcode table.
8290      If we find a match, go to step U.
8291
8292   2. Look up the last two characters of the identifier in the
8293      conditions table.  If we find a match, look up the first N-2
8294      characters of the identifier in the opcode table.  If we
8295      find a match, go to step CE.
8296
8297   3. Look up the fourth and fifth characters of the identifier in
8298      the conditions table.  If we find a match, extract those
8299      characters from the identifier, and look up the remaining
8300      characters in the opcode table.  If we find a match, go
8301      to step CM.
8302
8303   4. Fail.
8304
8305   U. Examine the tag field of the opcode structure, in case this is
8306      one of the six instructions with its conditional infix in an
8307      unusual place.  If it is, the tag tells us where to find the
8308      infix; look it up in the conditions table and set inst.cond
8309      accordingly.  Otherwise, this is an unconditional instruction.
8310      Again set inst.cond accordingly.  Return the opcode structure.
8311
8312  CE. Examine the tag field to make sure this is an instruction that
8313      should receive a conditional suffix.  If it is not, fail.
8314      Otherwise, set inst.cond from the suffix we already looked up,
8315      and return the opcode structure.
8316
8317  CM. Examine the tag field to make sure this is an instruction that
8318      should receive a conditional infix after the third character.
8319      If it is not, fail.  Otherwise, undo the edits to the current
8320      line of input and proceed as for case CE.  */
8321
8322static const struct asm_opcode *
8323opcode_lookup (char **str)
8324{
8325  char *end, *base;
8326  char *affix;
8327  const struct asm_opcode *opcode;
8328  const struct asm_cond *cond;
8329  char save[2];
8330
8331  /* Scan up to the end of the mnemonic, which must end in white space,
8332     '.' (in unified mode only), or end of string.  */
8333  for (base = end = *str; *end != '\0'; end++)
8334    if (*end == ' ' || (unified_syntax && *end == '.'))
8335      break;
8336
8337  if (end == base)
8338    return 0;
8339
8340  /* Handle a possible width suffix.  */
8341  if (end[0] == '.')
8342    {
8343      if (end[1] == 'w' && (end[2] == ' ' || end[2] == '\0'))
8344	inst.size_req = 4;
8345      else if (end[1] == 'n' && (end[2] == ' ' || end[2] == '\0'))
8346	inst.size_req = 2;
8347      else
8348	return 0;
8349
8350      *str = end + 2;
8351    }
8352  else
8353    *str = end;
8354
8355  /* Look for unaffixed or special-case affixed mnemonic.  */
8356  opcode = hash_find_n (arm_ops_hsh, base, end - base);
8357  if (opcode)
8358    {
8359      /* step U */
8360      if (opcode->tag < OT_odd_infix_0)
8361	{
8362	  inst.cond = COND_ALWAYS;
8363	  return opcode;
8364	}
8365
8366      if (unified_syntax)
8367	as_warn (_("conditional infixes are deprecated in unified syntax"));
8368      affix = base + (opcode->tag - OT_odd_infix_0);
8369      cond = hash_find_n (arm_cond_hsh, affix, 2);
8370      assert (cond);
8371
8372      inst.cond = cond->value;
8373      return opcode;
8374    }
8375
8376  /* Cannot have a conditional suffix on a mnemonic of less than two
8377     characters.  */
8378  if (end - base < 3)
8379    return 0;
8380
8381  /* Look for suffixed mnemonic.  */
8382  affix = end - 2;
8383  cond = hash_find_n (arm_cond_hsh, affix, 2);
8384  opcode = hash_find_n (arm_ops_hsh, base, affix - base);
8385  if (opcode && cond)
8386    {
8387      /* step CE */
8388      switch (opcode->tag)
8389	{
8390	case OT_cinfix3_legacy:
8391	  /* Ignore conditional suffixes matched on infix only mnemonics.  */
8392	  break;
8393
8394	case OT_cinfix3:
8395	case OT_odd_infix_unc:
8396	  if (!unified_syntax)
8397	    return 0;
8398	  /* else fall through */
8399
8400	case OT_csuffix:
8401	case OT_csuf_or_in3:
8402	  inst.cond = cond->value;
8403	  return opcode;
8404
8405	case OT_unconditional:
8406	case OT_unconditionalF:
8407	  if (thumb_mode)
8408	    {
8409	      inst.cond = cond->value;
8410	    }
8411	  else
8412	    {
8413	      /* delayed diagnostic */
8414	      inst.error = BAD_COND;
8415	      inst.cond = COND_ALWAYS;
8416	    }
8417	  return opcode;
8418
8419	default:
8420	  return 0;
8421	}
8422    }
8423
8424  /* Cannot have a usual-position infix on a mnemonic of less than
8425     six characters (five would be a suffix).  */
8426  if (end - base < 6)
8427    return 0;
8428
8429  /* Look for infixed mnemonic in the usual position.  */
8430  affix = base + 3;
8431  cond = hash_find_n (arm_cond_hsh, affix, 2);
8432  if (!cond)
8433    return 0;
8434
8435  memcpy (save, affix, 2);
8436  memmove (affix, affix + 2, (end - affix) - 2);
8437  opcode = hash_find_n (arm_ops_hsh, base, (end - base) - 2);
8438  memmove (affix + 2, affix, (end - affix) - 2);
8439  memcpy (affix, save, 2);
8440
8441  if (opcode && (opcode->tag == OT_cinfix3 || opcode->tag == OT_csuf_or_in3
8442		 || opcode->tag == OT_cinfix3_legacy))
8443    {
8444      /* step CM */
8445      if (unified_syntax && opcode->tag == OT_cinfix3)
8446	as_warn (_("conditional infixes are deprecated in unified syntax"));
8447
8448      inst.cond = cond->value;
8449      return opcode;
8450    }
8451
8452  return 0;
8453}
8454
8455void
8456md_assemble (char *str)
8457{
8458  char *p = str;
8459  const struct asm_opcode * opcode;
8460
8461  /* Align the previous label if needed.  */
8462  if (last_label_seen != NULL)
8463    {
8464      symbol_set_frag (last_label_seen, frag_now);
8465      S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
8466      S_SET_SEGMENT (last_label_seen, now_seg);
8467    }
8468
8469  memset (&inst, '\0', sizeof (inst));
8470  inst.reloc.type = BFD_RELOC_UNUSED;
8471
8472  opcode = opcode_lookup (&p);
8473  if (!opcode)
8474    {
8475      /* It wasn't an instruction, but it might be a register alias of
8476	 the form alias .req reg.  */
8477      if (!create_register_alias (str, p))
8478	as_bad (_("bad instruction `%s'"), str);
8479
8480      return;
8481    }
8482
8483  if (thumb_mode)
8484    {
8485      arm_feature_set variant;
8486
8487      variant = cpu_variant;
8488      /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
8489      if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
8490	ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
8491      /* Check that this instruction is supported for this CPU.  */
8492      if (!opcode->tvariant
8493	  || (thumb_mode == 1
8494	      && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
8495	{
8496	  as_bad (_("selected processor does not support `%s'"), str);
8497	  return;
8498	}
8499      if (inst.cond != COND_ALWAYS && !unified_syntax
8500	  && opcode->tencode != do_t_branch)
8501	{
8502	  as_bad (_("Thumb does not support conditional execution"));
8503	  return;
8504	}
8505
8506      /* Check conditional suffixes.  */
8507      if (current_it_mask)
8508	{
8509	  int cond;
8510	  cond = current_cc ^ ((current_it_mask >> 4) & 1) ^ 1;
8511	  current_it_mask <<= 1;
8512	  current_it_mask &= 0x1f;
8513	  /* The BKPT instruction is unconditional even in an IT block.  */
8514	  if (!inst.error
8515	      && cond != inst.cond && opcode->tencode != do_t_bkpt)
8516	    {
8517	      as_bad (_("incorrect condition in IT block"));
8518	      return;
8519	    }
8520	}
8521      else if (inst.cond != COND_ALWAYS && opcode->tencode != do_t_branch)
8522	{
8523	  as_bad (_("thumb conditional instrunction not in IT block"));
8524	  return;
8525	}
8526
8527      mapping_state (MAP_THUMB);
8528      inst.instruction = opcode->tvalue;
8529
8530      if (!parse_operands (p, opcode->operands))
8531	opcode->tencode ();
8532
8533      /* Clear current_it_mask at the end of an IT block.  */
8534      if (current_it_mask == 0x10)
8535	current_it_mask = 0;
8536
8537      if (!(inst.error || inst.relax))
8538	{
8539	  assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
8540	  inst.size = (inst.instruction > 0xffff ? 4 : 2);
8541	  if (inst.size_req && inst.size_req != inst.size)
8542	    {
8543	      as_bad (_("cannot honor width suffix -- `%s'"), str);
8544	      return;
8545	    }
8546	}
8547      ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
8548			      *opcode->tvariant);
8549      /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
8550	 set those bits when Thumb-2 32-bit instuctions are seen.  ie.
8551	 anything other than bl/blx.
8552	 This is overly pessimistic for relaxable instructions.  */
8553      if ((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
8554	  || inst.relax)
8555	ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
8556				arm_ext_v6t2);
8557    }
8558  else
8559    {
8560      /* Check that this instruction is supported for this CPU.  */
8561      if (!opcode->avariant ||
8562	  !ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant))
8563	{
8564	  as_bad (_("selected processor does not support `%s'"), str);
8565	  return;
8566	}
8567      if (inst.size_req)
8568	{
8569	  as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
8570	  return;
8571	}
8572
8573      mapping_state (MAP_ARM);
8574      inst.instruction = opcode->avalue;
8575      if (opcode->tag == OT_unconditionalF)
8576	inst.instruction |= 0xF << 28;
8577      else
8578	inst.instruction |= inst.cond << 28;
8579      inst.size = INSN_SIZE;
8580      if (!parse_operands (p, opcode->operands))
8581	opcode->aencode ();
8582      /* Arm mode bx is marked as both v4T and v5 because it's still required
8583         on a hypothetical non-thumb v5 core.  */
8584      if (ARM_CPU_HAS_FEATURE (*opcode->avariant, arm_ext_v4t)
8585	  || ARM_CPU_HAS_FEATURE (*opcode->avariant, arm_ext_v5))
8586	ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
8587      else
8588	ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
8589				*opcode->avariant);
8590    }
8591  output_inst (str);
8592}
8593
8594/* Various frobbings of labels and their addresses.  */
8595
8596void
8597arm_start_line_hook (void)
8598{
8599  last_label_seen = NULL;
8600}
8601
8602void
8603arm_frob_label (symbolS * sym)
8604{
8605  last_label_seen = sym;
8606
8607  ARM_SET_THUMB (sym, thumb_mode);
8608
8609#if defined OBJ_COFF || defined OBJ_ELF
8610  ARM_SET_INTERWORK (sym, support_interwork);
8611#endif
8612
8613  /* Note - do not allow local symbols (.Lxxx) to be labeled
8614     as Thumb functions.  This is because these labels, whilst
8615     they exist inside Thumb code, are not the entry points for
8616     possible ARM->Thumb calls.	 Also, these labels can be used
8617     as part of a computed goto or switch statement.  eg gcc
8618     can generate code that looks like this:
8619
8620		ldr  r2, [pc, .Laaa]
8621		lsl  r3, r3, #2
8622		ldr  r2, [r3, r2]
8623		mov  pc, r2
8624
8625       .Lbbb:  .word .Lxxx
8626       .Lccc:  .word .Lyyy
8627       ..etc...
8628       .Laaa:	.word Lbbb
8629
8630     The first instruction loads the address of the jump table.
8631     The second instruction converts a table index into a byte offset.
8632     The third instruction gets the jump address out of the table.
8633     The fourth instruction performs the jump.
8634
8635     If the address stored at .Laaa is that of a symbol which has the
8636     Thumb_Func bit set, then the linker will arrange for this address
8637     to have the bottom bit set, which in turn would mean that the
8638     address computation performed by the third instruction would end
8639     up with the bottom bit set.  Since the ARM is capable of unaligned
8640     word loads, the instruction would then load the incorrect address
8641     out of the jump table, and chaos would ensue.  */
8642  if (label_is_thumb_function_name
8643      && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
8644      && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
8645    {
8646      /* When the address of a Thumb function is taken the bottom
8647	 bit of that address should be set.  This will allow
8648	 interworking between Arm and Thumb functions to work
8649	 correctly.  */
8650
8651      THUMB_SET_FUNC (sym, 1);
8652
8653      label_is_thumb_function_name = FALSE;
8654    }
8655
8656#ifdef OBJ_ELF
8657  dwarf2_emit_label (sym);
8658#endif
8659}
8660
8661int
8662arm_data_in_code (void)
8663{
8664  if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
8665    {
8666      *input_line_pointer = '/';
8667      input_line_pointer += 5;
8668      *input_line_pointer = 0;
8669      return 1;
8670    }
8671
8672  return 0;
8673}
8674
8675char *
8676arm_canonicalize_symbol_name (char * name)
8677{
8678  int len;
8679
8680  if (thumb_mode && (len = strlen (name)) > 5
8681      && streq (name + len - 5, "/data"))
8682    *(name + len - 5) = 0;
8683
8684  return name;
8685}
8686
8687/* Table of all register names defined by default.  The user can
8688   define additional names with .req.  Note that all register names
8689   should appear in both upper and lowercase variants.	Some registers
8690   also have mixed-case names.	*/
8691
8692#define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE }
8693#define REGNUM(p,n,t) REGDEF(p##n, n, t)
8694#define REGSET(p,t) \
8695  REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
8696  REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
8697  REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
8698  REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
8699
8700static const struct reg_entry reg_names[] =
8701{
8702  /* ARM integer registers.  */
8703  REGSET(r, RN), REGSET(R, RN),
8704
8705  /* ATPCS synonyms.  */
8706  REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
8707  REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
8708  REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
8709
8710  REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
8711  REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
8712  REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
8713
8714  /* Well-known aliases.  */
8715  REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
8716  REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
8717
8718  REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
8719  REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
8720
8721  /* Coprocessor numbers.  */
8722  REGSET(p, CP), REGSET(P, CP),
8723
8724  /* Coprocessor register numbers.  The "cr" variants are for backward
8725     compatibility.  */
8726  REGSET(c,  CN), REGSET(C, CN),
8727  REGSET(cr, CN), REGSET(CR, CN),
8728
8729  /* FPA registers.  */
8730  REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
8731  REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
8732
8733  REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
8734  REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
8735
8736  /* VFP SP registers.	*/
8737  REGSET(s,VFS),
8738  REGNUM(s,16,VFS), REGNUM(s,17,VFS), REGNUM(s,18,VFS), REGNUM(s,19,VFS),
8739  REGNUM(s,20,VFS), REGNUM(s,21,VFS), REGNUM(s,22,VFS), REGNUM(s,23,VFS),
8740  REGNUM(s,24,VFS), REGNUM(s,25,VFS), REGNUM(s,26,VFS), REGNUM(s,27,VFS),
8741  REGNUM(s,28,VFS), REGNUM(s,29,VFS), REGNUM(s,30,VFS), REGNUM(s,31,VFS),
8742
8743  REGSET(S,VFS),
8744  REGNUM(S,16,VFS), REGNUM(S,17,VFS), REGNUM(S,18,VFS), REGNUM(S,19,VFS),
8745  REGNUM(S,20,VFS), REGNUM(S,21,VFS), REGNUM(S,22,VFS), REGNUM(S,23,VFS),
8746  REGNUM(S,24,VFS), REGNUM(S,25,VFS), REGNUM(S,26,VFS), REGNUM(S,27,VFS),
8747  REGNUM(S,28,VFS), REGNUM(S,29,VFS), REGNUM(S,30,VFS), REGNUM(S,31,VFS),
8748
8749  /* VFP DP Registers.	*/
8750  REGSET(d,VFD), REGSET(D,VFS),
8751
8752  /* VFP control registers.  */
8753  REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
8754  REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
8755
8756  /* Maverick DSP coprocessor registers.  */
8757  REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
8758  REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
8759
8760  REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
8761  REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
8762  REGDEF(dspsc,0,DSPSC),
8763
8764  REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
8765  REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
8766  REGDEF(DSPSC,0,DSPSC),
8767
8768  /* iWMMXt data registers - p0, c0-15.	 */
8769  REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
8770
8771  /* iWMMXt control registers - p1, c0-3.  */
8772  REGDEF(wcid,	0,MMXWC),  REGDEF(wCID,	 0,MMXWC),  REGDEF(WCID,  0,MMXWC),
8773  REGDEF(wcon,	1,MMXWC),  REGDEF(wCon,	 1,MMXWC),  REGDEF(WCON,  1,MMXWC),
8774  REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
8775  REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
8776
8777  /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
8778  REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
8779  REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
8780  REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
8781  REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
8782
8783  /* XScale accumulator registers.  */
8784  REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
8785};
8786#undef REGDEF
8787#undef REGNUM
8788#undef REGSET
8789
8790/* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
8791   within psr_required_here.  */
8792static const struct asm_psr psrs[] =
8793{
8794  /* Backward compatibility notation.  Note that "all" is no longer
8795     truly all possible PSR bits.  */
8796  {"all",  PSR_c | PSR_f},
8797  {"flg",  PSR_f},
8798  {"ctl",  PSR_c},
8799
8800  /* Individual flags.	*/
8801  {"f",	   PSR_f},
8802  {"c",	   PSR_c},
8803  {"x",	   PSR_x},
8804  {"s",	   PSR_s},
8805  /* Combinations of flags.  */
8806  {"fs",   PSR_f | PSR_s},
8807  {"fx",   PSR_f | PSR_x},
8808  {"fc",   PSR_f | PSR_c},
8809  {"sf",   PSR_s | PSR_f},
8810  {"sx",   PSR_s | PSR_x},
8811  {"sc",   PSR_s | PSR_c},
8812  {"xf",   PSR_x | PSR_f},
8813  {"xs",   PSR_x | PSR_s},
8814  {"xc",   PSR_x | PSR_c},
8815  {"cf",   PSR_c | PSR_f},
8816  {"cs",   PSR_c | PSR_s},
8817  {"cx",   PSR_c | PSR_x},
8818  {"fsx",  PSR_f | PSR_s | PSR_x},
8819  {"fsc",  PSR_f | PSR_s | PSR_c},
8820  {"fxs",  PSR_f | PSR_x | PSR_s},
8821  {"fxc",  PSR_f | PSR_x | PSR_c},
8822  {"fcs",  PSR_f | PSR_c | PSR_s},
8823  {"fcx",  PSR_f | PSR_c | PSR_x},
8824  {"sfx",  PSR_s | PSR_f | PSR_x},
8825  {"sfc",  PSR_s | PSR_f | PSR_c},
8826  {"sxf",  PSR_s | PSR_x | PSR_f},
8827  {"sxc",  PSR_s | PSR_x | PSR_c},
8828  {"scf",  PSR_s | PSR_c | PSR_f},
8829  {"scx",  PSR_s | PSR_c | PSR_x},
8830  {"xfs",  PSR_x | PSR_f | PSR_s},
8831  {"xfc",  PSR_x | PSR_f | PSR_c},
8832  {"xsf",  PSR_x | PSR_s | PSR_f},
8833  {"xsc",  PSR_x | PSR_s | PSR_c},
8834  {"xcf",  PSR_x | PSR_c | PSR_f},
8835  {"xcs",  PSR_x | PSR_c | PSR_s},
8836  {"cfs",  PSR_c | PSR_f | PSR_s},
8837  {"cfx",  PSR_c | PSR_f | PSR_x},
8838  {"csf",  PSR_c | PSR_s | PSR_f},
8839  {"csx",  PSR_c | PSR_s | PSR_x},
8840  {"cxf",  PSR_c | PSR_x | PSR_f},
8841  {"cxs",  PSR_c | PSR_x | PSR_s},
8842  {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
8843  {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
8844  {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
8845  {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
8846  {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
8847  {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
8848  {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
8849  {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
8850  {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
8851  {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
8852  {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
8853  {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
8854  {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
8855  {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
8856  {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
8857  {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
8858  {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
8859  {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
8860  {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
8861  {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
8862  {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
8863  {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
8864  {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
8865  {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
8866};
8867
8868/* Table of V7M psr names.  */
8869static const struct asm_psr v7m_psrs[] =
8870{
8871  {"apsr",	0 },
8872  {"iapsr",	1 },
8873  {"eapsr",	2 },
8874  {"psr",	3 },
8875  {"ipsr",	5 },
8876  {"epsr",	6 },
8877  {"iepsr",	7 },
8878  {"msp",	8 },
8879  {"psp",	9 },
8880  {"primask",	16},
8881  {"basepri",	17},
8882  {"basepri_max", 18},
8883  {"faultmask",	19},
8884  {"control",	20}
8885};
8886
8887/* Table of all shift-in-operand names.	 */
8888static const struct asm_shift_name shift_names [] =
8889{
8890  { "asl", SHIFT_LSL },	 { "ASL", SHIFT_LSL },
8891  { "lsl", SHIFT_LSL },	 { "LSL", SHIFT_LSL },
8892  { "lsr", SHIFT_LSR },	 { "LSR", SHIFT_LSR },
8893  { "asr", SHIFT_ASR },	 { "ASR", SHIFT_ASR },
8894  { "ror", SHIFT_ROR },	 { "ROR", SHIFT_ROR },
8895  { "rrx", SHIFT_RRX },	 { "RRX", SHIFT_RRX }
8896};
8897
8898/* Table of all explicit relocation names.  */
8899#ifdef OBJ_ELF
8900static struct reloc_entry reloc_names[] =
8901{
8902  { "got",     BFD_RELOC_ARM_GOT32   },	 { "GOT",     BFD_RELOC_ARM_GOT32   },
8903  { "gotoff",  BFD_RELOC_ARM_GOTOFF  },	 { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
8904  { "plt",     BFD_RELOC_ARM_PLT32   },	 { "PLT",     BFD_RELOC_ARM_PLT32   },
8905  { "target1", BFD_RELOC_ARM_TARGET1 },	 { "TARGET1", BFD_RELOC_ARM_TARGET1 },
8906  { "target2", BFD_RELOC_ARM_TARGET2 },	 { "TARGET2", BFD_RELOC_ARM_TARGET2 },
8907  { "sbrel",   BFD_RELOC_ARM_SBREL32 },	 { "SBREL",   BFD_RELOC_ARM_SBREL32 },
8908  { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
8909  { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
8910  { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
8911  { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
8912  { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32}
8913};
8914#endif
8915
8916/* Table of all conditional affixes.  0xF is not defined as a condition code.  */
8917static const struct asm_cond conds[] =
8918{
8919  {"eq", 0x0},
8920  {"ne", 0x1},
8921  {"cs", 0x2}, {"hs", 0x2},
8922  {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
8923  {"mi", 0x4},
8924  {"pl", 0x5},
8925  {"vs", 0x6},
8926  {"vc", 0x7},
8927  {"hi", 0x8},
8928  {"ls", 0x9},
8929  {"ge", 0xa},
8930  {"lt", 0xb},
8931  {"gt", 0xc},
8932  {"le", 0xd},
8933  {"al", 0xe}
8934};
8935
8936static struct asm_barrier_opt barrier_opt_names[] =
8937{
8938  { "oshst", 0x2 },
8939  { "osh",   0x3 },
8940  { "nshst", 0x6 },
8941  { "unst",  0x6 },
8942  { "nsh",   0x7 },
8943  { "un",    0x7 },
8944  { "ishst", 0xa },
8945  { "ish",   0xb },
8946  { "st",    0xe },
8947  { "sy",    0xf }
8948};
8949
8950/* Table of ARM-format instructions.	*/
8951
8952/* Macros for gluing together operand strings.  N.B. In all cases
8953   other than OPS0, the trailing OP_stop comes from default
8954   zero-initialization of the unspecified elements of the array.  */
8955#define OPS0()		  { OP_stop, }
8956#define OPS1(a)		  { OP_##a, }
8957#define OPS2(a,b)	  { OP_##a,OP_##b, }
8958#define OPS3(a,b,c)	  { OP_##a,OP_##b,OP_##c, }
8959#define OPS4(a,b,c,d)	  { OP_##a,OP_##b,OP_##c,OP_##d, }
8960#define OPS5(a,b,c,d,e)	  { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
8961#define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
8962
8963/* These macros abstract out the exact format of the mnemonic table and
8964   save some repeated characters.  */
8965
8966/* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
8967#define TxCE(mnem, op, top, nops, ops, ae, te) \
8968  { #mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
8969    THUMB_VARIANT, do_##ae, do_##te }
8970
8971/* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
8972   a T_MNEM_xyz enumerator.  */
8973#define TCE(mnem, aop, top, nops, ops, ae, te) \
8974       TxCE(mnem, aop, 0x##top, nops, ops, ae, te)
8975#define tCE(mnem, aop, top, nops, ops, ae, te) \
8976       TxCE(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
8977
8978/* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
8979   infix after the third character.  */
8980#define TxC3(mnem, op, top, nops, ops, ae, te) \
8981  { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
8982    THUMB_VARIANT, do_##ae, do_##te }
8983#define TC3(mnem, aop, top, nops, ops, ae, te) \
8984       TxC3(mnem, aop, 0x##top, nops, ops, ae, te)
8985#define tC3(mnem, aop, top, nops, ops, ae, te) \
8986       TxC3(mnem, aop, T_MNEM_##top, nops, ops, ae, te)
8987
8988/* Mnemonic with a conditional infix in an unusual place.  Each and every variant has to
8989   appear in the condition table.  */
8990#define TxCM_(m1, m2, m3, op, top, nops, ops, ae, te)	\
8991  { #m1 #m2 #m3, OPS##nops ops, sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
8992    0x##op, top, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##te }
8993
8994#define TxCM(m1, m2, op, top, nops, ops, ae, te)	\
8995  TxCM_(m1,   , m2, op, top, nops, ops, ae, te),	\
8996  TxCM_(m1, eq, m2, op, top, nops, ops, ae, te),	\
8997  TxCM_(m1, ne, m2, op, top, nops, ops, ae, te),	\
8998  TxCM_(m1, cs, m2, op, top, nops, ops, ae, te),	\
8999  TxCM_(m1, hs, m2, op, top, nops, ops, ae, te),	\
9000  TxCM_(m1, cc, m2, op, top, nops, ops, ae, te),	\
9001  TxCM_(m1, ul, m2, op, top, nops, ops, ae, te),	\
9002  TxCM_(m1, lo, m2, op, top, nops, ops, ae, te),	\
9003  TxCM_(m1, mi, m2, op, top, nops, ops, ae, te),	\
9004  TxCM_(m1, pl, m2, op, top, nops, ops, ae, te),	\
9005  TxCM_(m1, vs, m2, op, top, nops, ops, ae, te),	\
9006  TxCM_(m1, vc, m2, op, top, nops, ops, ae, te),	\
9007  TxCM_(m1, hi, m2, op, top, nops, ops, ae, te),	\
9008  TxCM_(m1, ls, m2, op, top, nops, ops, ae, te),	\
9009  TxCM_(m1, ge, m2, op, top, nops, ops, ae, te),	\
9010  TxCM_(m1, lt, m2, op, top, nops, ops, ae, te),	\
9011  TxCM_(m1, gt, m2, op, top, nops, ops, ae, te),	\
9012  TxCM_(m1, le, m2, op, top, nops, ops, ae, te),	\
9013  TxCM_(m1, al, m2, op, top, nops, ops, ae, te)
9014
9015#define TCM(m1,m2, aop, top, nops, ops, ae, te)		\
9016       TxCM(m1,m2, aop, 0x##top, nops, ops, ae, te)
9017#define tCM(m1,m2, aop, top, nops, ops, ae, te)			\
9018       TxCM(m1,m2, aop, T_MNEM_##top, nops, ops, ae, te)
9019
9020/* Mnemonic that cannot be conditionalized.  The ARM condition-code
9021   field is still 0xE.  Many of the Thumb variants can be executed
9022   conditionally, so this is checked separately.  */
9023#define TUE(mnem, op, top, nops, ops, ae, te)				\
9024  { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
9025    THUMB_VARIANT, do_##ae, do_##te }
9026
9027/* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
9028   condition code field.  */
9029#define TUF(mnem, op, top, nops, ops, ae, te)				\
9030  { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
9031    THUMB_VARIANT, do_##ae, do_##te }
9032
9033/* ARM-only variants of all the above.  */
9034#define CE(mnem,  op, nops, ops, ae)	\
9035  { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
9036
9037#define C3(mnem, op, nops, ops, ae)	\
9038  { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
9039
9040/* Legacy mnemonics that always have conditional infix after the third
9041   character.  */
9042#define CL(mnem, op, nops, ops, ae)	\
9043  { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
9044    0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
9045
9046/* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
9047#define cCE(mnem,  op, nops, ops, ae)	\
9048  { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
9049
9050/* Legacy coprocessor instructions where conditional infix and conditional
9051   suffix are ambiguous.  For consistency this includes all FPA instructions,
9052   not just the potentially ambiguous ones.  */
9053#define cCL(mnem, op, nops, ops, ae)	\
9054  { #mnem, OPS##nops ops, OT_cinfix3_legacy, \
9055    0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
9056
9057/* Coprocessor, takes either a suffix or a position-3 infix
9058   (for an FPA corner case). */
9059#define C3E(mnem, op, nops, ops, ae) \
9060  { #mnem, OPS##nops ops, OT_csuf_or_in3, \
9061    0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
9062
9063#define xCM_(m1, m2, m3, op, nops, ops, ae)	\
9064  { #m1 #m2 #m3, OPS##nops ops, \
9065    sizeof(#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof(#m1) - 1, \
9066    0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
9067
9068#define CM(m1, m2, op, nops, ops, ae)	\
9069  xCM_(m1,   , m2, op, nops, ops, ae),	\
9070  xCM_(m1, eq, m2, op, nops, ops, ae),	\
9071  xCM_(m1, ne, m2, op, nops, ops, ae),	\
9072  xCM_(m1, cs, m2, op, nops, ops, ae),	\
9073  xCM_(m1, hs, m2, op, nops, ops, ae),	\
9074  xCM_(m1, cc, m2, op, nops, ops, ae),	\
9075  xCM_(m1, ul, m2, op, nops, ops, ae),	\
9076  xCM_(m1, lo, m2, op, nops, ops, ae),	\
9077  xCM_(m1, mi, m2, op, nops, ops, ae),	\
9078  xCM_(m1, pl, m2, op, nops, ops, ae),	\
9079  xCM_(m1, vs, m2, op, nops, ops, ae),	\
9080  xCM_(m1, vc, m2, op, nops, ops, ae),	\
9081  xCM_(m1, hi, m2, op, nops, ops, ae),	\
9082  xCM_(m1, ls, m2, op, nops, ops, ae),	\
9083  xCM_(m1, ge, m2, op, nops, ops, ae),	\
9084  xCM_(m1, lt, m2, op, nops, ops, ae),	\
9085  xCM_(m1, gt, m2, op, nops, ops, ae),	\
9086  xCM_(m1, le, m2, op, nops, ops, ae),	\
9087  xCM_(m1, al, m2, op, nops, ops, ae)
9088
9089#define UE(mnem, op, nops, ops, ae)	\
9090  { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
9091
9092#define UF(mnem, op, nops, ops, ae)	\
9093  { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
9094
9095#define do_0 0
9096
9097/* Thumb-only, unconditional.  */
9098#define UT(mnem,  op, nops, ops, te) TUE(mnem,  0, op, nops, ops, 0, te)
9099
9100static const struct asm_opcode insns[] =
9101{
9102#define ARM_VARIANT &arm_ext_v1 /* Core ARM Instructions.  */
9103#define THUMB_VARIANT &arm_ext_v4t
9104 tCE(and,	0000000, and,      3, (RR, oRR, SH), arit, t_arit3c),
9105 tC3(ands,	0100000, ands,	   3, (RR, oRR, SH), arit, t_arit3c),
9106 tCE(eor,	0200000, eor,	   3, (RR, oRR, SH), arit, t_arit3c),
9107 tC3(eors,	0300000, eors,	   3, (RR, oRR, SH), arit, t_arit3c),
9108 tCE(sub,	0400000, sub,	   3, (RR, oRR, SH), arit, t_add_sub),
9109 tC3(subs,	0500000, subs,	   3, (RR, oRR, SH), arit, t_add_sub),
9110 tCE(add,	0800000, add,	   3, (RR, oRR, SH), arit, t_add_sub),
9111 tC3(adds,	0900000, adds,	   3, (RR, oRR, SH), arit, t_add_sub),
9112 tCE(adc,	0a00000, adc,	   3, (RR, oRR, SH), arit, t_arit3c),
9113 tC3(adcs,	0b00000, adcs,	   3, (RR, oRR, SH), arit, t_arit3c),
9114 tCE(sbc,	0c00000, sbc,	   3, (RR, oRR, SH), arit, t_arit3),
9115 tC3(sbcs,	0d00000, sbcs,	   3, (RR, oRR, SH), arit, t_arit3),
9116 tCE(orr,	1800000, orr,	   3, (RR, oRR, SH), arit, t_arit3c),
9117 tC3(orrs,	1900000, orrs,	   3, (RR, oRR, SH), arit, t_arit3c),
9118 tCE(bic,	1c00000, bic,	   3, (RR, oRR, SH), arit, t_arit3),
9119 tC3(bics,	1d00000, bics,	   3, (RR, oRR, SH), arit, t_arit3),
9120
9121 /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
9122    for setting PSR flag bits.  They are obsolete in V6 and do not
9123    have Thumb equivalents. */
9124 tCE(tst,	1100000, tst,	   2, (RR, SH),      cmp,  t_mvn_tst),
9125 tC3(tsts,	1100000, tst,	   2, (RR, SH),      cmp,  t_mvn_tst),
9126  CL(tstp,	110f000,     	   2, (RR, SH),      cmp),
9127 tCE(cmp,	1500000, cmp,	   2, (RR, SH),      cmp,  t_mov_cmp),
9128 tC3(cmps,	1500000, cmp,	   2, (RR, SH),      cmp,  t_mov_cmp),
9129  CL(cmpp,	150f000,     	   2, (RR, SH),      cmp),
9130 tCE(cmn,	1700000, cmn,	   2, (RR, SH),      cmp,  t_mvn_tst),
9131 tC3(cmns,	1700000, cmn,	   2, (RR, SH),      cmp,  t_mvn_tst),
9132  CL(cmnp,	170f000,     	   2, (RR, SH),      cmp),
9133
9134 tCE(mov,	1a00000, mov,	   2, (RR, SH),      mov,  t_mov_cmp),
9135 tC3(movs,	1b00000, movs,	   2, (RR, SH),      mov,  t_mov_cmp),
9136 tCE(mvn,	1e00000, mvn,	   2, (RR, SH),      mov,  t_mvn_tst),
9137 tC3(mvns,	1f00000, mvns,	   2, (RR, SH),      mov,  t_mvn_tst),
9138
9139 tCE(ldr,	4100000, ldr,	   2, (RR, ADDR),    ldst, t_ldst),
9140 tC3(ldrb,	4500000, ldrb,	   2, (RR, ADDR),    ldst, t_ldst),
9141 tCE(str,	4000000, str,	   2, (RR, ADDR),    ldst, t_ldst),
9142 tC3(strb,	4400000, strb,	   2, (RR, ADDR),    ldst, t_ldst),
9143
9144 tCE(stm,	8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
9145 tC3(stmia,	8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
9146 tC3(stmea,	8800000, stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
9147 tCE(ldm,	8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
9148 tC3(ldmia,	8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
9149 tC3(ldmfd,	8900000, ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
9150
9151 TCE(swi,	f000000, df00,     1, (EXPi),        swi, t_swi),
9152 TCE(svc,	f000000, df00,     1, (EXPi),        swi, t_swi),
9153 tCE(b,		a000000, b,	   1, (EXPr),	     branch, t_branch),
9154 TCE(bl,	b000000, f000f800, 1, (EXPr),	     bl, t_branch23),
9155
9156  /* Pseudo ops.  */
9157 tCE(adr,	28f0000, adr,	   2, (RR, EXP),     adr,  t_adr),
9158  C3(adrl,	28f0000,           2, (RR, EXP),     adrl),
9159 tCE(nop,	1a00000, nop,	   1, (oI255c),	     nop,  t_nop),
9160
9161  /* Thumb-compatibility pseudo ops.  */
9162 tCE(lsl,	1a00000, lsl,	   3, (RR, oRR, SH), shift, t_shift),
9163 tC3(lsls,	1b00000, lsls,	   3, (RR, oRR, SH), shift, t_shift),
9164 tCE(lsr,	1a00020, lsr,	   3, (RR, oRR, SH), shift, t_shift),
9165 tC3(lsrs,	1b00020, lsrs,	   3, (RR, oRR, SH), shift, t_shift),
9166 tCE(asr,	1a00040, asr,	   3, (RR, oRR, SH), shift, t_shift),
9167 tC3(asrs,      1b00040, asrs,     3, (RR, oRR, SH), shift, t_shift),
9168 tCE(ror,	1a00060, ror,	   3, (RR, oRR, SH), shift, t_shift),
9169 tC3(rors,	1b00060, rors,	   3, (RR, oRR, SH), shift, t_shift),
9170 tCE(neg,	2600000, neg,	   2, (RR, RR),      rd_rn, t_neg),
9171 tC3(negs,	2700000, negs,	   2, (RR, RR),      rd_rn, t_neg),
9172 tCE(push,	92d0000, push,     1, (REGLST),	     push_pop, t_push_pop),
9173 tCE(pop,	8bd0000, pop,	   1, (REGLST),	     push_pop, t_push_pop),
9174
9175#undef THUMB_VARIANT
9176#define THUMB_VARIANT &arm_ext_v6
9177 TCE(cpy,       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
9178
9179 /* V1 instructions with no Thumb analogue prior to V6T2.  */
9180#undef THUMB_VARIANT
9181#define THUMB_VARIANT &arm_ext_v6t2
9182 TCE(rsb,	0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
9183 TC3(rsbs,	0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
9184 TCE(teq,	1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
9185 TC3(teqs,	1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
9186  CL(teqp,	130f000,           2, (RR, SH),      cmp),
9187
9188 TC3(ldrt,	4300000, f8500e00, 2, (RR, ADDR),    ldstt, t_ldstt),
9189 TC3(ldrbt,	4700000, f8100e00, 2, (RR, ADDR),    ldstt, t_ldstt),
9190 TC3(strt,	4200000, f8400e00, 2, (RR, ADDR),    ldstt, t_ldstt),
9191 TC3(strbt,	4600000, f8000e00, 2, (RR, ADDR),    ldstt, t_ldstt),
9192
9193 TC3(stmdb,	9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
9194 TC3(stmfd,     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
9195
9196 TC3(ldmdb,	9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
9197 TC3(ldmea,	9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
9198
9199 /* V1 instructions with no Thumb analogue at all.  */
9200  CE(rsc,	0e00000,	   3, (RR, oRR, SH), arit),
9201  C3(rscs,	0f00000,	   3, (RR, oRR, SH), arit),
9202
9203  C3(stmib,	9800000,	   2, (RRw, REGLST), ldmstm),
9204  C3(stmfa,	9800000,	   2, (RRw, REGLST), ldmstm),
9205  C3(stmda,	8000000,	   2, (RRw, REGLST), ldmstm),
9206  C3(stmed,	8000000,	   2, (RRw, REGLST), ldmstm),
9207  C3(ldmib,	9900000,	   2, (RRw, REGLST), ldmstm),
9208  C3(ldmed,	9900000,	   2, (RRw, REGLST), ldmstm),
9209  C3(ldmda,	8100000,	   2, (RRw, REGLST), ldmstm),
9210  C3(ldmfa,	8100000,	   2, (RRw, REGLST), ldmstm),
9211
9212#undef ARM_VARIANT
9213#define ARM_VARIANT &arm_ext_v2	/* ARM 2 - multiplies.	*/
9214#undef THUMB_VARIANT
9215#define THUMB_VARIANT &arm_ext_v4t
9216 tCE(mul,	0000090, mul,	   3, (RRnpc, RRnpc, oRR), mul, t_mul),
9217 tC3(muls,	0100090, muls,	   3, (RRnpc, RRnpc, oRR), mul, t_mul),
9218
9219#undef THUMB_VARIANT
9220#define THUMB_VARIANT &arm_ext_v6t2
9221 TCE(mla,	0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
9222  C3(mlas,	0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
9223
9224  /* Generic coprocessor instructions.	*/
9225 TCE(cdp,	e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
9226 TCE(ldc,	c100000, ec100000, 3, (RCP, RCN, ADDR),		        lstc,   lstc),
9227 TC3(ldcl,	c500000, ec500000, 3, (RCP, RCN, ADDR),		        lstc,   lstc),
9228 TCE(stc,	c000000, ec000000, 3, (RCP, RCN, ADDR),		        lstc,   lstc),
9229 TC3(stcl,	c400000, ec400000, 3, (RCP, RCN, ADDR),		        lstc,   lstc),
9230 TCE(mcr,	e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
9231 TCE(mrc,	e100010, ee100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
9232
9233#undef ARM_VARIANT
9234#define ARM_VARIANT &arm_ext_v2s /* ARM 3 - swp instructions.  */
9235  CE(swp,	1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
9236  C3(swpb,	1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
9237
9238#undef ARM_VARIANT
9239#define ARM_VARIANT &arm_ext_v3	/* ARM 6 Status register instructions.	*/
9240 TCE(mrs,	10f0000, f3ef8000, 2, (RR, PSR),     mrs, t_mrs),
9241 TCE(msr,	120f000, f3808000, 2, (PSR, RR_EXi), msr, t_msr),
9242
9243#undef ARM_VARIANT
9244#define ARM_VARIANT &arm_ext_v3m	 /* ARM 7M long multiplies.  */
9245 TCE(smull,	0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
9246  CM(smull,s,	0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
9247 TCE(umull,	0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
9248  CM(umull,s,	0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
9249 TCE(smlal,	0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
9250  CM(smlal,s,	0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
9251 TCE(umlal,	0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
9252  CM(umlal,s,	0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
9253
9254#undef ARM_VARIANT
9255#define ARM_VARIANT &arm_ext_v4	/* ARM Architecture 4.	*/
9256#undef THUMB_VARIANT
9257#define THUMB_VARIANT &arm_ext_v4t
9258 tC3(ldrh,	01000b0, ldrh,     2, (RR, ADDR), ldstv4, t_ldst),
9259 tC3(strh,	00000b0, strh,     2, (RR, ADDR), ldstv4, t_ldst),
9260 tC3(ldrsh,	01000f0, ldrsh,    2, (RR, ADDR), ldstv4, t_ldst),
9261 tC3(ldrsb,	01000d0, ldrsb,    2, (RR, ADDR), ldstv4, t_ldst),
9262 tCM(ld,sh,	01000f0, ldrsh,    2, (RR, ADDR), ldstv4, t_ldst),
9263 tCM(ld,sb,	01000d0, ldrsb,    2, (RR, ADDR), ldstv4, t_ldst),
9264
9265#undef ARM_VARIANT
9266#define ARM_VARIANT &arm_ext_v4t_5
9267  /* ARM Architecture 4T.  */
9268  /* Note: bx (and blx) are required on V5, even if the processor does
9269     not support Thumb.	 */
9270 TCE(bx,	12fff10, 4700, 1, (RR),	bx, t_bx),
9271
9272#undef ARM_VARIANT
9273#define ARM_VARIANT &arm_ext_v5 /*  ARM Architecture 5T.	 */
9274#undef THUMB_VARIANT
9275#define THUMB_VARIANT &arm_ext_v5t
9276  /* Note: blx has 2 variants; the .value coded here is for
9277     BLX(2).  Only this variant has conditional execution.  */
9278 TCE(blx,	12fff30, 4780, 1, (RR_EXr),			    blx,  t_blx),
9279 TUE(bkpt,	1200070, be00, 1, (oIffffb),			    bkpt, t_bkpt),
9280
9281#undef THUMB_VARIANT
9282#define THUMB_VARIANT &arm_ext_v6t2
9283 TCE(clz,	16f0f10, fab0f080, 2, (RRnpc, RRnpc),		        rd_rm,  t_clz),
9284 TUF(ldc2,	c100000, fc100000, 3, (RCP, RCN, ADDR),		        lstc,	lstc),
9285 TUF(ldc2l,	c500000, fc500000, 3, (RCP, RCN, ADDR),		        lstc,	lstc),
9286 TUF(stc2,	c000000, fc000000, 3, (RCP, RCN, ADDR),		        lstc,	lstc),
9287 TUF(stc2l,	c400000, fc400000, 3, (RCP, RCN, ADDR),		        lstc,	lstc),
9288 TUF(cdp2,	e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
9289 TUF(mcr2,	e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
9290 TUF(mrc2,	e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
9291
9292#undef ARM_VARIANT
9293#define ARM_VARIANT &arm_ext_v5exp /*  ARM Architecture 5TExP.  */
9294 TCE(smlabb,	1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
9295 TCE(smlatb,	10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
9296 TCE(smlabt,	10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
9297 TCE(smlatt,	10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
9298
9299 TCE(smlawb,	1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
9300 TCE(smlawt,	12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
9301
9302 TCE(smlalbb,	1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
9303 TCE(smlaltb,	14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
9304 TCE(smlalbt,	14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
9305 TCE(smlaltt,	14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
9306
9307 TCE(smulbb,	1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),	    smul, t_simd),
9308 TCE(smultb,	16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),	    smul, t_simd),
9309 TCE(smulbt,	16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),	    smul, t_simd),
9310 TCE(smultt,	16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),	    smul, t_simd),
9311
9312 TCE(smulwb,	12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),	    smul, t_simd),
9313 TCE(smulwt,	12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),	    smul, t_simd),
9314
9315 TCE(qadd,	1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),	    rd_rm_rn, rd_rm_rn),
9316 TCE(qdadd,	1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),	    rd_rm_rn, rd_rm_rn),
9317 TCE(qsub,	1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),	    rd_rm_rn, rd_rm_rn),
9318 TCE(qdsub,	1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),	    rd_rm_rn, rd_rm_rn),
9319
9320#undef ARM_VARIANT
9321#define ARM_VARIANT &arm_ext_v5e /*  ARM Architecture 5TE.  */
9322 TUF(pld,	450f000, f810f000, 1, (ADDR),		     pld,  t_pld),
9323 TC3(ldrd,	00000d0, e9500000, 3, (RRnpc, oRRnpc, ADDR), ldrd, t_ldstd),
9324 TC3(strd,	00000f0, e9400000, 3, (RRnpc, oRRnpc, ADDR), ldrd, t_ldstd),
9325
9326 TCE(mcrr,	c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
9327 TCE(mrrc,	c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
9328
9329#undef ARM_VARIANT
9330#define ARM_VARIANT &arm_ext_v5j /*  ARM Architecture 5TEJ.  */
9331 TCE(bxj,	12fff20, f3c08f00, 1, (RR),			  bxj, t_bxj),
9332
9333#undef ARM_VARIANT
9334#define ARM_VARIANT &arm_ext_v6 /*  ARM V6.  */
9335#undef THUMB_VARIANT
9336#define THUMB_VARIANT &arm_ext_v6
9337 TUF(cpsie,     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
9338 TUF(cpsid,     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
9339 tCE(rev,       6bf0f30, rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
9340 tCE(rev16,     6bf0fb0, rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
9341 tCE(revsh,     6ff0fb0, revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
9342 tCE(sxth,      6bf0070, sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
9343 tCE(uxth,      6ff0070, uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
9344 tCE(sxtb,      6af0070, sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
9345 tCE(uxtb,      6ef0070, uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
9346 TUF(setend,    1010000, b650,     1, (ENDI),                     setend, t_setend),
9347
9348#undef THUMB_VARIANT
9349#define THUMB_VARIANT &arm_ext_v6t2
9350 TCE(ldrex,	1900f9f, e8500f00, 2, (RRnpc, ADDR),		  ldrex, t_ldrex),
9351 TUF(mcrr2,	c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
9352 TUF(mrrc2,	c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
9353
9354 TCE(ssat,	6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
9355 TCE(usat,	6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
9356
9357/*  ARM V6 not included in V7M (eg. integer SIMD).  */
9358#undef THUMB_VARIANT
9359#define THUMB_VARIANT &arm_ext_v6_notm
9360 TUF(cps,	1020000, f3af8100, 1, (I31b),			  imm0, t_cps),
9361 TCE(pkhbt,	6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
9362 TCE(pkhtb,	6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
9363 TCE(qadd16,	6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9364 TCE(qadd8,	6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9365 TCE(qaddsubx,	6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9366 TCE(qsub16,	6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9367 TCE(qsub8,	6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9368 TCE(qsubaddx,	6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9369 TCE(sadd16,	6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9370 TCE(sadd8,	6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9371 TCE(saddsubx,	6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9372 TCE(shadd16,	6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9373 TCE(shadd8,	6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9374 TCE(shaddsubx, 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9375 TCE(shsub16,	6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9376 TCE(shsub8,	6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9377 TCE(shsubaddx, 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9378 TCE(ssub16,	6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9379 TCE(ssub8,	6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9380 TCE(ssubaddx,	6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9381 TCE(uadd16,	6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9382 TCE(uadd8,	6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9383 TCE(uaddsubx,	6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9384 TCE(uhadd16,	6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9385 TCE(uhadd8,	6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9386 TCE(uhaddsubx, 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9387 TCE(uhsub16,	6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9388 TCE(uhsub8,	6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9389 TCE(uhsubaddx, 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9390 TCE(uqadd16,	6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9391 TCE(uqadd8,	6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9392 TCE(uqaddsubx, 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9393 TCE(uqsub16,	6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9394 TCE(uqsub8,	6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9395 TCE(uqsubaddx, 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9396 TCE(usub16,	6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9397 TCE(usub8,	6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9398 TCE(usubaddx,	6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9399 TUF(rfeia,	8900a00, e990c000, 1, (RRw),			   rfe, rfe),
9400  UF(rfeib,	9900a00,           1, (RRw),			   rfe),
9401  UF(rfeda,	8100a00,           1, (RRw),			   rfe),
9402 TUF(rfedb,	9100a00, e810c000, 1, (RRw),			   rfe, rfe),
9403 TUF(rfefd,	8900a00, e990c000, 1, (RRw),			   rfe, rfe),
9404  UF(rfefa,	9900a00,           1, (RRw),			   rfe),
9405  UF(rfeea,	8100a00,           1, (RRw),			   rfe),
9406 TUF(rfeed,	9100a00, e810c000, 1, (RRw),			   rfe, rfe),
9407 TCE(sxtah,	6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9408 TCE(sxtab16,	6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9409 TCE(sxtab,	6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9410 TCE(sxtb16,	68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),	   sxth,  t_sxth),
9411 TCE(uxtah,	6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9412 TCE(uxtab16,	6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9413 TCE(uxtab,	6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
9414 TCE(uxtb16,	6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),	   sxth,  t_sxth),
9415 TCE(sel,	6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),	   rd_rn_rm, t_simd),
9416 TCE(smlad,	7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9417 TCE(smladx,	7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9418 TCE(smlald,	7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
9419 TCE(smlaldx,	7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
9420 TCE(smlsd,	7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9421 TCE(smlsdx,	7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9422 TCE(smlsld,	7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
9423 TCE(smlsldx,	7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
9424 TCE(smmla,	7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9425 TCE(smmlar,	7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9426 TCE(smmls,	75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9427 TCE(smmlsr,	75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
9428 TCE(smmul,	750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),	   smul, t_simd),
9429 TCE(smmulr,	750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),	   smul, t_simd),
9430 TCE(smuad,	700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),	   smul, t_simd),
9431 TCE(smuadx,	700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),	   smul, t_simd),
9432 TCE(smusd,	700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),	   smul, t_simd),
9433 TCE(smusdx,	700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),	   smul, t_simd),
9434 TUF(srsia,	8cd0500, e980c000, 1, (I31w),			   srs,  srs),
9435  UF(srsib,	9cd0500,           1, (I31w),			   srs),
9436  UF(srsda,	84d0500,	   1, (I31w),			   srs),
9437 TUF(srsdb,	94d0500, e800c000, 1, (I31w),			   srs,  srs),
9438 TCE(ssat16,	6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),	   ssat16, t_ssat16),
9439 TCE(strex,	1800f90, e8400000, 3, (RRnpc, RRnpc, ADDR),	   strex,  t_strex),
9440 TCE(umaal,	0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
9441 TCE(usad8,	780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),	   smul,   t_simd),
9442 TCE(usada8,	7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
9443 TCE(usat16,	6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),	   usat16, t_usat16),
9444
9445#undef ARM_VARIANT
9446#define ARM_VARIANT &arm_ext_v6k
9447#undef THUMB_VARIANT
9448#define THUMB_VARIANT &arm_ext_v6k
9449 tCE(yield,	320f001, yield,    0, (), noargs, t_hint),
9450 tCE(wfe,	320f002, wfe,      0, (), noargs, t_hint),
9451 tCE(wfi,	320f003, wfi,      0, (), noargs, t_hint),
9452 tCE(sev,	320f004, sev,      0, (), noargs, t_hint),
9453
9454#undef THUMB_VARIANT
9455#define THUMB_VARIANT &arm_ext_v6_notm
9456 TCE(ldrexd,	1b00f9f, e8d0007f, 3, (RRnpc, oRRnpc, RRnpcb),        ldrexd, t_ldrexd),
9457 TCE(strexd,	1a00f90, e8c00070, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb), strexd, t_strexd),
9458
9459 TCE(rrx,      1a00060, ea4f0030, 2, (RR, RR),      rd_rm, t_rd_rm),
9460 TCE(rrxs,     1b00060, ea5f0030, 2, (RR, RR),      rd_rm, t_rd_rm),
9461
9462#undef THUMB_VARIANT
9463#define THUMB_VARIANT &arm_ext_v6t2
9464 TCE(ldrexb,	1d00f9f, e8d00f4f, 2, (RRnpc, RRnpcb),	              rd_rn,  rd_rn),
9465 TCE(ldrexh,	1f00f9f, e8d00f5f, 2, (RRnpc, RRnpcb),	              rd_rn,  rd_rn),
9466 TCE(strexb,	1c00f90, e8c00f40, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
9467 TCE(strexh,	1e00f90, e8c00f50, 3, (RRnpc, RRnpc, ADDR),           strex,  rm_rd_rn),
9468 TUF(clrex,	57ff01f, f3bf8f2f, 0, (),			      noargs, noargs),
9469
9470#undef ARM_VARIANT
9471#define ARM_VARIANT &arm_ext_v6z
9472 TCE(smc,	1600070, f7f08000, 1, (EXPi), smc, t_smc),
9473
9474#undef ARM_VARIANT
9475#define ARM_VARIANT &arm_ext_v6t2
9476 TCE(bfc,	7c0001f, f36f0000, 3, (RRnpc, I31, I32),	   bfc, t_bfc),
9477 TCE(bfi,	7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
9478 TCE(sbfx,	7a00050, f3400000, 4, (RR, RR, I31, I32),	   bfx, t_bfx),
9479 TCE(ubfx,	7e00050, f3c00000, 4, (RR, RR, I31, I32),	   bfx, t_bfx),
9480
9481 TCE(mls,	0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
9482 TCE(movw,	3000000, f2400000, 2, (RRnpc, HALF),		    mov16, t_mov16),
9483 TCE(movt,	3400000, f2c00000, 2, (RRnpc, HALF),		    mov16, t_mov16),
9484 TCE(rbit,	3ff0f30, fa90f0a0, 2, (RR, RR),			    rd_rm, t_rbit),
9485
9486 TC3(ldrht,	03000b0, f8300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9487 TC3(ldrsht,	03000f0, f9300e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9488 TC3(ldrsbt,	03000d0, f9100e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9489 TC3(strht,	02000b0, f8200e00, 2, (RR, ADDR), ldsttv4, t_ldstt),
9490
9491  UT(cbnz,      b900,    2, (RR, EXP), t_czb),
9492  UT(cbz,       b100,    2, (RR, EXP), t_czb),
9493 /* ARM does not really have an IT instruction.  */
9494 TUE(it,        0, bf08, 1, (COND),    it, t_it),
9495 TUE(itt,       0, bf0c, 1, (COND),    it, t_it),
9496 TUE(ite,       0, bf04, 1, (COND),    it, t_it),
9497 TUE(ittt,      0, bf0e, 1, (COND),    it, t_it),
9498 TUE(itet,      0, bf06, 1, (COND),    it, t_it),
9499 TUE(itte,      0, bf0a, 1, (COND),    it, t_it),
9500 TUE(itee,      0, bf02, 1, (COND),    it, t_it),
9501 TUE(itttt,     0, bf0f, 1, (COND),    it, t_it),
9502 TUE(itett,     0, bf07, 1, (COND),    it, t_it),
9503 TUE(ittet,     0, bf0b, 1, (COND),    it, t_it),
9504 TUE(iteet,     0, bf03, 1, (COND),    it, t_it),
9505 TUE(ittte,     0, bf0d, 1, (COND),    it, t_it),
9506 TUE(itete,     0, bf05, 1, (COND),    it, t_it),
9507 TUE(ittee,     0, bf09, 1, (COND),    it, t_it),
9508 TUE(iteee,     0, bf01, 1, (COND),    it, t_it),
9509
9510 /* Thumb2 only instructions.  */
9511#undef ARM_VARIANT
9512#define ARM_VARIANT NULL
9513
9514 TCE(addw,	0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
9515 TCE(subw,	0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
9516 TCE(tbb,       0, e8d0f000, 1, (TB), 0, t_tb),
9517 TCE(tbh,       0, e8d0f010, 1, (TB), 0, t_tb),
9518
9519 /* Thumb-2 hardware division instructions (R and M profiles only).  */
9520#undef THUMB_VARIANT
9521#define THUMB_VARIANT &arm_ext_div
9522 TCE(sdiv,	0, fb90f0f0, 3, (RR, oRR, RR), 0, t_div),
9523 TCE(udiv,	0, fbb0f0f0, 3, (RR, oRR, RR), 0, t_div),
9524
9525 /* ARM V7 instructions.  */
9526#undef ARM_VARIANT
9527#define ARM_VARIANT &arm_ext_v7
9528#undef THUMB_VARIANT
9529#define THUMB_VARIANT &arm_ext_v7
9530 TUF(pli,	450f000, f910f000, 1, (ADDR),	  pli,	    t_pld),
9531 TCE(dbg,	320f0f0, f3af80f0, 1, (I15),	  dbg,	    t_dbg),
9532 TUF(dmb,	57ff050, f3bf8f50, 1, (oBARRIER), barrier,  t_barrier),
9533 TUF(dsb,	57ff040, f3bf8f40, 1, (oBARRIER), barrier,  t_barrier),
9534 TUF(isb,	57ff060, f3bf8f60, 1, (oBARRIER), barrier,  t_barrier),
9535
9536#undef ARM_VARIANT
9537#define ARM_VARIANT &fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
9538 cCE(wfs,	e200110, 1, (RR),	     rd),
9539 cCE(rfs,	e300110, 1, (RR),	     rd),
9540 cCE(wfc,	e400110, 1, (RR),	     rd),
9541 cCE(rfc,	e500110, 1, (RR),	     rd),
9542
9543 cCL(ldfs,	c100100, 2, (RF, ADDR),	     rd_cpaddr),
9544 cCL(ldfd,	c108100, 2, (RF, ADDR),	     rd_cpaddr),
9545 cCL(ldfe,	c500100, 2, (RF, ADDR),	     rd_cpaddr),
9546 cCL(ldfp,	c508100, 2, (RF, ADDR),	     rd_cpaddr),
9547
9548 cCL(stfs,	c000100, 2, (RF, ADDR),	     rd_cpaddr),
9549 cCL(stfd,	c008100, 2, (RF, ADDR),	     rd_cpaddr),
9550 cCL(stfe,	c400100, 2, (RF, ADDR),	     rd_cpaddr),
9551 cCL(stfp,	c408100, 2, (RF, ADDR),	     rd_cpaddr),
9552
9553 cCL(mvfs,	e008100, 2, (RF, RF_IF),     rd_rm),
9554 cCL(mvfsp,	e008120, 2, (RF, RF_IF),     rd_rm),
9555 cCL(mvfsm,	e008140, 2, (RF, RF_IF),     rd_rm),
9556 cCL(mvfsz,	e008160, 2, (RF, RF_IF),     rd_rm),
9557 cCL(mvfd,	e008180, 2, (RF, RF_IF),     rd_rm),
9558 cCL(mvfdp,	e0081a0, 2, (RF, RF_IF),     rd_rm),
9559 cCL(mvfdm,	e0081c0, 2, (RF, RF_IF),     rd_rm),
9560 cCL(mvfdz,	e0081e0, 2, (RF, RF_IF),     rd_rm),
9561 cCL(mvfe,	e088100, 2, (RF, RF_IF),     rd_rm),
9562 cCL(mvfep,	e088120, 2, (RF, RF_IF),     rd_rm),
9563 cCL(mvfem,	e088140, 2, (RF, RF_IF),     rd_rm),
9564 cCL(mvfez,	e088160, 2, (RF, RF_IF),     rd_rm),
9565
9566 cCL(mnfs,	e108100, 2, (RF, RF_IF),     rd_rm),
9567 cCL(mnfsp,	e108120, 2, (RF, RF_IF),     rd_rm),
9568 cCL(mnfsm,	e108140, 2, (RF, RF_IF),     rd_rm),
9569 cCL(mnfsz,	e108160, 2, (RF, RF_IF),     rd_rm),
9570 cCL(mnfd,	e108180, 2, (RF, RF_IF),     rd_rm),
9571 cCL(mnfdp,	e1081a0, 2, (RF, RF_IF),     rd_rm),
9572 cCL(mnfdm,	e1081c0, 2, (RF, RF_IF),     rd_rm),
9573 cCL(mnfdz,	e1081e0, 2, (RF, RF_IF),     rd_rm),
9574 cCL(mnfe,	e188100, 2, (RF, RF_IF),     rd_rm),
9575 cCL(mnfep,	e188120, 2, (RF, RF_IF),     rd_rm),
9576 cCL(mnfem,	e188140, 2, (RF, RF_IF),     rd_rm),
9577 cCL(mnfez,	e188160, 2, (RF, RF_IF),     rd_rm),
9578
9579 cCL(abss,	e208100, 2, (RF, RF_IF),     rd_rm),
9580 cCL(abssp,	e208120, 2, (RF, RF_IF),     rd_rm),
9581 cCL(abssm,	e208140, 2, (RF, RF_IF),     rd_rm),
9582 cCL(abssz,	e208160, 2, (RF, RF_IF),     rd_rm),
9583 cCL(absd,	e208180, 2, (RF, RF_IF),     rd_rm),
9584 cCL(absdp,	e2081a0, 2, (RF, RF_IF),     rd_rm),
9585 cCL(absdm,	e2081c0, 2, (RF, RF_IF),     rd_rm),
9586 cCL(absdz,	e2081e0, 2, (RF, RF_IF),     rd_rm),
9587 cCL(abse,	e288100, 2, (RF, RF_IF),     rd_rm),
9588 cCL(absep,	e288120, 2, (RF, RF_IF),     rd_rm),
9589 cCL(absem,	e288140, 2, (RF, RF_IF),     rd_rm),
9590 cCL(absez,	e288160, 2, (RF, RF_IF),     rd_rm),
9591
9592 cCL(rnds,	e308100, 2, (RF, RF_IF),     rd_rm),
9593 cCL(rndsp,	e308120, 2, (RF, RF_IF),     rd_rm),
9594 cCL(rndsm,	e308140, 2, (RF, RF_IF),     rd_rm),
9595 cCL(rndsz,	e308160, 2, (RF, RF_IF),     rd_rm),
9596 cCL(rndd,	e308180, 2, (RF, RF_IF),     rd_rm),
9597 cCL(rnddp,	e3081a0, 2, (RF, RF_IF),     rd_rm),
9598 cCL(rnddm,	e3081c0, 2, (RF, RF_IF),     rd_rm),
9599 cCL(rnddz,	e3081e0, 2, (RF, RF_IF),     rd_rm),
9600 cCL(rnde,	e388100, 2, (RF, RF_IF),     rd_rm),
9601 cCL(rndep,	e388120, 2, (RF, RF_IF),     rd_rm),
9602 cCL(rndem,	e388140, 2, (RF, RF_IF),     rd_rm),
9603 cCL(rndez,	e388160, 2, (RF, RF_IF),     rd_rm),
9604
9605 cCL(sqts,	e408100, 2, (RF, RF_IF),     rd_rm),
9606 cCL(sqtsp,	e408120, 2, (RF, RF_IF),     rd_rm),
9607 cCL(sqtsm,	e408140, 2, (RF, RF_IF),     rd_rm),
9608 cCL(sqtsz,	e408160, 2, (RF, RF_IF),     rd_rm),
9609 cCL(sqtd,	e408180, 2, (RF, RF_IF),     rd_rm),
9610 cCL(sqtdp,	e4081a0, 2, (RF, RF_IF),     rd_rm),
9611 cCL(sqtdm,	e4081c0, 2, (RF, RF_IF),     rd_rm),
9612 cCL(sqtdz,	e4081e0, 2, (RF, RF_IF),     rd_rm),
9613 cCL(sqte,	e488100, 2, (RF, RF_IF),     rd_rm),
9614 cCL(sqtep,	e488120, 2, (RF, RF_IF),     rd_rm),
9615 cCL(sqtem,	e488140, 2, (RF, RF_IF),     rd_rm),
9616 cCL(sqtez,	e488160, 2, (RF, RF_IF),     rd_rm),
9617
9618 cCL(logs,	e508100, 2, (RF, RF_IF),     rd_rm),
9619 cCL(logsp,	e508120, 2, (RF, RF_IF),     rd_rm),
9620 cCL(logsm,	e508140, 2, (RF, RF_IF),     rd_rm),
9621 cCL(logsz,	e508160, 2, (RF, RF_IF),     rd_rm),
9622 cCL(logd,	e508180, 2, (RF, RF_IF),     rd_rm),
9623 cCL(logdp,	e5081a0, 2, (RF, RF_IF),     rd_rm),
9624 cCL(logdm,	e5081c0, 2, (RF, RF_IF),     rd_rm),
9625 cCL(logdz,	e5081e0, 2, (RF, RF_IF),     rd_rm),
9626 cCL(loge,	e588100, 2, (RF, RF_IF),     rd_rm),
9627 cCL(logep,	e588120, 2, (RF, RF_IF),     rd_rm),
9628 cCL(logem,	e588140, 2, (RF, RF_IF),     rd_rm),
9629 cCL(logez,	e588160, 2, (RF, RF_IF),     rd_rm),
9630
9631 cCL(lgns,	e608100, 2, (RF, RF_IF),     rd_rm),
9632 cCL(lgnsp,	e608120, 2, (RF, RF_IF),     rd_rm),
9633 cCL(lgnsm,	e608140, 2, (RF, RF_IF),     rd_rm),
9634 cCL(lgnsz,	e608160, 2, (RF, RF_IF),     rd_rm),
9635 cCL(lgnd,	e608180, 2, (RF, RF_IF),     rd_rm),
9636 cCL(lgndp,	e6081a0, 2, (RF, RF_IF),     rd_rm),
9637 cCL(lgndm,	e6081c0, 2, (RF, RF_IF),     rd_rm),
9638 cCL(lgndz,	e6081e0, 2, (RF, RF_IF),     rd_rm),
9639 cCL(lgne,	e688100, 2, (RF, RF_IF),     rd_rm),
9640 cCL(lgnep,	e688120, 2, (RF, RF_IF),     rd_rm),
9641 cCL(lgnem,	e688140, 2, (RF, RF_IF),     rd_rm),
9642 cCL(lgnez,	e688160, 2, (RF, RF_IF),     rd_rm),
9643
9644 cCL(exps,	e708100, 2, (RF, RF_IF),     rd_rm),
9645 cCL(expsp,	e708120, 2, (RF, RF_IF),     rd_rm),
9646 cCL(expsm,	e708140, 2, (RF, RF_IF),     rd_rm),
9647 cCL(expsz,	e708160, 2, (RF, RF_IF),     rd_rm),
9648 cCL(expd,	e708180, 2, (RF, RF_IF),     rd_rm),
9649 cCL(expdp,	e7081a0, 2, (RF, RF_IF),     rd_rm),
9650 cCL(expdm,	e7081c0, 2, (RF, RF_IF),     rd_rm),
9651 cCL(expdz,	e7081e0, 2, (RF, RF_IF),     rd_rm),
9652 cCL(expe,	e788100, 2, (RF, RF_IF),     rd_rm),
9653 cCL(expep,	e788120, 2, (RF, RF_IF),     rd_rm),
9654 cCL(expem,	e788140, 2, (RF, RF_IF),     rd_rm),
9655 cCL(expdz,	e788160, 2, (RF, RF_IF),     rd_rm),
9656
9657 cCL(sins,	e808100, 2, (RF, RF_IF),     rd_rm),
9658 cCL(sinsp,	e808120, 2, (RF, RF_IF),     rd_rm),
9659 cCL(sinsm,	e808140, 2, (RF, RF_IF),     rd_rm),
9660 cCL(sinsz,	e808160, 2, (RF, RF_IF),     rd_rm),
9661 cCL(sind,	e808180, 2, (RF, RF_IF),     rd_rm),
9662 cCL(sindp,	e8081a0, 2, (RF, RF_IF),     rd_rm),
9663 cCL(sindm,	e8081c0, 2, (RF, RF_IF),     rd_rm),
9664 cCL(sindz,	e8081e0, 2, (RF, RF_IF),     rd_rm),
9665 cCL(sine,	e888100, 2, (RF, RF_IF),     rd_rm),
9666 cCL(sinep,	e888120, 2, (RF, RF_IF),     rd_rm),
9667 cCL(sinem,	e888140, 2, (RF, RF_IF),     rd_rm),
9668 cCL(sinez,	e888160, 2, (RF, RF_IF),     rd_rm),
9669
9670 cCL(coss,	e908100, 2, (RF, RF_IF),     rd_rm),
9671 cCL(cossp,	e908120, 2, (RF, RF_IF),     rd_rm),
9672 cCL(cossm,	e908140, 2, (RF, RF_IF),     rd_rm),
9673 cCL(cossz,	e908160, 2, (RF, RF_IF),     rd_rm),
9674 cCL(cosd,	e908180, 2, (RF, RF_IF),     rd_rm),
9675 cCL(cosdp,	e9081a0, 2, (RF, RF_IF),     rd_rm),
9676 cCL(cosdm,	e9081c0, 2, (RF, RF_IF),     rd_rm),
9677 cCL(cosdz,	e9081e0, 2, (RF, RF_IF),     rd_rm),
9678 cCL(cose,	e988100, 2, (RF, RF_IF),     rd_rm),
9679 cCL(cosep,	e988120, 2, (RF, RF_IF),     rd_rm),
9680 cCL(cosem,	e988140, 2, (RF, RF_IF),     rd_rm),
9681 cCL(cosez,	e988160, 2, (RF, RF_IF),     rd_rm),
9682
9683 cCL(tans,	ea08100, 2, (RF, RF_IF),     rd_rm),
9684 cCL(tansp,	ea08120, 2, (RF, RF_IF),     rd_rm),
9685 cCL(tansm,	ea08140, 2, (RF, RF_IF),     rd_rm),
9686 cCL(tansz,	ea08160, 2, (RF, RF_IF),     rd_rm),
9687 cCL(tand,	ea08180, 2, (RF, RF_IF),     rd_rm),
9688 cCL(tandp,	ea081a0, 2, (RF, RF_IF),     rd_rm),
9689 cCL(tandm,	ea081c0, 2, (RF, RF_IF),     rd_rm),
9690 cCL(tandz,	ea081e0, 2, (RF, RF_IF),     rd_rm),
9691 cCL(tane,	ea88100, 2, (RF, RF_IF),     rd_rm),
9692 cCL(tanep,	ea88120, 2, (RF, RF_IF),     rd_rm),
9693 cCL(tanem,	ea88140, 2, (RF, RF_IF),     rd_rm),
9694 cCL(tanez,	ea88160, 2, (RF, RF_IF),     rd_rm),
9695
9696 cCL(asns,	eb08100, 2, (RF, RF_IF),     rd_rm),
9697 cCL(asnsp,	eb08120, 2, (RF, RF_IF),     rd_rm),
9698 cCL(asnsm,	eb08140, 2, (RF, RF_IF),     rd_rm),
9699 cCL(asnsz,	eb08160, 2, (RF, RF_IF),     rd_rm),
9700 cCL(asnd,	eb08180, 2, (RF, RF_IF),     rd_rm),
9701 cCL(asndp,	eb081a0, 2, (RF, RF_IF),     rd_rm),
9702 cCL(asndm,	eb081c0, 2, (RF, RF_IF),     rd_rm),
9703 cCL(asndz,	eb081e0, 2, (RF, RF_IF),     rd_rm),
9704 cCL(asne,	eb88100, 2, (RF, RF_IF),     rd_rm),
9705 cCL(asnep,	eb88120, 2, (RF, RF_IF),     rd_rm),
9706 cCL(asnem,	eb88140, 2, (RF, RF_IF),     rd_rm),
9707 cCL(asnez,	eb88160, 2, (RF, RF_IF),     rd_rm),
9708
9709 cCL(acss,	ec08100, 2, (RF, RF_IF),     rd_rm),
9710 cCL(acssp,	ec08120, 2, (RF, RF_IF),     rd_rm),
9711 cCL(acssm,	ec08140, 2, (RF, RF_IF),     rd_rm),
9712 cCL(acssz,	ec08160, 2, (RF, RF_IF),     rd_rm),
9713 cCL(acsd,	ec08180, 2, (RF, RF_IF),     rd_rm),
9714 cCL(acsdp,	ec081a0, 2, (RF, RF_IF),     rd_rm),
9715 cCL(acsdm,	ec081c0, 2, (RF, RF_IF),     rd_rm),
9716 cCL(acsdz,	ec081e0, 2, (RF, RF_IF),     rd_rm),
9717 cCL(acse,	ec88100, 2, (RF, RF_IF),     rd_rm),
9718 cCL(acsep,	ec88120, 2, (RF, RF_IF),     rd_rm),
9719 cCL(acsem,	ec88140, 2, (RF, RF_IF),     rd_rm),
9720 cCL(acsez,	ec88160, 2, (RF, RF_IF),     rd_rm),
9721
9722 cCL(atns,	ed08100, 2, (RF, RF_IF),     rd_rm),
9723 cCL(atnsp,	ed08120, 2, (RF, RF_IF),     rd_rm),
9724 cCL(atnsm,	ed08140, 2, (RF, RF_IF),     rd_rm),
9725 cCL(atnsz,	ed08160, 2, (RF, RF_IF),     rd_rm),
9726 cCL(atnd,	ed08180, 2, (RF, RF_IF),     rd_rm),
9727 cCL(atndp,	ed081a0, 2, (RF, RF_IF),     rd_rm),
9728 cCL(atndm,	ed081c0, 2, (RF, RF_IF),     rd_rm),
9729 cCL(atndz,	ed081e0, 2, (RF, RF_IF),     rd_rm),
9730 cCL(atne,	ed88100, 2, (RF, RF_IF),     rd_rm),
9731 cCL(atnep,	ed88120, 2, (RF, RF_IF),     rd_rm),
9732 cCL(atnem,	ed88140, 2, (RF, RF_IF),     rd_rm),
9733 cCL(atnez,	ed88160, 2, (RF, RF_IF),     rd_rm),
9734
9735 cCL(urds,	ee08100, 2, (RF, RF_IF),     rd_rm),
9736 cCL(urdsp,	ee08120, 2, (RF, RF_IF),     rd_rm),
9737 cCL(urdsm,	ee08140, 2, (RF, RF_IF),     rd_rm),
9738 cCL(urdsz,	ee08160, 2, (RF, RF_IF),     rd_rm),
9739 cCL(urdd,	ee08180, 2, (RF, RF_IF),     rd_rm),
9740 cCL(urddp,	ee081a0, 2, (RF, RF_IF),     rd_rm),
9741 cCL(urddm,	ee081c0, 2, (RF, RF_IF),     rd_rm),
9742 cCL(urddz,	ee081e0, 2, (RF, RF_IF),     rd_rm),
9743 cCL(urde,	ee88100, 2, (RF, RF_IF),     rd_rm),
9744 cCL(urdep,	ee88120, 2, (RF, RF_IF),     rd_rm),
9745 cCL(urdem,	ee88140, 2, (RF, RF_IF),     rd_rm),
9746 cCL(urdez,	ee88160, 2, (RF, RF_IF),     rd_rm),
9747
9748 cCL(nrms,	ef08100, 2, (RF, RF_IF),     rd_rm),
9749 cCL(nrmsp,	ef08120, 2, (RF, RF_IF),     rd_rm),
9750 cCL(nrmsm,	ef08140, 2, (RF, RF_IF),     rd_rm),
9751 cCL(nrmsz,	ef08160, 2, (RF, RF_IF),     rd_rm),
9752 cCL(nrmd,	ef08180, 2, (RF, RF_IF),     rd_rm),
9753 cCL(nrmdp,	ef081a0, 2, (RF, RF_IF),     rd_rm),
9754 cCL(nrmdm,	ef081c0, 2, (RF, RF_IF),     rd_rm),
9755 cCL(nrmdz,	ef081e0, 2, (RF, RF_IF),     rd_rm),
9756 cCL(nrme,	ef88100, 2, (RF, RF_IF),     rd_rm),
9757 cCL(nrmep,	ef88120, 2, (RF, RF_IF),     rd_rm),
9758 cCL(nrmem,	ef88140, 2, (RF, RF_IF),     rd_rm),
9759 cCL(nrmez,	ef88160, 2, (RF, RF_IF),     rd_rm),
9760
9761 cCL(adfs,	e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
9762 cCL(adfsp,	e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
9763 cCL(adfsm,	e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
9764 cCL(adfsz,	e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
9765 cCL(adfd,	e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
9766 cCL(adfdp,	e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9767 cCL(adfdm,	e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9768 cCL(adfdz,	e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9769 cCL(adfe,	e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
9770 cCL(adfep,	e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
9771 cCL(adfem,	e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
9772 cCL(adfez,	e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
9773
9774 cCL(sufs,	e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
9775 cCL(sufsp,	e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
9776 cCL(sufsm,	e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
9777 cCL(sufsz,	e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
9778 cCL(sufd,	e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
9779 cCL(sufdp,	e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9780 cCL(sufdm,	e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9781 cCL(sufdz,	e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9782 cCL(sufe,	e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
9783 cCL(sufep,	e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
9784 cCL(sufem,	e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
9785 cCL(sufez,	e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
9786
9787 cCL(rsfs,	e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
9788 cCL(rsfsp,	e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
9789 cCL(rsfsm,	e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
9790 cCL(rsfsz,	e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
9791 cCL(rsfd,	e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
9792 cCL(rsfdp,	e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9793 cCL(rsfdm,	e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9794 cCL(rsfdz,	e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9795 cCL(rsfe,	e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
9796 cCL(rsfep,	e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
9797 cCL(rsfem,	e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
9798 cCL(rsfez,	e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
9799
9800 cCL(mufs,	e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
9801 cCL(mufsp,	e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
9802 cCL(mufsm,	e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
9803 cCL(mufsz,	e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
9804 cCL(mufd,	e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
9805 cCL(mufdp,	e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9806 cCL(mufdm,	e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9807 cCL(mufdz,	e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9808 cCL(mufe,	e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
9809 cCL(mufep,	e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
9810 cCL(mufem,	e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
9811 cCL(mufez,	e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
9812
9813 cCL(dvfs,	e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
9814 cCL(dvfsp,	e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
9815 cCL(dvfsm,	e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
9816 cCL(dvfsz,	e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
9817 cCL(dvfd,	e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
9818 cCL(dvfdp,	e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9819 cCL(dvfdm,	e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9820 cCL(dvfdz,	e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9821 cCL(dvfe,	e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
9822 cCL(dvfep,	e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
9823 cCL(dvfem,	e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
9824 cCL(dvfez,	e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
9825
9826 cCL(rdfs,	e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
9827 cCL(rdfsp,	e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
9828 cCL(rdfsm,	e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
9829 cCL(rdfsz,	e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
9830 cCL(rdfd,	e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
9831 cCL(rdfdp,	e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9832 cCL(rdfdm,	e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9833 cCL(rdfdz,	e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9834 cCL(rdfe,	e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
9835 cCL(rdfep,	e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
9836 cCL(rdfem,	e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
9837 cCL(rdfez,	e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
9838
9839 cCL(pows,	e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
9840 cCL(powsp,	e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
9841 cCL(powsm,	e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
9842 cCL(powsz,	e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
9843 cCL(powd,	e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
9844 cCL(powdp,	e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9845 cCL(powdm,	e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9846 cCL(powdz,	e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9847 cCL(powe,	e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
9848 cCL(powep,	e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
9849 cCL(powem,	e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
9850 cCL(powez,	e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
9851
9852 cCL(rpws,	e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
9853 cCL(rpwsp,	e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
9854 cCL(rpwsm,	e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
9855 cCL(rpwsz,	e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
9856 cCL(rpwd,	e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
9857 cCL(rpwdp,	e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9858 cCL(rpwdm,	e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9859 cCL(rpwdz,	e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9860 cCL(rpwe,	e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
9861 cCL(rpwep,	e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
9862 cCL(rpwem,	e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
9863 cCL(rpwez,	e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
9864
9865 cCL(rmfs,	e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
9866 cCL(rmfsp,	e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
9867 cCL(rmfsm,	e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
9868 cCL(rmfsz,	e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
9869 cCL(rmfd,	e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
9870 cCL(rmfdp,	e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9871 cCL(rmfdm,	e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9872 cCL(rmfdz,	e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9873 cCL(rmfe,	e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
9874 cCL(rmfep,	e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
9875 cCL(rmfem,	e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
9876 cCL(rmfez,	e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
9877
9878 cCL(fmls,	e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
9879 cCL(fmlsp,	e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
9880 cCL(fmlsm,	e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
9881 cCL(fmlsz,	e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
9882 cCL(fmld,	e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
9883 cCL(fmldp,	e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9884 cCL(fmldm,	e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9885 cCL(fmldz,	e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9886 cCL(fmle,	e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
9887 cCL(fmlep,	e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
9888 cCL(fmlem,	e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
9889 cCL(fmlez,	e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
9890
9891 cCL(fdvs,	ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
9892 cCL(fdvsp,	ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
9893 cCL(fdvsm,	ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
9894 cCL(fdvsz,	ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
9895 cCL(fdvd,	ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
9896 cCL(fdvdp,	ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9897 cCL(fdvdm,	ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9898 cCL(fdvdz,	ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9899 cCL(fdve,	ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
9900 cCL(fdvep,	ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
9901 cCL(fdvem,	ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
9902 cCL(fdvez,	ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
9903
9904 cCL(frds,	eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
9905 cCL(frdsp,	eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
9906 cCL(frdsm,	eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
9907 cCL(frdsz,	eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
9908 cCL(frdd,	eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
9909 cCL(frddp,	eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9910 cCL(frddm,	eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9911 cCL(frddz,	eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9912 cCL(frde,	eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
9913 cCL(frdep,	eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
9914 cCL(frdem,	eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
9915 cCL(frdez,	eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
9916
9917 cCL(pols,	ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
9918 cCL(polsp,	ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
9919 cCL(polsm,	ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
9920 cCL(polsz,	ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
9921 cCL(pold,	ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
9922 cCL(poldp,	ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
9923 cCL(poldm,	ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
9924 cCL(poldz,	ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
9925 cCL(pole,	ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
9926 cCL(polep,	ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
9927 cCL(polem,	ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
9928 cCL(polez,	ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
9929
9930 cCE(cmf,	e90f110, 2, (RF, RF_IF),     fpa_cmp),
9931 C3E(cmfe,	ed0f110, 2, (RF, RF_IF),     fpa_cmp),
9932 cCE(cnf,	eb0f110, 2, (RF, RF_IF),     fpa_cmp),
9933 C3E(cnfe,	ef0f110, 2, (RF, RF_IF),     fpa_cmp),
9934
9935 cCL(flts,	e000110, 2, (RF, RR),	     rn_rd),
9936 cCL(fltsp,	e000130, 2, (RF, RR),	     rn_rd),
9937 cCL(fltsm,	e000150, 2, (RF, RR),	     rn_rd),
9938 cCL(fltsz,	e000170, 2, (RF, RR),	     rn_rd),
9939 cCL(fltd,	e000190, 2, (RF, RR),	     rn_rd),
9940 cCL(fltdp,	e0001b0, 2, (RF, RR),	     rn_rd),
9941 cCL(fltdm,	e0001d0, 2, (RF, RR),	     rn_rd),
9942 cCL(fltdz,	e0001f0, 2, (RF, RR),	     rn_rd),
9943 cCL(flte,	e080110, 2, (RF, RR),	     rn_rd),
9944 cCL(fltep,	e080130, 2, (RF, RR),	     rn_rd),
9945 cCL(fltem,	e080150, 2, (RF, RR),	     rn_rd),
9946 cCL(fltez,	e080170, 2, (RF, RR),	     rn_rd),
9947
9948  /* The implementation of the FIX instruction is broken on some
9949     assemblers, in that it accepts a precision specifier as well as a
9950     rounding specifier, despite the fact that this is meaningless.
9951     To be more compatible, we accept it as well, though of course it
9952     does not set any bits.  */
9953 cCE(fix,	e100110, 2, (RR, RF),	     rd_rm),
9954 cCL(fixp,	e100130, 2, (RR, RF),	     rd_rm),
9955 cCL(fixm,	e100150, 2, (RR, RF),	     rd_rm),
9956 cCL(fixz,	e100170, 2, (RR, RF),	     rd_rm),
9957 cCL(fixsp,	e100130, 2, (RR, RF),	     rd_rm),
9958 cCL(fixsm,	e100150, 2, (RR, RF),	     rd_rm),
9959 cCL(fixsz,	e100170, 2, (RR, RF),	     rd_rm),
9960 cCL(fixdp,	e100130, 2, (RR, RF),	     rd_rm),
9961 cCL(fixdm,	e100150, 2, (RR, RF),	     rd_rm),
9962 cCL(fixdz,	e100170, 2, (RR, RF),	     rd_rm),
9963 cCL(fixep,	e100130, 2, (RR, RF),	     rd_rm),
9964 cCL(fixem,	e100150, 2, (RR, RF),	     rd_rm),
9965 cCL(fixez,	e100170, 2, (RR, RF),	     rd_rm),
9966
9967  /* Instructions that were new with the real FPA, call them V2.  */
9968#undef ARM_VARIANT
9969#define ARM_VARIANT &fpu_fpa_ext_v2
9970 cCE(lfm,	c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9971 cCL(lfmfd,	c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9972 cCL(lfmea,	d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9973 cCE(sfm,	c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9974 cCL(sfmfd,	d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9975 cCL(sfmea,	c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
9976
9977#undef ARM_VARIANT
9978#define ARM_VARIANT &fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
9979  /* Moves and type conversions.  */
9980 cCE(fcpys,	eb00a40, 2, (RVS, RVS),	      vfp_sp_monadic),
9981 cCE(fmrs,	e100a10, 2, (RR, RVS),	      vfp_reg_from_sp),
9982 cCE(fmsr,	e000a10, 2, (RVS, RR),	      vfp_sp_from_reg),
9983 cCE(fmstat,	ef1fa10, 0, (),		      noargs),
9984 cCE(fsitos,	eb80ac0, 2, (RVS, RVS),	      vfp_sp_monadic),
9985 cCE(fuitos,	eb80a40, 2, (RVS, RVS),	      vfp_sp_monadic),
9986 cCE(ftosis,	ebd0a40, 2, (RVS, RVS),	      vfp_sp_monadic),
9987 cCE(ftosizs,	ebd0ac0, 2, (RVS, RVS),	      vfp_sp_monadic),
9988 cCE(ftouis,	ebc0a40, 2, (RVS, RVS),	      vfp_sp_monadic),
9989 cCE(ftouizs,	ebc0ac0, 2, (RVS, RVS),	      vfp_sp_monadic),
9990 cCE(fmrx,	ef00a10, 2, (RR, RVC),	      rd_rn),
9991 cCE(fmxr,	ee00a10, 2, (RVC, RR),	      rn_rd),
9992
9993  /* Memory operations.	 */
9994 cCE(flds,	d100a00, 2, (RVS, ADDR),      vfp_sp_ldst),
9995 cCE(fsts,	d000a00, 2, (RVS, ADDR),      vfp_sp_ldst),
9996 cCE(fldmias,	c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9997 cCE(fldmfds,	c900a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
9998 cCE(fldmdbs,	d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
9999 cCE(fldmeas,	d300a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
10000 cCE(fldmiax,	c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
10001 cCE(fldmfdx,	c900b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
10002 cCE(fldmdbx,	d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
10003 cCE(fldmeax,	d300b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
10004 cCE(fstmias,	c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
10005 cCE(fstmeas,	c800a00, 2, (RRw, VRSLST),    vfp_sp_ldstmia),
10006 cCE(fstmdbs,	d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
10007 cCE(fstmfds,	d200a00, 2, (RRw, VRSLST),    vfp_sp_ldstmdb),
10008 cCE(fstmiax,	c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
10009 cCE(fstmeax,	c800b00, 2, (RRw, VRDLST),    vfp_xp_ldstmia),
10010 cCE(fstmdbx,	d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
10011 cCE(fstmfdx,	d200b00, 2, (RRw, VRDLST),    vfp_xp_ldstmdb),
10012
10013  /* Monadic operations.  */
10014 cCE(fabss,	eb00ac0, 2, (RVS, RVS),	      vfp_sp_monadic),
10015 cCE(fnegs,	eb10a40, 2, (RVS, RVS),	      vfp_sp_monadic),
10016 cCE(fsqrts,	eb10ac0, 2, (RVS, RVS),	      vfp_sp_monadic),
10017
10018  /* Dyadic operations.	 */
10019 cCE(fadds,	e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
10020 cCE(fsubs,	e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
10021 cCE(fmuls,	e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
10022 cCE(fdivs,	e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
10023 cCE(fmacs,	e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
10024 cCE(fmscs,	e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
10025 cCE(fnmuls,	e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
10026 cCE(fnmacs,	e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
10027 cCE(fnmscs,	e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
10028
10029  /* Comparisons.  */
10030 cCE(fcmps,	eb40a40, 2, (RVS, RVS),	      vfp_sp_monadic),
10031 cCE(fcmpzs,	eb50a40, 1, (RVS),	      vfp_sp_compare_z),
10032 cCE(fcmpes,	eb40ac0, 2, (RVS, RVS),	      vfp_sp_monadic),
10033 cCE(fcmpezs,	eb50ac0, 1, (RVS),	      vfp_sp_compare_z),
10034
10035#undef ARM_VARIANT
10036#define ARM_VARIANT &fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
10037  /* Moves and type conversions.  */
10038 cCE(fcpyd,	eb00b40, 2, (RVD, RVD),	      rd_rm),
10039 cCE(fcvtds,	eb70ac0, 2, (RVD, RVS),	      vfp_dp_sp_cvt),
10040 cCE(fcvtsd,	eb70bc0, 2, (RVS, RVD),	      vfp_sp_dp_cvt),
10041 cCE(fmdhr,	e200b10, 2, (RVD, RR),	      rn_rd),
10042 cCE(fmdlr,	e000b10, 2, (RVD, RR),	      rn_rd),
10043 cCE(fmrdh,	e300b10, 2, (RR, RVD),	      rd_rn),
10044 cCE(fmrdl,	e100b10, 2, (RR, RVD),	      rd_rn),
10045 cCE(fsitod,	eb80bc0, 2, (RVD, RVS),	      vfp_dp_sp_cvt),
10046 cCE(fuitod,	eb80b40, 2, (RVD, RVS),	      vfp_dp_sp_cvt),
10047 cCE(ftosid,	ebd0b40, 2, (RVS, RVD),	      vfp_sp_dp_cvt),
10048 cCE(ftosizd,	ebd0bc0, 2, (RVS, RVD),	      vfp_sp_dp_cvt),
10049 cCE(ftouid,	ebc0b40, 2, (RVS, RVD),	      vfp_sp_dp_cvt),
10050 cCE(ftouizd,	ebc0bc0, 2, (RVS, RVD),	      vfp_sp_dp_cvt),
10051
10052  /* Memory operations.	 */
10053 cCE(fldd,	d100b00, 2, (RVD, ADDR),      vfp_dp_ldst),
10054 cCE(fstd,	d000b00, 2, (RVD, ADDR),      vfp_dp_ldst),
10055 cCE(fldmiad,	c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
10056 cCE(fldmfdd,	c900b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
10057 cCE(fldmdbd,	d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
10058 cCE(fldmead,	d300b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
10059 cCE(fstmiad,	c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
10060 cCE(fstmead,	c800b00, 2, (RRw, VRDLST),    vfp_dp_ldstmia),
10061 cCE(fstmdbd,	d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
10062 cCE(fstmfdd,	d200b00, 2, (RRw, VRDLST),    vfp_dp_ldstmdb),
10063
10064  /* Monadic operations.  */
10065 cCE(fabsd,	eb00bc0, 2, (RVD, RVD),	      rd_rm),
10066 cCE(fnegd,	eb10b40, 2, (RVD, RVD),	      rd_rm),
10067 cCE(fsqrtd,	eb10bc0, 2, (RVD, RVD),	      rd_rm),
10068
10069  /* Dyadic operations.	 */
10070 cCE(faddd,	e300b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
10071 cCE(fsubd,	e300b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
10072 cCE(fmuld,	e200b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
10073 cCE(fdivd,	e800b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
10074 cCE(fmacd,	e000b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
10075 cCE(fmscd,	e100b00, 3, (RVD, RVD, RVD),  rd_rn_rm),
10076 cCE(fnmuld,	e200b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
10077 cCE(fnmacd,	e000b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
10078 cCE(fnmscd,	e100b40, 3, (RVD, RVD, RVD),  rd_rn_rm),
10079
10080  /* Comparisons.  */
10081 cCE(fcmpd,	eb40b40, 2, (RVD, RVD),	      rd_rm),
10082 cCE(fcmpzd,	eb50b40, 1, (RVD),	      rd),
10083 cCE(fcmped,	eb40bc0, 2, (RVD, RVD),	      rd_rm),
10084 cCE(fcmpezd,	eb50bc0, 1, (RVD),	      rd),
10085
10086#undef ARM_VARIANT
10087#define ARM_VARIANT &fpu_vfp_ext_v2
10088 cCE(fmsrr,	c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
10089 cCE(fmrrs,	c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
10090 cCE(fmdrr,	c400b10, 3, (RVD, RR, RR),    rm_rd_rn),
10091 cCE(fmrrd,	c500b10, 3, (RR, RR, RVD),    rd_rn_rm),
10092
10093#undef ARM_VARIANT
10094#define ARM_VARIANT &arm_cext_xscale /* Intel XScale extensions.	 */
10095 cCE(mia,	e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
10096 cCE(miaph,	e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
10097 cCE(miabb,	e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
10098 cCE(miabt,	e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
10099 cCE(miatb,	e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
10100 cCE(miatt,	e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
10101 cCE(mar,	c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
10102 cCE(mra,	c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
10103
10104#undef ARM_VARIANT
10105#define ARM_VARIANT &arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
10106 cCE(tandcb,	e13f130, 1, (RR),		    iwmmxt_tandorc),
10107 cCE(tandch,	e53f130, 1, (RR),		    iwmmxt_tandorc),
10108 cCE(tandcw,	e93f130, 1, (RR),		    iwmmxt_tandorc),
10109 cCE(tbcstb,	e400010, 2, (RIWR, RR),		    rn_rd),
10110 cCE(tbcsth,	e400050, 2, (RIWR, RR),		    rn_rd),
10111 cCE(tbcstw,	e400090, 2, (RIWR, RR),		    rn_rd),
10112 cCE(textrcb,	e130170, 2, (RR, I7),		    iwmmxt_textrc),
10113 cCE(textrch,	e530170, 2, (RR, I7),		    iwmmxt_textrc),
10114 cCE(textrcw,	e930170, 2, (RR, I7),		    iwmmxt_textrc),
10115 cCE(textrmub,	e100070, 3, (RR, RIWR, I7),	    iwmmxt_textrm),
10116 cCE(textrmuh,	e500070, 3, (RR, RIWR, I7),	    iwmmxt_textrm),
10117 cCE(textrmuw,	e900070, 3, (RR, RIWR, I7),	    iwmmxt_textrm),
10118 cCE(textrmsb,	e100078, 3, (RR, RIWR, I7),	    iwmmxt_textrm),
10119 cCE(textrmsh,	e500078, 3, (RR, RIWR, I7),	    iwmmxt_textrm),
10120 cCE(textrmsw,	e900078, 3, (RR, RIWR, I7),	    iwmmxt_textrm),
10121 cCE(tinsrb,	e600010, 3, (RIWR, RR, I7),	    iwmmxt_tinsr),
10122 cCE(tinsrh,	e600050, 3, (RIWR, RR, I7),	    iwmmxt_tinsr),
10123 cCE(tinsrw,	e600090, 3, (RIWR, RR, I7),	    iwmmxt_tinsr),
10124 cCE(tmcr,	e000110, 2, (RIWC, RR),		    rn_rd),
10125 cCE(tmcrr,	c400000, 3, (RIWR, RR, RR),	    rm_rd_rn),
10126 cCE(tmia,	e200010, 3, (RIWR, RR, RR),	    iwmmxt_tmia),
10127 cCE(tmiaph,	e280010, 3, (RIWR, RR, RR),	    iwmmxt_tmia),
10128 cCE(tmiabb,	e2c0010, 3, (RIWR, RR, RR),	    iwmmxt_tmia),
10129 cCE(tmiabt,	e2d0010, 3, (RIWR, RR, RR),	    iwmmxt_tmia),
10130 cCE(tmiatb,	e2e0010, 3, (RIWR, RR, RR),	    iwmmxt_tmia),
10131 cCE(tmiatt,	e2f0010, 3, (RIWR, RR, RR),	    iwmmxt_tmia),
10132 cCE(tmovmskb,	e100030, 2, (RR, RIWR),		    rd_rn),
10133 cCE(tmovmskh,	e500030, 2, (RR, RIWR),		    rd_rn),
10134 cCE(tmovmskw,	e900030, 2, (RR, RIWR),		    rd_rn),
10135 cCE(tmrc,	e100110, 2, (RR, RIWC),		    rd_rn),
10136 cCE(tmrrc,	c500000, 3, (RR, RR, RIWR),	    rd_rn_rm),
10137 cCE(torcb,	e13f150, 1, (RR),		    iwmmxt_tandorc),
10138 cCE(torch,	e53f150, 1, (RR),		    iwmmxt_tandorc),
10139 cCE(torcw,	e93f150, 1, (RR),		    iwmmxt_tandorc),
10140 cCE(waccb,	e0001c0, 2, (RIWR, RIWR),	    rd_rn),
10141 cCE(wacch,	e4001c0, 2, (RIWR, RIWR),	    rd_rn),
10142 cCE(waccw,	e8001c0, 2, (RIWR, RIWR),	    rd_rn),
10143 cCE(waddbss,	e300180, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10144 cCE(waddb,	e000180, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10145 cCE(waddbus,	e100180, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10146 cCE(waddhss,	e700180, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10147 cCE(waddh,	e400180, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10148 cCE(waddhus,	e500180, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10149 cCE(waddwss,	eb00180, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10150 cCE(waddw,	e800180, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10151 cCE(waddwus,	e900180, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10152 cCE(waligni,	e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
10153 cCE(walignr0,	e800020, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10154 cCE(walignr1,	e900020, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10155 cCE(walignr2,	ea00020, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10156 cCE(walignr3,	eb00020, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10157 cCE(wand,	e200000, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10158 cCE(wandn,	e300000, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10159 cCE(wavg2b,	e800000, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10160 cCE(wavg2br,	e900000, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10161 cCE(wavg2h,	ec00000, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10162 cCE(wavg2hr,	ed00000, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10163 cCE(wcmpeqb,	e000060, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10164 cCE(wcmpeqh,	e400060, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10165 cCE(wcmpeqw,	e800060, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10166 cCE(wcmpgtub,	e100060, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10167 cCE(wcmpgtuh,	e500060, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10168 cCE(wcmpgtuw,	e900060, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10169 cCE(wcmpgtsb,	e300060, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10170 cCE(wcmpgtsh,	e700060, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10171 cCE(wcmpgtsw,	eb00060, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10172 cCE(wldrb,	c100000, 2, (RIWR, ADDR),	    iwmmxt_wldstbh),
10173 cCE(wldrh,	c500000, 2, (RIWR, ADDR),	    iwmmxt_wldstbh),
10174 cCE(wldrw,	c100100, 2, (RIWR_RIWC, ADDR),	    iwmmxt_wldstw),
10175 cCE(wldrd,	c500100, 2, (RIWR, ADDR),	    iwmmxt_wldstd),
10176 cCE(wmacs,	e600100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10177 cCE(wmacsz,	e700100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10178 cCE(wmacu,	e400100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10179 cCE(wmacuz,	e500100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10180 cCE(wmadds,	ea00100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10181 cCE(wmaddu,	e800100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10182 cCE(wmaxsb,	e200160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10183 cCE(wmaxsh,	e600160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10184 cCE(wmaxsw,	ea00160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10185 cCE(wmaxub,	e000160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10186 cCE(wmaxuh,	e400160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10187 cCE(wmaxuw,	e800160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10188 cCE(wminsb,	e300160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10189 cCE(wminsh,	e700160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10190 cCE(wminsw,	eb00160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10191 cCE(wminub,	e100160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10192 cCE(wminuh,	e500160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10193 cCE(wminuw,	e900160, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10194 cCE(wmov,	e000000, 2, (RIWR, RIWR),	    iwmmxt_wmov),
10195 cCE(wmulsm,	e300100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10196 cCE(wmulsl,	e200100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10197 cCE(wmulum,	e100100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10198 cCE(wmulul,	e000100, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10199 cCE(wor,	e000000, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10200 cCE(wpackhss,	e700080, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10201 cCE(wpackhus,	e500080, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10202 cCE(wpackwss,	eb00080, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10203 cCE(wpackwus,	e900080, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10204 cCE(wpackdss,	ef00080, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10205 cCE(wpackdus,	ed00080, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10206 cCE(wrorh,	e700040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10207 cCE(wrorhg,	e700148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10208 cCE(wrorw,	eb00040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10209 cCE(wrorwg,	eb00148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10210 cCE(wrord,	ef00040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10211 cCE(wrordg,	ef00148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10212 cCE(wsadb,	e000120, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10213 cCE(wsadbz,	e100120, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10214 cCE(wsadh,	e400120, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10215 cCE(wsadhz,	e500120, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10216 cCE(wshufh,	e0001e0, 3, (RIWR, RIWR, I255),	    iwmmxt_wshufh),
10217 cCE(wsllh,	e500040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10218 cCE(wsllhg,	e500148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10219 cCE(wsllw,	e900040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10220 cCE(wsllwg,	e900148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10221 cCE(wslld,	ed00040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10222 cCE(wslldg,	ed00148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10223 cCE(wsrah,	e400040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10224 cCE(wsrahg,	e400148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10225 cCE(wsraw,	e800040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10226 cCE(wsrawg,	e800148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10227 cCE(wsrad,	ec00040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10228 cCE(wsradg,	ec00148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10229 cCE(wsrlh,	e600040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10230 cCE(wsrlhg,	e600148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10231 cCE(wsrlw,	ea00040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10232 cCE(wsrlwg,	ea00148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10233 cCE(wsrld,	ee00040, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10234 cCE(wsrldg,	ee00148, 3, (RIWR, RIWR, RIWG),	    rd_rn_rm),
10235 cCE(wstrb,	c000000, 2, (RIWR, ADDR),	    iwmmxt_wldstbh),
10236 cCE(wstrh,	c400000, 2, (RIWR, ADDR),	    iwmmxt_wldstbh),
10237 cCE(wstrw,	c000100, 2, (RIWR_RIWC, ADDR),	    iwmmxt_wldstw),
10238 cCE(wstrd,	c400100, 2, (RIWR, ADDR),	    iwmmxt_wldstd),
10239 cCE(wsubbss,	e3001a0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10240 cCE(wsubb,	e0001a0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10241 cCE(wsubbus,	e1001a0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10242 cCE(wsubhss,	e7001a0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10243 cCE(wsubh,	e4001a0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10244 cCE(wsubhus,	e5001a0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10245 cCE(wsubwss,	eb001a0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10246 cCE(wsubw,	e8001a0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10247 cCE(wsubwus,	e9001a0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10248 cCE(wunpckehub,e0000c0, 2, (RIWR, RIWR),	    rd_rn),
10249 cCE(wunpckehuh,e4000c0, 2, (RIWR, RIWR),	    rd_rn),
10250 cCE(wunpckehuw,e8000c0, 2, (RIWR, RIWR),	    rd_rn),
10251 cCE(wunpckehsb,e2000c0, 2, (RIWR, RIWR),	    rd_rn),
10252 cCE(wunpckehsh,e6000c0, 2, (RIWR, RIWR),	    rd_rn),
10253 cCE(wunpckehsw,ea000c0, 2, (RIWR, RIWR),	    rd_rn),
10254 cCE(wunpckihb, e1000c0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10255 cCE(wunpckihh, e5000c0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10256 cCE(wunpckihw, e9000c0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10257 cCE(wunpckelub,e0000e0, 2, (RIWR, RIWR),	    rd_rn),
10258 cCE(wunpckeluh,e4000e0, 2, (RIWR, RIWR),	    rd_rn),
10259 cCE(wunpckeluw,e8000e0, 2, (RIWR, RIWR),	    rd_rn),
10260 cCE(wunpckelsb,e2000e0, 2, (RIWR, RIWR),	    rd_rn),
10261 cCE(wunpckelsh,e6000e0, 2, (RIWR, RIWR),	    rd_rn),
10262 cCE(wunpckelsw,ea000e0, 2, (RIWR, RIWR),	    rd_rn),
10263 cCE(wunpckilb, e1000e0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10264 cCE(wunpckilh, e5000e0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10265 cCE(wunpckilw, e9000e0, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10266 cCE(wxor,	e100000, 3, (RIWR, RIWR, RIWR),	    rd_rn_rm),
10267 cCE(wzero,	e300000, 1, (RIWR),		    iwmmxt_wzero),
10268
10269#undef ARM_VARIANT
10270#define ARM_VARIANT &arm_cext_maverick /* Cirrus Maverick instructions.	*/
10271 cCE(cfldrs,	c100400, 2, (RMF, ADDR),	      rd_cpaddr),
10272 cCE(cfldrd,	c500400, 2, (RMD, ADDR),	      rd_cpaddr),
10273 cCE(cfldr32,	c100500, 2, (RMFX, ADDR),	      rd_cpaddr),
10274 cCE(cfldr64,	c500500, 2, (RMDX, ADDR),	      rd_cpaddr),
10275 cCE(cfstrs,	c000400, 2, (RMF, ADDR),	      rd_cpaddr),
10276 cCE(cfstrd,	c400400, 2, (RMD, ADDR),	      rd_cpaddr),
10277 cCE(cfstr32,	c000500, 2, (RMFX, ADDR),	      rd_cpaddr),
10278 cCE(cfstr64,	c400500, 2, (RMDX, ADDR),	      rd_cpaddr),
10279 cCE(cfmvsr,	e000450, 2, (RMF, RR),		      rn_rd),
10280 cCE(cfmvrs,	e100450, 2, (RR, RMF),		      rd_rn),
10281 cCE(cfmvdlr,	e000410, 2, (RMD, RR),		      rn_rd),
10282 cCE(cfmvrdl,	e100410, 2, (RR, RMD),		      rd_rn),
10283 cCE(cfmvdhr,	e000430, 2, (RMD, RR),		      rn_rd),
10284 cCE(cfmvrdh,	e100430, 2, (RR, RMD),		      rd_rn),
10285 cCE(cfmv64lr,	e000510, 2, (RMDX, RR),		      rn_rd),
10286 cCE(cfmvr64l,	e100510, 2, (RR, RMDX),		      rd_rn),
10287 cCE(cfmv64hr,	e000530, 2, (RMDX, RR),		      rn_rd),
10288 cCE(cfmvr64h,	e100530, 2, (RR, RMDX),		      rd_rn),
10289 cCE(cfmval32,	e200440, 2, (RMAX, RMFX),	      rd_rn),
10290 cCE(cfmv32al,	e100440, 2, (RMFX, RMAX),	      rd_rn),
10291 cCE(cfmvam32,	e200460, 2, (RMAX, RMFX),	      rd_rn),
10292 cCE(cfmv32am,	e100460, 2, (RMFX, RMAX),	      rd_rn),
10293 cCE(cfmvah32,	e200480, 2, (RMAX, RMFX),	      rd_rn),
10294 cCE(cfmv32ah,	e100480, 2, (RMFX, RMAX),	      rd_rn),
10295 cCE(cfmva32,	e2004a0, 2, (RMAX, RMFX),	      rd_rn),
10296 cCE(cfmv32a,	e1004a0, 2, (RMFX, RMAX),	      rd_rn),
10297 cCE(cfmva64,	e2004c0, 2, (RMAX, RMDX),	      rd_rn),
10298 cCE(cfmv64a,	e1004c0, 2, (RMDX, RMAX),	      rd_rn),
10299 cCE(cfmvsc32,	e2004e0, 2, (RMDS, RMDX),	      mav_dspsc),
10300 cCE(cfmv32sc,	e1004e0, 2, (RMDX, RMDS),	      rd),
10301 cCE(cfcpys,	e000400, 2, (RMF, RMF),		      rd_rn),
10302 cCE(cfcpyd,	e000420, 2, (RMD, RMD),		      rd_rn),
10303 cCE(cfcvtsd,	e000460, 2, (RMD, RMF),		      rd_rn),
10304 cCE(cfcvtds,	e000440, 2, (RMF, RMD),		      rd_rn),
10305 cCE(cfcvt32s,	e000480, 2, (RMF, RMFX),	      rd_rn),
10306 cCE(cfcvt32d,	e0004a0, 2, (RMD, RMFX),	      rd_rn),
10307 cCE(cfcvt64s,	e0004c0, 2, (RMF, RMDX),	      rd_rn),
10308 cCE(cfcvt64d,	e0004e0, 2, (RMD, RMDX),	      rd_rn),
10309 cCE(cfcvts32,	e100580, 2, (RMFX, RMF),	      rd_rn),
10310 cCE(cfcvtd32,	e1005a0, 2, (RMFX, RMD),	      rd_rn),
10311 cCE(cftruncs32,e1005c0, 2, (RMFX, RMF),	      rd_rn),
10312 cCE(cftruncd32,e1005e0, 2, (RMFX, RMD),	      rd_rn),
10313 cCE(cfrshl32,	e000550, 3, (RMFX, RMFX, RR),	      mav_triple),
10314 cCE(cfrshl64,	e000570, 3, (RMDX, RMDX, RR),	      mav_triple),
10315 cCE(cfsh32,	e000500, 3, (RMFX, RMFX, I63s),	      mav_shift),
10316 cCE(cfsh64,	e200500, 3, (RMDX, RMDX, I63s),	      mav_shift),
10317 cCE(cfcmps,	e100490, 3, (RR, RMF, RMF),	      rd_rn_rm),
10318 cCE(cfcmpd,	e1004b0, 3, (RR, RMD, RMD),	      rd_rn_rm),
10319 cCE(cfcmp32,	e100590, 3, (RR, RMFX, RMFX),	      rd_rn_rm),
10320 cCE(cfcmp64,	e1005b0, 3, (RR, RMDX, RMDX),	      rd_rn_rm),
10321 cCE(cfabss,	e300400, 2, (RMF, RMF),		      rd_rn),
10322 cCE(cfabsd,	e300420, 2, (RMD, RMD),		      rd_rn),
10323 cCE(cfnegs,	e300440, 2, (RMF, RMF),		      rd_rn),
10324 cCE(cfnegd,	e300460, 2, (RMD, RMD),		      rd_rn),
10325 cCE(cfadds,	e300480, 3, (RMF, RMF, RMF),	      rd_rn_rm),
10326 cCE(cfaddd,	e3004a0, 3, (RMD, RMD, RMD),	      rd_rn_rm),
10327 cCE(cfsubs,	e3004c0, 3, (RMF, RMF, RMF),	      rd_rn_rm),
10328 cCE(cfsubd,	e3004e0, 3, (RMD, RMD, RMD),	      rd_rn_rm),
10329 cCE(cfmuls,	e100400, 3, (RMF, RMF, RMF),	      rd_rn_rm),
10330 cCE(cfmuld,	e100420, 3, (RMD, RMD, RMD),	      rd_rn_rm),
10331 cCE(cfabs32,	e300500, 2, (RMFX, RMFX),	      rd_rn),
10332 cCE(cfabs64,	e300520, 2, (RMDX, RMDX),	      rd_rn),
10333 cCE(cfneg32,	e300540, 2, (RMFX, RMFX),	      rd_rn),
10334 cCE(cfneg64,	e300560, 2, (RMDX, RMDX),	      rd_rn),
10335 cCE(cfadd32,	e300580, 3, (RMFX, RMFX, RMFX),	      rd_rn_rm),
10336 cCE(cfadd64,	e3005a0, 3, (RMDX, RMDX, RMDX),	      rd_rn_rm),
10337 cCE(cfsub32,	e3005c0, 3, (RMFX, RMFX, RMFX),	      rd_rn_rm),
10338 cCE(cfsub64,	e3005e0, 3, (RMDX, RMDX, RMDX),	      rd_rn_rm),
10339 cCE(cfmul32,	e100500, 3, (RMFX, RMFX, RMFX),	      rd_rn_rm),
10340 cCE(cfmul64,	e100520, 3, (RMDX, RMDX, RMDX),	      rd_rn_rm),
10341 cCE(cfmac32,	e100540, 3, (RMFX, RMFX, RMFX),	      rd_rn_rm),
10342 cCE(cfmsc32,	e100560, 3, (RMFX, RMFX, RMFX),	      rd_rn_rm),
10343 cCE(cfmadd32,	e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
10344 cCE(cfmsub32,	e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
10345 cCE(cfmadda32, e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
10346 cCE(cfmsuba32, e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
10347};
10348#undef ARM_VARIANT
10349#undef THUMB_VARIANT
10350#undef TCE
10351#undef TCM
10352#undef TUE
10353#undef TUF
10354#undef TCC
10355#undef cCE
10356#undef cCL
10357#undef C3E
10358#undef CE
10359#undef CM
10360#undef UE
10361#undef UF
10362#undef UT
10363#undef OPS0
10364#undef OPS1
10365#undef OPS2
10366#undef OPS3
10367#undef OPS4
10368#undef OPS5
10369#undef OPS6
10370#undef do_0
10371
10372/* MD interface: bits in the object file.  */
10373
10374/* Turn an integer of n bytes (in val) into a stream of bytes appropriate
10375   for use in the a.out file, and stores them in the array pointed to by buf.
10376   This knows about the endian-ness of the target machine and does
10377   THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
10378   2 (short) and 4 (long)  Floating numbers are put out as a series of
10379   LITTLENUMS (shorts, here at least).	*/
10380
10381void
10382md_number_to_chars (char * buf, valueT val, int n)
10383{
10384  if (target_big_endian)
10385    number_to_chars_bigendian (buf, val, n);
10386  else
10387    number_to_chars_littleendian (buf, val, n);
10388}
10389
10390static valueT
10391md_chars_to_number (char * buf, int n)
10392{
10393  valueT result = 0;
10394  unsigned char * where = (unsigned char *) buf;
10395
10396  if (target_big_endian)
10397    {
10398      while (n--)
10399	{
10400	  result <<= 8;
10401	  result |= (*where++ & 255);
10402	}
10403    }
10404  else
10405    {
10406      while (n--)
10407	{
10408	  result <<= 8;
10409	  result |= (where[n] & 255);
10410	}
10411    }
10412
10413  return result;
10414}
10415
10416/* MD interface: Sections.  */
10417
10418/* Estimate the size of a frag before relaxing.  Assume everything fits in
10419   2 bytes.  */
10420
10421int
10422md_estimate_size_before_relax (fragS * fragp,
10423			       segT    segtype ATTRIBUTE_UNUSED)
10424{
10425  fragp->fr_var = 2;
10426  return 2;
10427}
10428
10429/* Convert a machine dependent frag.  */
10430
10431void
10432md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
10433{
10434  unsigned long insn;
10435  unsigned long old_op;
10436  char *buf;
10437  expressionS exp;
10438  fixS *fixp;
10439  int reloc_type;
10440  int pc_rel;
10441  int opcode;
10442
10443  buf = fragp->fr_literal + fragp->fr_fix;
10444
10445  old_op = bfd_get_16(abfd, buf);
10446  if (fragp->fr_symbol) {
10447      exp.X_op = O_symbol;
10448      exp.X_add_symbol = fragp->fr_symbol;
10449  } else {
10450      exp.X_op = O_constant;
10451  }
10452  exp.X_add_number = fragp->fr_offset;
10453  opcode = fragp->fr_subtype;
10454  switch (opcode)
10455    {
10456    case T_MNEM_ldr_pc:
10457    case T_MNEM_ldr_pc2:
10458    case T_MNEM_ldr_sp:
10459    case T_MNEM_str_sp:
10460    case T_MNEM_ldr:
10461    case T_MNEM_ldrb:
10462    case T_MNEM_ldrh:
10463    case T_MNEM_str:
10464    case T_MNEM_strb:
10465    case T_MNEM_strh:
10466      if (fragp->fr_var == 4)
10467	{
10468	  insn = THUMB_OP32(opcode);
10469	  if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
10470	    {
10471	      insn |= (old_op & 0x700) << 4;
10472	    }
10473	  else
10474	    {
10475	      insn |= (old_op & 7) << 12;
10476	      insn |= (old_op & 0x38) << 13;
10477	    }
10478	  insn |= 0x00000c00;
10479	  put_thumb32_insn (buf, insn);
10480	  reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10481	}
10482      else
10483	{
10484	  reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
10485	}
10486      pc_rel = (opcode == T_MNEM_ldr_pc2);
10487      break;
10488    case T_MNEM_adr:
10489      if (fragp->fr_var == 4)
10490	{
10491	  insn = THUMB_OP32 (opcode);
10492	  insn |= (old_op & 0xf0) << 4;
10493	  put_thumb32_insn (buf, insn);
10494	  reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
10495	}
10496      else
10497	{
10498	  reloc_type = BFD_RELOC_ARM_THUMB_ADD;
10499	  exp.X_add_number -= 4;
10500	}
10501      pc_rel = 1;
10502      break;
10503    case T_MNEM_mov:
10504    case T_MNEM_movs:
10505    case T_MNEM_cmp:
10506    case T_MNEM_cmn:
10507      if (fragp->fr_var == 4)
10508	{
10509	  int r0off = (opcode == T_MNEM_mov
10510		       || opcode == T_MNEM_movs) ? 0 : 8;
10511	  insn = THUMB_OP32 (opcode);
10512	  insn = (insn & 0xe1ffffff) | 0x10000000;
10513	  insn |= (old_op & 0x700) << r0off;
10514	  put_thumb32_insn (buf, insn);
10515	  reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
10516	}
10517      else
10518	{
10519	  reloc_type = BFD_RELOC_ARM_THUMB_IMM;
10520	}
10521      pc_rel = 0;
10522      break;
10523    case T_MNEM_b:
10524      if (fragp->fr_var == 4)
10525	{
10526	  insn = THUMB_OP32(opcode);
10527	  put_thumb32_insn (buf, insn);
10528	  reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
10529	}
10530      else
10531	reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
10532      pc_rel = 1;
10533      break;
10534    case T_MNEM_bcond:
10535      if (fragp->fr_var == 4)
10536	{
10537	  insn = THUMB_OP32(opcode);
10538	  insn |= (old_op & 0xf00) << 14;
10539	  put_thumb32_insn (buf, insn);
10540	  reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
10541	}
10542      else
10543	reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
10544      pc_rel = 1;
10545      break;
10546    case T_MNEM_add_sp:
10547    case T_MNEM_add_pc:
10548    case T_MNEM_inc_sp:
10549    case T_MNEM_dec_sp:
10550      if (fragp->fr_var == 4)
10551	{
10552	  /* ??? Choose between add and addw.  */
10553	  insn = THUMB_OP32 (opcode);
10554	  insn |= (old_op & 0xf0) << 4;
10555	  put_thumb32_insn (buf, insn);
10556	  reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
10557	}
10558      else
10559	reloc_type = BFD_RELOC_ARM_THUMB_ADD;
10560      pc_rel = 0;
10561      break;
10562
10563    case T_MNEM_addi:
10564    case T_MNEM_addis:
10565    case T_MNEM_subi:
10566    case T_MNEM_subis:
10567      if (fragp->fr_var == 4)
10568	{
10569	  insn = THUMB_OP32 (opcode);
10570	  insn |= (old_op & 0xf0) << 4;
10571	  insn |= (old_op & 0xf) << 16;
10572	  put_thumb32_insn (buf, insn);
10573	  reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
10574	}
10575      else
10576	reloc_type = BFD_RELOC_ARM_THUMB_ADD;
10577      pc_rel = 0;
10578      break;
10579    default:
10580      abort();
10581    }
10582  fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
10583		      reloc_type);
10584  fixp->fx_file = fragp->fr_file;
10585  fixp->fx_line = fragp->fr_line;
10586  fragp->fr_fix += fragp->fr_var;
10587}
10588
10589/* Return the size of a relaxable immediate operand instruction.
10590   SHIFT and SIZE specify the form of the allowable immediate.  */
10591static int
10592relax_immediate (fragS *fragp, int size, int shift)
10593{
10594  offsetT offset;
10595  offsetT mask;
10596  offsetT low;
10597
10598  /* ??? Should be able to do better than this.  */
10599  if (fragp->fr_symbol)
10600    return 4;
10601
10602  low = (1 << shift) - 1;
10603  mask = (1 << (shift + size)) - (1 << shift);
10604  offset = fragp->fr_offset;
10605  /* Force misaligned offsets to 32-bit variant.  */
10606  if (offset & low)
10607    return -4;
10608  if (offset & ~mask)
10609    return 4;
10610  return 2;
10611}
10612
10613/* Return the size of a relaxable adr pseudo-instruction or PC-relative
10614   load.  */
10615static int
10616relax_adr (fragS *fragp, asection *sec)
10617{
10618  addressT addr;
10619  offsetT val;
10620
10621  /* Assume worst case for symbols not known to be in the same section.  */
10622  if (!S_IS_DEFINED(fragp->fr_symbol)
10623      || sec != S_GET_SEGMENT (fragp->fr_symbol))
10624    return 4;
10625
10626  val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
10627  addr = fragp->fr_address + fragp->fr_fix;
10628  addr = (addr + 4) & ~3;
10629  /* Fix the insn as the 4-byte version if the target address is not
10630     sufficiently aligned.  This is prevents an infinite loop when two
10631     instructions have contradictory range/alignment requirements.  */
10632  if (val & 3)
10633    return -4;
10634  val -= addr;
10635  if (val < 0 || val > 1020)
10636    return 4;
10637  return 2;
10638}
10639
10640/* Return the size of a relaxable add/sub immediate instruction.  */
10641static int
10642relax_addsub (fragS *fragp, asection *sec)
10643{
10644  char *buf;
10645  int op;
10646
10647  buf = fragp->fr_literal + fragp->fr_fix;
10648  op = bfd_get_16(sec->owner, buf);
10649  if ((op & 0xf) == ((op >> 4) & 0xf))
10650    return relax_immediate (fragp, 8, 0);
10651  else
10652    return relax_immediate (fragp, 3, 0);
10653}
10654
10655
10656/* Return the size of a relaxable branch instruction.  BITS is the
10657   size of the offset field in the narrow instruction.  */
10658
10659static int
10660relax_branch (fragS *fragp, asection *sec, int bits)
10661{
10662  addressT addr;
10663  offsetT val;
10664  offsetT limit;
10665
10666  /* Assume worst case for symbols not known to be in the same section.  */
10667  if (!S_IS_DEFINED(fragp->fr_symbol)
10668      || sec != S_GET_SEGMENT (fragp->fr_symbol))
10669    return 4;
10670
10671  val = S_GET_VALUE(fragp->fr_symbol) + fragp->fr_offset;
10672  addr = fragp->fr_address + fragp->fr_fix + 4;
10673  val -= addr;
10674
10675  /* Offset is a signed value *2 */
10676  limit = 1 << bits;
10677  if (val >= limit || val < -limit)
10678    return 4;
10679  return 2;
10680}
10681
10682
10683/* Relax a machine dependent frag.  This returns the amount by which
10684   the current size of the frag should change.  */
10685
10686int
10687arm_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
10688{
10689  int oldsize;
10690  int newsize;
10691
10692  oldsize = fragp->fr_var;
10693  switch (fragp->fr_subtype)
10694    {
10695    case T_MNEM_ldr_pc2:
10696      newsize = relax_adr(fragp, sec);
10697      break;
10698    case T_MNEM_ldr_pc:
10699    case T_MNEM_ldr_sp:
10700    case T_MNEM_str_sp:
10701      newsize = relax_immediate(fragp, 8, 2);
10702      break;
10703    case T_MNEM_ldr:
10704    case T_MNEM_str:
10705      newsize = relax_immediate(fragp, 5, 2);
10706      break;
10707    case T_MNEM_ldrh:
10708    case T_MNEM_strh:
10709      newsize = relax_immediate(fragp, 5, 1);
10710      break;
10711    case T_MNEM_ldrb:
10712    case T_MNEM_strb:
10713      newsize = relax_immediate(fragp, 5, 0);
10714      break;
10715    case T_MNEM_adr:
10716      newsize = relax_adr(fragp, sec);
10717      break;
10718    case T_MNEM_mov:
10719    case T_MNEM_movs:
10720    case T_MNEM_cmp:
10721    case T_MNEM_cmn:
10722      newsize = relax_immediate(fragp, 8, 0);
10723      break;
10724    case T_MNEM_b:
10725      newsize = relax_branch(fragp, sec, 11);
10726      break;
10727    case T_MNEM_bcond:
10728      newsize = relax_branch(fragp, sec, 8);
10729      break;
10730    case T_MNEM_add_sp:
10731    case T_MNEM_add_pc:
10732      newsize = relax_immediate (fragp, 8, 2);
10733      break;
10734    case T_MNEM_inc_sp:
10735    case T_MNEM_dec_sp:
10736      newsize = relax_immediate (fragp, 7, 2);
10737      break;
10738    case T_MNEM_addi:
10739    case T_MNEM_addis:
10740    case T_MNEM_subi:
10741    case T_MNEM_subis:
10742      newsize = relax_addsub (fragp, sec);
10743      break;
10744    default:
10745      abort();
10746    }
10747  if (newsize < 0)
10748    {
10749      fragp->fr_var = -newsize;
10750      md_convert_frag (sec->owner, sec, fragp);
10751      frag_wane(fragp);
10752      return -(newsize + oldsize);
10753    }
10754  fragp->fr_var = newsize;
10755  return newsize - oldsize;
10756}
10757
10758/* Round up a section size to the appropriate boundary.	 */
10759
10760valueT
10761md_section_align (segT	 segment ATTRIBUTE_UNUSED,
10762		  valueT size)
10763{
10764#ifdef OBJ_ELF
10765  return size;
10766#else
10767  /* Round all sects to multiple of 4.	*/
10768  return (size + 3) & ~3;
10769#endif
10770}
10771
10772/* This is called from HANDLE_ALIGN in write.c.	 Fill in the contents
10773   of an rs_align_code fragment.  */
10774
10775void
10776arm_handle_align (fragS * fragP)
10777{
10778  static char const arm_noop[4] = { 0x00, 0x00, 0xa0, 0xe1 };
10779  static char const thumb_noop[2] = { 0xc0, 0x46 };
10780  static char const arm_bigend_noop[4] = { 0xe1, 0xa0, 0x00, 0x00 };
10781  static char const thumb_bigend_noop[2] = { 0x46, 0xc0 };
10782
10783  int bytes, fix, noop_size;
10784  char * p;
10785  const char * noop;
10786
10787  if (fragP->fr_type != rs_align_code)
10788    return;
10789
10790  bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
10791  p = fragP->fr_literal + fragP->fr_fix;
10792  fix = 0;
10793
10794  if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
10795    bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
10796
10797  if (fragP->tc_frag_data)
10798    {
10799      if (target_big_endian)
10800	noop = thumb_bigend_noop;
10801      else
10802	noop = thumb_noop;
10803      noop_size = sizeof (thumb_noop);
10804    }
10805  else
10806    {
10807      if (target_big_endian)
10808	noop = arm_bigend_noop;
10809      else
10810	noop = arm_noop;
10811      noop_size = sizeof (arm_noop);
10812    }
10813
10814  if (bytes & (noop_size - 1))
10815    {
10816      fix = bytes & (noop_size - 1);
10817      memset (p, 0, fix);
10818      p += fix;
10819      bytes -= fix;
10820    }
10821
10822  while (bytes >= noop_size)
10823    {
10824      memcpy (p, noop, noop_size);
10825      p += noop_size;
10826      bytes -= noop_size;
10827      fix += noop_size;
10828    }
10829
10830  fragP->fr_fix += fix;
10831  fragP->fr_var = noop_size;
10832}
10833
10834/* Called from md_do_align.  Used to create an alignment
10835   frag in a code section.  */
10836
10837void
10838arm_frag_align_code (int n, int max)
10839{
10840  char * p;
10841
10842  /* We assume that there will never be a requirement
10843     to support alignments greater than 32 bytes.  */
10844  if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
10845    as_fatal (_("alignments greater than 32 bytes not supported in .text sections."));
10846
10847  p = frag_var (rs_align_code,
10848		MAX_MEM_FOR_RS_ALIGN_CODE,
10849		1,
10850		(relax_substateT) max,
10851		(symbolS *) NULL,
10852		(offsetT) n,
10853		(char *) NULL);
10854  *p = 0;
10855}
10856
10857/* Perform target specific initialisation of a frag.  */
10858
10859void
10860arm_init_frag (fragS * fragP)
10861{
10862  /* Record whether this frag is in an ARM or a THUMB area.  */
10863  fragP->tc_frag_data = thumb_mode;
10864}
10865
10866#ifdef OBJ_ELF
10867/* When we change sections we need to issue a new mapping symbol.  */
10868
10869void
10870arm_elf_change_section (void)
10871{
10872  flagword flags;
10873  segment_info_type *seginfo;
10874
10875  /* Link an unlinked unwind index table section to the .text section.	*/
10876  if (elf_section_type (now_seg) == SHT_ARM_EXIDX
10877      && elf_linked_to_section (now_seg) == NULL)
10878    elf_linked_to_section (now_seg) = text_section;
10879
10880  if (!SEG_NORMAL (now_seg))
10881    return;
10882
10883  flags = bfd_get_section_flags (stdoutput, now_seg);
10884
10885  /* We can ignore sections that only contain debug info.  */
10886  if ((flags & SEC_ALLOC) == 0)
10887    return;
10888
10889  seginfo = seg_info (now_seg);
10890  mapstate = seginfo->tc_segment_info_data.mapstate;
10891  marked_pr_dependency = seginfo->tc_segment_info_data.marked_pr_dependency;
10892}
10893
10894int
10895arm_elf_section_type (const char * str, size_t len)
10896{
10897  if (len == 5 && strncmp (str, "exidx", 5) == 0)
10898    return SHT_ARM_EXIDX;
10899
10900  return -1;
10901}
10902
10903/* Code to deal with unwinding tables.	*/
10904
10905static void add_unwind_adjustsp (offsetT);
10906
10907/* Cenerate and deferred unwind frame offset.  */
10908
10909static void
10910flush_pending_unwind (void)
10911{
10912  offsetT offset;
10913
10914  offset = unwind.pending_offset;
10915  unwind.pending_offset = 0;
10916  if (offset != 0)
10917    add_unwind_adjustsp (offset);
10918}
10919
10920/* Add an opcode to this list for this function.  Two-byte opcodes should
10921   be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
10922   order.  */
10923
10924static void
10925add_unwind_opcode (valueT op, int length)
10926{
10927  /* Add any deferred stack adjustment.	 */
10928  if (unwind.pending_offset)
10929    flush_pending_unwind ();
10930
10931  unwind.sp_restored = 0;
10932
10933  if (unwind.opcode_count + length > unwind.opcode_alloc)
10934    {
10935      unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
10936      if (unwind.opcodes)
10937	unwind.opcodes = xrealloc (unwind.opcodes,
10938				   unwind.opcode_alloc);
10939      else
10940	unwind.opcodes = xmalloc (unwind.opcode_alloc);
10941    }
10942  while (length > 0)
10943    {
10944      length--;
10945      unwind.opcodes[unwind.opcode_count] = op & 0xff;
10946      op >>= 8;
10947      unwind.opcode_count++;
10948    }
10949}
10950
10951/* Add unwind opcodes to adjust the stack pointer.  */
10952
10953static void
10954add_unwind_adjustsp (offsetT offset)
10955{
10956  valueT op;
10957
10958  if (offset > 0x200)
10959    {
10960      /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
10961      char bytes[5];
10962      int n;
10963      valueT o;
10964
10965      /* Long form: 0xb2, uleb128.  */
10966      /* This might not fit in a word so add the individual bytes,
10967	 remembering the list is built in reverse order.  */
10968      o = (valueT) ((offset - 0x204) >> 2);
10969      if (o == 0)
10970	add_unwind_opcode (0, 1);
10971
10972      /* Calculate the uleb128 encoding of the offset.	*/
10973      n = 0;
10974      while (o)
10975	{
10976	  bytes[n] = o & 0x7f;
10977	  o >>= 7;
10978	  if (o)
10979	    bytes[n] |= 0x80;
10980	  n++;
10981	}
10982      /* Add the insn.	*/
10983      for (; n; n--)
10984	add_unwind_opcode (bytes[n - 1], 1);
10985      add_unwind_opcode (0xb2, 1);
10986    }
10987  else if (offset > 0x100)
10988    {
10989      /* Two short opcodes.  */
10990      add_unwind_opcode (0x3f, 1);
10991      op = (offset - 0x104) >> 2;
10992      add_unwind_opcode (op, 1);
10993    }
10994  else if (offset > 0)
10995    {
10996      /* Short opcode.	*/
10997      op = (offset - 4) >> 2;
10998      add_unwind_opcode (op, 1);
10999    }
11000  else if (offset < 0)
11001    {
11002      offset = -offset;
11003      while (offset > 0x100)
11004	{
11005	  add_unwind_opcode (0x7f, 1);
11006	  offset -= 0x100;
11007	}
11008      op = ((offset - 4) >> 2) | 0x40;
11009      add_unwind_opcode (op, 1);
11010    }
11011}
11012
11013/* Finish the list of unwind opcodes for this function.	 */
11014static void
11015finish_unwind_opcodes (void)
11016{
11017  valueT op;
11018
11019  if (unwind.fp_used)
11020    {
11021      /* Adjust sp as neccessary.  */
11022      unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
11023      flush_pending_unwind ();
11024
11025      /* After restoring sp from the frame pointer.  */
11026      op = 0x90 | unwind.fp_reg;
11027      add_unwind_opcode (op, 1);
11028    }
11029  else
11030    flush_pending_unwind ();
11031}
11032
11033
11034/* Start an exception table entry.  If idx is nonzero this is an index table
11035   entry.  */
11036
11037static void
11038start_unwind_section (const segT text_seg, int idx)
11039{
11040  const char * text_name;
11041  const char * prefix;
11042  const char * prefix_once;
11043  const char * group_name;
11044  size_t prefix_len;
11045  size_t text_len;
11046  char * sec_name;
11047  size_t sec_name_len;
11048  int type;
11049  int flags;
11050  int linkonce;
11051
11052  if (idx)
11053    {
11054      prefix = ELF_STRING_ARM_unwind;
11055      prefix_once = ELF_STRING_ARM_unwind_once;
11056      type = SHT_ARM_EXIDX;
11057    }
11058  else
11059    {
11060      prefix = ELF_STRING_ARM_unwind_info;
11061      prefix_once = ELF_STRING_ARM_unwind_info_once;
11062      type = SHT_PROGBITS;
11063    }
11064
11065  text_name = segment_name (text_seg);
11066  if (streq (text_name, ".text"))
11067    text_name = "";
11068
11069  if (strncmp (text_name, ".gnu.linkonce.t.",
11070	       strlen (".gnu.linkonce.t.")) == 0)
11071    {
11072      prefix = prefix_once;
11073      text_name += strlen (".gnu.linkonce.t.");
11074    }
11075
11076  prefix_len = strlen (prefix);
11077  text_len = strlen (text_name);
11078  sec_name_len = prefix_len + text_len;
11079  sec_name = xmalloc (sec_name_len + 1);
11080  memcpy (sec_name, prefix, prefix_len);
11081  memcpy (sec_name + prefix_len, text_name, text_len);
11082  sec_name[prefix_len + text_len] = '\0';
11083
11084  flags = SHF_ALLOC;
11085  linkonce = 0;
11086  group_name = 0;
11087
11088  /* Handle COMDAT group.  */
11089  if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
11090    {
11091      group_name = elf_group_name (text_seg);
11092      if (group_name == NULL)
11093	{
11094	  as_bad ("Group section `%s' has no group signature",
11095		  segment_name (text_seg));
11096	  ignore_rest_of_line ();
11097	  return;
11098	}
11099      flags |= SHF_GROUP;
11100      linkonce = 1;
11101    }
11102
11103  obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
11104
11105  /* Set the setion link for index tables.  */
11106  if (idx)
11107    elf_linked_to_section (now_seg) = text_seg;
11108}
11109
11110
11111/* Start an unwind table entry.	 HAVE_DATA is nonzero if we have additional
11112   personality routine data.  Returns zero, or the index table value for
11113   and inline entry.  */
11114
11115static valueT
11116create_unwind_entry (int have_data)
11117{
11118  int size;
11119  addressT where;
11120  char *ptr;
11121  /* The current word of data.	*/
11122  valueT data;
11123  /* The number of bytes left in this word.  */
11124  int n;
11125
11126  finish_unwind_opcodes ();
11127
11128  /* Remember the current text section.	 */
11129  unwind.saved_seg = now_seg;
11130  unwind.saved_subseg = now_subseg;
11131
11132  start_unwind_section (now_seg, 0);
11133
11134  if (unwind.personality_routine == NULL)
11135    {
11136      if (unwind.personality_index == -2)
11137	{
11138	  if (have_data)
11139	    as_bad (_("handerdata in cantunwind frame"));
11140	  return 1; /* EXIDX_CANTUNWIND.  */
11141	}
11142
11143      /* Use a default personality routine if none is specified.  */
11144      if (unwind.personality_index == -1)
11145	{
11146	  if (unwind.opcode_count > 3)
11147	    unwind.personality_index = 1;
11148	  else
11149	    unwind.personality_index = 0;
11150	}
11151
11152      /* Space for the personality routine entry.  */
11153      if (unwind.personality_index == 0)
11154	{
11155	  if (unwind.opcode_count > 3)
11156	    as_bad (_("too many unwind opcodes for personality routine 0"));
11157
11158	  if (!have_data)
11159	    {
11160	      /* All the data is inline in the index table.  */
11161	      data = 0x80;
11162	      n = 3;
11163	      while (unwind.opcode_count > 0)
11164		{
11165		  unwind.opcode_count--;
11166		  data = (data << 8) | unwind.opcodes[unwind.opcode_count];
11167		  n--;
11168		}
11169
11170	      /* Pad with "finish" opcodes.  */
11171	      while (n--)
11172		data = (data << 8) | 0xb0;
11173
11174	      return data;
11175	    }
11176	  size = 0;
11177	}
11178      else
11179	/* We get two opcodes "free" in the first word.	 */
11180	size = unwind.opcode_count - 2;
11181    }
11182  else
11183    /* An extra byte is required for the opcode count.	*/
11184    size = unwind.opcode_count + 1;
11185
11186  size = (size + 3) >> 2;
11187  if (size > 0xff)
11188    as_bad (_("too many unwind opcodes"));
11189
11190  frag_align (2, 0, 0);
11191  record_alignment (now_seg, 2);
11192  unwind.table_entry = expr_build_dot ();
11193
11194  /* Allocate the table entry.	*/
11195  ptr = frag_more ((size << 2) + 4);
11196  where = frag_now_fix () - ((size << 2) + 4);
11197
11198  switch (unwind.personality_index)
11199    {
11200    case -1:
11201      /* ??? Should this be a PLT generating relocation?  */
11202      /* Custom personality routine.  */
11203      fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
11204	       BFD_RELOC_ARM_PREL31);
11205
11206      where += 4;
11207      ptr += 4;
11208
11209      /* Set the first byte to the number of additional words.	*/
11210      data = size - 1;
11211      n = 3;
11212      break;
11213
11214    /* ABI defined personality routines.  */
11215    case 0:
11216      /* Three opcodes bytes are packed into the first word.  */
11217      data = 0x80;
11218      n = 3;
11219      break;
11220
11221    case 1:
11222    case 2:
11223      /* The size and first two opcode bytes go in the first word.  */
11224      data = ((0x80 + unwind.personality_index) << 8) | size;
11225      n = 2;
11226      break;
11227
11228    default:
11229      /* Should never happen.  */
11230      abort ();
11231    }
11232
11233  /* Pack the opcodes into words (MSB first), reversing the list at the same
11234     time.  */
11235  while (unwind.opcode_count > 0)
11236    {
11237      if (n == 0)
11238	{
11239	  md_number_to_chars (ptr, data, 4);
11240	  ptr += 4;
11241	  n = 4;
11242	  data = 0;
11243	}
11244      unwind.opcode_count--;
11245      n--;
11246      data = (data << 8) | unwind.opcodes[unwind.opcode_count];
11247    }
11248
11249  /* Finish off the last word.	*/
11250  if (n < 4)
11251    {
11252      /* Pad with "finish" opcodes.  */
11253      while (n--)
11254	data = (data << 8) | 0xb0;
11255
11256      md_number_to_chars (ptr, data, 4);
11257    }
11258
11259  if (!have_data)
11260    {
11261      /* Add an empty descriptor if there is no user-specified data.   */
11262      ptr = frag_more (4);
11263      md_number_to_chars (ptr, 0, 4);
11264    }
11265
11266  return 0;
11267}
11268
11269/* Convert REGNAME to a DWARF-2 register number.  */
11270
11271int
11272tc_arm_regname_to_dw2regnum (const char *regname)
11273{
11274  int reg = arm_reg_parse ((char **) &regname, REG_TYPE_RN);
11275
11276  if (reg == FAIL)
11277    return -1;
11278
11279  return reg;
11280}
11281
11282/* Initialize the DWARF-2 unwind information for this procedure.  */
11283
11284void
11285tc_arm_frame_initial_instructions (void)
11286{
11287  cfi_add_CFA_def_cfa (REG_SP, 0);
11288}
11289#endif /* OBJ_ELF */
11290
11291
11292/* MD interface: Symbol and relocation handling.  */
11293
11294/* Return the address within the segment that a PC-relative fixup is
11295   relative to.  For ARM, PC-relative fixups applied to instructions
11296   are generally relative to the location of the fixup plus 8 bytes.
11297   Thumb branches are offset by 4, and Thumb loads relative to PC
11298   require special handling.  */
11299
11300long
11301md_pcrel_from_section (fixS * fixP, segT seg)
11302{
11303  offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
11304
11305  /* If this is pc-relative and we are going to emit a relocation
11306     then we just want to put out any pipeline compensation that the linker
11307     will need.  Otherwise we want to use the calculated base.  */
11308  if (fixP->fx_pcrel
11309      && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
11310	  || arm_force_relocation (fixP)))
11311    base = 0;
11312
11313  switch (fixP->fx_r_type)
11314    {
11315      /* PC relative addressing on the Thumb is slightly odd as the
11316	 bottom two bits of the PC are forced to zero for the
11317	 calculation.  This happens *after* application of the
11318	 pipeline offset.  However, Thumb adrl already adjusts for
11319	 this, so we need not do it again.  */
11320    case BFD_RELOC_ARM_THUMB_ADD:
11321      return base & ~3;
11322
11323    case BFD_RELOC_ARM_THUMB_OFFSET:
11324    case BFD_RELOC_ARM_T32_OFFSET_IMM:
11325    case BFD_RELOC_ARM_T32_ADD_PC12:
11326    case BFD_RELOC_ARM_T32_CP_OFF_IMM:
11327      return (base + 4) & ~3;
11328
11329      /* Thumb branches are simply offset by +4.  */
11330    case BFD_RELOC_THUMB_PCREL_BRANCH7:
11331    case BFD_RELOC_THUMB_PCREL_BRANCH9:
11332    case BFD_RELOC_THUMB_PCREL_BRANCH12:
11333    case BFD_RELOC_THUMB_PCREL_BRANCH20:
11334    case BFD_RELOC_THUMB_PCREL_BRANCH23:
11335    case BFD_RELOC_THUMB_PCREL_BRANCH25:
11336    case BFD_RELOC_THUMB_PCREL_BLX:
11337      return base + 4;
11338
11339      /* ARM mode branches are offset by +8.  However, the Windows CE
11340	 loader expects the relocation not to take this into account.  */
11341    case BFD_RELOC_ARM_PCREL_BRANCH:
11342    case BFD_RELOC_ARM_PCREL_CALL:
11343    case BFD_RELOC_ARM_PCREL_JUMP:
11344    case BFD_RELOC_ARM_PCREL_BLX:
11345    case BFD_RELOC_ARM_PLT32:
11346#ifdef TE_WINCE
11347      return base;
11348#else
11349      return base + 8;
11350#endif
11351
11352      /* ARM mode loads relative to PC are also offset by +8.  Unlike
11353	 branches, the Windows CE loader *does* expect the relocation
11354	 to take this into account.  */
11355    case BFD_RELOC_ARM_OFFSET_IMM:
11356    case BFD_RELOC_ARM_OFFSET_IMM8:
11357    case BFD_RELOC_ARM_HWLITERAL:
11358    case BFD_RELOC_ARM_LITERAL:
11359    case BFD_RELOC_ARM_CP_OFF_IMM:
11360      return base + 8;
11361
11362
11363      /* Other PC-relative relocations are un-offset.  */
11364    default:
11365      return base;
11366    }
11367}
11368
11369/* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
11370   Otherwise we have no need to default values of symbols.  */
11371
11372symbolS *
11373md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
11374{
11375#ifdef OBJ_ELF
11376  if (name[0] == '_' && name[1] == 'G'
11377      && streq (name, GLOBAL_OFFSET_TABLE_NAME))
11378    {
11379      if (!GOT_symbol)
11380	{
11381	  if (symbol_find (name))
11382	    as_bad ("GOT already in the symbol table");
11383
11384	  GOT_symbol = symbol_new (name, undefined_section,
11385				   (valueT) 0, & zero_address_frag);
11386	}
11387
11388      return GOT_symbol;
11389    }
11390#endif
11391
11392  return 0;
11393}
11394
11395/* Subroutine of md_apply_fix.	 Check to see if an immediate can be
11396   computed as two separate immediate values, added together.  We
11397   already know that this value cannot be computed by just one ARM
11398   instruction.	 */
11399
11400static unsigned int
11401validate_immediate_twopart (unsigned int   val,
11402			    unsigned int * highpart)
11403{
11404  unsigned int a;
11405  unsigned int i;
11406
11407  for (i = 0; i < 32; i += 2)
11408    if (((a = rotate_left (val, i)) & 0xff) != 0)
11409      {
11410	if (a & 0xff00)
11411	  {
11412	    if (a & ~ 0xffff)
11413	      continue;
11414	    * highpart = (a  >> 8) | ((i + 24) << 7);
11415	  }
11416	else if (a & 0xff0000)
11417	  {
11418	    if (a & 0xff000000)
11419	      continue;
11420	    * highpart = (a >> 16) | ((i + 16) << 7);
11421	  }
11422	else
11423	  {
11424	    assert (a & 0xff000000);
11425	    * highpart = (a >> 24) | ((i + 8) << 7);
11426	  }
11427
11428	return (a & 0xff) | (i << 7);
11429      }
11430
11431  return FAIL;
11432}
11433
11434static int
11435validate_offset_imm (unsigned int val, int hwse)
11436{
11437  if ((hwse && val > 255) || val > 4095)
11438    return FAIL;
11439  return val;
11440}
11441
11442/* Subroutine of md_apply_fix.	 Do those data_ops which can take a
11443   negative immediate constant by altering the instruction.  A bit of
11444   a hack really.
11445	MOV <-> MVN
11446	AND <-> BIC
11447	ADC <-> SBC
11448	by inverting the second operand, and
11449	ADD <-> SUB
11450	CMP <-> CMN
11451	by negating the second operand.	 */
11452
11453static int
11454negate_data_op (unsigned long * instruction,
11455		unsigned long	value)
11456{
11457  int op, new_inst;
11458  unsigned long negated, inverted;
11459
11460  negated = encode_arm_immediate (-value);
11461  inverted = encode_arm_immediate (~value);
11462
11463  op = (*instruction >> DATA_OP_SHIFT) & 0xf;
11464  switch (op)
11465    {
11466      /* First negates.	 */
11467    case OPCODE_SUB:		 /* ADD <-> SUB	 */
11468      new_inst = OPCODE_ADD;
11469      value = negated;
11470      break;
11471
11472    case OPCODE_ADD:
11473      new_inst = OPCODE_SUB;
11474      value = negated;
11475      break;
11476
11477    case OPCODE_CMP:		 /* CMP <-> CMN	 */
11478      new_inst = OPCODE_CMN;
11479      value = negated;
11480      break;
11481
11482    case OPCODE_CMN:
11483      new_inst = OPCODE_CMP;
11484      value = negated;
11485      break;
11486
11487      /* Now Inverted ops.  */
11488    case OPCODE_MOV:		 /* MOV <-> MVN	 */
11489      new_inst = OPCODE_MVN;
11490      value = inverted;
11491      break;
11492
11493    case OPCODE_MVN:
11494      new_inst = OPCODE_MOV;
11495      value = inverted;
11496      break;
11497
11498    case OPCODE_AND:		 /* AND <-> BIC	 */
11499      new_inst = OPCODE_BIC;
11500      value = inverted;
11501      break;
11502
11503    case OPCODE_BIC:
11504      new_inst = OPCODE_AND;
11505      value = inverted;
11506      break;
11507
11508    case OPCODE_ADC:		  /* ADC <-> SBC  */
11509      new_inst = OPCODE_SBC;
11510      value = inverted;
11511      break;
11512
11513    case OPCODE_SBC:
11514      new_inst = OPCODE_ADC;
11515      value = inverted;
11516      break;
11517
11518      /* We cannot do anything.	 */
11519    default:
11520      return FAIL;
11521    }
11522
11523  if (value == (unsigned) FAIL)
11524    return FAIL;
11525
11526  *instruction &= OPCODE_MASK;
11527  *instruction |= new_inst << DATA_OP_SHIFT;
11528  return value;
11529}
11530
11531/* Like negate_data_op, but for Thumb-2.   */
11532
11533static unsigned int
11534thumb32_negate_data_op (offsetT *instruction, offsetT value)
11535{
11536  int op, new_inst;
11537  int rd;
11538  offsetT negated, inverted;
11539
11540  negated = encode_thumb32_immediate (-value);
11541  inverted = encode_thumb32_immediate (~value);
11542
11543  rd = (*instruction >> 8) & 0xf;
11544  op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
11545  switch (op)
11546    {
11547      /* ADD <-> SUB.  Includes CMP <-> CMN.  */
11548    case T2_OPCODE_SUB:
11549      new_inst = T2_OPCODE_ADD;
11550      value = negated;
11551      break;
11552
11553    case T2_OPCODE_ADD:
11554      new_inst = T2_OPCODE_SUB;
11555      value = negated;
11556      break;
11557
11558      /* ORR <-> ORN.  Includes MOV <-> MVN.  */
11559    case T2_OPCODE_ORR:
11560      new_inst = T2_OPCODE_ORN;
11561      value = inverted;
11562      break;
11563
11564    case T2_OPCODE_ORN:
11565      new_inst = T2_OPCODE_ORR;
11566      value = inverted;
11567      break;
11568
11569      /* AND <-> BIC.  TST has no inverted equivalent.  */
11570    case T2_OPCODE_AND:
11571      new_inst = T2_OPCODE_BIC;
11572      if (rd == 15)
11573	value = FAIL;
11574      else
11575	value = inverted;
11576      break;
11577
11578    case T2_OPCODE_BIC:
11579      new_inst = T2_OPCODE_AND;
11580      value = inverted;
11581      break;
11582
11583      /* ADC <-> SBC  */
11584    case T2_OPCODE_ADC:
11585      new_inst = T2_OPCODE_SBC;
11586      value = inverted;
11587      break;
11588
11589    case T2_OPCODE_SBC:
11590      new_inst = T2_OPCODE_ADC;
11591      value = inverted;
11592      break;
11593
11594      /* We cannot do anything.	 */
11595    default:
11596      return FAIL;
11597    }
11598
11599  if (value == FAIL)
11600    return FAIL;
11601
11602  *instruction &= T2_OPCODE_MASK;
11603  *instruction |= new_inst << T2_DATA_OP_SHIFT;
11604  return value;
11605}
11606
11607/* Read a 32-bit thumb instruction from buf.  */
11608static unsigned long
11609get_thumb32_insn (char * buf)
11610{
11611  unsigned long insn;
11612  insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
11613  insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
11614
11615  return insn;
11616}
11617
11618
11619/* We usually want to set the low bit on the address of thumb function
11620   symbols.  In particular .word foo - . should have the low bit set.
11621   Generic code tries to fold the difference of two symbols to
11622   a constant.  Prevent this and force a relocation when the first symbols
11623   is a thumb function.  */
11624int
11625arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
11626{
11627  if (op == O_subtract
11628      && l->X_op == O_symbol
11629      && r->X_op == O_symbol
11630      && THUMB_IS_FUNC (l->X_add_symbol))
11631    {
11632      l->X_op = O_subtract;
11633      l->X_op_symbol = r->X_add_symbol;
11634      l->X_add_number -= r->X_add_number;
11635      return 1;
11636    }
11637  /* Process as normal.  */
11638  return 0;
11639}
11640
11641void
11642md_apply_fix (fixS *	fixP,
11643	       valueT * valP,
11644	       segT	seg)
11645{
11646  offsetT	 value = * valP;
11647  offsetT	 newval;
11648  unsigned int	 newimm;
11649  unsigned long	 temp;
11650  int		 sign;
11651  char *	 buf = fixP->fx_where + fixP->fx_frag->fr_literal;
11652
11653  assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
11654
11655  /* Note whether this will delete the relocation.  */
11656  if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
11657    fixP->fx_done = 1;
11658
11659  /* On a 64-bit host, silently truncate 'value' to 32 bits for
11660     consistency with the behavior on 32-bit hosts.  Remember value
11661     for emit_reloc.  */
11662  value &= 0xffffffff;
11663  value ^= 0x80000000;
11664  value -= 0x80000000;
11665
11666  *valP = value;
11667  fixP->fx_addnumber = value;
11668
11669  /* Same treatment for fixP->fx_offset.  */
11670  fixP->fx_offset &= 0xffffffff;
11671  fixP->fx_offset ^= 0x80000000;
11672  fixP->fx_offset -= 0x80000000;
11673
11674  switch (fixP->fx_r_type)
11675    {
11676    case BFD_RELOC_NONE:
11677      /* This will need to go in the object file.  */
11678      fixP->fx_done = 0;
11679      break;
11680
11681    case BFD_RELOC_ARM_IMMEDIATE:
11682      /* We claim that this fixup has been processed here,
11683	 even if in fact we generate an error because we do
11684	 not have a reloc for it, so tc_gen_reloc will reject it.  */
11685      fixP->fx_done = 1;
11686
11687      if (fixP->fx_addsy
11688	  && ! S_IS_DEFINED (fixP->fx_addsy))
11689	{
11690	  as_bad_where (fixP->fx_file, fixP->fx_line,
11691			_("undefined symbol %s used as an immediate value"),
11692			S_GET_NAME (fixP->fx_addsy));
11693	  break;
11694	}
11695
11696      newimm = encode_arm_immediate (value);
11697      temp = md_chars_to_number (buf, INSN_SIZE);
11698
11699      /* If the instruction will fail, see if we can fix things up by
11700	 changing the opcode.  */
11701      if (newimm == (unsigned int) FAIL
11702	  && (newimm = negate_data_op (&temp, value)) == (unsigned int) FAIL)
11703	{
11704	  as_bad_where (fixP->fx_file, fixP->fx_line,
11705			_("invalid constant (%lx) after fixup"),
11706			(unsigned long) value);
11707	  break;
11708	}
11709
11710      newimm |= (temp & 0xfffff000);
11711      md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
11712      break;
11713
11714    case BFD_RELOC_ARM_ADRL_IMMEDIATE:
11715      {
11716	unsigned int highpart = 0;
11717	unsigned int newinsn  = 0xe1a00000; /* nop.  */
11718
11719	newimm = encode_arm_immediate (value);
11720	temp = md_chars_to_number (buf, INSN_SIZE);
11721
11722	/* If the instruction will fail, see if we can fix things up by
11723	   changing the opcode.	 */
11724	if (newimm == (unsigned int) FAIL
11725	    && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
11726	  {
11727	    /* No ?  OK - try using two ADD instructions to generate
11728	       the value.  */
11729	    newimm = validate_immediate_twopart (value, & highpart);
11730
11731	    /* Yes - then make sure that the second instruction is
11732	       also an add.  */
11733	    if (newimm != (unsigned int) FAIL)
11734	      newinsn = temp;
11735	    /* Still No ?  Try using a negated value.  */
11736	    else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
11737	      temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
11738	    /* Otherwise - give up.  */
11739	    else
11740	      {
11741		as_bad_where (fixP->fx_file, fixP->fx_line,
11742			      _("unable to compute ADRL instructions for PC offset of 0x%lx"),
11743			      (long) value);
11744		break;
11745	      }
11746
11747	    /* Replace the first operand in the 2nd instruction (which
11748	       is the PC) with the destination register.  We have
11749	       already added in the PC in the first instruction and we
11750	       do not want to do it again.  */
11751	    newinsn &= ~ 0xf0000;
11752	    newinsn |= ((newinsn & 0x0f000) << 4);
11753	  }
11754
11755	newimm |= (temp & 0xfffff000);
11756	md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
11757
11758	highpart |= (newinsn & 0xfffff000);
11759	md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
11760      }
11761      break;
11762
11763    case BFD_RELOC_ARM_OFFSET_IMM:
11764      if (!fixP->fx_done && seg->use_rela_p)
11765	value = 0;
11766
11767    case BFD_RELOC_ARM_LITERAL:
11768      sign = value >= 0;
11769
11770      if (value < 0)
11771	value = - value;
11772
11773      if (validate_offset_imm (value, 0) == FAIL)
11774	{
11775	  if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
11776	    as_bad_where (fixP->fx_file, fixP->fx_line,
11777			  _("invalid literal constant: pool needs to be closer"));
11778	  else
11779	    as_bad_where (fixP->fx_file, fixP->fx_line,
11780			  _("bad immediate value for offset (%ld)"),
11781			  (long) value);
11782	  break;
11783	}
11784
11785      newval = md_chars_to_number (buf, INSN_SIZE);
11786      newval &= 0xff7ff000;
11787      newval |= value | (sign ? INDEX_UP : 0);
11788      md_number_to_chars (buf, newval, INSN_SIZE);
11789      break;
11790
11791    case BFD_RELOC_ARM_OFFSET_IMM8:
11792    case BFD_RELOC_ARM_HWLITERAL:
11793      sign = value >= 0;
11794
11795      if (value < 0)
11796	value = - value;
11797
11798      if (validate_offset_imm (value, 1) == FAIL)
11799	{
11800	  if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
11801	    as_bad_where (fixP->fx_file, fixP->fx_line,
11802			  _("invalid literal constant: pool needs to be closer"));
11803	  else
11804	    as_bad (_("bad immediate value for half-word offset (%ld)"),
11805		    (long) value);
11806	  break;
11807	}
11808
11809      newval = md_chars_to_number (buf, INSN_SIZE);
11810      newval &= 0xff7ff0f0;
11811      newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
11812      md_number_to_chars (buf, newval, INSN_SIZE);
11813      break;
11814
11815    case BFD_RELOC_ARM_T32_OFFSET_U8:
11816      if (value < 0 || value > 1020 || value % 4 != 0)
11817	as_bad_where (fixP->fx_file, fixP->fx_line,
11818		      _("bad immediate value for offset (%ld)"), (long) value);
11819      value /= 4;
11820
11821      newval = md_chars_to_number (buf+2, THUMB_SIZE);
11822      newval |= value;
11823      md_number_to_chars (buf+2, newval, THUMB_SIZE);
11824      break;
11825
11826    case BFD_RELOC_ARM_T32_OFFSET_IMM:
11827      /* This is a complicated relocation used for all varieties of Thumb32
11828	 load/store instruction with immediate offset:
11829
11830	 1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
11831	                                           *4, optional writeback(W)
11832						   (doubleword load/store)
11833
11834	 1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
11835	 1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
11836	 1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
11837	 1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
11838	 1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
11839
11840	 Uppercase letters indicate bits that are already encoded at
11841	 this point.  Lowercase letters are our problem.  For the
11842	 second block of instructions, the secondary opcode nybble
11843	 (bits 8..11) is present, and bit 23 is zero, even if this is
11844	 a PC-relative operation.  */
11845      newval = md_chars_to_number (buf, THUMB_SIZE);
11846      newval <<= 16;
11847      newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
11848
11849      if ((newval & 0xf0000000) == 0xe0000000)
11850	{
11851	  /* Doubleword load/store: 8-bit offset, scaled by 4.  */
11852	  if (value >= 0)
11853	    newval |= (1 << 23);
11854	  else
11855	    value = -value;
11856	  if (value % 4 != 0)
11857	    {
11858	      as_bad_where (fixP->fx_file, fixP->fx_line,
11859			    _("offset not a multiple of 4"));
11860	      break;
11861	    }
11862	  value /= 4;
11863	  if (value > 0xff)
11864	    {
11865	      as_bad_where (fixP->fx_file, fixP->fx_line,
11866			    _("offset out of range"));
11867	      break;
11868	    }
11869	  newval &= ~0xff;
11870	}
11871      else if ((newval & 0x000f0000) == 0x000f0000)
11872	{
11873	  /* PC-relative, 12-bit offset.  */
11874	  if (value >= 0)
11875	    newval |= (1 << 23);
11876	  else
11877	    value = -value;
11878	  if (value > 0xfff)
11879	    {
11880	      as_bad_where (fixP->fx_file, fixP->fx_line,
11881			    _("offset out of range"));
11882	      break;
11883	    }
11884	  newval &= ~0xfff;
11885	}
11886      else if ((newval & 0x00000100) == 0x00000100)
11887	{
11888	  /* Writeback: 8-bit, +/- offset.  */
11889	  if (value >= 0)
11890	    newval |= (1 << 9);
11891	  else
11892	    value = -value;
11893	  if (value > 0xff)
11894	    {
11895	      as_bad_where (fixP->fx_file, fixP->fx_line,
11896			    _("offset out of range"));
11897	      break;
11898	    }
11899	  newval &= ~0xff;
11900	}
11901      else if ((newval & 0x00000f00) == 0x00000e00)
11902	{
11903	  /* T-instruction: positive 8-bit offset.  */
11904	  if (value < 0 || value > 0xff)
11905	    {
11906	      as_bad_where (fixP->fx_file, fixP->fx_line,
11907			    _("offset out of range"));
11908	      break;
11909	    }
11910	  newval &= ~0xff;
11911	  newval |= value;
11912	}
11913      else
11914	{
11915	  /* Positive 12-bit or negative 8-bit offset.  */
11916	  int limit;
11917	  if (value >= 0)
11918	    {
11919	      newval |= (1 << 23);
11920	      limit = 0xfff;
11921	    }
11922	  else
11923	    {
11924	      value = -value;
11925	      limit = 0xff;
11926	    }
11927	  if (value > limit)
11928	    {
11929	      as_bad_where (fixP->fx_file, fixP->fx_line,
11930			    _("offset out of range"));
11931	      break;
11932	    }
11933	  newval &= ~limit;
11934	}
11935
11936      newval |= value;
11937      md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
11938      md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
11939      break;
11940
11941    case BFD_RELOC_ARM_SHIFT_IMM:
11942      newval = md_chars_to_number (buf, INSN_SIZE);
11943      if (((unsigned long) value) > 32
11944	  || (value == 32
11945	      && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
11946	{
11947	  as_bad_where (fixP->fx_file, fixP->fx_line,
11948			_("shift expression is too large"));
11949	  break;
11950	}
11951
11952      if (value == 0)
11953	/* Shifts of zero must be done as lsl.	*/
11954	newval &= ~0x60;
11955      else if (value == 32)
11956	value = 0;
11957      newval &= 0xfffff07f;
11958      newval |= (value & 0x1f) << 7;
11959      md_number_to_chars (buf, newval, INSN_SIZE);
11960      break;
11961
11962    case BFD_RELOC_ARM_T32_IMMEDIATE:
11963    case BFD_RELOC_ARM_T32_IMM12:
11964    case BFD_RELOC_ARM_T32_ADD_PC12:
11965      /* We claim that this fixup has been processed here,
11966	 even if in fact we generate an error because we do
11967	 not have a reloc for it, so tc_gen_reloc will reject it.  */
11968      fixP->fx_done = 1;
11969
11970      if (fixP->fx_addsy
11971	  && ! S_IS_DEFINED (fixP->fx_addsy))
11972	{
11973	  as_bad_where (fixP->fx_file, fixP->fx_line,
11974			_("undefined symbol %s used as an immediate value"),
11975			S_GET_NAME (fixP->fx_addsy));
11976	  break;
11977	}
11978
11979      newval = md_chars_to_number (buf, THUMB_SIZE);
11980      newval <<= 16;
11981      newval |= md_chars_to_number (buf+2, THUMB_SIZE);
11982
11983      /* FUTURE: Implement analogue of negate_data_op for T32.  */
11984      if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE)
11985	{
11986	  newimm = encode_thumb32_immediate (value);
11987	  if (newimm == (unsigned int) FAIL)
11988	    newimm = thumb32_negate_data_op (&newval, value);
11989	}
11990      else
11991	{
11992	  /* 12 bit immediate for addw/subw.  */
11993	  if (value < 0)
11994	    {
11995	      value = -value;
11996	      newval ^= 0x00a00000;
11997	    }
11998	  if (value > 0xfff)
11999	    newimm = (unsigned int) FAIL;
12000	  else
12001	    newimm = value;
12002	}
12003
12004      if (newimm == (unsigned int)FAIL)
12005	{
12006	  as_bad_where (fixP->fx_file, fixP->fx_line,
12007			_("invalid constant (%lx) after fixup"),
12008			(unsigned long) value);
12009	  break;
12010	}
12011
12012      newval |= (newimm & 0x800) << 15;
12013      newval |= (newimm & 0x700) << 4;
12014      newval |= (newimm & 0x0ff);
12015
12016      md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
12017      md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
12018      break;
12019
12020    case BFD_RELOC_ARM_SMC:
12021      if (((unsigned long) value) > 0xffff)
12022	as_bad_where (fixP->fx_file, fixP->fx_line,
12023		      _("invalid smc expression"));
12024      newval = md_chars_to_number (buf, INSN_SIZE);
12025      newval |= (value & 0xf) | ((value & 0xfff0) << 4);
12026      md_number_to_chars (buf, newval, INSN_SIZE);
12027      break;
12028
12029    case BFD_RELOC_ARM_SWI:
12030      if (fixP->tc_fix_data != 0)
12031	{
12032	  if (((unsigned long) value) > 0xff)
12033	    as_bad_where (fixP->fx_file, fixP->fx_line,
12034			  _("invalid swi expression"));
12035	  newval = md_chars_to_number (buf, THUMB_SIZE);
12036	  newval |= value;
12037	  md_number_to_chars (buf, newval, THUMB_SIZE);
12038	}
12039      else
12040	{
12041	  if (((unsigned long) value) > 0x00ffffff)
12042	    as_bad_where (fixP->fx_file, fixP->fx_line,
12043			  _("invalid swi expression"));
12044	  newval = md_chars_to_number (buf, INSN_SIZE);
12045	  newval |= value;
12046	  md_number_to_chars (buf, newval, INSN_SIZE);
12047	}
12048      break;
12049
12050    case BFD_RELOC_ARM_MULTI:
12051      if (((unsigned long) value) > 0xffff)
12052	as_bad_where (fixP->fx_file, fixP->fx_line,
12053		      _("invalid expression in load/store multiple"));
12054      newval = value | md_chars_to_number (buf, INSN_SIZE);
12055      md_number_to_chars (buf, newval, INSN_SIZE);
12056      break;
12057
12058#ifdef OBJ_ELF
12059    case BFD_RELOC_ARM_PCREL_CALL:
12060      newval = md_chars_to_number (buf, INSN_SIZE);
12061      if ((newval & 0xf0000000) == 0xf0000000)
12062	temp = 1;
12063      else
12064	temp = 3;
12065      goto arm_branch_common;
12066
12067    case BFD_RELOC_ARM_PCREL_JUMP:
12068    case BFD_RELOC_ARM_PLT32:
12069#endif
12070    case BFD_RELOC_ARM_PCREL_BRANCH:
12071      temp = 3;
12072      goto arm_branch_common;
12073
12074    case BFD_RELOC_ARM_PCREL_BLX:
12075      temp = 1;
12076    arm_branch_common:
12077      /* We are going to store value (shifted right by two) in the
12078	 instruction, in a 24 bit, signed field.  Bits 26 through 32 either
12079	 all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
12080	 also be be clear.  */
12081      if (value & temp)
12082	as_bad_where (fixP->fx_file, fixP->fx_line,
12083		      _("misaligned branch destination"));
12084      if ((value & (offsetT)0xfe000000) != (offsetT)0
12085	  && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
12086	as_bad_where (fixP->fx_file, fixP->fx_line,
12087		      _("branch out of range"));
12088
12089      if (fixP->fx_done || !seg->use_rela_p)
12090	{
12091	  newval = md_chars_to_number (buf, INSN_SIZE);
12092	  newval |= (value >> 2) & 0x00ffffff;
12093	  /* Set the H bit on BLX instructions.  */
12094	  if (temp == 1)
12095	    {
12096	      if (value & 2)
12097		newval |= 0x01000000;
12098	      else
12099		newval &= ~0x01000000;
12100	    }
12101	  md_number_to_chars (buf, newval, INSN_SIZE);
12102	}
12103      break;
12104
12105    case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CZB */
12106      /* CZB can only branch forward.  */
12107      if (value & ~0x7e)
12108	as_bad_where (fixP->fx_file, fixP->fx_line,
12109		      _("branch out of range"));
12110
12111      if (fixP->fx_done || !seg->use_rela_p)
12112	{
12113	  newval = md_chars_to_number (buf, THUMB_SIZE);
12114	  newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
12115	  md_number_to_chars (buf, newval, THUMB_SIZE);
12116	}
12117      break;
12118
12119    case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.	*/
12120      if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
12121	as_bad_where (fixP->fx_file, fixP->fx_line,
12122		      _("branch out of range"));
12123
12124      if (fixP->fx_done || !seg->use_rela_p)
12125	{
12126	  newval = md_chars_to_number (buf, THUMB_SIZE);
12127	  newval |= (value & 0x1ff) >> 1;
12128	  md_number_to_chars (buf, newval, THUMB_SIZE);
12129	}
12130      break;
12131
12132    case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
12133      if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
12134	as_bad_where (fixP->fx_file, fixP->fx_line,
12135		      _("branch out of range"));
12136
12137      if (fixP->fx_done || !seg->use_rela_p)
12138	{
12139	  newval = md_chars_to_number (buf, THUMB_SIZE);
12140	  newval |= (value & 0xfff) >> 1;
12141	  md_number_to_chars (buf, newval, THUMB_SIZE);
12142	}
12143      break;
12144
12145    case BFD_RELOC_THUMB_PCREL_BRANCH20:
12146      if ((value & ~0x1fffff) && ((value & ~0x1fffff) != ~0x1fffff))
12147	as_bad_where (fixP->fx_file, fixP->fx_line,
12148		      _("conditional branch out of range"));
12149
12150      if (fixP->fx_done || !seg->use_rela_p)
12151	{
12152	  offsetT newval2;
12153	  addressT S, J1, J2, lo, hi;
12154
12155	  S  = (value & 0x00100000) >> 20;
12156	  J2 = (value & 0x00080000) >> 19;
12157	  J1 = (value & 0x00040000) >> 18;
12158	  hi = (value & 0x0003f000) >> 12;
12159	  lo = (value & 0x00000ffe) >> 1;
12160
12161	  newval   = md_chars_to_number (buf, THUMB_SIZE);
12162	  newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
12163	  newval  |= (S << 10) | hi;
12164	  newval2 |= (J1 << 13) | (J2 << 11) | lo;
12165	  md_number_to_chars (buf, newval, THUMB_SIZE);
12166	  md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
12167	}
12168      break;
12169
12170    case BFD_RELOC_THUMB_PCREL_BLX:
12171    case BFD_RELOC_THUMB_PCREL_BRANCH23:
12172      if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
12173	as_bad_where (fixP->fx_file, fixP->fx_line,
12174		      _("branch out of range"));
12175
12176      if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
12177	/* For a BLX instruction, make sure that the relocation is rounded up
12178	   to a word boundary.  This follows the semantics of the instruction
12179	   which specifies that bit 1 of the target address will come from bit
12180	   1 of the base address.  */
12181	value = (value + 1) & ~ 1;
12182
12183      if (fixP->fx_done || !seg->use_rela_p)
12184	{
12185	  offsetT newval2;
12186
12187	  newval   = md_chars_to_number (buf, THUMB_SIZE);
12188	  newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
12189	  newval  |= (value & 0x7fffff) >> 12;
12190	  newval2 |= (value & 0xfff) >> 1;
12191	  md_number_to_chars (buf, newval, THUMB_SIZE);
12192	  md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
12193	}
12194      break;
12195
12196    case BFD_RELOC_THUMB_PCREL_BRANCH25:
12197      if ((value & ~0x1ffffff) && ((value & ~0x1ffffff) != ~0x1ffffff))
12198	as_bad_where (fixP->fx_file, fixP->fx_line,
12199		      _("branch out of range"));
12200
12201      if (fixP->fx_done || !seg->use_rela_p)
12202	{
12203	  offsetT newval2;
12204	  addressT S, I1, I2, lo, hi;
12205
12206	  S  = (value & 0x01000000) >> 24;
12207	  I1 = (value & 0x00800000) >> 23;
12208	  I2 = (value & 0x00400000) >> 22;
12209	  hi = (value & 0x003ff000) >> 12;
12210	  lo = (value & 0x00000ffe) >> 1;
12211
12212	  I1 = !(I1 ^ S);
12213	  I2 = !(I2 ^ S);
12214
12215	  newval   = md_chars_to_number (buf, THUMB_SIZE);
12216	  newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
12217	  newval  |= (S << 10) | hi;
12218	  newval2 |= (I1 << 13) | (I2 << 11) | lo;
12219	  md_number_to_chars (buf, newval, THUMB_SIZE);
12220	  md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
12221	}
12222      break;
12223
12224    case BFD_RELOC_8:
12225      if (fixP->fx_done || !seg->use_rela_p)
12226	md_number_to_chars (buf, value, 1);
12227      break;
12228
12229    case BFD_RELOC_16:
12230      if (fixP->fx_done || !seg->use_rela_p)
12231	md_number_to_chars (buf, value, 2);
12232      break;
12233
12234#ifdef OBJ_ELF
12235    case BFD_RELOC_ARM_TLS_GD32:
12236    case BFD_RELOC_ARM_TLS_LE32:
12237    case BFD_RELOC_ARM_TLS_IE32:
12238    case BFD_RELOC_ARM_TLS_LDM32:
12239    case BFD_RELOC_ARM_TLS_LDO32:
12240      S_SET_THREAD_LOCAL (fixP->fx_addsy);
12241      /* fall through */
12242
12243    case BFD_RELOC_ARM_GOT32:
12244    case BFD_RELOC_ARM_GOTOFF:
12245    case BFD_RELOC_ARM_TARGET2:
12246      if (fixP->fx_done || !seg->use_rela_p)
12247	md_number_to_chars (buf, 0, 4);
12248      break;
12249#endif
12250
12251    case BFD_RELOC_RVA:
12252    case BFD_RELOC_32:
12253    case BFD_RELOC_ARM_TARGET1:
12254    case BFD_RELOC_ARM_ROSEGREL32:
12255    case BFD_RELOC_ARM_SBREL32:
12256    case BFD_RELOC_32_PCREL:
12257      if (fixP->fx_done || !seg->use_rela_p)
12258	md_number_to_chars (buf, value, 4);
12259      break;
12260
12261#ifdef OBJ_ELF
12262    case BFD_RELOC_ARM_PREL31:
12263      if (fixP->fx_done || !seg->use_rela_p)
12264	{
12265	  newval = md_chars_to_number (buf, 4) & 0x80000000;
12266	  if ((value ^ (value >> 1)) & 0x40000000)
12267	    {
12268	      as_bad_where (fixP->fx_file, fixP->fx_line,
12269			    _("rel31 relocation overflow"));
12270	    }
12271	  newval |= value & 0x7fffffff;
12272	  md_number_to_chars (buf, newval, 4);
12273	}
12274      break;
12275#endif
12276
12277    case BFD_RELOC_ARM_CP_OFF_IMM:
12278    case BFD_RELOC_ARM_T32_CP_OFF_IMM:
12279      if (value < -1023 || value > 1023 || (value & 3))
12280	as_bad_where (fixP->fx_file, fixP->fx_line,
12281		      _("co-processor offset out of range"));
12282    cp_off_common:
12283      sign = value >= 0;
12284      if (value < 0)
12285	value = -value;
12286      if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
12287	  || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
12288	newval = md_chars_to_number (buf, INSN_SIZE);
12289      else
12290	newval = get_thumb32_insn (buf);
12291      newval &= 0xff7fff00;
12292      newval |= (value >> 2) | (sign ? INDEX_UP : 0);
12293      if (value == 0)
12294	newval &= ~WRITE_BACK;
12295      if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
12296	  || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
12297	md_number_to_chars (buf, newval, INSN_SIZE);
12298      else
12299	put_thumb32_insn (buf, newval);
12300      break;
12301
12302    case BFD_RELOC_ARM_CP_OFF_IMM_S2:
12303    case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
12304      if (value < -255 || value > 255)
12305	as_bad_where (fixP->fx_file, fixP->fx_line,
12306		      _("co-processor offset out of range"));
12307      value *= 4;
12308      goto cp_off_common;
12309
12310    case BFD_RELOC_ARM_THUMB_OFFSET:
12311      newval = md_chars_to_number (buf, THUMB_SIZE);
12312      /* Exactly what ranges, and where the offset is inserted depends
12313	 on the type of instruction, we can establish this from the
12314	 top 4 bits.  */
12315      switch (newval >> 12)
12316	{
12317	case 4: /* PC load.  */
12318	  /* Thumb PC loads are somewhat odd, bit 1 of the PC is
12319	     forced to zero for these loads; md_pcrel_from has already
12320	     compensated for this.  */
12321	  if (value & 3)
12322	    as_bad_where (fixP->fx_file, fixP->fx_line,
12323			  _("invalid offset, target not word aligned (0x%08lX)"),
12324			  (((unsigned long) fixP->fx_frag->fr_address
12325			    + (unsigned long) fixP->fx_where) & ~3)
12326			  + (unsigned long) value);
12327
12328	  if (value & ~0x3fc)
12329	    as_bad_where (fixP->fx_file, fixP->fx_line,
12330			  _("invalid offset, value too big (0x%08lX)"),
12331			  (long) value);
12332
12333	  newval |= value >> 2;
12334	  break;
12335
12336	case 9: /* SP load/store.  */
12337	  if (value & ~0x3fc)
12338	    as_bad_where (fixP->fx_file, fixP->fx_line,
12339			  _("invalid offset, value too big (0x%08lX)"),
12340			  (long) value);
12341	  newval |= value >> 2;
12342	  break;
12343
12344	case 6: /* Word load/store.  */
12345	  if (value & ~0x7c)
12346	    as_bad_where (fixP->fx_file, fixP->fx_line,
12347			  _("invalid offset, value too big (0x%08lX)"),
12348			  (long) value);
12349	  newval |= value << 4; /* 6 - 2.  */
12350	  break;
12351
12352	case 7: /* Byte load/store.  */
12353	  if (value & ~0x1f)
12354	    as_bad_where (fixP->fx_file, fixP->fx_line,
12355			  _("invalid offset, value too big (0x%08lX)"),
12356			  (long) value);
12357	  newval |= value << 6;
12358	  break;
12359
12360	case 8: /* Halfword load/store.	 */
12361	  if (value & ~0x3e)
12362	    as_bad_where (fixP->fx_file, fixP->fx_line,
12363			  _("invalid offset, value too big (0x%08lX)"),
12364			  (long) value);
12365	  newval |= value << 5; /* 6 - 1.  */
12366	  break;
12367
12368	default:
12369	  as_bad_where (fixP->fx_file, fixP->fx_line,
12370			"Unable to process relocation for thumb opcode: %lx",
12371			(unsigned long) newval);
12372	  break;
12373	}
12374      md_number_to_chars (buf, newval, THUMB_SIZE);
12375      break;
12376
12377    case BFD_RELOC_ARM_THUMB_ADD:
12378      /* This is a complicated relocation, since we use it for all of
12379	 the following immediate relocations:
12380
12381	    3bit ADD/SUB
12382	    8bit ADD/SUB
12383	    9bit ADD/SUB SP word-aligned
12384	   10bit ADD PC/SP word-aligned
12385
12386	 The type of instruction being processed is encoded in the
12387	 instruction field:
12388
12389	   0x8000  SUB
12390	   0x00F0  Rd
12391	   0x000F  Rs
12392      */
12393      newval = md_chars_to_number (buf, THUMB_SIZE);
12394      {
12395	int rd = (newval >> 4) & 0xf;
12396	int rs = newval & 0xf;
12397	int subtract = !!(newval & 0x8000);
12398
12399	/* Check for HI regs, only very restricted cases allowed:
12400	   Adjusting SP, and using PC or SP to get an address.	*/
12401	if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
12402	    || (rs > 7 && rs != REG_SP && rs != REG_PC))
12403	  as_bad_where (fixP->fx_file, fixP->fx_line,
12404			_("invalid Hi register with immediate"));
12405
12406	/* If value is negative, choose the opposite instruction.  */
12407	if (value < 0)
12408	  {
12409	    value = -value;
12410	    subtract = !subtract;
12411	    if (value < 0)
12412	      as_bad_where (fixP->fx_file, fixP->fx_line,
12413			    _("immediate value out of range"));
12414	  }
12415
12416	if (rd == REG_SP)
12417	  {
12418	    if (value & ~0x1fc)
12419	      as_bad_where (fixP->fx_file, fixP->fx_line,
12420			    _("invalid immediate for stack address calculation"));
12421	    newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
12422	    newval |= value >> 2;
12423	  }
12424	else if (rs == REG_PC || rs == REG_SP)
12425	  {
12426	    if (subtract || value & ~0x3fc)
12427	      as_bad_where (fixP->fx_file, fixP->fx_line,
12428			    _("invalid immediate for address calculation (value = 0x%08lX)"),
12429			    (unsigned long) value);
12430	    newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
12431	    newval |= rd << 8;
12432	    newval |= value >> 2;
12433	  }
12434	else if (rs == rd)
12435	  {
12436	    if (value & ~0xff)
12437	      as_bad_where (fixP->fx_file, fixP->fx_line,
12438			    _("immediate value out of range"));
12439	    newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
12440	    newval |= (rd << 8) | value;
12441	  }
12442	else
12443	  {
12444	    if (value & ~0x7)
12445	      as_bad_where (fixP->fx_file, fixP->fx_line,
12446			    _("immediate value out of range"));
12447	    newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
12448	    newval |= rd | (rs << 3) | (value << 6);
12449	  }
12450      }
12451      md_number_to_chars (buf, newval, THUMB_SIZE);
12452      break;
12453
12454    case BFD_RELOC_ARM_THUMB_IMM:
12455      newval = md_chars_to_number (buf, THUMB_SIZE);
12456      if (value < 0 || value > 255)
12457	as_bad_where (fixP->fx_file, fixP->fx_line,
12458		      _("invalid immediate: %ld is too large"),
12459		      (long) value);
12460      newval |= value;
12461      md_number_to_chars (buf, newval, THUMB_SIZE);
12462      break;
12463
12464    case BFD_RELOC_ARM_THUMB_SHIFT:
12465      /* 5bit shift value (0..32).  LSL cannot take 32.	 */
12466      newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
12467      temp = newval & 0xf800;
12468      if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
12469	as_bad_where (fixP->fx_file, fixP->fx_line,
12470		      _("invalid shift value: %ld"), (long) value);
12471      /* Shifts of zero must be encoded as LSL.	 */
12472      if (value == 0)
12473	newval = (newval & 0x003f) | T_OPCODE_LSL_I;
12474      /* Shifts of 32 are encoded as zero.  */
12475      else if (value == 32)
12476	value = 0;
12477      newval |= value << 6;
12478      md_number_to_chars (buf, newval, THUMB_SIZE);
12479      break;
12480
12481    case BFD_RELOC_VTABLE_INHERIT:
12482    case BFD_RELOC_VTABLE_ENTRY:
12483      fixP->fx_done = 0;
12484      return;
12485
12486    case BFD_RELOC_ARM_MOVW:
12487    case BFD_RELOC_ARM_MOVT:
12488    case BFD_RELOC_ARM_THUMB_MOVW:
12489    case BFD_RELOC_ARM_THUMB_MOVT:
12490      if (fixP->fx_done || !seg->use_rela_p)
12491	{
12492	  /* REL format relocations are limited to a 16-bit addend.  */
12493	  if (!fixP->fx_done)
12494	    {
12495	      if (value < -0x1000 || value > 0xffff)
12496		  as_bad_where (fixP->fx_file, fixP->fx_line,
12497				_("offset too big"));
12498	    }
12499	  else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
12500		   || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
12501	    {
12502	      value >>= 16;
12503	    }
12504
12505	  if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
12506	      || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
12507	    {
12508	      newval = get_thumb32_insn (buf);
12509	      newval &= 0xfbf08f00;
12510	      newval |= (value & 0xf000) << 4;
12511	      newval |= (value & 0x0800) << 15;
12512	      newval |= (value & 0x0700) << 4;
12513	      newval |= (value & 0x00ff);
12514	      put_thumb32_insn (buf, newval);
12515	    }
12516	  else
12517	    {
12518	      newval = md_chars_to_number (buf, 4);
12519	      newval &= 0xfff0f000;
12520	      newval |= value & 0x0fff;
12521	      newval |= (value & 0xf000) << 4;
12522	      md_number_to_chars (buf, newval, 4);
12523	    }
12524	}
12525      return;
12526
12527    case BFD_RELOC_UNUSED:
12528    default:
12529      as_bad_where (fixP->fx_file, fixP->fx_line,
12530		    _("bad relocation fixup type (%d)"), fixP->fx_r_type);
12531    }
12532}
12533
12534/* Translate internal representation of relocation info to BFD target
12535   format.  */
12536
12537arelent *
12538tc_gen_reloc (asection *section, fixS *fixp)
12539{
12540  arelent * reloc;
12541  bfd_reloc_code_real_type code;
12542
12543  reloc = xmalloc (sizeof (arelent));
12544
12545  reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
12546  *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12547  reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12548
12549  if (fixp->fx_pcrel)
12550    {
12551      if (section->use_rela_p)
12552	fixp->fx_offset -= md_pcrel_from_section (fixp, section);
12553      else
12554	fixp->fx_offset = reloc->address;
12555    }
12556  reloc->addend = fixp->fx_offset;
12557
12558  switch (fixp->fx_r_type)
12559    {
12560    case BFD_RELOC_8:
12561      if (fixp->fx_pcrel)
12562	{
12563	  code = BFD_RELOC_8_PCREL;
12564	  break;
12565	}
12566
12567    case BFD_RELOC_16:
12568      if (fixp->fx_pcrel)
12569	{
12570	  code = BFD_RELOC_16_PCREL;
12571	  break;
12572	}
12573
12574    case BFD_RELOC_32:
12575      if (fixp->fx_pcrel)
12576	{
12577	  code = BFD_RELOC_32_PCREL;
12578	  break;
12579	}
12580
12581    case BFD_RELOC_ARM_MOVW:
12582      if (fixp->fx_pcrel)
12583	{
12584	  code = BFD_RELOC_ARM_MOVW_PCREL;
12585	  break;
12586	}
12587
12588    case BFD_RELOC_ARM_MOVT:
12589      if (fixp->fx_pcrel)
12590	{
12591	  code = BFD_RELOC_ARM_MOVT_PCREL;
12592	  break;
12593	}
12594
12595    case BFD_RELOC_ARM_THUMB_MOVW:
12596      if (fixp->fx_pcrel)
12597	{
12598	  code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
12599	  break;
12600	}
12601
12602    case BFD_RELOC_ARM_THUMB_MOVT:
12603      if (fixp->fx_pcrel)
12604	{
12605	  code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
12606	  break;
12607	}
12608
12609    case BFD_RELOC_NONE:
12610    case BFD_RELOC_ARM_PCREL_BRANCH:
12611    case BFD_RELOC_ARM_PCREL_BLX:
12612    case BFD_RELOC_RVA:
12613    case BFD_RELOC_THUMB_PCREL_BRANCH7:
12614    case BFD_RELOC_THUMB_PCREL_BRANCH9:
12615    case BFD_RELOC_THUMB_PCREL_BRANCH12:
12616    case BFD_RELOC_THUMB_PCREL_BRANCH20:
12617    case BFD_RELOC_THUMB_PCREL_BRANCH23:
12618    case BFD_RELOC_THUMB_PCREL_BRANCH25:
12619    case BFD_RELOC_THUMB_PCREL_BLX:
12620    case BFD_RELOC_VTABLE_ENTRY:
12621    case BFD_RELOC_VTABLE_INHERIT:
12622      code = fixp->fx_r_type;
12623      break;
12624
12625    case BFD_RELOC_ARM_LITERAL:
12626    case BFD_RELOC_ARM_HWLITERAL:
12627      /* If this is called then the a literal has
12628	 been referenced across a section boundary.  */
12629      as_bad_where (fixp->fx_file, fixp->fx_line,
12630		    _("literal referenced across section boundary"));
12631      return NULL;
12632
12633#ifdef OBJ_ELF
12634    case BFD_RELOC_ARM_GOT32:
12635    case BFD_RELOC_ARM_GOTOFF:
12636    case BFD_RELOC_ARM_PLT32:
12637    case BFD_RELOC_ARM_TARGET1:
12638    case BFD_RELOC_ARM_ROSEGREL32:
12639    case BFD_RELOC_ARM_SBREL32:
12640    case BFD_RELOC_ARM_PREL31:
12641    case BFD_RELOC_ARM_TARGET2:
12642    case BFD_RELOC_ARM_TLS_LE32:
12643    case BFD_RELOC_ARM_TLS_LDO32:
12644    case BFD_RELOC_ARM_PCREL_CALL:
12645    case BFD_RELOC_ARM_PCREL_JUMP:
12646      code = fixp->fx_r_type;
12647      break;
12648
12649    case BFD_RELOC_ARM_TLS_GD32:
12650    case BFD_RELOC_ARM_TLS_IE32:
12651    case BFD_RELOC_ARM_TLS_LDM32:
12652      /* BFD will include the symbol's address in the addend.
12653	 But we don't want that, so subtract it out again here.  */
12654      if (!S_IS_COMMON (fixp->fx_addsy))
12655	reloc->addend -= (*reloc->sym_ptr_ptr)->value;
12656      code = fixp->fx_r_type;
12657      break;
12658#endif
12659
12660    case BFD_RELOC_ARM_IMMEDIATE:
12661      as_bad_where (fixp->fx_file, fixp->fx_line,
12662		    _("internal relocation (type: IMMEDIATE) not fixed up"));
12663      return NULL;
12664
12665    case BFD_RELOC_ARM_ADRL_IMMEDIATE:
12666      as_bad_where (fixp->fx_file, fixp->fx_line,
12667		    _("ADRL used for a symbol not defined in the same file"));
12668      return NULL;
12669
12670    case BFD_RELOC_ARM_OFFSET_IMM:
12671      if (section->use_rela_p)
12672	{
12673	  code = fixp->fx_r_type;
12674	  break;
12675	}
12676
12677      if (fixp->fx_addsy != NULL
12678	  && !S_IS_DEFINED (fixp->fx_addsy)
12679	  && S_IS_LOCAL (fixp->fx_addsy))
12680	{
12681	  as_bad_where (fixp->fx_file, fixp->fx_line,
12682			_("undefined local label `%s'"),
12683			S_GET_NAME (fixp->fx_addsy));
12684	  return NULL;
12685	}
12686
12687      as_bad_where (fixp->fx_file, fixp->fx_line,
12688		    _("internal_relocation (type: OFFSET_IMM) not fixed up"));
12689      return NULL;
12690
12691    default:
12692      {
12693	char * type;
12694
12695	switch (fixp->fx_r_type)
12696	  {
12697	  case BFD_RELOC_NONE:		   type = "NONE";	  break;
12698	  case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
12699	  case BFD_RELOC_ARM_SHIFT_IMM:	   type = "SHIFT_IMM";	  break;
12700	  case BFD_RELOC_ARM_SMC:	   type = "SMC";	  break;
12701	  case BFD_RELOC_ARM_SWI:	   type = "SWI";	  break;
12702	  case BFD_RELOC_ARM_MULTI:	   type = "MULTI";	  break;
12703	  case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";	  break;
12704	  case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
12705	  case BFD_RELOC_ARM_THUMB_ADD:	   type = "THUMB_ADD";	  break;
12706	  case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
12707	  case BFD_RELOC_ARM_THUMB_IMM:	   type = "THUMB_IMM";	  break;
12708	  case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
12709	  default:			   type = _("<unknown>"); break;
12710	  }
12711	as_bad_where (fixp->fx_file, fixp->fx_line,
12712		      _("cannot represent %s relocation in this object file format"),
12713		      type);
12714	return NULL;
12715      }
12716    }
12717
12718#ifdef OBJ_ELF
12719  if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
12720      && GOT_symbol
12721      && fixp->fx_addsy == GOT_symbol)
12722    {
12723      code = BFD_RELOC_ARM_GOTPC;
12724      reloc->addend = fixp->fx_offset = reloc->address;
12725    }
12726#endif
12727
12728  reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12729
12730  if (reloc->howto == NULL)
12731    {
12732      as_bad_where (fixp->fx_file, fixp->fx_line,
12733		    _("cannot represent %s relocation in this object file format"),
12734		    bfd_get_reloc_code_name (code));
12735      return NULL;
12736    }
12737
12738  /* HACK: Since arm ELF uses Rel instead of Rela, encode the
12739     vtable entry to be used in the relocation's section offset.  */
12740  if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12741    reloc->address = fixp->fx_offset;
12742
12743  return reloc;
12744}
12745
12746/* This fix_new is called by cons via TC_CONS_FIX_NEW.	*/
12747
12748void
12749cons_fix_new_arm (fragS *	frag,
12750		  int		where,
12751		  int		size,
12752		  expressionS * exp)
12753{
12754  bfd_reloc_code_real_type type;
12755  int pcrel = 0;
12756
12757  /* Pick a reloc.
12758     FIXME: @@ Should look at CPU word size.  */
12759  switch (size)
12760    {
12761    case 1:
12762      type = BFD_RELOC_8;
12763      break;
12764    case 2:
12765      type = BFD_RELOC_16;
12766      break;
12767    case 4:
12768    default:
12769      type = BFD_RELOC_32;
12770      break;
12771    case 8:
12772      type = BFD_RELOC_64;
12773      break;
12774    }
12775
12776  fix_new_exp (frag, where, (int) size, exp, pcrel, type);
12777}
12778
12779#if defined OBJ_COFF || defined OBJ_ELF
12780void
12781arm_validate_fix (fixS * fixP)
12782{
12783  /* If the destination of the branch is a defined symbol which does not have
12784     the THUMB_FUNC attribute, then we must be calling a function which has
12785     the (interfacearm) attribute.  We look for the Thumb entry point to that
12786     function and change the branch to refer to that function instead.	*/
12787  if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
12788      && fixP->fx_addsy != NULL
12789      && S_IS_DEFINED (fixP->fx_addsy)
12790      && ! THUMB_IS_FUNC (fixP->fx_addsy))
12791    {
12792      fixP->fx_addsy = find_real_start (fixP->fx_addsy);
12793    }
12794}
12795#endif
12796
12797int
12798arm_force_relocation (struct fix * fixp)
12799{
12800#if defined (OBJ_COFF) && defined (TE_PE)
12801  if (fixp->fx_r_type == BFD_RELOC_RVA)
12802    return 1;
12803#endif
12804
12805  /* Resolve these relocations even if the symbol is extern or weak.  */
12806  if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
12807      || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
12808      || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
12809      || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
12810      || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
12811      || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12)
12812    return 0;
12813
12814  return generic_force_reloc (fixp);
12815}
12816
12817#ifdef OBJ_COFF
12818/* This is a little hack to help the gas/arm/adrl.s test.  It prevents
12819   local labels from being added to the output symbol table when they
12820   are used with the ADRL pseudo op.  The ADRL relocation should always
12821   be resolved before the binbary is emitted, so it is safe to say that
12822   it is adjustable.  */
12823
12824bfd_boolean
12825arm_fix_adjustable (fixS * fixP)
12826{
12827  if (fixP->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE)
12828    return 1;
12829  return 0;
12830}
12831#endif
12832
12833#ifdef OBJ_ELF
12834/* Relocations against Thumb function names must be left unadjusted,
12835   so that the linker can use this information to correctly set the
12836   bottom bit of their addresses.  The MIPS version of this function
12837   also prevents relocations that are mips-16 specific, but I do not
12838   know why it does this.
12839
12840   FIXME:
12841   There is one other problem that ought to be addressed here, but
12842   which currently is not:  Taking the address of a label (rather
12843   than a function) and then later jumping to that address.  Such
12844   addresses also ought to have their bottom bit set (assuming that
12845   they reside in Thumb code), but at the moment they will not.	 */
12846
12847bfd_boolean
12848arm_fix_adjustable (fixS * fixP)
12849{
12850  if (fixP->fx_addsy == NULL)
12851    return 1;
12852
12853  if (THUMB_IS_FUNC (fixP->fx_addsy)
12854      && fixP->fx_subsy == NULL)
12855    return 0;
12856
12857  /* We need the symbol name for the VTABLE entries.  */
12858  if (	 fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12859      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12860    return 0;
12861
12862  /* Don't allow symbols to be discarded on GOT related relocs.	 */
12863  if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
12864      || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
12865      || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
12866      || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
12867      || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
12868      || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
12869      || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
12870      || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
12871      || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
12872    return 0;
12873
12874  if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
12875      || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
12876      || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
12877      || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
12878    return 0;
12879
12880  return 1;
12881}
12882
12883const char *
12884elf32_arm_target_format (void)
12885{
12886#ifdef TE_SYMBIAN
12887  return (target_big_endian
12888	  ? "elf32-bigarm-symbian"
12889	  : "elf32-littlearm-symbian");
12890#elif defined (TE_VXWORKS)
12891  return (target_big_endian
12892	  ? "elf32-bigarm-vxworks"
12893	  : "elf32-littlearm-vxworks");
12894#else
12895  if (target_big_endian)
12896    return "elf32-bigarm";
12897  else
12898    return "elf32-littlearm";
12899#endif
12900}
12901
12902void
12903armelf_frob_symbol (symbolS * symp,
12904		    int *     puntp)
12905{
12906  elf_frob_symbol (symp, puntp);
12907}
12908#endif
12909
12910/* MD interface: Finalization.	*/
12911
12912/* A good place to do this, although this was probably not intended
12913   for this kind of use.  We need to dump the literal pool before
12914   references are made to a null symbol pointer.  */
12915
12916void
12917arm_cleanup (void)
12918{
12919  literal_pool * pool;
12920
12921  for (pool = list_of_pools; pool; pool = pool->next)
12922    {
12923      /* Put it at the end of the relevent section.  */
12924      subseg_set (pool->section, pool->sub_section);
12925#ifdef OBJ_ELF
12926      arm_elf_change_section ();
12927#endif
12928      s_ltorg (0);
12929    }
12930}
12931
12932/* Adjust the symbol table.  This marks Thumb symbols as distinct from
12933   ARM ones.  */
12934
12935void
12936arm_adjust_symtab (void)
12937{
12938#ifdef OBJ_COFF
12939  symbolS * sym;
12940
12941  for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
12942    {
12943      if (ARM_IS_THUMB (sym))
12944	{
12945	  if (THUMB_IS_FUNC (sym))
12946	    {
12947	      /* Mark the symbol as a Thumb function.  */
12948	      if (   S_GET_STORAGE_CLASS (sym) == C_STAT
12949		  || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!	 */
12950		S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
12951
12952	      else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
12953		S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
12954	      else
12955		as_bad (_("%s: unexpected function type: %d"),
12956			S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
12957	    }
12958	  else switch (S_GET_STORAGE_CLASS (sym))
12959	    {
12960	    case C_EXT:
12961	      S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
12962	      break;
12963	    case C_STAT:
12964	      S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
12965	      break;
12966	    case C_LABEL:
12967	      S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
12968	      break;
12969	    default:
12970	      /* Do nothing.  */
12971	      break;
12972	    }
12973	}
12974
12975      if (ARM_IS_INTERWORK (sym))
12976	coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
12977    }
12978#endif
12979#ifdef OBJ_ELF
12980  symbolS * sym;
12981  char	    bind;
12982
12983  for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
12984    {
12985      if (ARM_IS_THUMB (sym))
12986	{
12987	  elf_symbol_type * elf_sym;
12988
12989	  elf_sym = elf_symbol (symbol_get_bfdsym (sym));
12990	  bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
12991
12992	  if (! bfd_is_arm_mapping_symbol_name (elf_sym->symbol.name))
12993	    {
12994	      /* If it's a .thumb_func, declare it as so,
12995		 otherwise tag label as .code 16.  */
12996	      if (THUMB_IS_FUNC (sym))
12997		elf_sym->internal_elf_sym.st_info =
12998		  ELF_ST_INFO (bind, STT_ARM_TFUNC);
12999	      else
13000		elf_sym->internal_elf_sym.st_info =
13001		  ELF_ST_INFO (bind, STT_ARM_16BIT);
13002	    }
13003	}
13004    }
13005#endif
13006}
13007
13008/* MD interface: Initialization.  */
13009
13010static void
13011set_constant_flonums (void)
13012{
13013  int i;
13014
13015  for (i = 0; i < NUM_FLOAT_VALS; i++)
13016    if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
13017      abort ();
13018}
13019
13020void
13021md_begin (void)
13022{
13023  unsigned mach;
13024  unsigned int i;
13025
13026  if (	 (arm_ops_hsh = hash_new ()) == NULL
13027      || (arm_cond_hsh = hash_new ()) == NULL
13028      || (arm_shift_hsh = hash_new ()) == NULL
13029      || (arm_psr_hsh = hash_new ()) == NULL
13030      || (arm_v7m_psr_hsh = hash_new ()) == NULL
13031      || (arm_reg_hsh = hash_new ()) == NULL
13032      || (arm_reloc_hsh = hash_new ()) == NULL
13033      || (arm_barrier_opt_hsh = hash_new ()) == NULL)
13034    as_fatal (_("virtual memory exhausted"));
13035
13036  for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
13037    hash_insert (arm_ops_hsh, insns[i].template, (PTR) (insns + i));
13038  for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
13039    hash_insert (arm_cond_hsh, conds[i].template, (PTR) (conds + i));
13040  for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
13041    hash_insert (arm_shift_hsh, shift_names[i].name, (PTR) (shift_names + i));
13042  for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
13043    hash_insert (arm_psr_hsh, psrs[i].template, (PTR) (psrs + i));
13044  for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
13045    hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template, (PTR) (v7m_psrs + i));
13046  for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
13047    hash_insert (arm_reg_hsh, reg_names[i].name, (PTR) (reg_names + i));
13048  for (i = 0;
13049       i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
13050       i++)
13051    hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template,
13052		 (PTR) (barrier_opt_names + i));
13053#ifdef OBJ_ELF
13054  for (i = 0; i < sizeof (reloc_names) / sizeof (struct reloc_entry); i++)
13055    hash_insert (arm_reloc_hsh, reloc_names[i].name, (PTR) (reloc_names + i));
13056#endif
13057
13058  set_constant_flonums ();
13059
13060  /* Set the cpu variant based on the command-line options.  We prefer
13061     -mcpu= over -march= if both are set (as for GCC); and we prefer
13062     -mfpu= over any other way of setting the floating point unit.
13063     Use of legacy options with new options are faulted.  */
13064  if (legacy_cpu)
13065    {
13066      if (mcpu_cpu_opt || march_cpu_opt)
13067	as_bad (_("use of old and new-style options to set CPU type"));
13068
13069      mcpu_cpu_opt = legacy_cpu;
13070    }
13071  else if (!mcpu_cpu_opt)
13072    mcpu_cpu_opt = march_cpu_opt;
13073
13074  if (legacy_fpu)
13075    {
13076      if (mfpu_opt)
13077	as_bad (_("use of old and new-style options to set FPU type"));
13078
13079      mfpu_opt = legacy_fpu;
13080    }
13081  else if (!mfpu_opt)
13082    {
13083#if !(defined (TE_LINUX) || defined (TE_NetBSD) || defined (TE_OpenBSD) || defined (TE_VXWORKS))
13084      /* Some environments specify a default FPU.  If they don't, infer it
13085	 from the processor.  */
13086      if (mcpu_fpu_opt)
13087	mfpu_opt = mcpu_fpu_opt;
13088      else
13089	mfpu_opt = march_fpu_opt;
13090#else
13091      mfpu_opt = &fpu_default;
13092#endif
13093    }
13094
13095  if (!mfpu_opt)
13096    {
13097      if (!mcpu_cpu_opt)
13098	mfpu_opt = &fpu_default;
13099      else if (ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
13100	mfpu_opt = &fpu_arch_vfp_v2;
13101      else
13102	mfpu_opt = &fpu_arch_fpa;
13103    }
13104
13105#ifdef CPU_DEFAULT
13106  if (!mcpu_cpu_opt)
13107    {
13108      mcpu_cpu_opt = &cpu_default;
13109      selected_cpu = cpu_default;
13110    }
13111#else
13112  if (mcpu_cpu_opt)
13113    selected_cpu = *mcpu_cpu_opt;
13114  else
13115    mcpu_cpu_opt = &arm_arch_any;
13116#endif
13117
13118  ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
13119
13120  arm_arch_used = thumb_arch_used = arm_arch_none;
13121
13122#if defined OBJ_COFF || defined OBJ_ELF
13123  {
13124    unsigned int flags = 0;
13125
13126#if defined OBJ_ELF
13127    flags = meabi_flags;
13128
13129    switch (meabi_flags)
13130      {
13131      case EF_ARM_EABI_UNKNOWN:
13132#endif
13133	/* Set the flags in the private structure.  */
13134	if (uses_apcs_26)      flags |= F_APCS26;
13135	if (support_interwork) flags |= F_INTERWORK;
13136	if (uses_apcs_float)   flags |= F_APCS_FLOAT;
13137	if (pic_code)	       flags |= F_PIC;
13138	if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
13139	  flags |= F_SOFT_FLOAT;
13140
13141	switch (mfloat_abi_opt)
13142	  {
13143	  case ARM_FLOAT_ABI_SOFT:
13144	  case ARM_FLOAT_ABI_SOFTFP:
13145	    flags |= F_SOFT_FLOAT;
13146	    break;
13147
13148	  case ARM_FLOAT_ABI_HARD:
13149	    if (flags & F_SOFT_FLOAT)
13150	      as_bad (_("hard-float conflicts with specified fpu"));
13151	    break;
13152	  }
13153
13154	/* Using pure-endian doubles (even if soft-float).	*/
13155	if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
13156	  flags |= F_VFP_FLOAT;
13157
13158#if defined OBJ_ELF
13159	if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
13160	    flags |= EF_ARM_MAVERICK_FLOAT;
13161	break;
13162
13163      case EF_ARM_EABI_VER4:
13164      case EF_ARM_EABI_VER5:
13165	/* No additional flags to set.	*/
13166	break;
13167
13168      default:
13169	abort ();
13170      }
13171#endif
13172    bfd_set_private_flags (stdoutput, flags);
13173
13174    /* We have run out flags in the COFF header to encode the
13175       status of ATPCS support, so instead we create a dummy,
13176       empty, debug section called .arm.atpcs.	*/
13177    if (atpcs)
13178      {
13179	asection * sec;
13180
13181	sec = bfd_make_section (stdoutput, ".arm.atpcs");
13182
13183	if (sec != NULL)
13184	  {
13185	    bfd_set_section_flags
13186	      (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
13187	    bfd_set_section_size (stdoutput, sec, 0);
13188	    bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
13189	  }
13190      }
13191  }
13192#endif
13193
13194  /* Record the CPU type as well.  */
13195  if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
13196    mach = bfd_mach_arm_iWMMXt;
13197  else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
13198    mach = bfd_mach_arm_XScale;
13199  else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
13200    mach = bfd_mach_arm_ep9312;
13201  else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
13202    mach = bfd_mach_arm_5TE;
13203  else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
13204    {
13205      if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
13206	mach = bfd_mach_arm_5T;
13207      else
13208	mach = bfd_mach_arm_5;
13209    }
13210  else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
13211    {
13212      if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
13213	mach = bfd_mach_arm_4T;
13214      else
13215	mach = bfd_mach_arm_4;
13216    }
13217  else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
13218    mach = bfd_mach_arm_3M;
13219  else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
13220    mach = bfd_mach_arm_3;
13221  else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
13222    mach = bfd_mach_arm_2a;
13223  else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
13224    mach = bfd_mach_arm_2;
13225  else
13226    mach = bfd_mach_arm_unknown;
13227
13228  bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
13229}
13230
13231/* Command line processing.  */
13232
13233/* md_parse_option
13234      Invocation line includes a switch not recognized by the base assembler.
13235      See if it's a processor-specific option.
13236
13237      This routine is somewhat complicated by the need for backwards
13238      compatibility (since older releases of gcc can't be changed).
13239      The new options try to make the interface as compatible as
13240      possible with GCC.
13241
13242      New options (supported) are:
13243
13244	      -mcpu=<cpu name>		 Assemble for selected processor
13245	      -march=<architecture name> Assemble for selected architecture
13246	      -mfpu=<fpu architecture>	 Assemble for selected FPU.
13247	      -EB/-mbig-endian		 Big-endian
13248	      -EL/-mlittle-endian	 Little-endian
13249	      -k			 Generate PIC code
13250	      -mthumb			 Start in Thumb mode
13251	      -mthumb-interwork		 Code supports ARM/Thumb interworking
13252
13253      For now we will also provide support for:
13254
13255	      -mapcs-32			 32-bit Program counter
13256	      -mapcs-26			 26-bit Program counter
13257	      -macps-float		 Floats passed in FP registers
13258	      -mapcs-reentrant		 Reentrant code
13259	      -matpcs
13260      (sometime these will probably be replaced with -mapcs=<list of options>
13261      and -matpcs=<list of options>)
13262
13263      The remaining options are only supported for back-wards compatibility.
13264      Cpu variants, the arm part is optional:
13265	      -m[arm]1		      Currently not supported.
13266	      -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
13267	      -m[arm]3		      Arm 3 processor
13268	      -m[arm]6[xx],	      Arm 6 processors
13269	      -m[arm]7[xx][t][[d]m]   Arm 7 processors
13270	      -m[arm]8[10]	      Arm 8 processors
13271	      -m[arm]9[20][tdmi]      Arm 9 processors
13272	      -mstrongarm[110[0]]     StrongARM processors
13273	      -mxscale		      XScale processors
13274	      -m[arm]v[2345[t[e]]]    Arm architectures
13275	      -mall		      All (except the ARM1)
13276      FP variants:
13277	      -mfpa10, -mfpa11	      FPA10 and 11 co-processor instructions
13278	      -mfpe-old		      (No float load/store multiples)
13279	      -mvfpxd		      VFP Single precision
13280	      -mvfp		      All VFP
13281	      -mno-fpu		      Disable all floating point instructions
13282
13283      The following CPU names are recognized:
13284	      arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
13285	      arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
13286	      arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
13287	      arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
13288	      arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
13289	      arm10t arm10e, arm1020t, arm1020e, arm10200e,
13290	      strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
13291
13292      */
13293
13294const char * md_shortopts = "m:k";
13295
13296#ifdef ARM_BI_ENDIAN
13297#define OPTION_EB (OPTION_MD_BASE + 0)
13298#define OPTION_EL (OPTION_MD_BASE + 1)
13299#else
13300#if TARGET_BYTES_BIG_ENDIAN
13301#define OPTION_EB (OPTION_MD_BASE + 0)
13302#else
13303#define OPTION_EL (OPTION_MD_BASE + 1)
13304#endif
13305#endif
13306
13307struct option md_longopts[] =
13308{
13309#ifdef OPTION_EB
13310  {"EB", no_argument, NULL, OPTION_EB},
13311#endif
13312#ifdef OPTION_EL
13313  {"EL", no_argument, NULL, OPTION_EL},
13314#endif
13315  {NULL, no_argument, NULL, 0}
13316};
13317
13318size_t md_longopts_size = sizeof (md_longopts);
13319
13320struct arm_option_table
13321{
13322  char *option;		/* Option name to match.  */
13323  char *help;		/* Help information.  */
13324  int  *var;		/* Variable to change.	*/
13325  int	value;		/* What to change it to.  */
13326  char *deprecated;	/* If non-null, print this message.  */
13327};
13328
13329struct arm_option_table arm_opts[] =
13330{
13331  {"k",	     N_("generate PIC code"),	   &pic_code,	 1, NULL},
13332  {"mthumb", N_("assemble Thumb code"),	   &thumb_mode,	 1, NULL},
13333  {"mthumb-interwork", N_("support ARM/Thumb interworking"),
13334   &support_interwork, 1, NULL},
13335  {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
13336  {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
13337  {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
13338   1, NULL},
13339  {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
13340  {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
13341  {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
13342  {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
13343   NULL},
13344
13345  /* These are recognized by the assembler, but have no affect on code.	 */
13346  {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
13347  {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
13348  {NULL, NULL, NULL, 0, NULL}
13349};
13350
13351struct arm_legacy_option_table
13352{
13353  char *option;				/* Option name to match.  */
13354  const arm_feature_set	**var;		/* Variable to change.	*/
13355  const arm_feature_set	value;		/* What to change it to.  */
13356  char *deprecated;			/* If non-null, print this message.  */
13357};
13358
13359const struct arm_legacy_option_table arm_legacy_opts[] =
13360{
13361  /* DON'T add any new processors to this list -- we want the whole list
13362     to go away...  Add them to the processors table instead.  */
13363  {"marm1",	 &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
13364  {"m1",	 &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
13365  {"marm2",	 &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
13366  {"m2",	 &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
13367  {"marm250",	 &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
13368  {"m250",	 &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
13369  {"marm3",	 &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
13370  {"m3",	 &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
13371  {"marm6",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
13372  {"m6",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
13373  {"marm600",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
13374  {"m600",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
13375  {"marm610",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
13376  {"m610",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
13377  {"marm620",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
13378  {"m620",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
13379  {"marm7",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
13380  {"m7",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
13381  {"marm70",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
13382  {"m70",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
13383  {"marm700",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
13384  {"m700",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
13385  {"marm700i",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
13386  {"m700i",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
13387  {"marm710",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
13388  {"m710",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
13389  {"marm710c",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
13390  {"m710c",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
13391  {"marm720",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
13392  {"m720",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
13393  {"marm7d",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
13394  {"m7d",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
13395  {"marm7di",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
13396  {"m7di",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
13397  {"marm7m",	 &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
13398  {"m7m",	 &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
13399  {"marm7dm",	 &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
13400  {"m7dm",	 &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
13401  {"marm7dmi",	 &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
13402  {"m7dmi",	 &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
13403  {"marm7100",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
13404  {"m7100",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
13405  {"marm7500",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
13406  {"m7500",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
13407  {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
13408  {"m7500fe",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
13409  {"marm7t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
13410  {"m7t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
13411  {"marm7tdmi",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
13412  {"m7tdmi",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
13413  {"marm710t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
13414  {"m710t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
13415  {"marm720t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
13416  {"m720t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
13417  {"marm740t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
13418  {"m740t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
13419  {"marm8",	 &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
13420  {"m8",	 &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
13421  {"marm810",	 &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
13422  {"m810",	 &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
13423  {"marm9",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
13424  {"m9",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
13425  {"marm9tdmi",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
13426  {"m9tdmi",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
13427  {"marm920",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
13428  {"m920",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
13429  {"marm940",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
13430  {"m940",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
13431  {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
13432  {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
13433   N_("use -mcpu=strongarm110")},
13434  {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
13435   N_("use -mcpu=strongarm1100")},
13436  {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
13437   N_("use -mcpu=strongarm1110")},
13438  {"mxscale",	 &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
13439  {"miwmmxt",	 &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
13440  {"mall",	 &legacy_cpu, ARM_ANY,	       N_("use -mcpu=all")},
13441
13442  /* Architecture variants -- don't add any more to this list either.  */
13443  {"mv2",	 &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
13444  {"marmv2",	 &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
13445  {"mv2a",	 &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
13446  {"marmv2a",	 &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
13447  {"mv3",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
13448  {"marmv3",	 &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
13449  {"mv3m",	 &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
13450  {"marmv3m",	 &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
13451  {"mv4",	 &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
13452  {"marmv4",	 &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
13453  {"mv4t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
13454  {"marmv4t",	 &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
13455  {"mv5",	 &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
13456  {"marmv5",	 &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
13457  {"mv5t",	 &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
13458  {"marmv5t",	 &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
13459  {"mv5e",	 &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
13460  {"marmv5e",	 &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
13461
13462  /* Floating point variants -- don't add any more to this list either.	 */
13463  {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
13464  {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
13465  {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
13466  {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
13467   N_("use either -mfpu=softfpa or -mfpu=softvfp")},
13468
13469  {NULL, NULL, ARM_ARCH_NONE, NULL}
13470};
13471
13472struct arm_cpu_option_table
13473{
13474  char *name;
13475  const arm_feature_set	value;
13476  /* For some CPUs we assume an FPU unless the user explicitly sets
13477     -mfpu=...	*/
13478  const arm_feature_set	default_fpu;
13479  /* The canonical name of the CPU, or NULL to use NAME converted to upper
13480     case.  */
13481  const char *canonical_name;
13482};
13483
13484/* This list should, at a minimum, contain all the cpu names
13485   recognized by GCC.  */
13486static const struct arm_cpu_option_table arm_cpus[] =
13487{
13488  {"all",		ARM_ANY,	 FPU_ARCH_FPA,    NULL},
13489  {"arm1",		ARM_ARCH_V1,	 FPU_ARCH_FPA,    NULL},
13490  {"arm2",		ARM_ARCH_V2,	 FPU_ARCH_FPA,    NULL},
13491  {"arm250",		ARM_ARCH_V2S,	 FPU_ARCH_FPA,    NULL},
13492  {"arm3",		ARM_ARCH_V2S,	 FPU_ARCH_FPA,    NULL},
13493  {"arm6",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13494  {"arm60",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13495  {"arm600",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13496  {"arm610",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13497  {"arm620",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13498  {"arm7",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13499  {"arm7m",		ARM_ARCH_V3M,	 FPU_ARCH_FPA,    NULL},
13500  {"arm7d",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13501  {"arm7dm",		ARM_ARCH_V3M,	 FPU_ARCH_FPA,    NULL},
13502  {"arm7di",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13503  {"arm7dmi",		ARM_ARCH_V3M,	 FPU_ARCH_FPA,    NULL},
13504  {"arm70",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13505  {"arm700",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13506  {"arm700i",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13507  {"arm710",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13508  {"arm710t",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13509  {"arm720",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13510  {"arm720t",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13511  {"arm740t",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13512  {"arm710c",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13513  {"arm7100",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13514  {"arm7500",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13515  {"arm7500fe",		ARM_ARCH_V3,	 FPU_ARCH_FPA,    NULL},
13516  {"arm7t",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13517  {"arm7tdmi",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13518  {"arm7tdmi-s",	ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13519  {"arm8",		ARM_ARCH_V4,	 FPU_ARCH_FPA,    NULL},
13520  {"arm810",		ARM_ARCH_V4,	 FPU_ARCH_FPA,    NULL},
13521  {"strongarm",		ARM_ARCH_V4,	 FPU_ARCH_FPA,    NULL},
13522  {"strongarm1",	ARM_ARCH_V4,	 FPU_ARCH_FPA,    NULL},
13523  {"strongarm110",	ARM_ARCH_V4,	 FPU_ARCH_FPA,    NULL},
13524  {"strongarm1100",	ARM_ARCH_V4,	 FPU_ARCH_FPA,    NULL},
13525  {"strongarm1110",	ARM_ARCH_V4,	 FPU_ARCH_FPA,    NULL},
13526  {"arm9",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13527  {"arm920",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    "ARM920T"},
13528  {"arm920t",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13529  {"arm922t",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13530  {"arm940t",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,    NULL},
13531  {"arm9tdmi",		ARM_ARCH_V4T,	 FPU_ARCH_FPA,	  NULL},
13532  /* For V5 or later processors we default to using VFP; but the user
13533     should really set the FPU type explicitly.	 */
13534  {"arm9e-r0",		ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
13535  {"arm9e",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, NULL},
13536  {"arm926ej",		ARM_ARCH_V5TEJ,	 FPU_ARCH_VFP_V2, "ARM926EJ-S"},
13537  {"arm926ejs",		ARM_ARCH_V5TEJ,	 FPU_ARCH_VFP_V2, "ARM926EJ-S"},
13538  {"arm926ej-s",	ARM_ARCH_V5TEJ,	 FPU_ARCH_VFP_V2, NULL},
13539  {"arm946e-r0",	ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
13540  {"arm946e",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, "ARM946E-S"},
13541  {"arm946e-s",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, NULL},
13542  {"arm966e-r0",	ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL},
13543  {"arm966e",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, "ARM966E-S"},
13544  {"arm966e-s",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, NULL},
13545  {"arm968e-s",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, NULL},
13546  {"arm10t",		ARM_ARCH_V5T,	 FPU_ARCH_VFP_V1, NULL},
13547  {"arm10tdmi",		ARM_ARCH_V5T,	 FPU_ARCH_VFP_V1, NULL},
13548  {"arm10e",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, NULL},
13549  {"arm1020",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, "ARM1020E"},
13550  {"arm1020t",		ARM_ARCH_V5T,	 FPU_ARCH_VFP_V1, NULL},
13551  {"arm1020e",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, NULL},
13552  {"arm1022e",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP_V2, NULL},
13553  {"arm1026ejs",	ARM_ARCH_V5TEJ,	 FPU_ARCH_VFP_V2, "ARM1026EJ-S"},
13554  {"arm1026ej-s",	ARM_ARCH_V5TEJ,	 FPU_ARCH_VFP_V2, NULL},
13555  {"arm1136js",		ARM_ARCH_V6,	 FPU_NONE,	  "ARM1136J-S"},
13556  {"arm1136j-s",	ARM_ARCH_V6,	 FPU_NONE,	  NULL},
13557  {"arm1136jfs",	ARM_ARCH_V6,	 FPU_ARCH_VFP_V2, "ARM1136JF-S"},
13558  {"arm1136jf-s",	ARM_ARCH_V6,	 FPU_ARCH_VFP_V2, NULL},
13559  {"mpcore",		ARM_ARCH_V6K,	 FPU_ARCH_VFP_V2, NULL},
13560  {"mpcorenovfp",	ARM_ARCH_V6K,	 FPU_NONE,	  NULL},
13561  {"arm1156t2-s",	ARM_ARCH_V6T2,	 FPU_NONE,	  NULL},
13562  {"arm1156t2f-s",	ARM_ARCH_V6T2,	 FPU_ARCH_VFP_V2, NULL},
13563  {"arm1176jz-s",	ARM_ARCH_V6ZK,	 FPU_NONE,	  NULL},
13564  {"arm1176jzf-s",	ARM_ARCH_V6ZK,	 FPU_ARCH_VFP_V2, NULL},
13565  {"cortex-a8",		ARM_ARCH_V7A,	 FPU_ARCH_VFP_V2, NULL},
13566  {"cortex-r4",		ARM_ARCH_V7R,	 FPU_NONE,	  NULL},
13567  {"cortex-m3",		ARM_ARCH_V7M,	 FPU_NONE,	  NULL},
13568  /* ??? XSCALE is really an architecture.  */
13569  {"xscale",		ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
13570  /* ??? iwmmxt is not a processor.  */
13571  {"iwmmxt",		ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL},
13572  {"i80200",		ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL},
13573  /* Maverick */
13574  {"ep9312",	ARM_FEATURE(ARM_AEXT_V4T, ARM_CEXT_MAVERICK), FPU_ARCH_MAVERICK, "ARM920T"},
13575  {NULL,		ARM_ARCH_NONE,	 ARM_ARCH_NONE, NULL}
13576};
13577
13578struct arm_arch_option_table
13579{
13580  char *name;
13581  const arm_feature_set	value;
13582  const arm_feature_set	default_fpu;
13583};
13584
13585/* This list should, at a minimum, contain all the architecture names
13586   recognized by GCC.  */
13587static const struct arm_arch_option_table arm_archs[] =
13588{
13589  {"all",		ARM_ANY,	 FPU_ARCH_FPA},
13590  {"armv1",		ARM_ARCH_V1,	 FPU_ARCH_FPA},
13591  {"armv2",		ARM_ARCH_V2,	 FPU_ARCH_FPA},
13592  {"armv2a",		ARM_ARCH_V2S,	 FPU_ARCH_FPA},
13593  {"armv2s",		ARM_ARCH_V2S,	 FPU_ARCH_FPA},
13594  {"armv3",		ARM_ARCH_V3,	 FPU_ARCH_FPA},
13595  {"armv3m",		ARM_ARCH_V3M,	 FPU_ARCH_FPA},
13596  {"armv4",		ARM_ARCH_V4,	 FPU_ARCH_FPA},
13597  {"armv4xm",		ARM_ARCH_V4xM,	 FPU_ARCH_FPA},
13598  {"armv4t",		ARM_ARCH_V4T,	 FPU_ARCH_FPA},
13599  {"armv4txm",		ARM_ARCH_V4TxM,	 FPU_ARCH_FPA},
13600  {"armv5",		ARM_ARCH_V5,	 FPU_ARCH_VFP},
13601  {"armv5t",		ARM_ARCH_V5T,	 FPU_ARCH_VFP},
13602  {"armv5txm",		ARM_ARCH_V5TxM,	 FPU_ARCH_VFP},
13603  {"armv5te",		ARM_ARCH_V5TE,	 FPU_ARCH_VFP},
13604  {"armv5texp",		ARM_ARCH_V5TExP, FPU_ARCH_VFP},
13605  {"armv5tej",		ARM_ARCH_V5TEJ,	 FPU_ARCH_VFP},
13606  {"armv6",		ARM_ARCH_V6,	 FPU_ARCH_VFP},
13607  {"armv6j",		ARM_ARCH_V6,	 FPU_ARCH_VFP},
13608  {"armv6k",		ARM_ARCH_V6K,	 FPU_ARCH_VFP},
13609  {"armv6z",		ARM_ARCH_V6Z,	 FPU_ARCH_VFP},
13610  {"armv6zk",		ARM_ARCH_V6ZK,	 FPU_ARCH_VFP},
13611  {"armv6t2",		ARM_ARCH_V6T2,	 FPU_ARCH_VFP},
13612  {"armv6kt2",		ARM_ARCH_V6KT2,	 FPU_ARCH_VFP},
13613  {"armv6zt2",		ARM_ARCH_V6ZT2,	 FPU_ARCH_VFP},
13614  {"armv6zkt2",		ARM_ARCH_V6ZKT2, FPU_ARCH_VFP},
13615  {"armv7",		ARM_ARCH_V7,	 FPU_ARCH_VFP},
13616  {"armv7a",		ARM_ARCH_V7A,	 FPU_ARCH_VFP},
13617  {"armv7r",		ARM_ARCH_V7R,	 FPU_ARCH_VFP},
13618  {"armv7m",		ARM_ARCH_V7M,	 FPU_ARCH_VFP},
13619  {"xscale",		ARM_ARCH_XSCALE, FPU_ARCH_VFP},
13620  {"iwmmxt",		ARM_ARCH_IWMMXT, FPU_ARCH_VFP},
13621  {NULL,		ARM_ARCH_NONE,	 ARM_ARCH_NONE}
13622};
13623
13624/* ISA extensions in the co-processor space.  */
13625struct arm_option_cpu_value_table
13626{
13627  char *name;
13628  const arm_feature_set value;
13629};
13630
13631static const struct arm_option_cpu_value_table arm_extensions[] =
13632{
13633  {"maverick",		ARM_FEATURE (0, ARM_CEXT_MAVERICK)},
13634  {"xscale",		ARM_FEATURE (0, ARM_CEXT_XSCALE)},
13635  {"iwmmxt",		ARM_FEATURE (0, ARM_CEXT_IWMMXT)},
13636  {NULL,		ARM_ARCH_NONE}
13637};
13638
13639/* This list should, at a minimum, contain all the fpu names
13640   recognized by GCC.  */
13641static const struct arm_option_cpu_value_table arm_fpus[] =
13642{
13643  {"softfpa",		FPU_NONE},
13644  {"fpe",		FPU_ARCH_FPE},
13645  {"fpe2",		FPU_ARCH_FPE},
13646  {"fpe3",		FPU_ARCH_FPA},	/* Third release supports LFM/SFM.  */
13647  {"fpa",		FPU_ARCH_FPA},
13648  {"fpa10",		FPU_ARCH_FPA},
13649  {"fpa11",		FPU_ARCH_FPA},
13650  {"arm7500fe",		FPU_ARCH_FPA},
13651  {"softvfp",		FPU_ARCH_VFP},
13652  {"softvfp+vfp",	FPU_ARCH_VFP_V2},
13653  {"vfp",		FPU_ARCH_VFP_V2},
13654  {"vfp9",		FPU_ARCH_VFP_V2},
13655  {"vfp10",		FPU_ARCH_VFP_V2},
13656  {"vfp10-r0",		FPU_ARCH_VFP_V1},
13657  {"vfpxd",		FPU_ARCH_VFP_V1xD},
13658  {"arm1020t",		FPU_ARCH_VFP_V1},
13659  {"arm1020e",		FPU_ARCH_VFP_V2},
13660  {"arm1136jfs",	FPU_ARCH_VFP_V2},
13661  {"arm1136jf-s",	FPU_ARCH_VFP_V2},
13662  {"maverick",		FPU_ARCH_MAVERICK},
13663  {NULL,		ARM_ARCH_NONE}
13664};
13665
13666struct arm_option_value_table
13667{
13668  char *name;
13669  long value;
13670};
13671
13672static const struct arm_option_value_table arm_float_abis[] =
13673{
13674  {"hard",	ARM_FLOAT_ABI_HARD},
13675  {"softfp",	ARM_FLOAT_ABI_SOFTFP},
13676  {"soft",	ARM_FLOAT_ABI_SOFT},
13677  {NULL,	0}
13678};
13679
13680#ifdef OBJ_ELF
13681/* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
13682static const struct arm_option_value_table arm_eabis[] =
13683{
13684  {"gnu",	EF_ARM_EABI_UNKNOWN},
13685  {"4",		EF_ARM_EABI_VER4},
13686  {"5",		EF_ARM_EABI_VER5},
13687  {NULL,	0}
13688};
13689#endif
13690
13691struct arm_long_option_table
13692{
13693  char * option;		/* Substring to match.	*/
13694  char * help;			/* Help information.  */
13695  int (* func) (char * subopt);	/* Function to decode sub-option.  */
13696  char * deprecated;		/* If non-null, print this message.  */
13697};
13698
13699static int
13700arm_parse_extension (char * str, const arm_feature_set **opt_p)
13701{
13702  arm_feature_set *ext_set = xmalloc (sizeof (arm_feature_set));
13703
13704  /* Copy the feature set, so that we can modify it.  */
13705  *ext_set = **opt_p;
13706  *opt_p = ext_set;
13707
13708  while (str != NULL && *str != 0)
13709    {
13710      const struct arm_option_cpu_value_table * opt;
13711      char * ext;
13712      int optlen;
13713
13714      if (*str != '+')
13715	{
13716	  as_bad (_("invalid architectural extension"));
13717	  return 0;
13718	}
13719
13720      str++;
13721      ext = strchr (str, '+');
13722
13723      if (ext != NULL)
13724	optlen = ext - str;
13725      else
13726	optlen = strlen (str);
13727
13728      if (optlen == 0)
13729	{
13730	  as_bad (_("missing architectural extension"));
13731	  return 0;
13732	}
13733
13734      for (opt = arm_extensions; opt->name != NULL; opt++)
13735	if (strncmp (opt->name, str, optlen) == 0)
13736	  {
13737	    ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->value);
13738	    break;
13739	  }
13740
13741      if (opt->name == NULL)
13742	{
13743	  as_bad (_("unknown architectural extnsion `%s'"), str);
13744	  return 0;
13745	}
13746
13747      str = ext;
13748    };
13749
13750  return 1;
13751}
13752
13753static int
13754arm_parse_cpu (char * str)
13755{
13756  const struct arm_cpu_option_table * opt;
13757  char * ext = strchr (str, '+');
13758  int optlen;
13759
13760  if (ext != NULL)
13761    optlen = ext - str;
13762  else
13763    optlen = strlen (str);
13764
13765  if (optlen == 0)
13766    {
13767      as_bad (_("missing cpu name `%s'"), str);
13768      return 0;
13769    }
13770
13771  for (opt = arm_cpus; opt->name != NULL; opt++)
13772    if (strncmp (opt->name, str, optlen) == 0)
13773      {
13774	mcpu_cpu_opt = &opt->value;
13775	mcpu_fpu_opt = &opt->default_fpu;
13776	if (opt->canonical_name)
13777	  strcpy(selected_cpu_name, opt->canonical_name);
13778	else
13779	  {
13780	    int i;
13781	    for (i = 0; i < optlen; i++)
13782	      selected_cpu_name[i] = TOUPPER (opt->name[i]);
13783	    selected_cpu_name[i] = 0;
13784	  }
13785
13786	if (ext != NULL)
13787	  return arm_parse_extension (ext, &mcpu_cpu_opt);
13788
13789	return 1;
13790      }
13791
13792  as_bad (_("unknown cpu `%s'"), str);
13793  return 0;
13794}
13795
13796static int
13797arm_parse_arch (char * str)
13798{
13799  const struct arm_arch_option_table *opt;
13800  char *ext = strchr (str, '+');
13801  int optlen;
13802
13803  if (ext != NULL)
13804    optlen = ext - str;
13805  else
13806    optlen = strlen (str);
13807
13808  if (optlen == 0)
13809    {
13810      as_bad (_("missing architecture name `%s'"), str);
13811      return 0;
13812    }
13813
13814  for (opt = arm_archs; opt->name != NULL; opt++)
13815    if (streq (opt->name, str))
13816      {
13817	march_cpu_opt = &opt->value;
13818	march_fpu_opt = &opt->default_fpu;
13819	strcpy(selected_cpu_name, opt->name);
13820
13821	if (ext != NULL)
13822	  return arm_parse_extension (ext, &march_cpu_opt);
13823
13824	return 1;
13825      }
13826
13827  as_bad (_("unknown architecture `%s'\n"), str);
13828  return 0;
13829}
13830
13831static int
13832arm_parse_fpu (char * str)
13833{
13834  const struct arm_option_cpu_value_table * opt;
13835
13836  for (opt = arm_fpus; opt->name != NULL; opt++)
13837    if (streq (opt->name, str))
13838      {
13839	mfpu_opt = &opt->value;
13840	return 1;
13841      }
13842
13843  as_bad (_("unknown floating point format `%s'\n"), str);
13844  return 0;
13845}
13846
13847static int
13848arm_parse_float_abi (char * str)
13849{
13850  const struct arm_option_value_table * opt;
13851
13852  for (opt = arm_float_abis; opt->name != NULL; opt++)
13853    if (streq (opt->name, str))
13854      {
13855	mfloat_abi_opt = opt->value;
13856	return 1;
13857      }
13858
13859  as_bad (_("unknown floating point abi `%s'\n"), str);
13860  return 0;
13861}
13862
13863#ifdef OBJ_ELF
13864static int
13865arm_parse_eabi (char * str)
13866{
13867  const struct arm_option_value_table *opt;
13868
13869  for (opt = arm_eabis; opt->name != NULL; opt++)
13870    if (streq (opt->name, str))
13871      {
13872	meabi_flags = opt->value;
13873	return 1;
13874      }
13875  as_bad (_("unknown EABI `%s'\n"), str);
13876  return 0;
13877}
13878#endif
13879
13880struct arm_long_option_table arm_long_opts[] =
13881{
13882  {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
13883   arm_parse_cpu, NULL},
13884  {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
13885   arm_parse_arch, NULL},
13886  {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
13887   arm_parse_fpu, NULL},
13888  {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
13889   arm_parse_float_abi, NULL},
13890#ifdef OBJ_ELF
13891  {"meabi=", N_("<ver>\t  assemble for eabi version <ver>"),
13892   arm_parse_eabi, NULL},
13893#endif
13894  {NULL, NULL, 0, NULL}
13895};
13896
13897int
13898md_parse_option (int c, char * arg)
13899{
13900  struct arm_option_table *opt;
13901  const struct arm_legacy_option_table *fopt;
13902  struct arm_long_option_table *lopt;
13903
13904  switch (c)
13905    {
13906#ifdef OPTION_EB
13907    case OPTION_EB:
13908      target_big_endian = 1;
13909      break;
13910#endif
13911
13912#ifdef OPTION_EL
13913    case OPTION_EL:
13914      target_big_endian = 0;
13915      break;
13916#endif
13917
13918    case 'a':
13919      /* Listing option.  Just ignore these, we don't support additional
13920	 ones.	*/
13921      return 0;
13922
13923    default:
13924      for (opt = arm_opts; opt->option != NULL; opt++)
13925	{
13926	  if (c == opt->option[0]
13927	      && ((arg == NULL && opt->option[1] == 0)
13928		  || streq (arg, opt->option + 1)))
13929	    {
13930#if WARN_DEPRECATED
13931	      /* If the option is deprecated, tell the user.  */
13932	      if (opt->deprecated != NULL)
13933		as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
13934			   arg ? arg : "", _(opt->deprecated));
13935#endif
13936
13937	      if (opt->var != NULL)
13938		*opt->var = opt->value;
13939
13940	      return 1;
13941	    }
13942	}
13943
13944      for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
13945	{
13946	  if (c == fopt->option[0]
13947	      && ((arg == NULL && fopt->option[1] == 0)
13948		  || streq (arg, fopt->option + 1)))
13949	    {
13950#if WARN_DEPRECATED
13951	      /* If the option is deprecated, tell the user.  */
13952	      if (fopt->deprecated != NULL)
13953		as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
13954			   arg ? arg : "", _(fopt->deprecated));
13955#endif
13956
13957	      if (fopt->var != NULL)
13958		*fopt->var = &fopt->value;
13959
13960	      return 1;
13961	    }
13962	}
13963
13964      for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
13965	{
13966	  /* These options are expected to have an argument.  */
13967	  if (c == lopt->option[0]
13968	      && arg != NULL
13969	      && strncmp (arg, lopt->option + 1,
13970			  strlen (lopt->option + 1)) == 0)
13971	    {
13972#if WARN_DEPRECATED
13973	      /* If the option is deprecated, tell the user.  */
13974	      if (lopt->deprecated != NULL)
13975		as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
13976			   _(lopt->deprecated));
13977#endif
13978
13979	      /* Call the sup-option parser.  */
13980	      return lopt->func (arg + strlen (lopt->option) - 1);
13981	    }
13982	}
13983
13984      return 0;
13985    }
13986
13987  return 1;
13988}
13989
13990void
13991md_show_usage (FILE * fp)
13992{
13993  struct arm_option_table *opt;
13994  struct arm_long_option_table *lopt;
13995
13996  fprintf (fp, _(" ARM-specific assembler options:\n"));
13997
13998  for (opt = arm_opts; opt->option != NULL; opt++)
13999    if (opt->help != NULL)
14000      fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
14001
14002  for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
14003    if (lopt->help != NULL)
14004      fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
14005
14006#ifdef OPTION_EB
14007  fprintf (fp, _("\
14008  -EB                     assemble code for a big-endian cpu\n"));
14009#endif
14010
14011#ifdef OPTION_EL
14012  fprintf (fp, _("\
14013  -EL                     assemble code for a little-endian cpu\n"));
14014#endif
14015}
14016
14017
14018#ifdef OBJ_ELF
14019typedef struct
14020{
14021  int val;
14022  arm_feature_set flags;
14023} cpu_arch_ver_table;
14024
14025/* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
14026   least features first.  */
14027static const cpu_arch_ver_table cpu_arch_ver[] =
14028{
14029    {1, ARM_ARCH_V4},
14030    {2, ARM_ARCH_V4T},
14031    {3, ARM_ARCH_V5},
14032    {4, ARM_ARCH_V5TE},
14033    {5, ARM_ARCH_V5TEJ},
14034    {6, ARM_ARCH_V6},
14035    {7, ARM_ARCH_V6Z},
14036    {8, ARM_ARCH_V6K},
14037    {9, ARM_ARCH_V6T2},
14038    {10, ARM_ARCH_V7A},
14039    {10, ARM_ARCH_V7R},
14040    {10, ARM_ARCH_V7M},
14041    {0, ARM_ARCH_NONE}
14042};
14043
14044/* Set the public EABI object attributes.  */
14045static void
14046aeabi_set_public_attributes (void)
14047{
14048  int arch;
14049  arm_feature_set flags;
14050  arm_feature_set tmp;
14051  const cpu_arch_ver_table *p;
14052
14053  /* Choose the architecture based on the capabilities of the requested cpu
14054     (if any) and/or the instructions actually used.  */
14055  ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
14056  ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
14057  ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
14058
14059  tmp = flags;
14060  arch = 0;
14061  for (p = cpu_arch_ver; p->val; p++)
14062    {
14063      if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
14064	{
14065	  arch = p->val;
14066	  ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
14067	}
14068    }
14069
14070  /* Tag_CPU_name.  */
14071  if (selected_cpu_name[0])
14072    {
14073      char *p;
14074
14075      p = selected_cpu_name;
14076      if (strncmp(p, "armv", 4) == 0)
14077	{
14078	  int i;
14079
14080	  p += 4;
14081	  for (i = 0; p[i]; i++)
14082	    p[i] = TOUPPER (p[i]);
14083	}
14084      elf32_arm_add_eabi_attr_string (stdoutput, 5, p);
14085    }
14086  /* Tag_CPU_arch.  */
14087  elf32_arm_add_eabi_attr_int (stdoutput, 6, arch);
14088  /* Tag_CPU_arch_profile.  */
14089  if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a))
14090    elf32_arm_add_eabi_attr_int (stdoutput, 7, 'A');
14091  else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
14092    elf32_arm_add_eabi_attr_int (stdoutput, 7, 'R');
14093  else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m))
14094    elf32_arm_add_eabi_attr_int (stdoutput, 7, 'M');
14095  /* Tag_ARM_ISA_use.  */
14096  if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_full))
14097    elf32_arm_add_eabi_attr_int (stdoutput, 8, 1);
14098  /* Tag_THUMB_ISA_use.  */
14099  if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_full))
14100    elf32_arm_add_eabi_attr_int (stdoutput, 9,
14101	ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2) ? 2 : 1);
14102  /* Tag_VFP_arch.  */
14103  if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_arch_vfp_v2)
14104      || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_arch_vfp_v2))
14105    elf32_arm_add_eabi_attr_int (stdoutput, 10, 2);
14106  else if (ARM_CPU_HAS_FEATURE (thumb_arch_used, fpu_arch_vfp_v1)
14107	   || ARM_CPU_HAS_FEATURE (arm_arch_used, fpu_arch_vfp_v1))
14108    elf32_arm_add_eabi_attr_int (stdoutput, 10, 1);
14109  /* Tag_WMMX_arch.  */
14110  if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_cext_iwmmxt)
14111      || ARM_CPU_HAS_FEATURE (arm_arch_used, arm_cext_iwmmxt))
14112    elf32_arm_add_eabi_attr_int (stdoutput, 11, 1);
14113}
14114
14115/* Add the .ARM.attributes section.  */
14116void
14117arm_md_end (void)
14118{
14119  segT s;
14120  char *p;
14121  addressT addr;
14122  offsetT size;
14123
14124  if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
14125    return;
14126
14127  aeabi_set_public_attributes ();
14128  size = elf32_arm_eabi_attr_size (stdoutput);
14129  s = subseg_new (".ARM.attributes", 0);
14130  bfd_set_section_flags (stdoutput, s, SEC_READONLY | SEC_DATA);
14131  addr = frag_now_fix ();
14132  p = frag_more (size);
14133  elf32_arm_set_eabi_attr_contents (stdoutput, (bfd_byte *)p, size);
14134}
14135
14136
14137/* Parse a .cpu directive.  */
14138
14139static void
14140s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
14141{
14142  const struct arm_cpu_option_table *opt;
14143  char *name;
14144  char saved_char;
14145
14146  name = input_line_pointer;
14147  while (*input_line_pointer && !ISSPACE(*input_line_pointer))
14148    input_line_pointer++;
14149  saved_char = *input_line_pointer;
14150  *input_line_pointer = 0;
14151
14152  /* Skip the first "all" entry.  */
14153  for (opt = arm_cpus + 1; opt->name != NULL; opt++)
14154    if (streq (opt->name, name))
14155      {
14156	mcpu_cpu_opt = &opt->value;
14157	selected_cpu = opt->value;
14158	if (opt->canonical_name)
14159	  strcpy(selected_cpu_name, opt->canonical_name);
14160	else
14161	  {
14162	    int i;
14163	    for (i = 0; opt->name[i]; i++)
14164	      selected_cpu_name[i] = TOUPPER (opt->name[i]);
14165	    selected_cpu_name[i] = 0;
14166	  }
14167	ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
14168	*input_line_pointer = saved_char;
14169	demand_empty_rest_of_line ();
14170	return;
14171      }
14172  as_bad (_("unknown cpu `%s'"), name);
14173  *input_line_pointer = saved_char;
14174  ignore_rest_of_line ();
14175}
14176
14177
14178/* Parse a .arch directive.  */
14179
14180static void
14181s_arm_arch (int ignored ATTRIBUTE_UNUSED)
14182{
14183  const struct arm_arch_option_table *opt;
14184  char saved_char;
14185  char *name;
14186
14187  name = input_line_pointer;
14188  while (*input_line_pointer && !ISSPACE(*input_line_pointer))
14189    input_line_pointer++;
14190  saved_char = *input_line_pointer;
14191  *input_line_pointer = 0;
14192
14193  /* Skip the first "all" entry.  */
14194  for (opt = arm_archs + 1; opt->name != NULL; opt++)
14195    if (streq (opt->name, name))
14196      {
14197	mcpu_cpu_opt = &opt->value;
14198	selected_cpu = opt->value;
14199	strcpy(selected_cpu_name, opt->name);
14200	ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
14201	*input_line_pointer = saved_char;
14202	demand_empty_rest_of_line ();
14203	return;
14204      }
14205
14206  as_bad (_("unknown architecture `%s'\n"), name);
14207  *input_line_pointer = saved_char;
14208  ignore_rest_of_line ();
14209}
14210
14211
14212/* Parse a .fpu directive.  */
14213
14214static void
14215s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
14216{
14217  const struct arm_option_cpu_value_table *opt;
14218  char saved_char;
14219  char *name;
14220
14221  name = input_line_pointer;
14222  while (*input_line_pointer && !ISSPACE(*input_line_pointer))
14223    input_line_pointer++;
14224  saved_char = *input_line_pointer;
14225  *input_line_pointer = 0;
14226
14227  for (opt = arm_fpus; opt->name != NULL; opt++)
14228    if (streq (opt->name, name))
14229      {
14230	mfpu_opt = &opt->value;
14231	ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
14232	*input_line_pointer = saved_char;
14233	demand_empty_rest_of_line ();
14234	return;
14235      }
14236
14237  as_bad (_("unknown floating point format `%s'\n"), name);
14238  *input_line_pointer = saved_char;
14239  ignore_rest_of_line ();
14240}
14241#endif /* OBJ_ELF */
14242
14243