Don't treat `;' as a line separator by default.
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright (C) 1994, 95, 96, 97, 98, 1999, 2000 Free Software Foundation, Inc.
3    Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
4         Modified by David Taylor (dtaylor@armltd.co.uk)
5
6    This file is part of GAS, the GNU Assembler.
7
8    GAS is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GAS is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GAS; see the file COPYING.  If not, write to the Free
20    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 #include <ctype.h>
24 #include <string.h>
25 #define  NO_RELOC 0
26 #include "as.h"
27
28 /* need TARGET_CPU */
29 #include "config.h"
30 #include "subsegs.h"
31 #include "obstack.h"
32 #include "symbols.h"
33 #include "listing.h"
34
35 #ifdef OBJ_ELF
36 #include "elf/arm.h"
37 #endif
38
39 /* Types of processor to assemble for.  */
40 #define ARM_1           0x00000001
41 #define ARM_2           0x00000002
42 #define ARM_3           0x00000004
43 #define ARM_250         ARM_3
44 #define ARM_6           0x00000008
45 #define ARM_7           ARM_6           /* same core instruction set */
46 #define ARM_8           ARM_6           /* same core instruction set */
47 #define ARM_9           ARM_6           /* same core instruction set */
48 #define ARM_CPU_MASK    0x0000000f
49
50 /* The following bitmasks control CPU extensions (ARM7 onwards): */
51 #define ARM_LONGMUL     0x00000010      /* allow long multiplies */
52 #define ARM_HALFWORD    0x00000020      /* allow half word loads */
53 #define ARM_THUMB       0x00000040      /* allow BX instruction  */
54 #define ARM_EXT_V5      0x00000080      /* allow CLZ etc         */
55
56 /* Architectures are the sum of the base and extensions.  */
57 #define ARM_ARCH_V4     (ARM_7 | ARM_LONGMUL | ARM_HALFWORD)
58 #define ARM_ARCH_V4T    (ARM_ARCH_V4 | ARM_THUMB)
59 #define ARM_ARCH_V5     (ARM_ARCH_V4 | ARM_EXT_V5)
60 #define ARM_ARCH_V5T    (ARM_ARCH_V5 | ARM_THUMB)
61
62 /* Some useful combinations:  */
63 #define ARM_ANY         0x00ffffff
64 #define ARM_2UP         (ARM_ANY - ARM_1)
65 #define ARM_ALL         ARM_2UP         /* Not arm1 only */
66 #define ARM_3UP         0x00fffffc
67 #define ARM_6UP         0x00fffff8      /* Includes ARM7 */
68
69 #define FPU_CORE        0x80000000
70 #define FPU_FPA10       0x40000000
71 #define FPU_FPA11       0x40000000
72 #define FPU_NONE        0
73
74 /* Some useful combinations  */
75 #define FPU_ALL         0xff000000      /* Note this is ~ARM_ANY */
76 #define FPU_MEMMULTI    0x7f000000      /* Not fpu_core */
77
78      
79 #ifndef CPU_DEFAULT
80 #if defined __thumb__
81 #define CPU_DEFAULT (ARM_ARCH_V4 | ARM_THUMB)
82 #else
83 #define CPU_DEFAULT ARM_ALL
84 #endif
85 #endif
86
87 #ifndef FPU_DEFAULT
88 #define FPU_DEFAULT FPU_ALL
89 #endif
90
91 #define streq(a, b)           (strcmp (a, b) == 0)
92 #define skip_whitespace(str)  while (* (str) == ' ') ++ (str)
93
94 static unsigned long    cpu_variant = CPU_DEFAULT | FPU_DEFAULT;
95 static int target_oabi = 0;
96
97 #if defined OBJ_COFF || defined OBJ_ELF
98 /* Flags stored in private area of BFD structure */
99 static boolean          uses_apcs_26 = false;
100 static boolean          support_interwork = false;
101 static boolean          uses_apcs_float = false;
102 static boolean          pic_code = false;
103 #endif
104
105 /* This array holds the chars that always start a comment.  If the
106    pre-processor is disabled, these aren't very useful.  */
107 CONST char comment_chars[] = "@";
108
109 /* This array holds the chars that only start a comment at the beginning of
110    a line.  If the line seems to have the form '# 123 filename'
111    .line and .file directives will appear in the pre-processed output.  */
112 /* Note that input_file.c hand checks for '#' at the beginning of the
113    first line of the input file.  This is because the compiler outputs
114    #NO_APP at the beginning of its output.  */
115 /* Also note that comments like this one will always work.  */
116 CONST char line_comment_chars[] = "#";
117
118 CONST char line_separator_chars[] = ";";
119
120 /* Chars that can be used to separate mant
121    from exp in floating point numbers.  */
122 CONST char EXP_CHARS[] = "eE";
123
124 /* Chars that mean this number is a floating point constant */
125 /* As in 0f12.456 */
126 /* or    0d1.2345e12 */
127
128 CONST char FLT_CHARS[] = "rRsSfFdDxXeEpP";
129
130 /* Prefix characters that indicate the start of an immediate
131    value.  */
132 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
133
134 #ifdef OBJ_ELF
135 symbolS * GOT_symbol;           /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
136 #endif
137
138 CONST int md_reloc_size = 8;    /* Size of relocation record */
139
140 static int thumb_mode = 0;      /* 0: assemble for ARM, 1: assemble for Thumb,
141                                    2: assemble for Thumb even though target cpu
142                                    does not support thumb instructions.  */
143 typedef struct arm_fix
144 {
145   int thumb_mode;
146 } arm_fix_data;
147
148 struct arm_it
149 {
150   CONST char *  error;
151   unsigned long instruction;
152   int           suffix;
153   int           size;
154   struct
155     {
156       bfd_reloc_code_real_type type;
157       expressionS              exp;
158       int                      pc_rel;
159     } reloc;
160 };
161
162 struct arm_it inst;
163
164 struct asm_shift
165 {
166   CONST char *  template;
167   unsigned long value;
168 };
169
170 static CONST struct asm_shift shift[] =
171 {
172   {"asl", 0},
173   {"lsl", 0},
174   {"lsr", 0x00000020},
175   {"asr", 0x00000040},
176   {"ror", 0x00000060},
177   {"rrx", 0x00000060},
178   {"ASL", 0},
179   {"LSL", 0},
180   {"LSR", 0x00000020},
181   {"ASR", 0x00000040},
182   {"ROR", 0x00000060},
183   {"RRX", 0x00000060}
184 };
185
186 #define NO_SHIFT_RESTRICT 1
187 #define SHIFT_RESTRICT    0
188
189 #define NUM_FLOAT_VALS 8
190
191 CONST char * fp_const[] = 
192 {
193   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
194 };
195
196 /* Number of littlenums required to hold an extended precision number.  */
197 #define MAX_LITTLENUMS 6
198
199 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
200
201 #define FAIL    (-1)
202 #define SUCCESS (0)
203
204 #define SUFF_S 1
205 #define SUFF_D 2
206 #define SUFF_E 3
207 #define SUFF_P 4
208
209 #define CP_T_X   0x00008000
210 #define CP_T_Y   0x00400000
211 #define CP_T_Pre 0x01000000
212 #define CP_T_UD  0x00800000
213 #define CP_T_WB  0x00200000
214
215 #define CONDS_BIT       (0x00100000)
216 #define LOAD_BIT        (0x00100000)
217 #define TRANS_BIT       (0x00200000)
218
219 struct asm_cond
220 {
221   CONST char *  template;
222   unsigned long value;
223 };
224
225 /* This is to save a hash look-up in the common case.  */
226 #define COND_ALWAYS 0xe0000000
227
228 static CONST struct asm_cond conds[] = 
229 {
230   {"eq", 0x00000000},
231   {"ne", 0x10000000},
232   {"cs", 0x20000000}, {"hs", 0x20000000},
233   {"cc", 0x30000000}, {"ul", 0x30000000}, {"lo", 0x30000000},
234   {"mi", 0x40000000},
235   {"pl", 0x50000000},
236   {"vs", 0x60000000},
237   {"vc", 0x70000000},
238   {"hi", 0x80000000},
239   {"ls", 0x90000000},
240   {"ge", 0xa0000000},
241   {"lt", 0xb0000000},
242   {"gt", 0xc0000000},
243   {"le", 0xd0000000},
244   {"al", 0xe0000000},
245   {"nv", 0xf0000000}
246 };
247
248 /* Warning: If the top bit of the set_bits is set, then the standard
249    instruction bitmask is ignored, and the new bitmask is taken from
250    the set_bits:  */
251 struct asm_flg
252 {
253   CONST char *  template;       /* Basic flag string */
254   unsigned long set_bits;       /* Bits to set */
255 };
256
257 static CONST struct asm_flg s_flag[] =
258 {
259   {"s", CONDS_BIT},
260   {NULL, 0}
261 };
262
263 static CONST struct asm_flg ldr_flags[] =
264 {
265   {"b",  0x00400000},
266   {"t",  TRANS_BIT},
267   {"bt", 0x00400000 | TRANS_BIT},
268   {"h",  0x801000b0},
269   {"sh", 0x801000f0},
270   {"sb", 0x801000d0},
271   {NULL, 0}
272 };
273
274 static CONST struct asm_flg str_flags[] =
275 {
276   {"b",  0x00400000},
277   {"t",  TRANS_BIT},
278   {"bt", 0x00400000 | TRANS_BIT},
279   {"h",  0x800000b0},
280   {NULL, 0}
281 };
282
283 static CONST struct asm_flg byte_flag[] =
284 {
285   {"b", 0x00400000},
286   {NULL, 0}
287 };
288
289 static CONST struct asm_flg cmp_flags[] =
290 {
291   {"s", CONDS_BIT},
292   {"p", 0x0010f000},
293   {NULL, 0}
294 };
295
296 static CONST struct asm_flg ldm_flags[] =
297 {
298   {"ed", 0x01800000},
299   {"fd", 0x00800000},
300   {"ea", 0x01000000},
301   {"fa", 0x08000000},
302   {"ib", 0x01800000},
303   {"ia", 0x00800000},
304   {"db", 0x01000000},
305   {"da", 0x08000000},
306   {NULL, 0}
307 };
308
309 static CONST struct asm_flg stm_flags[] =
310 {
311   {"ed", 0x08000000},
312   {"fd", 0x01000000},
313   {"ea", 0x00800000},
314   {"fa", 0x01800000},
315   {"ib", 0x01800000},
316   {"ia", 0x00800000},
317   {"db", 0x01000000},
318   {"da", 0x08000000},
319   {NULL, 0}
320 };
321
322 static CONST struct asm_flg lfm_flags[] =
323 {
324   {"fd", 0x00800000},
325   {"ea", 0x01000000},
326   {NULL, 0}
327 };
328
329 static CONST struct asm_flg sfm_flags[] =
330 {
331   {"fd", 0x01000000},
332   {"ea", 0x00800000},
333   {NULL, 0}
334 };
335
336 static CONST struct asm_flg round_flags[] =
337 {
338   {"p", 0x00000020},
339   {"m", 0x00000040},
340   {"z", 0x00000060},
341   {NULL, 0}
342 };
343
344 /* The implementation of the FIX instruction is broken on some assemblers,
345    in that it accepts a precision specifier as well as a rounding specifier,
346    despite the fact that this is meaningless.  To be more compatible, we
347    accept it as well, though of course it does not set any bits.  */
348 static CONST struct asm_flg fix_flags[] =
349 {
350   {"p", 0x00000020},
351   {"m", 0x00000040},
352   {"z", 0x00000060},
353   {"sp", 0x00000020},
354   {"sm", 0x00000040},
355   {"sz", 0x00000060},
356   {"dp", 0x00000020},
357   {"dm", 0x00000040},
358   {"dz", 0x00000060},
359   {"ep", 0x00000020},
360   {"em", 0x00000040},
361   {"ez", 0x00000060},
362   {NULL, 0}
363 };
364
365 static CONST struct asm_flg except_flag[] =
366 {
367   {"e", 0x00400000},
368   {NULL, 0}
369 };
370
371 static CONST struct asm_flg cplong_flag[] =
372 {
373   {"l", 0x00400000},
374   {NULL, 0}
375 };
376
377 struct asm_psr
378 {
379   CONST char *  template;
380   boolean       cpsr;
381   unsigned long field;
382 };
383
384 #define SPSR_BIT   (1 << 22)  /* The bit that distnguishes CPSR and SPSR.  */
385 #define PSR_SHIFT  16  /* How many bits to shift the PSR_xxx bits up by.  */
386
387 #define PSR_c   (1 << 0)
388 #define PSR_x   (1 << 1)
389 #define PSR_s   (1 << 2)
390 #define PSR_f   (1 << 3)
391
392 static CONST struct asm_psr psrs[] =
393 {
394   {"CPSR",      true,  PSR_c | PSR_f},
395   {"CPSR_all",  true,  PSR_c | PSR_f},
396   {"SPSR",      false, PSR_c | PSR_f},
397   {"SPSR_all",  false, PSR_c | PSR_f},
398   {"CPSR_flg",  true,  PSR_f},
399   {"CPSR_f",    true,  PSR_f},
400   {"SPSR_flg",  false, PSR_f},
401   {"SPSR_f",    false, PSR_f}, 
402   {"CPSR_c",    true,  PSR_c},
403   {"CPSR_ctl",  true,  PSR_c},
404   {"SPSR_c",    false, PSR_c},
405   {"SPSR_ctl",  false, PSR_c},
406   {"CPSR_x",    true,  PSR_x},
407   {"CPSR_s",    true,  PSR_s},
408   {"SPSR_x",    false, PSR_x},
409   {"SPSR_s",    false, PSR_s},
410   /* For backwards compatability with older toolchain we also
411      support lower case versions of some of these flags.  */
412   {"cpsr",      true,  PSR_c | PSR_f},
413   {"cpsr_all",  true,  PSR_c | PSR_f},
414   {"spsr",      false, PSR_c | PSR_f},
415   {"spsr_all",  false, PSR_c | PSR_f},
416   {"cpsr_flg",  true,  PSR_f},
417   {"cpsr_f",    true,  PSR_f},
418   {"spsr_flg",  false, PSR_f},
419   {"spsr_f",    false, PSR_f}, 
420   {"cpsr_c",    true,  PSR_c},
421   {"cpsr_ctl",  true,  PSR_c},
422   {"spsr_c",    false, PSR_c},
423   {"spsr_ctl",  false, PSR_c}
424 };
425
426 /* Functions called by parser.  */
427 /* ARM instructions */
428 static void do_arit             PARAMS ((char *, unsigned long));
429 static void do_cmp              PARAMS ((char *, unsigned long));
430 static void do_mov              PARAMS ((char *, unsigned long));
431 static void do_ldst             PARAMS ((char *, unsigned long));
432 static void do_ldmstm           PARAMS ((char *, unsigned long));
433 static void do_branch           PARAMS ((char *, unsigned long));
434 static void do_swi              PARAMS ((char *, unsigned long));
435 /* Pseudo Op codes */                                         
436 static void do_adr              PARAMS ((char *, unsigned long));
437 static void do_adrl             PARAMS ((char *, unsigned long));
438 static void do_nop              PARAMS ((char *, unsigned long));
439 /* ARM 2 */                                                   
440 static void do_mul              PARAMS ((char *, unsigned long));
441 static void do_mla              PARAMS ((char *, unsigned long));
442 /* ARM 3 */                                                   
443 static void do_swap             PARAMS ((char *, unsigned long));
444 /* ARM 6 */                                                   
445 static void do_msr              PARAMS ((char *, unsigned long));
446 static void do_mrs              PARAMS ((char *, unsigned long));
447 /* ARM 7M */                                                  
448 static void do_mull             PARAMS ((char *, unsigned long));
449 /* ARM THUMB */                                               
450 static void do_bx               PARAMS ((char *, unsigned long));
451
452                                                               
453 /* Coprocessor Instructions */                                
454 static void do_cdp              PARAMS ((char *, unsigned long));
455 static void do_lstc             PARAMS ((char *, unsigned long));
456 static void do_co_reg           PARAMS ((char *, unsigned long));
457 static void do_fp_ctrl          PARAMS ((char *, unsigned long));
458 static void do_fp_ldst          PARAMS ((char *, unsigned long));
459 static void do_fp_ldmstm        PARAMS ((char *, unsigned long));
460 static void do_fp_dyadic        PARAMS ((char *, unsigned long));
461 static void do_fp_monadic       PARAMS ((char *, unsigned long));
462 static void do_fp_cmp           PARAMS ((char *, unsigned long));
463 static void do_fp_from_reg      PARAMS ((char *, unsigned long));
464 static void do_fp_to_reg        PARAMS ((char *, unsigned long));
465
466 static void fix_new_arm         PARAMS ((fragS *, int, short, expressionS *, int, int));
467 static int arm_reg_parse        PARAMS ((char **));
468 static CONST struct asm_psr * arm_psr_parse PARAMS ((char **));
469 static void symbol_locate       PARAMS ((symbolS *, CONST char *, segT, valueT, fragS *));
470 static int add_to_lit_pool      PARAMS ((void));
471 static unsigned validate_immediate PARAMS ((unsigned));
472 static unsigned validate_immediate_twopart PARAMS ((unsigned int, unsigned int *));
473 static int validate_offset_imm  PARAMS ((unsigned int, int));
474 static void opcode_select       PARAMS ((int));
475 static void end_of_line         PARAMS ((char *));
476 static int reg_required_here    PARAMS ((char **, int));
477 static int psr_required_here    PARAMS ((char **));
478 static int co_proc_number       PARAMS ((char **));
479 static int cp_opc_expr          PARAMS ((char **, int, int));
480 static int cp_reg_required_here PARAMS ((char **, int));
481 static int fp_reg_required_here PARAMS ((char **, int));
482 static int cp_address_offset    PARAMS ((char **));
483 static int cp_address_required_here     PARAMS ((char **));
484 static int my_get_float_expression      PARAMS ((char **));
485 static int skip_past_comma      PARAMS ((char **));
486 static int walk_no_bignums      PARAMS ((symbolS *));
487 static int negate_data_op       PARAMS ((unsigned long *, unsigned long));
488 static int data_op2             PARAMS ((char **));
489 static int fp_op2               PARAMS ((char **));
490 static long reg_list            PARAMS ((char **));
491 static void thumb_load_store    PARAMS ((char *, int, int));
492 static int decode_shift         PARAMS ((char **, int));
493 static int ldst_extend          PARAMS ((char **, int));
494 static void thumb_add_sub       PARAMS ((char *, int));
495 static void insert_reg          PARAMS ((int));
496 static void thumb_shift         PARAMS ((char *, int));
497 static void thumb_mov_compare   PARAMS ((char *, int));
498 static void set_constant_flonums        PARAMS ((void));
499 static valueT md_chars_to_number        PARAMS ((char *, int));
500 static void insert_reg_alias    PARAMS ((char *, int));
501 static void output_inst         PARAMS ((void));
502 #ifdef OBJ_ELF
503 static bfd_reloc_code_real_type arm_parse_reloc PARAMS ((void));
504 #endif
505
506 /* ARM instructions take 4bytes in the object file, Thumb instructions
507    take 2:  */
508 #define INSN_SIZE       4
509
510 /* LONGEST_INST is the longest basic instruction name without conditions or 
511    flags.  ARM7M has 4 of length 5.  */
512
513 #define LONGEST_INST 5
514
515
516 struct asm_opcode 
517 {
518   CONST char *           template;      /* Basic string to match */
519   unsigned long          value;         /* Basic instruction code */
520
521   /* Compulsory suffix that must follow conds. If "", then the
522      instruction is not conditional and must have no suffix. */
523   CONST char *           comp_suffix;   
524
525   CONST struct asm_flg * flags;         /* Bits to toggle if flag 'n' set */
526   unsigned long          variants;      /* Which CPU variants this exists for */
527   /* Function to call to parse args */
528   void (*                parms) PARAMS ((char *, unsigned long));
529 };
530
531 static CONST struct asm_opcode insns[] = 
532 {
533 /* ARM Instructions */
534   {"and",   0x00000000, NULL,   s_flag,      ARM_ANY,      do_arit},
535   {"eor",   0x00200000, NULL,   s_flag,      ARM_ANY,      do_arit},
536   {"sub",   0x00400000, NULL,   s_flag,      ARM_ANY,      do_arit},
537   {"rsb",   0x00600000, NULL,   s_flag,      ARM_ANY,      do_arit},
538   {"add",   0x00800000, NULL,   s_flag,      ARM_ANY,      do_arit},
539   {"adc",   0x00a00000, NULL,   s_flag,      ARM_ANY,      do_arit},
540   {"sbc",   0x00c00000, NULL,   s_flag,      ARM_ANY,      do_arit},
541   {"rsc",   0x00e00000, NULL,   s_flag,      ARM_ANY,      do_arit},
542   {"orr",   0x01800000, NULL,   s_flag,      ARM_ANY,      do_arit},
543   {"bic",   0x01c00000, NULL,   s_flag,      ARM_ANY,      do_arit},
544   {"tst",   0x01000000, NULL,   cmp_flags,   ARM_ANY,      do_cmp},
545   {"teq",   0x01200000, NULL,   cmp_flags,   ARM_ANY,      do_cmp},
546   {"cmp",   0x01400000, NULL,   cmp_flags,   ARM_ANY,      do_cmp},
547   {"cmn",   0x01600000, NULL,   cmp_flags,   ARM_ANY,      do_cmp},
548   {"mov",   0x01a00000, NULL,   s_flag,      ARM_ANY,      do_mov},
549   {"mvn",   0x01e00000, NULL,   s_flag,      ARM_ANY,      do_mov},
550   {"str",   0x04000000, NULL,   str_flags,   ARM_ANY,      do_ldst},
551   {"ldr",   0x04100000, NULL,   ldr_flags,   ARM_ANY,      do_ldst},
552   {"stm",   0x08000000, NULL,   stm_flags,   ARM_ANY,      do_ldmstm},
553   {"ldm",   0x08100000, NULL,   ldm_flags,   ARM_ANY,      do_ldmstm},
554   {"swi",   0x0f000000, NULL,   NULL,        ARM_ANY,      do_swi},
555 #ifdef TE_WINCE
556   {"bl",    0x0b000000, NULL,   NULL,        ARM_ANY,      do_branch},
557   {"b",     0x0a000000, NULL,   NULL,        ARM_ANY,      do_branch},
558 #else
559   {"bl",    0x0bfffffe, NULL,   NULL,        ARM_ANY,      do_branch},
560   {"b",     0x0afffffe, NULL,   NULL,        ARM_ANY,      do_branch},
561 #endif
562   
563 /* Pseudo ops */
564   {"adr",   0x028f0000, NULL,   NULL,        ARM_ANY,      do_adr},
565   {"adrl",  0x028f0000, NULL,   NULL,        ARM_ANY,      do_adrl},
566   {"nop",   0x01a00000, NULL,   NULL,        ARM_ANY,      do_nop},
567
568 /* ARM 2 multiplies */
569   {"mul",   0x00000090, NULL,   s_flag,      ARM_2UP,      do_mul},
570   {"mla",   0x00200090, NULL,   s_flag,      ARM_2UP,      do_mla},
571
572 /* ARM 3 - swp instructions */
573   {"swp",   0x01000090, NULL,   byte_flag,   ARM_3UP,      do_swap},
574
575 /* ARM 6 Coprocessor instructions */
576   {"mrs",   0x010f0000, NULL,   NULL,        ARM_6UP,      do_mrs},
577   {"msr",   0x0120f000, NULL,   NULL,        ARM_6UP,      do_msr},
578 /* ScottB: our code uses 0x0128f000 for msr.
579    NickC:  but this is wrong because the bits 16 through 19 are
580            handled by the PSR_xxx defines above.  */
581
582 /* ARM 7M long multiplies - need signed/unsigned flags! */
583   {"smull", 0x00c00090, NULL,   s_flag,      ARM_LONGMUL,  do_mull},
584   {"umull", 0x00800090, NULL,   s_flag,      ARM_LONGMUL,  do_mull},
585   {"smlal", 0x00e00090, NULL,   s_flag,      ARM_LONGMUL,  do_mull},
586   {"umlal", 0x00a00090, NULL,   s_flag,      ARM_LONGMUL,  do_mull},
587
588 /* ARM THUMB interworking */
589   {"bx",    0x012fff10, NULL,   NULL,        ARM_THUMB,    do_bx},
590
591 /* Floating point instructions */
592   {"wfs",   0x0e200110, NULL,   NULL,        FPU_ALL,      do_fp_ctrl},
593   {"rfs",   0x0e300110, NULL,   NULL,        FPU_ALL,      do_fp_ctrl},
594   {"wfc",   0x0e400110, NULL,   NULL,        FPU_ALL,      do_fp_ctrl},
595   {"rfc",   0x0e500110, NULL,   NULL,        FPU_ALL,      do_fp_ctrl},
596   {"ldf",   0x0c100100, "sdep", NULL,        FPU_ALL,      do_fp_ldst},
597   {"stf",   0x0c000100, "sdep", NULL,        FPU_ALL,      do_fp_ldst},
598   {"lfm",   0x0c100200, NULL,   lfm_flags,   FPU_MEMMULTI, do_fp_ldmstm},
599   {"sfm",   0x0c000200, NULL,   sfm_flags,   FPU_MEMMULTI, do_fp_ldmstm},
600   {"mvf",   0x0e008100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
601   {"mnf",   0x0e108100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
602   {"abs",   0x0e208100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
603   {"rnd",   0x0e308100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
604   {"sqt",   0x0e408100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
605   {"log",   0x0e508100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
606   {"lgn",   0x0e608100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
607   {"exp",   0x0e708100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
608   {"sin",   0x0e808100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
609   {"cos",   0x0e908100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
610   {"tan",   0x0ea08100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
611   {"asn",   0x0eb08100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
612   {"acs",   0x0ec08100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
613   {"atn",   0x0ed08100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
614   {"urd",   0x0ee08100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
615   {"nrm",   0x0ef08100, "sde",  round_flags, FPU_ALL,      do_fp_monadic},
616   {"adf",   0x0e000100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
617   {"suf",   0x0e200100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
618   {"rsf",   0x0e300100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
619   {"muf",   0x0e100100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
620   {"dvf",   0x0e400100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
621   {"rdf",   0x0e500100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
622   {"pow",   0x0e600100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
623   {"rpw",   0x0e700100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
624   {"rmf",   0x0e800100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
625   {"fml",   0x0e900100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
626   {"fdv",   0x0ea00100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
627   {"frd",   0x0eb00100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
628   {"pol",   0x0ec00100, "sde",  round_flags, FPU_ALL,      do_fp_dyadic},
629   {"cmf",   0x0e90f110, NULL,   except_flag, FPU_ALL,      do_fp_cmp},
630   {"cnf",   0x0eb0f110, NULL,   except_flag, FPU_ALL,      do_fp_cmp},
631 /* The FPA10 data sheet suggests that the 'E' of cmfe/cnfe should not
632    be an optional suffix, but part of the instruction.  To be compatible,
633    we accept either.  */
634   {"cmfe",  0x0ed0f110, NULL,   NULL,        FPU_ALL,      do_fp_cmp},
635   {"cnfe",  0x0ef0f110, NULL,   NULL,        FPU_ALL,      do_fp_cmp},
636   {"flt",   0x0e000110, "sde",  round_flags, FPU_ALL,      do_fp_from_reg},
637   {"fix",   0x0e100110, NULL,   fix_flags,   FPU_ALL,      do_fp_to_reg},
638
639 /* Generic copressor instructions.  */
640   {"cdp",   0x0e000000, NULL,  NULL,         ARM_2UP,      do_cdp},
641   {"ldc",   0x0c100000, NULL,  cplong_flag,  ARM_2UP,      do_lstc},
642   {"stc",   0x0c000000, NULL,  cplong_flag,  ARM_2UP,      do_lstc},
643   {"mcr",   0x0e000010, NULL,  NULL,         ARM_2UP,      do_co_reg},
644   {"mrc",   0x0e100010, NULL,  NULL,         ARM_2UP,      do_co_reg},
645 };
646
647 /* Defines for various bits that we will want to toggle.  */
648 #define INST_IMMEDIATE  0x02000000
649 #define OFFSET_REG      0x02000000
650 #define HWOFFSET_IMM    0x00400000
651 #define SHIFT_BY_REG    0x00000010
652 #define PRE_INDEX       0x01000000
653 #define INDEX_UP        0x00800000
654 #define WRITE_BACK      0x00200000
655 #define LDM_TYPE_2_OR_3 0x00400000
656
657 #define LITERAL_MASK    0xf000f000
658 #define COND_MASK       0xf0000000
659 #define OPCODE_MASK     0xfe1fffff
660 #define DATA_OP_SHIFT   21
661
662 /* Codes to distinguish the arithmetic instructions.  */
663 #define OPCODE_AND      0
664 #define OPCODE_EOR      1
665 #define OPCODE_SUB      2
666 #define OPCODE_RSB      3
667 #define OPCODE_ADD      4
668 #define OPCODE_ADC      5
669 #define OPCODE_SBC      6
670 #define OPCODE_RSC      7
671 #define OPCODE_TST      8
672 #define OPCODE_TEQ      9
673 #define OPCODE_CMP      10
674 #define OPCODE_CMN      11
675 #define OPCODE_ORR      12
676 #define OPCODE_MOV      13
677 #define OPCODE_BIC      14
678 #define OPCODE_MVN      15
679
680 static void do_t_nop            PARAMS ((char *));
681 static void do_t_arit           PARAMS ((char *));
682 static void do_t_add            PARAMS ((char *));
683 static void do_t_asr            PARAMS ((char *));
684 static void do_t_branch9        PARAMS ((char *));
685 static void do_t_branch12       PARAMS ((char *));
686 static void do_t_branch23       PARAMS ((char *));
687 static void do_t_bx             PARAMS ((char *));
688 static void do_t_compare        PARAMS ((char *));
689 static void do_t_ldmstm         PARAMS ((char *));
690 static void do_t_ldr            PARAMS ((char *));
691 static void do_t_ldrb           PARAMS ((char *));
692 static void do_t_ldrh           PARAMS ((char *));
693 static void do_t_lds            PARAMS ((char *));
694 static void do_t_lsl            PARAMS ((char *));
695 static void do_t_lsr            PARAMS ((char *));
696 static void do_t_mov            PARAMS ((char *));
697 static void do_t_push_pop       PARAMS ((char *));
698 static void do_t_str            PARAMS ((char *));
699 static void do_t_strb           PARAMS ((char *));
700 static void do_t_strh           PARAMS ((char *));
701 static void do_t_sub            PARAMS ((char *));
702 static void do_t_swi            PARAMS ((char *));
703 static void do_t_adr            PARAMS ((char *));
704
705 #define T_OPCODE_MUL 0x4340
706 #define T_OPCODE_TST 0x4200
707 #define T_OPCODE_CMN 0x42c0
708 #define T_OPCODE_NEG 0x4240
709 #define T_OPCODE_MVN 0x43c0
710
711 #define T_OPCODE_ADD_R3 0x1800
712 #define T_OPCODE_SUB_R3 0x1a00
713 #define T_OPCODE_ADD_HI 0x4400
714 #define T_OPCODE_ADD_ST 0xb000
715 #define T_OPCODE_SUB_ST 0xb080
716 #define T_OPCODE_ADD_SP 0xa800
717 #define T_OPCODE_ADD_PC 0xa000
718 #define T_OPCODE_ADD_I8 0x3000
719 #define T_OPCODE_SUB_I8 0x3800
720 #define T_OPCODE_ADD_I3 0x1c00
721 #define T_OPCODE_SUB_I3 0x1e00
722
723 #define T_OPCODE_ASR_R  0x4100
724 #define T_OPCODE_LSL_R  0x4080
725 #define T_OPCODE_LSR_R  0x40c0
726 #define T_OPCODE_ASR_I  0x1000
727 #define T_OPCODE_LSL_I  0x0000
728 #define T_OPCODE_LSR_I  0x0800
729
730 #define T_OPCODE_MOV_I8 0x2000
731 #define T_OPCODE_CMP_I8 0x2800
732 #define T_OPCODE_CMP_LR 0x4280
733 #define T_OPCODE_MOV_HR 0x4600
734 #define T_OPCODE_CMP_HR 0x4500
735
736 #define T_OPCODE_LDR_PC 0x4800
737 #define T_OPCODE_LDR_SP 0x9800
738 #define T_OPCODE_STR_SP 0x9000
739 #define T_OPCODE_LDR_IW 0x6800
740 #define T_OPCODE_STR_IW 0x6000
741 #define T_OPCODE_LDR_IH 0x8800
742 #define T_OPCODE_STR_IH 0x8000
743 #define T_OPCODE_LDR_IB 0x7800
744 #define T_OPCODE_STR_IB 0x7000
745 #define T_OPCODE_LDR_RW 0x5800
746 #define T_OPCODE_STR_RW 0x5000
747 #define T_OPCODE_LDR_RH 0x5a00
748 #define T_OPCODE_STR_RH 0x5200
749 #define T_OPCODE_LDR_RB 0x5c00
750 #define T_OPCODE_STR_RB 0x5400
751
752 #define T_OPCODE_PUSH   0xb400
753 #define T_OPCODE_POP    0xbc00
754
755 #define T_OPCODE_BRANCH 0xe7fe
756
757 static int thumb_reg            PARAMS ((char ** str, int hi_lo));
758
759 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
760 #define THUMB_REG_LO    0x1
761 #define THUMB_REG_HI    0x2
762 #define THUMB_REG_ANY   0x3
763
764 #define THUMB_H1        0x0080
765 #define THUMB_H2        0x0040
766
767 #define THUMB_ASR 0
768 #define THUMB_LSL 1
769 #define THUMB_LSR 2
770
771 #define THUMB_MOVE 0
772 #define THUMB_COMPARE 1
773
774 #define THUMB_LOAD 0
775 #define THUMB_STORE 1
776
777 #define THUMB_PP_PC_LR 0x0100
778
779 /* These three are used for immediate shifts, do not alter.  */
780 #define THUMB_WORD 2
781 #define THUMB_HALFWORD 1
782 #define THUMB_BYTE 0
783
784 struct thumb_opcode 
785 {
786   CONST char *  template;       /* Basic string to match */
787   unsigned long value;          /* Basic instruction code */
788   int           size;
789   unsigned long          variants;    /* Which CPU variants this exists for */
790   void (*       parms) PARAMS ((char *));  /* Function to call to parse args */
791 };
792
793 static CONST struct thumb_opcode tinsns[] =
794 {
795   {"adc",       0x4140,         2,      ARM_THUMB, do_t_arit},
796   {"add",       0x0000,         2,      ARM_THUMB, do_t_add},
797   {"and",       0x4000,         2,      ARM_THUMB, do_t_arit},
798   {"asr",       0x0000,         2,      ARM_THUMB, do_t_asr},
799   {"b",         T_OPCODE_BRANCH, 2,     ARM_THUMB, do_t_branch12},
800   {"beq",       0xd0fe,         2,      ARM_THUMB, do_t_branch9},
801   {"bne",       0xd1fe,         2,      ARM_THUMB, do_t_branch9},
802   {"bcs",       0xd2fe,         2,      ARM_THUMB, do_t_branch9},
803   {"bhs",       0xd2fe,         2,      ARM_THUMB, do_t_branch9},
804   {"bcc",       0xd3fe,         2,      ARM_THUMB, do_t_branch9},
805   {"bul",       0xd3fe,         2,      ARM_THUMB, do_t_branch9},
806   {"blo",       0xd3fe,         2,      ARM_THUMB, do_t_branch9},
807   {"bmi",       0xd4fe,         2,      ARM_THUMB, do_t_branch9},
808   {"bpl",       0xd5fe,         2,      ARM_THUMB, do_t_branch9},
809   {"bvs",       0xd6fe,         2,      ARM_THUMB, do_t_branch9},
810   {"bvc",       0xd7fe,         2,      ARM_THUMB, do_t_branch9},
811   {"bhi",       0xd8fe,         2,      ARM_THUMB, do_t_branch9},
812   {"bls",       0xd9fe,         2,      ARM_THUMB, do_t_branch9},
813   {"bge",       0xdafe,         2,      ARM_THUMB, do_t_branch9},
814   {"blt",       0xdbfe,         2,      ARM_THUMB, do_t_branch9},
815   {"bgt",       0xdcfe,         2,      ARM_THUMB, do_t_branch9},
816   {"ble",       0xddfe,         2,      ARM_THUMB, do_t_branch9},
817   {"bal",       0xdefe,         2,      ARM_THUMB, do_t_branch9},
818   {"bic",       0x4380,         2,      ARM_THUMB, do_t_arit},
819   {"bl",        0xf7fffffe,     4,      ARM_THUMB, do_t_branch23},
820   {"bx",        0x4700,         2,      ARM_THUMB, do_t_bx},
821   {"cmn",       T_OPCODE_CMN,   2,      ARM_THUMB, do_t_arit},
822   {"cmp",       0x0000,         2,      ARM_THUMB, do_t_compare},
823   {"eor",       0x4040,         2,      ARM_THUMB, do_t_arit},
824   {"ldmia",     0xc800,         2,      ARM_THUMB, do_t_ldmstm},
825   {"ldr",       0x0000,         2,      ARM_THUMB, do_t_ldr},
826   {"ldrb",      0x0000,         2,      ARM_THUMB, do_t_ldrb},
827   {"ldrh",      0x0000,         2,      ARM_THUMB, do_t_ldrh},
828   {"ldrsb",     0x5600,         2,      ARM_THUMB, do_t_lds},
829   {"ldrsh",     0x5e00,         2,      ARM_THUMB, do_t_lds},
830   {"ldsb",      0x5600,         2,      ARM_THUMB, do_t_lds},
831   {"ldsh",      0x5e00,         2,      ARM_THUMB, do_t_lds},
832   {"lsl",       0x0000,         2,      ARM_THUMB, do_t_lsl},
833   {"lsr",       0x0000,         2,      ARM_THUMB, do_t_lsr},
834   {"mov",       0x0000,         2,      ARM_THUMB, do_t_mov},
835   {"mul",       T_OPCODE_MUL,   2,      ARM_THUMB, do_t_arit},
836   {"mvn",       T_OPCODE_MVN,   2,      ARM_THUMB, do_t_arit},
837   {"neg",       T_OPCODE_NEG,   2,      ARM_THUMB, do_t_arit},
838   {"orr",       0x4300,         2,      ARM_THUMB, do_t_arit},
839   {"pop",       0xbc00,         2,      ARM_THUMB, do_t_push_pop},
840   {"push",      0xb400,         2,      ARM_THUMB, do_t_push_pop},
841   {"ror",       0x41c0,         2,      ARM_THUMB, do_t_arit},
842   {"sbc",       0x4180,         2,      ARM_THUMB, do_t_arit},
843   {"stmia",     0xc000,         2,      ARM_THUMB, do_t_ldmstm},
844   {"str",       0x0000,         2,      ARM_THUMB, do_t_str},
845   {"strb",      0x0000,         2,      ARM_THUMB, do_t_strb},
846   {"strh",      0x0000,         2,      ARM_THUMB, do_t_strh},
847   {"swi",       0xdf00,         2,      ARM_THUMB, do_t_swi},
848   {"sub",       0x0000,         2,      ARM_THUMB, do_t_sub},
849   {"tst",       T_OPCODE_TST,   2,      ARM_THUMB, do_t_arit},
850   /* Pseudo ops: */
851   {"adr",       0x0000,         2,      ARM_THUMB, do_t_adr},
852   {"nop",       0x46C0,         2,      ARM_THUMB, do_t_nop},      /* mov r8,r8 */
853 };
854
855 struct reg_entry
856 {
857   CONST char * name;
858   int          number;
859 };
860
861 #define int_register(reg) ((reg) >= 0 && (reg) <= 15)
862 #define cp_register(reg) ((reg) >= 32 && (reg) <= 47)
863 #define fp_register(reg) ((reg) >= 16 && (reg) <= 23)
864
865 #define REG_PC  15
866 #define REG_LR  14
867 #define REG_SP  13
868
869 /* These are the standard names.  Users can add aliases with .req  */
870 static CONST struct reg_entry reg_table[] =
871 {
872   /* Processor Register Numbers.  */
873   {"r0", 0},    {"r1", 1},      {"r2", 2},      {"r3", 3},
874   {"r4", 4},    {"r5", 5},      {"r6", 6},      {"r7", 7},
875   {"r8", 8},    {"r9", 9},      {"r10", 10},    {"r11", 11},
876   {"r12", 12},  {"r13", REG_SP},{"r14", REG_LR},{"r15", REG_PC},
877   /* APCS conventions.  */
878   {"a1", 0},    {"a2", 1},    {"a3", 2},     {"a4", 3},
879   {"v1", 4},    {"v2", 5},    {"v3", 6},     {"v4", 7},     {"v5", 8},
880   {"v6", 9},    {"sb", 9},    {"v7", 10},    {"sl", 10},
881   {"fp", 11},   {"ip", 12},   {"sp", REG_SP},{"lr", REG_LR},{"pc", REG_PC},
882   /* ATPCS additions to APCS conventions.  */
883   {"wr", 7},    {"v8", 11},
884   /* FP Registers.  */
885   {"f0", 16},   {"f1", 17},   {"f2", 18},   {"f3", 19},
886   {"f4", 20},   {"f5", 21},   {"f6", 22},   {"f7", 23},
887   {"c0", 32},   {"c1", 33},   {"c2", 34},   {"c3", 35},
888   {"c4", 36},   {"c5", 37},   {"c6", 38},   {"c7", 39},
889   {"c8", 40},   {"c9", 41},   {"c10", 42},  {"c11", 43},
890   {"c12", 44},  {"c13", 45},  {"c14", 46},  {"c15", 47},
891   {"cr0", 32},  {"cr1", 33},  {"cr2", 34},  {"cr3", 35},
892   {"cr4", 36},  {"cr5", 37},  {"cr6", 38},  {"cr7", 39},
893   {"cr8", 40},  {"cr9", 41},  {"cr10", 42}, {"cr11", 43},
894   {"cr12", 44}, {"cr13", 45}, {"cr14", 46}, {"cr15", 47},
895   /* ATPCS additions to float register names.  */
896   {"s0",16},    {"s1",17},      {"s2",18},      {"s3",19},
897   {"s4",20},    {"s5",21},      {"s6",22},      {"s7",23},
898   {"d0",16},    {"d1",17},      {"d2",18},      {"d3",19},
899   {"d4",20},    {"d5",21},      {"d6",22},      {"d7",23},
900   /* FIXME: At some point we need to add VFP register names.  */
901   /* Array terminator.  */
902   {NULL, 0}
903 };
904
905 #define BAD_ARGS        _("Bad arguments to instruction")
906 #define BAD_PC          _("r15 not allowed here")
907 #define BAD_FLAGS       _("Instruction should not have flags")
908 #define BAD_COND        _("Instruction is not conditional")
909
910 static struct hash_control * arm_ops_hsh = NULL;
911 static struct hash_control * arm_tops_hsh = NULL;
912 static struct hash_control * arm_cond_hsh = NULL;
913 static struct hash_control * arm_shift_hsh = NULL;
914 static struct hash_control * arm_reg_hsh = NULL;
915 static struct hash_control * arm_psr_hsh = NULL;
916
917 /* This table describes all the machine specific pseudo-ops the assembler
918    has to support.  The fields are:
919      pseudo-op name without dot
920      function to call to execute this pseudo-op
921      Integer arg to pass to the function.  */
922
923 static void s_req PARAMS ((int));
924 static void s_align PARAMS ((int));
925 static void s_bss PARAMS ((int));
926 static void s_even PARAMS ((int));
927 static void s_ltorg PARAMS ((int));
928 static void s_arm PARAMS ((int));
929 static void s_thumb PARAMS ((int));
930 static void s_code PARAMS ((int));
931 static void s_force_thumb PARAMS ((int));
932 static void s_thumb_func PARAMS ((int));
933 static void s_thumb_set PARAMS ((int));
934 static void arm_s_text PARAMS ((int));
935 static void arm_s_data PARAMS ((int));
936 #ifdef OBJ_ELF
937 static void arm_s_section PARAMS ((int));
938 static void s_arm_elf_cons PARAMS ((int));
939 #endif
940
941 static int my_get_expression PARAMS ((expressionS *, char **));
942
943 CONST pseudo_typeS md_pseudo_table[] =
944 {
945   { "req",         s_req,         0 },  /* Never called becasue '.req' does not start line */
946   { "bss",         s_bss,         0 },
947   { "align",       s_align,       0 },
948   { "arm",         s_arm,         0 },
949   { "thumb",       s_thumb,       0 },
950   { "code",        s_code,        0 },
951   { "force_thumb", s_force_thumb, 0 },
952   { "thumb_func",  s_thumb_func,  0 },
953   { "thumb_set",   s_thumb_set,   0 },
954   { "even",        s_even,        0 },
955   { "ltorg",       s_ltorg,       0 },
956   { "pool",        s_ltorg,       0 },
957   /* Allow for the effect of section changes.  */
958   { "text",        arm_s_text,    0 },
959   { "data",        arm_s_data,    0 },
960 #ifdef OBJ_ELF  
961   { "section",     arm_s_section, 0 },
962   { "section.s",   arm_s_section, 0 },
963   { "sect",        arm_s_section, 0 },
964   { "sect.s",      arm_s_section, 0 },
965   { "word",        s_arm_elf_cons, 4 },
966   { "long",        s_arm_elf_cons, 4 },
967 #else
968   { "word",        cons, 4},
969 #endif
970   { "extend",      float_cons, 'x' },
971   { "ldouble",     float_cons, 'x' },
972   { "packed",      float_cons, 'p' },
973   { 0, 0, 0 }
974 };
975
976 /* Stuff needed to resolve the label ambiguity
977    As:
978      ...
979      label:   <insn>
980    may differ from:
981      ...
982      label:
983               <insn>
984 */
985
986 symbolS *  last_label_seen;
987 static int label_is_thumb_function_name = false;
988
989 /* Literal stuff */
990
991 #define MAX_LITERAL_POOL_SIZE 1024
992
993 typedef struct literalS
994 {
995   struct expressionS  exp;
996   struct arm_it *     inst;
997 } literalT;
998
999 literalT  literals[MAX_LITERAL_POOL_SIZE];
1000 int       next_literal_pool_place = 0; /* Next free entry in the pool */
1001 int       lit_pool_num = 1; /* Next literal pool number */
1002 symbolS * current_poolP = NULL;
1003
1004 static int
1005 add_to_lit_pool ()
1006 {
1007   int lit_count = 0;
1008
1009   if (current_poolP == NULL)
1010     current_poolP = symbol_create (FAKE_LABEL_NAME, undefined_section,
1011                                    (valueT) 0, &zero_address_frag);
1012
1013   /* Check if this literal value is already in the pool:  */
1014   while (lit_count < next_literal_pool_place)
1015     {
1016       if (literals[lit_count].exp.X_op == inst.reloc.exp.X_op
1017           && inst.reloc.exp.X_op == O_constant
1018           && literals[lit_count].exp.X_add_number
1019              == inst.reloc.exp.X_add_number
1020           && literals[lit_count].exp.X_unsigned == inst.reloc.exp.X_unsigned)
1021         break;
1022       lit_count++;
1023     }
1024
1025   if (lit_count == next_literal_pool_place) /* new entry */
1026     {
1027       if (next_literal_pool_place > MAX_LITERAL_POOL_SIZE)
1028         {
1029           inst.error = _("Literal Pool Overflow");
1030           return FAIL;
1031         }
1032
1033       literals[next_literal_pool_place].exp = inst.reloc.exp;
1034       lit_count = next_literal_pool_place++;
1035     }
1036
1037   inst.reloc.exp.X_op = O_symbol;
1038   inst.reloc.exp.X_add_number = (lit_count) * 4 - 8;
1039   inst.reloc.exp.X_add_symbol = current_poolP;
1040
1041   return SUCCESS;
1042 }
1043  
1044 /* Can't use symbol_new here, so have to create a symbol and then at
1045    a later date assign it a value. Thats what these functions do.  */
1046 static void
1047 symbol_locate (symbolP, name, segment, valu, frag)
1048      symbolS *    symbolP; 
1049      CONST char * name;         /* It is copied, the caller can modify */
1050      segT         segment;      /* Segment identifier (SEG_<something>) */
1051      valueT       valu;         /* Symbol value */
1052      fragS *      frag;         /* Associated fragment */
1053 {
1054   unsigned int name_length;
1055   char * preserved_copy_of_name;
1056
1057   name_length = strlen (name) + 1;      /* +1 for \0 */
1058   obstack_grow (&notes, name, name_length);
1059   preserved_copy_of_name = obstack_finish (&notes);
1060 #ifdef STRIP_UNDERSCORE
1061   if (preserved_copy_of_name[0] == '_')
1062     preserved_copy_of_name++;
1063 #endif
1064
1065 #ifdef tc_canonicalize_symbol_name
1066   preserved_copy_of_name =
1067     tc_canonicalize_symbol_name (preserved_copy_of_name);
1068 #endif
1069
1070   S_SET_NAME (symbolP, preserved_copy_of_name);
1071
1072   S_SET_SEGMENT (symbolP, segment);
1073   S_SET_VALUE (symbolP, valu);
1074   symbol_clear_list_pointers(symbolP);
1075
1076   symbol_set_frag (symbolP, frag);
1077
1078   /* Link to end of symbol chain.  */
1079   {
1080     extern int symbol_table_frozen;
1081     if (symbol_table_frozen)
1082       abort ();
1083   }
1084
1085   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
1086
1087   obj_symbol_new_hook (symbolP);
1088
1089 #ifdef tc_symbol_new_hook
1090   tc_symbol_new_hook (symbolP);
1091 #endif
1092  
1093 #ifdef DEBUG_SYMS
1094   verify_symbol_chain (symbol_rootP, symbol_lastP);
1095 #endif /* DEBUG_SYMS */
1096 }
1097
1098 /* Check that an immediate is valid, and if so,
1099    convert it to the right format.  */
1100 static unsigned int
1101 validate_immediate (val)
1102      unsigned int val;
1103 {
1104   unsigned int a;
1105   unsigned int i;
1106   
1107 #define rotate_left(v, n) (v << n | v >> (32 - n))
1108   
1109   for (i = 0; i < 32; i += 2)
1110     if ((a = rotate_left (val, i)) <= 0xff)
1111       return a | (i << 7); /* 12-bit pack: [shift-cnt,const] */
1112   
1113   return FAIL;
1114 }
1115
1116 /* Check to see if an immediate can be computed as two seperate immediate
1117    values, added together.  We already know that this value cannot be
1118    computed by just one ARM instruction.  */
1119 static unsigned int
1120 validate_immediate_twopart (val, highpart)
1121      unsigned int val;
1122      unsigned int * highpart;
1123 {
1124   unsigned int a;
1125   unsigned int i;
1126   
1127   for (i = 0; i < 32; i += 2)
1128     if (((a = rotate_left (val, i)) & 0xff) != 0)
1129       {
1130         if (a & 0xff00)
1131           {
1132             if (a & ~ 0xffff)
1133               continue;
1134             * highpart = (a  >> 8) | ((i + 24) << 7);
1135           }
1136         else if (a & 0xff0000)
1137           {
1138             if (a & 0xff000000)
1139               continue;
1140
1141             * highpart = (a >> 16) | ((i + 16) << 7);
1142           }
1143         else
1144           {
1145             assert (a & 0xff000000);
1146
1147             * highpart = (a >> 24) | ((i + 8) << 7);
1148           }
1149
1150         return (a & 0xff) | (i << 7);
1151       }
1152   
1153   return FAIL;
1154 }
1155
1156 static int
1157 validate_offset_imm (val, hwse)
1158      unsigned int val;
1159      int hwse;
1160 {
1161   if ((hwse && val > 255) || val > 4095)
1162      return FAIL;
1163   return val;
1164 }
1165
1166     
1167 static void
1168 s_req (a)
1169      int a ATTRIBUTE_UNUSED;
1170 {
1171   as_bad (_("Invalid syntax for .req directive."));
1172 }
1173
1174 static void
1175 s_bss (ignore)
1176      int ignore ATTRIBUTE_UNUSED;
1177 {
1178   /* We don't support putting frags in the BSS segment, we fake it by
1179      marking in_bss, then looking at s_skip for clues?.. */
1180   subseg_set (bss_section, 0);
1181   demand_empty_rest_of_line ();
1182 }
1183
1184 static void
1185 s_even (ignore)
1186      int ignore ATTRIBUTE_UNUSED;
1187 {
1188   if (!need_pass_2)             /* Never make frag if expect extra pass. */
1189     frag_align (1, 0, 0);
1190   
1191   record_alignment (now_seg, 1);
1192   
1193   demand_empty_rest_of_line ();
1194 }
1195
1196 static void
1197 s_ltorg (ignored)
1198      int ignored ATTRIBUTE_UNUSED;
1199 {
1200   int lit_count = 0;
1201   char sym_name[20];
1202
1203   if (current_poolP == NULL)
1204     return;
1205
1206   /* Align pool as you have word accesses */
1207   /* Only make a frag if we have to ... */
1208   if (!need_pass_2)
1209     frag_align (2, 0, 0);
1210
1211   record_alignment (now_seg, 2);
1212
1213   sprintf (sym_name, "$$lit_\002%x", lit_pool_num++);
1214
1215   symbol_locate (current_poolP, sym_name, now_seg,
1216                  (valueT) frag_now_fix (), frag_now);
1217   symbol_table_insert (current_poolP);
1218
1219   ARM_SET_THUMB (current_poolP, thumb_mode);
1220   
1221 #if defined OBJ_COFF || defined OBJ_ELF
1222   ARM_SET_INTERWORK (current_poolP, support_interwork);
1223 #endif
1224   
1225   while (lit_count < next_literal_pool_place)
1226     /* First output the expression in the instruction to the pool.  */
1227     emit_expr (&(literals[lit_count++].exp), 4); /* .word */
1228
1229   next_literal_pool_place = 0;
1230   current_poolP = NULL;
1231 }
1232
1233 static void
1234 s_align (unused)        /* Same as s_align_ptwo but align 0 => align 2 */
1235      int unused ATTRIBUTE_UNUSED;
1236 {
1237   register int temp;
1238   register long temp_fill;
1239   long max_alignment = 15;
1240
1241   temp = get_absolute_expression ();
1242   if (temp > max_alignment)
1243     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
1244   else if (temp < 0)
1245     {
1246       as_bad (_("Alignment negative. 0 assumed."));
1247       temp = 0;
1248     }
1249
1250   if (*input_line_pointer == ',')
1251     {
1252       input_line_pointer++;
1253       temp_fill = get_absolute_expression ();
1254     }
1255   else
1256     temp_fill = 0;
1257
1258   if (!temp)
1259     temp = 2;
1260
1261   /* Only make a frag if we HAVE to. . . */
1262   if (temp && !need_pass_2)
1263     frag_align (temp, (int) temp_fill, 0);
1264   demand_empty_rest_of_line ();
1265
1266   record_alignment (now_seg, temp);
1267 }
1268
1269 static void
1270 s_force_thumb (ignore)
1271      int ignore ATTRIBUTE_UNUSED;
1272 {
1273   /* If we are not already in thumb mode go into it, EVEN if
1274      the target processor does not support thumb instructions.
1275      This is used by gcc/config/arm/lib1funcs.asm for example
1276      to compile interworking support functions even if the
1277      target processor should not support interworking.  */
1278      
1279   if (! thumb_mode)
1280     {
1281       thumb_mode = 2;
1282       
1283       record_alignment (now_seg, 1);
1284     }
1285   
1286   demand_empty_rest_of_line ();
1287 }
1288
1289 static void
1290 s_thumb_func (ignore)
1291      int ignore ATTRIBUTE_UNUSED;
1292 {
1293   /* The following label is the name/address of the start of a Thumb function.
1294      We need to know this for the interworking support.  */
1295
1296   label_is_thumb_function_name = true;
1297   
1298   demand_empty_rest_of_line ();
1299 }
1300
1301 /* Perform a .set directive, but also mark the alias as
1302    being a thumb function.  */
1303
1304 static void
1305 s_thumb_set (equiv)
1306      int equiv;
1307 {
1308   /* XXX the following is a duplicate of the code for s_set() in read.c
1309      We cannot just call that code as we need to get at the symbol that
1310      is created.  */
1311   register char *    name;
1312   register char      delim;
1313   register char *    end_name;
1314   register symbolS * symbolP;
1315
1316   /*
1317    * Especial apologies for the random logic:
1318    * this just grew, and could be parsed much more simply!
1319    * Dean in haste.
1320    */
1321   name      = input_line_pointer;
1322   delim     = get_symbol_end ();
1323   end_name  = input_line_pointer;
1324   *end_name = delim;
1325   
1326   SKIP_WHITESPACE ();
1327
1328   if (*input_line_pointer != ',')
1329     {
1330       *end_name = 0;
1331       as_bad (_("Expected comma after name \"%s\""), name);
1332       *end_name = delim;
1333       ignore_rest_of_line ();
1334       return;
1335     }
1336
1337   input_line_pointer++;
1338   *end_name = 0;
1339
1340   if (name[0] == '.' && name[1] == '\0')
1341     {
1342       /* XXX - this should not happen to .thumb_set  */
1343       abort ();
1344     }
1345
1346   if ((symbolP = symbol_find (name)) == NULL
1347       && (symbolP = md_undefined_symbol (name)) == NULL)
1348     {
1349 #ifndef NO_LISTING
1350       /* When doing symbol listings, play games with dummy fragments living
1351          outside the normal fragment chain to record the file and line info
1352          for this symbol.  */
1353       if (listing & LISTING_SYMBOLS)
1354         {
1355           extern struct list_info_struct * listing_tail;
1356           fragS * dummy_frag = (fragS *) xmalloc (sizeof(fragS));
1357           memset (dummy_frag, 0, sizeof(fragS));
1358           dummy_frag->fr_type = rs_fill;
1359           dummy_frag->line = listing_tail;
1360           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
1361           dummy_frag->fr_symbol = symbolP;
1362         }
1363       else
1364 #endif
1365         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
1366                             
1367 #ifdef OBJ_COFF
1368       /* "set" symbols are local unless otherwise specified. */
1369       SF_SET_LOCAL (symbolP);
1370 #endif /* OBJ_COFF */
1371     }                           /* make a new symbol */
1372
1373   symbol_table_insert (symbolP);
1374
1375   * end_name = delim;
1376
1377   if (equiv
1378       && S_IS_DEFINED (symbolP)
1379       && S_GET_SEGMENT (symbolP) != reg_section)
1380     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
1381
1382   pseudo_set (symbolP);
1383   
1384   demand_empty_rest_of_line ();
1385
1386   /* XXX Now we come to the Thumb specific bit of code.  */
1387   
1388   THUMB_SET_FUNC (symbolP, 1);
1389   ARM_SET_THUMB (symbolP, 1);
1390 #if defined OBJ_ELF || defined OBJ_COFF
1391   ARM_SET_INTERWORK (symbolP, support_interwork);
1392 #endif
1393 }
1394
1395 /* If we change section we must dump the literal pool first.  */
1396 static void
1397 arm_s_text (ignore)
1398      int ignore;
1399 {
1400   if (now_seg != text_section)
1401     s_ltorg (0);
1402   
1403 #ifdef OBJ_ELF
1404   obj_elf_text (ignore);
1405 #else
1406   s_text (ignore);
1407 #endif
1408 }
1409
1410 static void
1411 arm_s_data (ignore)
1412      int ignore;
1413 {
1414   if (flag_readonly_data_in_text)
1415     {
1416       if (now_seg != text_section)
1417         s_ltorg (0);
1418     }
1419   else if (now_seg != data_section)
1420     s_ltorg (0);
1421   
1422 #ifdef OBJ_ELF
1423   obj_elf_data (ignore);
1424 #else
1425   s_data (ignore);
1426 #endif
1427 }
1428
1429 #ifdef OBJ_ELF
1430 static void
1431 arm_s_section (ignore)
1432      int ignore;
1433 {
1434   s_ltorg (0);
1435
1436   obj_elf_section (ignore);
1437 }
1438 #endif
1439
1440 static void
1441 opcode_select (width)
1442      int width;
1443 {
1444   switch (width)
1445     {
1446     case 16:
1447       if (! thumb_mode)
1448         {
1449           if (! (cpu_variant & ARM_THUMB))
1450             as_bad (_("selected processor does not support THUMB opcodes"));
1451           thumb_mode = 1;
1452           /* No need to force the alignment, since we will have been
1453              coming from ARM mode, which is word-aligned. */
1454           record_alignment (now_seg, 1);
1455         }
1456       break;
1457
1458     case 32:
1459       if (thumb_mode)
1460         {
1461           if ((cpu_variant & ARM_ANY) == ARM_THUMB)
1462             as_bad (_("selected processor does not support ARM opcodes"));
1463           thumb_mode = 0;
1464           if (!need_pass_2)
1465             frag_align (2, 0, 0);
1466           record_alignment (now_seg, 1);
1467         }
1468       break;
1469
1470     default:
1471       as_bad (_("invalid instruction size selected (%d)"), width);
1472     }
1473 }
1474
1475 static void
1476 s_arm (ignore)
1477      int ignore ATTRIBUTE_UNUSED;
1478 {
1479   opcode_select (32);
1480   demand_empty_rest_of_line ();
1481 }
1482
1483 static void
1484 s_thumb (ignore)
1485      int ignore ATTRIBUTE_UNUSED;
1486 {
1487   opcode_select (16);
1488   demand_empty_rest_of_line ();
1489 }
1490
1491 static void
1492 s_code (unused)
1493      int unused ATTRIBUTE_UNUSED;
1494 {
1495   register int temp;
1496
1497   temp = get_absolute_expression ();
1498   switch (temp)
1499     {
1500     case 16:
1501     case 32:
1502       opcode_select (temp);
1503       break;
1504
1505     default:
1506       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
1507     }
1508 }
1509
1510 static void
1511 end_of_line (str)
1512      char * str;
1513 {
1514   skip_whitespace (str);
1515
1516   if (* str != '\0')
1517     inst.error = _("Garbage following instruction");
1518 }
1519
1520 static int
1521 skip_past_comma (str)
1522      char ** str;
1523 {
1524   char *p = *str, c;
1525   int comma = 0;
1526     
1527   while ((c = *p) == ' ' || c == ',')
1528     {
1529       p++;
1530       if (c == ',' && comma++)
1531         return FAIL;
1532     }
1533
1534   if (c == '\0')
1535     return FAIL;
1536
1537   *str = p;
1538   return comma ? SUCCESS : FAIL;
1539 }
1540
1541 /* A standard register must be given at this point.
1542    Shift is the place to put it in inst.instruction.
1543    Restores input start point on err.
1544    Returns the reg#, or FAIL.  */
1545 static int
1546 reg_required_here (str, shift)
1547      char ** str;
1548      int     shift;
1549 {
1550   static char buff [128]; /* XXX */
1551   int    reg;
1552   char * start = *str;
1553
1554   if ((reg = arm_reg_parse (str)) != FAIL && int_register (reg))
1555     {
1556       if (shift >= 0)
1557         inst.instruction |= reg << shift;
1558       return reg;
1559     }
1560
1561   /* Restore the start point, we may have got a reg of the wrong class.  */
1562   *str = start;
1563   
1564   /* In the few cases where we might be able to accept something else
1565      this error can be overridden.  */
1566   sprintf (buff, _("Register expected, not '%.100s'"), start);
1567   inst.error = buff;
1568
1569   return FAIL;
1570 }
1571
1572 static CONST struct asm_psr *
1573 arm_psr_parse (ccp)
1574      register char ** ccp;
1575 {
1576   char * start = * ccp;
1577   char   c;
1578   char * p;
1579   CONST struct asm_psr * psr;
1580
1581   p = start;
1582
1583   /* Skip to the end of the next word in the input stream.  */
1584   do
1585     {
1586       c = *p++;
1587     }
1588   while (isalpha (c) || c == '_');
1589
1590   /* Terminate the word.  */
1591   *--p = 0;
1592
1593   /* Now locate the word in the psr hash table.  */
1594   psr = (CONST struct asm_psr *) hash_find (arm_psr_hsh, start);
1595
1596   /* Restore the input stream.  */
1597   *p = c;
1598
1599   /* If we found a valid match, advance the
1600      stream pointer past the end of the word.  */
1601   *ccp = p;
1602
1603   return psr;
1604 }
1605
1606 /* Parse the input looking for a PSR flag.  */
1607 static int
1608 psr_required_here (str)
1609      char ** str;
1610 {
1611   char * start = *str;
1612   CONST struct asm_psr * psr;
1613   
1614   psr = arm_psr_parse (str);
1615
1616   if (psr)
1617     {
1618       /* If this is the SPSR that is being modified, set the R bit.  */
1619       if (! psr->cpsr)
1620         inst.instruction |= SPSR_BIT;
1621
1622       /* Set the psr flags in the MSR instruction.  */
1623       inst.instruction |= psr->field << PSR_SHIFT;
1624       
1625       return SUCCESS;
1626     }
1627
1628   /* In the few cases where we might be able to accept
1629      something else this error can be overridden.  */
1630   inst.error = _("flag for {c}psr instruction expected");
1631
1632   /* Restore the start point.  */
1633   *str = start;
1634   return FAIL;
1635 }
1636
1637 static int
1638 co_proc_number (str)
1639      char ** str;
1640 {
1641   int processor, pchar;
1642
1643   skip_whitespace (* str);
1644
1645   /* The data sheet seems to imply that just a number on its own is valid
1646      here, but the RISC iX assembler seems to accept a prefix 'p'.  We will
1647      accept either.  */
1648   if (**str == 'p' || **str == 'P')
1649     (*str)++;
1650
1651   pchar = *(*str)++;
1652   if (pchar >= '0' && pchar <= '9')
1653     {
1654       processor = pchar - '0';
1655       if (**str >= '0' && **str <= '9')
1656         {
1657           processor = processor * 10 + *(*str)++ - '0';
1658           if (processor > 15)
1659             {
1660               inst.error = _("Illegal co-processor number");
1661               return FAIL;
1662             }
1663         }
1664     }
1665   else
1666     {
1667       inst.error = _("Bad or missing co-processor number");
1668       return FAIL;
1669     }
1670
1671   inst.instruction |= processor << 8;
1672   return SUCCESS;
1673 }
1674
1675 static int
1676 cp_opc_expr (str, where, length)
1677      char ** str;
1678      int where;
1679      int length;
1680 {
1681   expressionS expr;
1682
1683   skip_whitespace (* str);
1684
1685   memset (&expr, '\0', sizeof (expr));
1686
1687   if (my_get_expression (&expr, str))
1688     return FAIL;
1689   if (expr.X_op != O_constant)
1690     {
1691       inst.error = _("bad or missing expression");
1692       return FAIL;
1693     }
1694
1695   if ((expr.X_add_number & ((1 << length) - 1)) != expr.X_add_number)
1696     {
1697       inst.error = _("immediate co-processor expression too large");
1698       return FAIL;
1699     }
1700
1701   inst.instruction |= expr.X_add_number << where;
1702   return SUCCESS;
1703 }
1704
1705 static int
1706 cp_reg_required_here (str, where)
1707      char ** str;
1708      int     where;
1709 {
1710   int    reg;
1711   char * start = *str;
1712
1713   if ((reg = arm_reg_parse (str)) != FAIL && cp_register (reg))
1714     {
1715       reg &= 15;
1716       inst.instruction |= reg << where;
1717       return reg;
1718     }
1719
1720   /* In the few cases where we might be able to accept something else
1721      this error can be overridden.  */
1722   inst.error = _("Co-processor register expected");
1723
1724   /* Restore the start point.  */
1725   *str = start;
1726   return FAIL;
1727 }
1728
1729 static int
1730 fp_reg_required_here (str, where)
1731      char ** str;
1732      int     where;
1733 {
1734   int reg;
1735   char * start = *str;
1736
1737   if ((reg = arm_reg_parse (str)) != FAIL && fp_register (reg))
1738     {
1739       reg &= 7;
1740       inst.instruction |= reg << where;
1741       return reg;
1742     }
1743
1744   /* In the few cases where we might be able to accept something else
1745      this error can be overridden.  */
1746   inst.error = _("Floating point register expected");
1747
1748   /* Restore the start point.  */
1749   *str = start;
1750   return FAIL;
1751 }
1752
1753 static int
1754 cp_address_offset (str)
1755      char ** str;
1756 {
1757   int offset;
1758
1759   skip_whitespace (* str);
1760
1761   if (! is_immediate_prefix (**str))
1762     {
1763       inst.error = _("immediate expression expected");
1764       return FAIL;
1765     }
1766
1767   (*str)++;
1768   
1769   if (my_get_expression (& inst.reloc.exp, str))
1770     return FAIL;
1771   
1772   if (inst.reloc.exp.X_op == O_constant)
1773     {
1774       offset = inst.reloc.exp.X_add_number;
1775       
1776       if (offset & 3)
1777         {
1778           inst.error = _("co-processor address must be word aligned");
1779           return FAIL;
1780         }
1781
1782       if (offset > 1023 || offset < -1023)
1783         {
1784           inst.error = _("offset too large");
1785           return FAIL;
1786         }
1787
1788       if (offset >= 0)
1789         inst.instruction |= INDEX_UP;
1790       else
1791         offset = -offset;
1792
1793       inst.instruction |= offset >> 2;
1794     }
1795   else
1796     inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
1797
1798   return SUCCESS;
1799 }
1800
1801 static int
1802 cp_address_required_here (str)
1803      char ** str;
1804 {
1805   char * p = * str;
1806   int    pre_inc = 0;
1807   int    write_back = 0;
1808
1809   if (*p == '[')
1810     {
1811       int reg;
1812
1813       p++;
1814       skip_whitespace (p);
1815
1816       if ((reg = reg_required_here (& p, 16)) == FAIL)
1817         return FAIL;
1818
1819       skip_whitespace (p);
1820
1821       if (*p == ']')
1822         {
1823           p++;
1824           
1825           if (skip_past_comma (& p) == SUCCESS)
1826             {
1827               /* [Rn], #expr */
1828               write_back = WRITE_BACK;
1829               
1830               if (reg == REG_PC)
1831                 {
1832                   inst.error = _("pc may not be used in post-increment");
1833                   return FAIL;
1834                 }
1835
1836               if (cp_address_offset (& p) == FAIL)
1837                 return FAIL;
1838             }
1839           else
1840             pre_inc = PRE_INDEX | INDEX_UP;
1841         }
1842       else
1843         {
1844           /* '['Rn, #expr']'[!] */
1845
1846           if (skip_past_comma (& p) == FAIL)
1847             {
1848               inst.error = _("pre-indexed expression expected");
1849               return FAIL;
1850             }
1851
1852           pre_inc = PRE_INDEX;
1853           
1854           if (cp_address_offset (& p) == FAIL)
1855             return FAIL;
1856
1857           skip_whitespace (p);
1858
1859           if (*p++ != ']')
1860             {
1861               inst.error = _("missing ]");
1862               return FAIL;
1863             }
1864
1865           skip_whitespace (p);
1866
1867           if (*p == '!')
1868             {
1869               if (reg == REG_PC)
1870                 {
1871                   inst.error = _("pc may not be used with write-back");
1872                   return FAIL;
1873                 }
1874
1875               p++;
1876               write_back = WRITE_BACK;
1877             }
1878         }
1879     }
1880   else
1881     {
1882       if (my_get_expression (&inst.reloc.exp, &p))
1883         return FAIL;
1884
1885       inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
1886       inst.reloc.exp.X_add_number -= 8;  /* PC rel adjust */
1887       inst.reloc.pc_rel = 1;
1888       inst.instruction |= (REG_PC << 16);
1889       pre_inc = PRE_INDEX;
1890     }
1891
1892   inst.instruction |= write_back | pre_inc;
1893   *str = p;
1894   return SUCCESS;
1895 }
1896
1897 static void
1898 do_nop (str, flags)
1899      char * str;
1900      unsigned long flags;
1901 {
1902   /* Do nothing really.  */
1903   inst.instruction |= flags; /* This is pointless.  */
1904   end_of_line (str);
1905   return;
1906 }
1907
1908 static void
1909 do_mrs (str, flags)
1910      char *str;
1911      unsigned long flags;
1912 {
1913   int skip = 0;
1914   
1915   /* Only one syntax.  */
1916   skip_whitespace (str);
1917
1918   if (reg_required_here (&str, 12) == FAIL)
1919     {
1920       inst.error = BAD_ARGS;
1921       return;
1922     }
1923
1924   if (skip_past_comma (&str) == FAIL)
1925     {
1926       inst.error = _("comma expected after register name");
1927       return;
1928     }
1929
1930   skip_whitespace (str);
1931
1932   if (   strcmp (str, "CPSR") == 0
1933       || strcmp (str, "SPSR") == 0
1934          /* Lower case versions for backwards compatability.  */
1935       || strcmp (str, "cpsr") == 0
1936       || strcmp (str, "spsr") == 0)
1937     skip = 4;
1938   /* This is for backwards compatability with older toolchains.  */
1939   else if (strcmp (str, "cpsr_all") == 0
1940            || strcmp (str, "spsr_all") == 0)
1941     skip = 7;
1942   else
1943     {
1944       inst.error = _("{C|S}PSR expected");
1945       return;
1946     }
1947
1948   if (* str == 's' || * str == 'S')
1949     inst.instruction |= SPSR_BIT;
1950   str += skip;
1951   
1952   inst.instruction |= flags;
1953   end_of_line (str);
1954 }
1955
1956 /* Two possible forms:
1957       "{C|S}PSR_<field>, Rm",
1958       "{C|S}PSR_f, #expression".  */
1959 static void
1960 do_msr (str, flags)
1961      char * str;
1962      unsigned long flags;
1963 {
1964   skip_whitespace (str);
1965
1966   if (psr_required_here (& str) == FAIL)
1967     return;
1968     
1969   if (skip_past_comma (& str) == FAIL)
1970     {
1971       inst.error = _("comma missing after psr flags");
1972       return;
1973     }
1974
1975   skip_whitespace (str);
1976
1977   if (reg_required_here (& str, 0) != FAIL)
1978     {
1979       inst.error = NULL; 
1980       inst.instruction |= flags;
1981       end_of_line (str);
1982       return;
1983     }
1984
1985   if (! is_immediate_prefix (* str))
1986     {
1987       inst.error = _("only a register or immediate value can follow a psr flag");
1988       return;
1989     }
1990
1991   str ++;
1992   inst.error = NULL;
1993   
1994   if (my_get_expression (& inst.reloc.exp, & str))
1995     {
1996       inst.error = _("only a register or immediate value can follow a psr flag");
1997       return;
1998     }
1999   
2000   if (inst.instruction & ((PSR_c | PSR_x | PSR_s) << PSR_SHIFT))
2001     {
2002       inst.error = _("can only set flag field with immediate value");
2003       return;
2004     }
2005   
2006   flags |= INST_IMMEDIATE;
2007           
2008   if (inst.reloc.exp.X_add_symbol)
2009     {
2010       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
2011       inst.reloc.pc_rel = 0;
2012     }
2013   else
2014     {
2015       unsigned value = validate_immediate (inst.reloc.exp.X_add_number);
2016       
2017       if (value == (unsigned) FAIL)
2018         {
2019           inst.error = _("Invalid constant");
2020           return;
2021         }
2022       
2023       inst.instruction |= value;
2024     }
2025
2026   inst.error = NULL; 
2027   inst.instruction |= flags;
2028   end_of_line (str);
2029 }
2030
2031 /* Long Multiply Parser
2032    UMULL RdLo, RdHi, Rm, Rs
2033    SMULL RdLo, RdHi, Rm, Rs
2034    UMLAL RdLo, RdHi, Rm, Rs
2035    SMLAL RdLo, RdHi, Rm, Rs
2036 */   
2037 static void
2038 do_mull (str, flags)
2039      char * str;
2040      unsigned long flags;
2041 {
2042   int rdlo, rdhi, rm, rs;
2043
2044   /* Only one format "rdlo, rdhi, rm, rs" */
2045   skip_whitespace (str);
2046
2047   if ((rdlo = reg_required_here (&str, 12)) == FAIL)
2048     {
2049       inst.error = BAD_ARGS;
2050       return;
2051     }
2052
2053   if (skip_past_comma (&str) == FAIL
2054       || (rdhi = reg_required_here (&str, 16)) == FAIL)
2055     {
2056       inst.error = BAD_ARGS;
2057       return;
2058     }
2059
2060   if (skip_past_comma (&str) == FAIL
2061       || (rm = reg_required_here (&str, 0)) == FAIL)
2062     {
2063       inst.error = BAD_ARGS;
2064       return;
2065     }
2066
2067   /* rdhi, rdlo and rm must all be different */
2068   if (rdlo == rdhi || rdlo == rm || rdhi == rm)
2069     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
2070
2071   if (skip_past_comma (&str) == FAIL
2072       || (rs = reg_required_here (&str, 8)) == FAIL)
2073     {
2074       inst.error = BAD_ARGS;
2075       return;
2076     }
2077
2078   if (rdhi == REG_PC || rdhi == REG_PC || rdhi == REG_PC || rdhi == REG_PC)
2079     {
2080       inst.error = BAD_PC;
2081       return;
2082     }
2083    
2084   inst.instruction |= flags;
2085   end_of_line (str);
2086   return;
2087 }
2088
2089 static void
2090 do_mul (str, flags)
2091      char *        str;
2092      unsigned long flags;
2093 {
2094   int rd, rm;
2095   
2096   /* Only one format "rd, rm, rs" */
2097   skip_whitespace (str);
2098
2099   if ((rd = reg_required_here (&str, 16)) == FAIL)
2100     {
2101       inst.error = BAD_ARGS;
2102       return;
2103     }
2104
2105   if (rd == REG_PC)
2106     {
2107       inst.error = BAD_PC;
2108       return;
2109     }
2110
2111   if (skip_past_comma (&str) == FAIL
2112       || (rm = reg_required_here (&str, 0)) == FAIL)
2113     {
2114       inst.error = BAD_ARGS;
2115       return;
2116     }
2117
2118   if (rm == REG_PC)
2119     {
2120       inst.error = BAD_PC;
2121       return;
2122     }
2123
2124   if (rm == rd)
2125     as_tsktsk (_("rd and rm should be different in mul"));
2126
2127   if (skip_past_comma (&str) == FAIL
2128       || (rm = reg_required_here (&str, 8)) == FAIL)
2129     {
2130       inst.error = BAD_ARGS;
2131       return;
2132     }
2133
2134   if (rm == REG_PC)
2135     {
2136       inst.error = BAD_PC;
2137       return;
2138     }
2139
2140   inst.instruction |= flags;
2141   end_of_line (str);
2142   return;
2143 }
2144
2145 static void
2146 do_mla (str, flags)
2147      char *        str;
2148      unsigned long flags;
2149 {
2150   int rd, rm;
2151
2152   /* Only one format "rd, rm, rs, rn" */
2153   skip_whitespace (str);
2154
2155   if ((rd = reg_required_here (&str, 16)) == FAIL)
2156     {
2157       inst.error = BAD_ARGS;
2158       return;
2159     }
2160
2161   if (rd == REG_PC)
2162     {
2163       inst.error = BAD_PC;
2164       return;
2165     }
2166
2167   if (skip_past_comma (&str) == FAIL
2168       || (rm = reg_required_here (&str, 0)) == FAIL)
2169     {
2170       inst.error = BAD_ARGS;
2171       return;
2172     }
2173
2174   if (rm == REG_PC)
2175     {
2176       inst.error = BAD_PC;
2177       return;
2178     }
2179
2180   if (rm == rd)
2181     as_tsktsk (_("rd and rm should be different in mla"));
2182
2183   if (skip_past_comma (&str) == FAIL
2184       || (rd = reg_required_here (&str, 8)) == FAIL
2185       || skip_past_comma (&str) == FAIL
2186       || (rm = reg_required_here (&str, 12)) == FAIL)
2187     {
2188       inst.error = BAD_ARGS;
2189       return;
2190     }
2191
2192   if (rd == REG_PC || rm == REG_PC)
2193     {
2194       inst.error = BAD_PC;
2195       return;
2196     }
2197
2198   inst.instruction |= flags;
2199   end_of_line (str);
2200   return;
2201 }
2202
2203 /* Returns the index into fp_values of a floating point number, or -1 if
2204    not in the table.  */
2205 static int
2206 my_get_float_expression (str)
2207      char ** str;
2208 {
2209   LITTLENUM_TYPE words[MAX_LITTLENUMS];
2210   char *         save_in;
2211   expressionS    exp;
2212   int            i;
2213   int            j;
2214
2215   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
2216   /* Look for a raw floating point number */
2217   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
2218       && is_end_of_line [(unsigned char) *save_in])
2219     {
2220       for (i = 0; i < NUM_FLOAT_VALS; i++)
2221         {
2222           for (j = 0; j < MAX_LITTLENUMS; j++)
2223             {
2224               if (words[j] != fp_values[i][j])
2225                 break;
2226             }
2227
2228           if (j == MAX_LITTLENUMS)
2229             {
2230               *str = save_in;
2231               return i;
2232             }
2233         }
2234     }
2235
2236   /* Try and parse a more complex expression, this will probably fail
2237      unless the code uses a floating point prefix (eg "0f") */
2238   save_in = input_line_pointer;
2239   input_line_pointer = *str;
2240   if (expression (&exp) == absolute_section
2241       && exp.X_op == O_big
2242       && exp.X_add_number < 0)
2243     {
2244       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
2245          Ditto for 15.  */
2246       if (gen_to_words (words, 5, (long)15) == 0)
2247         {
2248           for (i = 0; i < NUM_FLOAT_VALS; i++)
2249             {
2250               for (j = 0; j < MAX_LITTLENUMS; j++)
2251                 {
2252                   if (words[j] != fp_values[i][j])
2253                     break;
2254                 }
2255
2256               if (j == MAX_LITTLENUMS)
2257                 {
2258                   *str = input_line_pointer;
2259                   input_line_pointer = save_in;
2260                   return i;
2261                 }
2262             }
2263         }
2264     }
2265
2266   *str = input_line_pointer;
2267   input_line_pointer = save_in;
2268   return -1;
2269 }
2270
2271 /* Return true if anything in the expression is a bignum */
2272 static int
2273 walk_no_bignums (sp)
2274      symbolS * sp;
2275 {
2276   if (symbol_get_value_expression (sp)->X_op == O_big)
2277     return 1;
2278
2279   if (symbol_get_value_expression (sp)->X_add_symbol)
2280     {
2281       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
2282               || (symbol_get_value_expression (sp)->X_op_symbol
2283                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
2284     }
2285
2286   return 0;
2287 }
2288
2289 static int
2290 my_get_expression (ep, str)
2291      expressionS * ep;
2292      char ** str;
2293 {
2294   char * save_in;
2295   segT   seg;
2296   
2297   save_in = input_line_pointer;
2298   input_line_pointer = *str;
2299   seg = expression (ep);
2300
2301 #ifdef OBJ_AOUT
2302   if (seg != absolute_section
2303       && seg != text_section
2304       && seg != data_section
2305       && seg != bss_section
2306       && seg != undefined_section)
2307     {
2308       inst.error = _("bad_segment");
2309       *str = input_line_pointer;
2310       input_line_pointer = save_in;
2311       return 1;
2312     }
2313 #endif
2314
2315   /* Get rid of any bignums now, so that we don't generate an error for which
2316      we can't establish a line number later on.  Big numbers are never valid
2317      in instructions, which is where this routine is always called.  */
2318   if (ep->X_op == O_big
2319       || (ep->X_add_symbol
2320           && (walk_no_bignums (ep->X_add_symbol)
2321               || (ep->X_op_symbol
2322                   && walk_no_bignums (ep->X_op_symbol)))))
2323     {
2324       inst.error = _("Invalid constant");
2325       *str = input_line_pointer;
2326       input_line_pointer = save_in;
2327       return 1;
2328     }
2329
2330   *str = input_line_pointer;
2331   input_line_pointer = save_in;
2332   return 0;
2333 }
2334
2335 /* unrestrict should be one if <shift> <register> is permitted for this
2336    instruction */
2337
2338 static int
2339 decode_shift (str, unrestrict)
2340      char ** str;
2341      int     unrestrict;
2342 {
2343   struct asm_shift * shft;
2344   char * p;
2345   char   c;
2346     
2347   skip_whitespace (* str);
2348     
2349   for (p = *str; isalpha (*p); p++)
2350     ;
2351
2352   if (p == *str)
2353     {
2354       inst.error = _("Shift expression expected");
2355       return FAIL;
2356     }
2357
2358   c = *p;
2359   *p = '\0';
2360   shft = (struct asm_shift *) hash_find (arm_shift_hsh, *str);
2361   *p = c;
2362   if (shft)
2363     {
2364       if (!strncmp (*str, "rrx", 3)
2365           || !strncmp (*str, "RRX", 3))
2366         {
2367           *str = p;
2368           inst.instruction |= shft->value;
2369           return SUCCESS;
2370         }
2371
2372       skip_whitespace (p);
2373       
2374       if (unrestrict && reg_required_here (&p, 8) != FAIL)
2375         {
2376           inst.instruction |= shft->value | SHIFT_BY_REG;
2377           *str = p;
2378           return SUCCESS;
2379         }
2380       else if (is_immediate_prefix (* p))
2381         {
2382           inst.error = NULL;
2383           p++;
2384           if (my_get_expression (&inst.reloc.exp, &p))
2385             return FAIL;
2386
2387           /* Validate some simple #expressions */
2388           if (inst.reloc.exp.X_op == O_constant)
2389             {
2390               unsigned num = inst.reloc.exp.X_add_number;
2391
2392               /* Reject operations greater than 32, or lsl #32 */
2393               if (num > 32 || (num == 32 && shft->value == 0))
2394                 {
2395                   inst.error = _("Invalid immediate shift");
2396                   return FAIL;
2397                 }
2398
2399               /* Shifts of zero should be converted to lsl (which is zero)*/
2400               if (num == 0)
2401                 {
2402                   *str = p;
2403                   return SUCCESS;
2404                 }
2405
2406               /* Shifts of 32 are encoded as 0, for those shifts that
2407                  support it.  */
2408               if (num == 32)
2409                 num = 0;
2410
2411               inst.instruction |= (num << 7) | shft->value;
2412               *str = p;
2413               return SUCCESS;
2414             }
2415
2416           inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
2417           inst.reloc.pc_rel = 0;
2418           inst.instruction |= shft->value;
2419           *str = p;
2420           return SUCCESS;
2421         }
2422       else
2423         {
2424           inst.error = unrestrict ? _("shift requires register or #expression")
2425             : _("shift requires #expression");
2426           *str = p;
2427           return FAIL;
2428         }
2429     }
2430
2431   inst.error = _("Shift expression expected");
2432   return FAIL;
2433 }
2434
2435 /* Do those data_ops which can take a negative immediate constant */
2436 /* by altering the instuction. A bit of a hack really */
2437 /*      MOV <-> MVN
2438         AND <-> BIC
2439         ADC <-> SBC
2440         by inverting the second operand, and
2441         ADD <-> SUB
2442         CMP <-> CMN
2443         by negating the second operand.
2444 */
2445 static int
2446 negate_data_op (instruction, value)
2447      unsigned long * instruction;
2448      unsigned long   value;
2449 {
2450   int op, new_inst;
2451   unsigned long negated, inverted;
2452
2453   negated = validate_immediate (-value);
2454   inverted = validate_immediate (~value);
2455
2456   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
2457   switch (op)
2458     {
2459       /* First negates */
2460     case OPCODE_SUB:             /* ADD <-> SUB */
2461       new_inst = OPCODE_ADD;
2462       value = negated;
2463       break;
2464
2465     case OPCODE_ADD: 
2466       new_inst = OPCODE_SUB;               
2467       value = negated;
2468       break;
2469
2470     case OPCODE_CMP:             /* CMP <-> CMN */
2471       new_inst = OPCODE_CMN;
2472       value = negated;
2473       break;
2474
2475     case OPCODE_CMN: 
2476       new_inst = OPCODE_CMP;               
2477       value = negated;
2478       break;
2479
2480       /* Now Inverted ops */
2481     case OPCODE_MOV:             /* MOV <-> MVN */
2482       new_inst = OPCODE_MVN;               
2483       value = inverted;
2484       break;
2485
2486     case OPCODE_MVN: 
2487       new_inst = OPCODE_MOV;
2488       value = inverted;
2489       break;
2490
2491     case OPCODE_AND:             /* AND <-> BIC */ 
2492       new_inst = OPCODE_BIC;               
2493       value = inverted;
2494       break;
2495
2496     case OPCODE_BIC: 
2497       new_inst = OPCODE_AND;
2498       value = inverted;
2499       break;
2500
2501     case OPCODE_ADC:              /* ADC <-> SBC */
2502       new_inst = OPCODE_SBC;               
2503       value = inverted;
2504       break;
2505
2506     case OPCODE_SBC: 
2507       new_inst = OPCODE_ADC;
2508       value = inverted;
2509       break;
2510
2511       /* We cannot do anything */
2512     default:  
2513       return FAIL;
2514     }
2515
2516   if (value == (unsigned) FAIL)
2517     return FAIL;
2518
2519   *instruction &= OPCODE_MASK;
2520   *instruction |= new_inst << DATA_OP_SHIFT;
2521   return value; 
2522 }
2523
2524 static int
2525 data_op2 (str)
2526      char ** str;
2527 {
2528   int value;
2529   expressionS expr;
2530
2531   skip_whitespace (* str);
2532     
2533   if (reg_required_here (str, 0) != FAIL)
2534     {
2535       if (skip_past_comma (str) == SUCCESS)
2536         /* Shift operation on register.  */
2537         return decode_shift (str, NO_SHIFT_RESTRICT);
2538
2539       return SUCCESS;
2540     }
2541   else
2542     {
2543       /* Immediate expression */
2544       if (is_immediate_prefix (**str))
2545         {
2546           (*str)++;
2547           inst.error = NULL;
2548           
2549           if (my_get_expression (&inst.reloc.exp, str))
2550             return FAIL;
2551
2552           if (inst.reloc.exp.X_add_symbol)
2553             {
2554               inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
2555               inst.reloc.pc_rel = 0;
2556             }
2557           else
2558             {
2559               if (skip_past_comma (str) == SUCCESS)
2560                 {
2561                   /* #x, y -- ie explicit rotation by Y  */
2562                   if (my_get_expression (&expr, str))
2563                     return FAIL;
2564
2565                   if (expr.X_op != O_constant)
2566                     {
2567                       inst.error = _("Constant expression expected");
2568                       return FAIL;
2569                     }
2570  
2571                   /* Rotate must be a multiple of 2 */
2572                   if (((unsigned) expr.X_add_number) > 30
2573                       || (expr.X_add_number & 1) != 0
2574                       || ((unsigned) inst.reloc.exp.X_add_number) > 255)
2575                     {
2576                       inst.error = _("Invalid constant");
2577                       return FAIL;
2578                     }
2579                   inst.instruction |= INST_IMMEDIATE;
2580                   inst.instruction |= inst.reloc.exp.X_add_number;
2581                   inst.instruction |= expr.X_add_number << 7;
2582                   return SUCCESS;
2583                 }
2584
2585               /* Implicit rotation, select a suitable one  */
2586               value = validate_immediate (inst.reloc.exp.X_add_number);
2587
2588               if (value == FAIL)
2589                 {
2590                   /* Can't be done, perhaps the code reads something like
2591                      "add Rd, Rn, #-n", where "sub Rd, Rn, #n" would be ok */
2592                   if ((value = negate_data_op (&inst.instruction,
2593                                                inst.reloc.exp.X_add_number))
2594                       == FAIL)
2595                     {
2596                       inst.error = _("Invalid constant");
2597                       return FAIL;
2598                     }
2599                 }
2600
2601               inst.instruction |= value;
2602             }
2603
2604           inst.instruction |= INST_IMMEDIATE;
2605           return SUCCESS;
2606         }
2607
2608       (*str)++;
2609       inst.error = _("Register or shift expression expected");
2610       return FAIL;
2611     }
2612 }
2613
2614 static int
2615 fp_op2 (str)
2616      char ** str;
2617 {
2618   skip_whitespace (* str);
2619
2620   if (fp_reg_required_here (str, 0) != FAIL)
2621     return SUCCESS;
2622   else
2623     {
2624       /* Immediate expression */
2625       if (*((*str)++) == '#')
2626         {
2627           int i;
2628
2629           inst.error = NULL;
2630
2631           skip_whitespace (* str);
2632
2633           /* First try and match exact strings, this is to guarantee that
2634              some formats will work even for cross assembly */
2635
2636           for (i = 0; fp_const[i]; i++)
2637             {
2638               if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
2639                 {
2640                   char *start = *str;
2641
2642                   *str += strlen (fp_const[i]);
2643                   if (is_end_of_line[(unsigned char) **str])
2644                     {
2645                       inst.instruction |= i + 8;
2646                       return SUCCESS;
2647                     }
2648                   *str = start;
2649                 }
2650             }
2651
2652           /* Just because we didn't get a match doesn't mean that the
2653              constant isn't valid, just that it is in a format that we
2654              don't automatically recognize.  Try parsing it with
2655              the standard expression routines.  */
2656           if ((i = my_get_float_expression (str)) >= 0)
2657             {
2658               inst.instruction |= i + 8;
2659               return SUCCESS;
2660             }
2661
2662           inst.error = _("Invalid floating point immediate expression");
2663           return FAIL;
2664         }
2665       inst.error = _("Floating point register or immediate expression expected");
2666       return FAIL;
2667     }
2668 }
2669
2670 static void
2671 do_arit (str, flags)
2672      char *        str;
2673      unsigned long flags;
2674 {
2675   skip_whitespace (str);
2676
2677   if (reg_required_here (&str, 12) == FAIL
2678       || skip_past_comma (&str) == FAIL
2679       || reg_required_here (&str, 16) == FAIL
2680       || skip_past_comma (&str) == FAIL
2681       || data_op2 (&str) == FAIL)
2682     {
2683       if (!inst.error)
2684         inst.error = BAD_ARGS;
2685       return;
2686     }
2687
2688   inst.instruction |= flags;
2689   end_of_line (str);
2690   return;
2691 }
2692
2693 static void
2694 do_adr (str, flags)
2695      char *        str;
2696      unsigned long flags;
2697 {
2698   /* This is a pseudo-op of the form "adr rd, label" to be converted
2699      into a relative address of the form "add rd, pc, #label-.-8".  */
2700   skip_whitespace (str);
2701
2702   if (reg_required_here (&str, 12) == FAIL
2703       || skip_past_comma (&str) == FAIL
2704       || my_get_expression (&inst.reloc.exp, &str))
2705     {
2706       if (!inst.error)
2707         inst.error = BAD_ARGS;
2708       return;
2709     }
2710   
2711   /* Frag hacking will turn this into a sub instruction if the offset turns
2712      out to be negative.  */
2713   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
2714   inst.reloc.exp.X_add_number -= 8; /* PC relative adjust.  */
2715   inst.reloc.pc_rel = 1;
2716   inst.instruction |= flags;
2717   
2718   end_of_line (str);
2719 }
2720
2721 static void
2722 do_adrl (str, flags)
2723      char *        str;
2724      unsigned long flags;
2725 {
2726   /* This is a pseudo-op of the form "adrl rd, label" to be converted
2727      into a relative address of the form:
2728         add rd, pc, #low(label-.-8)"
2729         add rd, rd, #high(label-.-8)"   */
2730
2731   skip_whitespace (str);
2732
2733   if (reg_required_here (& str, 12) == FAIL
2734       || skip_past_comma (& str) == FAIL
2735       || my_get_expression (& inst.reloc.exp, & str))
2736     {
2737       if (!inst.error)
2738         inst.error = BAD_ARGS;
2739       return;
2740     }
2741   
2742   end_of_line (str);
2743   
2744   /* Frag hacking will turn this into a sub instruction if the offset turns
2745      out to be negative.  */
2746   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
2747   inst.reloc.exp.X_add_number -= 8; /* PC relative adjust */
2748   inst.reloc.pc_rel            = 1;
2749   inst.instruction            |= flags;
2750   inst.size                    = INSN_SIZE * 2;
2751   
2752   return;
2753 }
2754
2755 static void
2756 do_cmp (str, flags)
2757      char *        str;
2758      unsigned long flags;
2759 {
2760   skip_whitespace (str);
2761
2762   if (reg_required_here (&str, 16) == FAIL)
2763     {
2764       if (!inst.error)
2765         inst.error = BAD_ARGS;
2766       return;
2767     }
2768
2769   if (skip_past_comma (&str) == FAIL
2770       || data_op2 (&str) == FAIL)
2771     {
2772       if (!inst.error)
2773         inst.error = BAD_ARGS;
2774       return;
2775     }
2776
2777   inst.instruction |= flags;
2778   if ((flags & 0x0000f000) == 0)
2779     inst.instruction |= CONDS_BIT;
2780
2781   end_of_line (str);
2782   return;
2783 }
2784
2785 static void
2786 do_mov (str, flags)
2787      char *        str;
2788      unsigned long flags;
2789 {
2790   skip_whitespace (str);
2791
2792   if (reg_required_here (&str, 12) == FAIL)
2793     {
2794       if (!inst.error)
2795         inst.error = BAD_ARGS;
2796       return;
2797     }
2798
2799   if (skip_past_comma (&str) == FAIL
2800       || data_op2 (&str) == FAIL)
2801     {
2802       if (!inst.error)
2803         inst.error = BAD_ARGS;
2804       return;
2805     }
2806
2807   inst.instruction |= flags;
2808   end_of_line (str);
2809   return;
2810 }
2811
2812 static int
2813 ldst_extend (str, hwse)
2814      char ** str;
2815      int     hwse;
2816 {
2817   int add = INDEX_UP;
2818
2819   switch (**str)
2820     {
2821     case '#':
2822     case '$':
2823       (*str)++;
2824       if (my_get_expression (& inst.reloc.exp, str))
2825         return FAIL;
2826
2827       if (inst.reloc.exp.X_op == O_constant)
2828         {
2829           int value = inst.reloc.exp.X_add_number;
2830
2831           if ((hwse && (value < -255 || value > 255))
2832                || (value < -4095 || value > 4095))
2833             {
2834               inst.error = _("address offset too large");
2835               return FAIL;
2836             }
2837
2838           if (value < 0)
2839             {
2840               value = -value;
2841               add = 0;
2842             }
2843
2844           /* Halfword and signextension instructions have the
2845              immediate value split across bits 11..8 and bits 3..0 */
2846           if (hwse)
2847             inst.instruction |= add | HWOFFSET_IMM | ((value >> 4) << 8) | (value & 0xF);
2848           else
2849             inst.instruction |= add | value;
2850         }
2851       else
2852         {
2853           if (hwse)
2854             {
2855               inst.instruction |= HWOFFSET_IMM;
2856               inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
2857             }
2858           else
2859             inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
2860           inst.reloc.pc_rel = 0;
2861         }
2862       return SUCCESS;
2863
2864     case '-':
2865       add = 0;  /* and fall through */
2866     case '+':
2867       (*str)++; /* and fall through */
2868     default:
2869       if (reg_required_here (str, 0) == FAIL)
2870         return FAIL;
2871
2872       if (hwse)
2873         inst.instruction |= add;
2874       else
2875         {
2876           inst.instruction |= add | OFFSET_REG;
2877           if (skip_past_comma (str) == SUCCESS)
2878             return decode_shift (str, SHIFT_RESTRICT);
2879         }
2880
2881       return SUCCESS;
2882     }
2883 }
2884
2885 static void
2886 do_ldst (str, flags)
2887      char *        str;
2888      unsigned long flags;
2889 {
2890   int halfword = 0;
2891   int pre_inc = 0;
2892   int conflict_reg;
2893   int value;
2894
2895   /* This is not ideal, but it is the simplest way of dealing with the
2896      ARM7T halfword instructions (since they use a different
2897      encoding, but the same mnemonic): */
2898   halfword = (flags & 0x80000000) != 0;
2899   if (halfword)
2900     {
2901       /* This is actually a load/store of a halfword, or a
2902          signed-extension load */
2903       if ((cpu_variant & ARM_HALFWORD) == 0)
2904         {
2905           inst.error
2906             = _("Processor does not support halfwords or signed bytes");
2907           return;
2908         }
2909
2910       inst.instruction = (inst.instruction & COND_MASK)
2911                          | (flags & ~COND_MASK);
2912
2913       flags = 0;
2914     }
2915
2916   skip_whitespace (str);
2917     
2918   if ((conflict_reg = reg_required_here (& str, 12)) == FAIL)
2919     {
2920       if (!inst.error)
2921         inst.error = BAD_ARGS;
2922       return;
2923     }
2924
2925   if (skip_past_comma (& str) == FAIL)
2926     {
2927       inst.error = _("Address expected");
2928       return;
2929     }
2930
2931   if (*str == '[')
2932     {
2933       int reg;
2934
2935       str++;
2936
2937       skip_whitespace (str);
2938
2939       if ((reg = reg_required_here (&str, 16)) == FAIL)
2940         return;
2941
2942       /* Conflicts can occur on stores as well as loads.  */
2943       conflict_reg = (conflict_reg == reg);
2944
2945       skip_whitespace (str);
2946
2947       if (*str == ']')
2948         {
2949           str ++;
2950           
2951           if (skip_past_comma (&str) == SUCCESS)
2952             {
2953               /* [Rn],... (post inc) */
2954               if (ldst_extend (&str, halfword) == FAIL)
2955                 return;
2956               if (conflict_reg)
2957                 as_warn (_("%s register same as write-back base"),
2958                          (inst.instruction & LOAD_BIT) ? _("destination") : _("source") );
2959             }
2960           else
2961             {
2962               /* [Rn] */
2963               if (halfword)
2964                 inst.instruction |= HWOFFSET_IMM;
2965
2966               skip_whitespace (str);
2967
2968               if (*str == '!')
2969                {
2970                  if (conflict_reg)
2971                    as_warn (_("%s register same as write-back base"),
2972                             (inst.instruction & LOAD_BIT) ? _("destination") : _("source") );
2973                  str++;
2974                  inst.instruction |= WRITE_BACK;
2975                }
2976
2977               flags |= INDEX_UP;
2978               if (! (flags & TRANS_BIT))
2979                 pre_inc = 1;
2980             }
2981         }
2982       else
2983         {
2984           /* [Rn,...] */
2985           if (skip_past_comma (&str) == FAIL)
2986             {
2987               inst.error = _("pre-indexed expression expected");
2988               return;
2989             }
2990
2991           pre_inc = 1;
2992           if (ldst_extend (&str, halfword) == FAIL)
2993             return;
2994
2995           skip_whitespace (str);
2996
2997           if (*str++ != ']')
2998             {
2999               inst.error = _("missing ]");
3000               return;
3001             }
3002
3003           skip_whitespace (str);
3004
3005           if (*str == '!')
3006             {
3007               if (conflict_reg)
3008                 as_warn (_("%s register same as write-back base"),
3009                          (inst.instruction & LOAD_BIT) ? _("destination") : _("source") );
3010               str++;
3011               inst.instruction |= WRITE_BACK;
3012             }
3013         }
3014     }
3015   else if (*str == '=')
3016     {
3017       /* Parse an "ldr Rd, =expr" instruction; this is another pseudo op */
3018       str++;
3019
3020       skip_whitespace (str);
3021
3022       if (my_get_expression (&inst.reloc.exp, &str))
3023         return;
3024
3025       if (inst.reloc.exp.X_op != O_constant
3026           && inst.reloc.exp.X_op != O_symbol)
3027         {
3028           inst.error = _("Constant expression expected");
3029           return;
3030         }
3031
3032       if (inst.reloc.exp.X_op == O_constant
3033           && (value = validate_immediate(inst.reloc.exp.X_add_number)) != FAIL)
3034         {
3035           /* This can be done with a mov instruction */
3036           inst.instruction &= LITERAL_MASK;
3037           inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
3038           inst.instruction |= (flags & COND_MASK) | (value & 0xfff);
3039           end_of_line(str);
3040           return; 
3041         }
3042       else
3043         {
3044           /* Insert into literal pool */     
3045           if (add_to_lit_pool () == FAIL)
3046             {
3047               if (!inst.error)
3048                 inst.error = _("literal pool insertion failed"); 
3049               return;
3050             }
3051
3052           /* Change the instruction exp to point to the pool */
3053           if (halfword)
3054             {
3055               inst.instruction |= HWOFFSET_IMM;
3056               inst.reloc.type = BFD_RELOC_ARM_HWLITERAL;
3057             }
3058           else
3059             inst.reloc.type = BFD_RELOC_ARM_LITERAL;
3060           inst.reloc.pc_rel = 1;
3061           inst.instruction |= (REG_PC << 16);
3062           pre_inc = 1; 
3063         }
3064     }
3065   else
3066     {
3067       if (my_get_expression (&inst.reloc.exp, &str))
3068         return;
3069
3070       if (halfword)
3071         {
3072           inst.instruction |= HWOFFSET_IMM;
3073           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
3074         }
3075       else
3076         inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
3077 #ifndef TE_WINCE
3078       inst.reloc.exp.X_add_number -= 8;  /* PC rel adjust */
3079 #endif
3080       inst.reloc.pc_rel = 1;
3081       inst.instruction |= (REG_PC << 16);
3082       pre_inc = 1;
3083     }
3084     
3085   if (pre_inc && (flags & TRANS_BIT))
3086     inst.error = _("Pre-increment instruction with translate");
3087
3088   inst.instruction |= flags | (pre_inc ? PRE_INDEX : 0);
3089   end_of_line (str);
3090   return;
3091 }
3092
3093 static long
3094 reg_list (strp)
3095      char ** strp;
3096 {
3097   char * str = *strp;
3098   long   range = 0;
3099   int    another_range;
3100
3101   /* We come back here if we get ranges concatenated by '+' or '|' */
3102   do
3103     {
3104       another_range = 0;
3105
3106       if (*str == '{')
3107         {
3108           int in_range = 0;
3109           int cur_reg = -1;
3110       
3111           str++;
3112           do
3113             {
3114               int reg;
3115             
3116               skip_whitespace (str);
3117
3118               if ((reg = reg_required_here (& str, -1)) == FAIL)
3119                 return FAIL;
3120               
3121               if (in_range)
3122                 {
3123                   int i;
3124               
3125                   if (reg <= cur_reg)
3126                     {
3127                       inst.error = _("Bad range in register list");
3128                       return FAIL;
3129                     }
3130
3131                   for (i = cur_reg + 1; i < reg; i++)
3132                     {
3133                       if (range & (1 << i))
3134                         as_tsktsk 
3135                           (_("Warning: Duplicated register (r%d) in register list"),
3136                            i);
3137                       else
3138                         range |= 1 << i;
3139                     }
3140                   in_range = 0;
3141                 }
3142
3143               if (range & (1 << reg))
3144                 as_tsktsk (_("Warning: Duplicated register (r%d) in register list"),
3145                            reg);
3146               else if (reg <= cur_reg)
3147                 as_tsktsk (_("Warning: Register range not in ascending order"));
3148
3149               range |= 1 << reg;
3150               cur_reg = reg;
3151             } while (skip_past_comma (&str) != FAIL
3152                      || (in_range = 1, *str++ == '-'));
3153           str--;
3154           skip_whitespace (str);
3155
3156           if (*str++ != '}')
3157             {
3158               inst.error = _("Missing `}'");
3159               return FAIL;
3160             }
3161         }
3162       else
3163         {
3164           expressionS expr;
3165
3166           if (my_get_expression (&expr, &str))
3167             return FAIL;
3168
3169           if (expr.X_op == O_constant)
3170             {
3171               if (expr.X_add_number 
3172                   != (expr.X_add_number & 0x0000ffff))
3173                 {
3174                   inst.error = _("invalid register mask");
3175                   return FAIL;
3176                 }
3177
3178               if ((range & expr.X_add_number) != 0)
3179                 {
3180                   int regno = range & expr.X_add_number;
3181
3182                   regno &= -regno;
3183                   regno = (1 << regno) - 1;
3184                   as_tsktsk 
3185                     (_("Warning: Duplicated register (r%d) in register list"),
3186                      regno);
3187                 }
3188
3189               range |= expr.X_add_number;
3190             }
3191           else
3192             {
3193               if (inst.reloc.type != 0)
3194                 {
3195                   inst.error = _("expression too complex");
3196                   return FAIL;
3197                 }
3198
3199               memcpy (&inst.reloc.exp, &expr, sizeof (expressionS));
3200               inst.reloc.type = BFD_RELOC_ARM_MULTI;
3201               inst.reloc.pc_rel = 0;
3202             }
3203         }
3204
3205       skip_whitespace (str);
3206
3207       if (*str == '|' || *str == '+')
3208         {
3209           str++;
3210           another_range = 1;
3211         }
3212     } while (another_range);
3213
3214   *strp = str;
3215   return range;
3216 }
3217
3218 static void
3219 do_ldmstm (str, flags)
3220      char *        str;
3221      unsigned long flags;
3222 {
3223   int base_reg;
3224   long range;
3225
3226   skip_whitespace (str);
3227
3228   if ((base_reg = reg_required_here (&str, 16)) == FAIL)
3229     return;
3230
3231   if (base_reg == REG_PC)
3232     {
3233       inst.error = _("r15 not allowed as base register");
3234       return;
3235     }
3236
3237   skip_whitespace (str);
3238
3239   if (*str == '!')
3240     {
3241       flags |= WRITE_BACK;
3242       str++;
3243     }
3244
3245   if (skip_past_comma (&str) == FAIL
3246       || (range = reg_list (&str)) == FAIL)
3247     {
3248       if (! inst.error)
3249         inst.error = BAD_ARGS;
3250       return;
3251     }
3252
3253   if (*str == '^')
3254     {
3255       str++;
3256       flags |= LDM_TYPE_2_OR_3;
3257     }
3258
3259   inst.instruction |= flags | range;
3260   end_of_line (str);
3261   return;
3262 }
3263
3264 static void
3265 do_swi (str, flags)
3266      char *        str;
3267      unsigned long flags;
3268 {
3269   skip_whitespace (str);
3270   
3271   /* Allow optional leading '#'.  */
3272   if (is_immediate_prefix (*str))
3273     str++;
3274
3275   if (my_get_expression (& inst.reloc.exp, & str))
3276     return;
3277
3278   inst.reloc.type = BFD_RELOC_ARM_SWI;
3279   inst.reloc.pc_rel = 0;
3280   inst.instruction |= flags;
3281   
3282   end_of_line (str);
3283   
3284   return;
3285 }
3286
3287 static void
3288 do_swap (str, flags)
3289      char *        str;
3290      unsigned long flags;
3291 {
3292   int reg;
3293   
3294   skip_whitespace (str);
3295
3296   if ((reg = reg_required_here (&str, 12)) == FAIL)
3297     return;
3298
3299   if (reg == REG_PC)
3300     {
3301       inst.error = _("r15 not allowed in swap");
3302       return;
3303     }
3304
3305   if (skip_past_comma (&str) == FAIL
3306       || (reg = reg_required_here (&str, 0)) == FAIL)
3307     {
3308       if (!inst.error)
3309         inst.error = BAD_ARGS;
3310       return;
3311     }
3312
3313   if (reg == REG_PC)
3314     {
3315       inst.error = _("r15 not allowed in swap");
3316       return;
3317     }
3318
3319   if (skip_past_comma (&str) == FAIL
3320       || *str++ != '[')
3321     {
3322       inst.error = BAD_ARGS;
3323       return;
3324     }
3325
3326   skip_whitespace (str);
3327
3328   if ((reg = reg_required_here (&str, 16)) == FAIL)
3329     return;
3330
3331   if (reg == REG_PC)
3332     {
3333       inst.error = BAD_PC;
3334       return;
3335     }
3336
3337   skip_whitespace (str);
3338
3339   if (*str++ != ']')
3340     {
3341       inst.error = _("missing ]");
3342       return;
3343     }
3344
3345   inst.instruction |= flags;
3346   end_of_line (str);
3347   return;
3348 }
3349
3350 static void
3351 do_branch (str, flags)
3352      char *        str;
3353      unsigned long flags ATTRIBUTE_UNUSED;
3354 {
3355   if (my_get_expression (&inst.reloc.exp, &str))
3356     return;
3357   
3358 #ifdef OBJ_ELF
3359   {
3360     char * save_in;
3361   
3362     /* ScottB: February 5, 1998 */
3363     /* Check to see of PLT32 reloc required for the instruction.  */
3364     
3365     /* arm_parse_reloc() works on input_line_pointer.
3366        We actually want to parse the operands to the branch instruction
3367        passed in 'str'.  Save the input pointer and restore it later.  */
3368     save_in = input_line_pointer;
3369     input_line_pointer = str;
3370     if (inst.reloc.exp.X_op == O_symbol
3371         && *str == '('
3372         && arm_parse_reloc () == BFD_RELOC_ARM_PLT32)
3373       {
3374         inst.reloc.type   = BFD_RELOC_ARM_PLT32;
3375         inst.reloc.pc_rel = 0;
3376         /* Modify str to point to after parsed operands, otherwise
3377            end_of_line() will complain about the (PLT) left in str.  */
3378         str = input_line_pointer;
3379       }
3380     else
3381       {
3382         inst.reloc.type   = BFD_RELOC_ARM_PCREL_BRANCH;
3383         inst.reloc.pc_rel = 1;
3384       }
3385     input_line_pointer = save_in;
3386   }
3387 #else
3388   inst.reloc.type   = BFD_RELOC_ARM_PCREL_BRANCH;
3389   inst.reloc.pc_rel = 1;
3390 #endif /* OBJ_ELF */
3391   
3392   end_of_line (str);
3393   return;
3394 }
3395
3396 static void
3397 do_bx (str, flags)
3398      char *        str;
3399      unsigned long flags ATTRIBUTE_UNUSED;
3400 {
3401   int reg;
3402
3403   skip_whitespace (str);
3404
3405   if ((reg = reg_required_here (&str, 0)) == FAIL)
3406     {
3407       inst.error = BAD_ARGS;
3408       return;
3409     }
3410
3411   if (reg == REG_PC)
3412     inst.error = BAD_PC;
3413
3414   end_of_line (str);
3415 }
3416
3417 static void
3418 do_cdp (str, flags)
3419      char *        str;
3420      unsigned long flags ATTRIBUTE_UNUSED;
3421 {
3422   /* Co-processor data operation.
3423      Format: CDP{cond} CP#,<expr>,CRd,CRn,CRm{,<expr>}  */
3424   skip_whitespace (str);
3425
3426   if (co_proc_number (&str) == FAIL)
3427     {
3428       if (!inst.error)
3429         inst.error = BAD_ARGS;
3430       return;
3431     }
3432
3433   if (skip_past_comma (&str) == FAIL
3434       || cp_opc_expr (&str, 20,4) == FAIL)
3435     {
3436       if (!inst.error)
3437         inst.error = BAD_ARGS;
3438       return;
3439     }
3440
3441   if (skip_past_comma (&str) == FAIL
3442       || cp_reg_required_here (&str, 12) == FAIL)
3443     {
3444       if (!inst.error)
3445         inst.error = BAD_ARGS;
3446       return;
3447     }
3448
3449   if (skip_past_comma (&str) == FAIL
3450       || cp_reg_required_here (&str, 16) == FAIL)
3451     {
3452       if (!inst.error)
3453         inst.error = BAD_ARGS;
3454       return;
3455     }
3456
3457   if (skip_past_comma (&str) == FAIL
3458       || cp_reg_required_here (&str, 0) == FAIL)
3459     {
3460       if (!inst.error)
3461         inst.error = BAD_ARGS;
3462       return;
3463     }
3464
3465   if (skip_past_comma (&str) == SUCCESS)
3466     {
3467       if (cp_opc_expr (&str, 5, 3) == FAIL)
3468         {
3469           if (!inst.error)
3470             inst.error = BAD_ARGS;
3471           return;
3472         }
3473     }
3474
3475   end_of_line (str);
3476   return;
3477 }
3478
3479 static void
3480 do_lstc (str, flags)
3481      char *        str;
3482      unsigned long flags;
3483 {
3484   /* Co-processor register load/store.
3485      Format: <LDC|STC{cond}[L] CP#,CRd,<address>  */
3486
3487   skip_whitespace (str);
3488
3489   if (co_proc_number (&str) == FAIL)
3490     {
3491       if (!inst.error)
3492         inst.error = BAD_ARGS;
3493       return;
3494     }
3495
3496   if (skip_past_comma (&str) == FAIL
3497       || cp_reg_required_here (&str, 12) == FAIL)
3498     {
3499       if (!inst.error)
3500         inst.error = BAD_ARGS;
3501       return;
3502     }
3503
3504   if (skip_past_comma (&str) == FAIL
3505       || cp_address_required_here (&str) == FAIL)
3506     {
3507       if (! inst.error)
3508         inst.error = BAD_ARGS;
3509       return;
3510     }
3511
3512   inst.instruction |= flags;
3513   end_of_line (str);
3514   return;
3515 }
3516
3517 static void
3518 do_co_reg (str, flags)
3519      char *        str;
3520      unsigned long flags;
3521 {
3522   /* Co-processor register transfer.
3523      Format: <MCR|MRC>{cond} CP#,<expr1>,Rd,CRn,CRm{,<expr2>}  */
3524
3525   skip_whitespace (str);
3526
3527   if (co_proc_number (&str) == FAIL)
3528     {
3529       if (!inst.error)
3530         inst.error = BAD_ARGS;
3531       return;
3532     }
3533
3534   if (skip_past_comma (&str) == FAIL
3535       || cp_opc_expr (&str, 21, 3) == FAIL)
3536     {
3537       if (!inst.error)
3538         inst.error = BAD_ARGS;
3539       return;
3540     }
3541
3542   if (skip_past_comma (&str) == FAIL
3543       || reg_required_here (&str, 12) == FAIL)
3544     {
3545       if (!inst.error)
3546         inst.error = BAD_ARGS;
3547       return;
3548     }
3549
3550   if (skip_past_comma (&str) == FAIL
3551       || cp_reg_required_here (&str, 16) == FAIL)
3552     {
3553       if (!inst.error)
3554         inst.error = BAD_ARGS;
3555       return;
3556     }
3557
3558   if (skip_past_comma (&str) == FAIL
3559       || cp_reg_required_here (&str, 0) == FAIL)
3560     {
3561       if (!inst.error)
3562         inst.error = BAD_ARGS;
3563       return;
3564     }
3565
3566   if (skip_past_comma (&str) == SUCCESS)
3567     {
3568       if (cp_opc_expr (&str, 5, 3) == FAIL)
3569         {
3570           if (!inst.error)
3571             inst.error = BAD_ARGS;
3572           return;
3573         }
3574     }
3575   if (flags)
3576     {
3577       inst.error = BAD_COND;
3578     }
3579
3580   end_of_line (str);
3581   return;
3582 }
3583
3584 static void
3585 do_fp_ctrl (str, flags)
3586      char *        str;
3587      unsigned long flags ATTRIBUTE_UNUSED;
3588 {
3589   /* FP control registers.
3590      Format: <WFS|RFS|WFC|RFC>{cond} Rn  */
3591
3592   skip_whitespace (str);
3593
3594   if (reg_required_here (&str, 12) == FAIL)
3595     {
3596       if (!inst.error)
3597         inst.error = BAD_ARGS;
3598       return;
3599     }
3600
3601   end_of_line (str);
3602   return;
3603 }
3604
3605 static void
3606 do_fp_ldst (str, flags)
3607      char *        str;
3608      unsigned long flags ATTRIBUTE_UNUSED;
3609 {
3610   skip_whitespace (str);
3611
3612   switch (inst.suffix)
3613     {
3614     case SUFF_S:
3615       break;
3616     case SUFF_D:
3617       inst.instruction |= CP_T_X;
3618       break;
3619     case SUFF_E:
3620       inst.instruction |= CP_T_Y;
3621       break;
3622     case SUFF_P:
3623       inst.instruction |= CP_T_X | CP_T_Y;
3624       break;
3625     default:
3626       abort ();
3627     }
3628
3629   if (fp_reg_required_here (&str, 12) == FAIL)
3630     {
3631       if (!inst.error)
3632         inst.error = BAD_ARGS;
3633       return;
3634     }
3635
3636   if (skip_past_comma (&str) == FAIL
3637       || cp_address_required_here (&str) == FAIL)
3638     {
3639       if (!inst.error)
3640         inst.error = BAD_ARGS;
3641       return;
3642     }
3643
3644   end_of_line (str);
3645 }
3646
3647 static void
3648 do_fp_ldmstm (str, flags)
3649      char *        str;
3650      unsigned long flags;
3651 {
3652   int num_regs;
3653
3654   skip_whitespace (str);
3655
3656   if (fp_reg_required_here (&str, 12) == FAIL)
3657     {
3658       if (! inst.error)
3659         inst.error = BAD_ARGS;
3660       return;
3661     }
3662
3663   /* Get Number of registers to transfer */
3664   if (skip_past_comma (&str) == FAIL
3665       || my_get_expression (&inst.reloc.exp, &str))
3666     {
3667       if (! inst.error)
3668         inst.error = _("constant expression expected");
3669       return;
3670     }
3671
3672   if (inst.reloc.exp.X_op != O_constant)
3673     {
3674       inst.error = _("Constant value required for number of registers");
3675       return;
3676     }
3677
3678   num_regs = inst.reloc.exp.X_add_number;
3679
3680   if (num_regs < 1 || num_regs > 4)
3681     {
3682       inst.error = _("number of registers must be in the range [1:4]");
3683       return;
3684     }
3685
3686   switch (num_regs)
3687     {
3688     case 1:
3689       inst.instruction |= CP_T_X;
3690       break;
3691     case 2:
3692       inst.instruction |= CP_T_Y;
3693       break;
3694     case 3:
3695       inst.instruction |= CP_T_Y | CP_T_X;
3696       break;
3697     case 4:
3698       break;
3699     default:
3700       abort ();
3701     }
3702
3703   if (flags)
3704     {
3705       int reg;
3706       int write_back;
3707       int offset;
3708
3709       /* The instruction specified "ea" or "fd", so we can only accept
3710          [Rn]{!}.  The instruction does not really support stacking or
3711          unstacking, so we have to emulate these by setting appropriate
3712          bits and offsets.  */
3713       if (skip_past_comma (&str) == FAIL
3714           || *str != '[')
3715         {
3716           if (! inst.error)
3717             inst.error = BAD_ARGS;
3718           return;
3719         }
3720
3721       str++;
3722       skip_whitespace (str);
3723
3724       if ((reg = reg_required_here (&str, 16)) == FAIL)
3725         return;
3726
3727       skip_whitespace (str);
3728
3729       if (*str != ']')
3730         {
3731           inst.error = BAD_ARGS;
3732           return;
3733         }
3734
3735       str++;
3736       if (*str == '!')
3737         {
3738           write_back = 1;
3739           str++;
3740           if (reg == REG_PC)
3741             {
3742               inst.error = _("R15 not allowed as base register with write-back");
3743               return;
3744             }
3745         }
3746       else
3747         write_back = 0;
3748
3749       if (flags & CP_T_Pre)
3750         {
3751           /* Pre-decrement */
3752           offset = 3 * num_regs;
3753           if (write_back)
3754             flags |= CP_T_WB;
3755         }
3756       else
3757         {
3758           /* Post-increment */
3759           if (write_back)
3760             {
3761               flags |= CP_T_WB;
3762               offset = 3 * num_regs;
3763             }
3764           else
3765             {
3766               /* No write-back, so convert this into a standard pre-increment
3767                  instruction -- aesthetically more pleasing.  */
3768               flags = CP_T_Pre | CP_T_UD;
3769               offset = 0;
3770             }
3771         }
3772
3773       inst.instruction |= flags | offset;
3774     }
3775   else if (skip_past_comma (&str) == FAIL
3776            || cp_address_required_here (&str) == FAIL)
3777     {
3778       if (! inst.error)
3779         inst.error = BAD_ARGS;
3780       return;
3781     }
3782
3783   end_of_line (str);
3784 }
3785
3786 static void
3787 do_fp_dyadic (str, flags)
3788      char *        str;
3789      unsigned long flags;
3790 {
3791   skip_whitespace (str);
3792
3793   switch (inst.suffix)
3794     {
3795     case SUFF_S:
3796       break;
3797     case SUFF_D:
3798       inst.instruction |= 0x00000080;
3799       break;
3800     case SUFF_E:
3801       inst.instruction |= 0x00080000;
3802       break;
3803     default:
3804       abort ();
3805     }
3806
3807   if (fp_reg_required_here (&str, 12) == FAIL)
3808     {
3809       if (! inst.error)
3810         inst.error = BAD_ARGS;
3811       return;
3812     }
3813
3814   if (skip_past_comma (&str) == FAIL
3815       || fp_reg_required_here (&str, 16) == FAIL)
3816     {
3817       if (! inst.error)
3818         inst.error = BAD_ARGS;
3819       return;
3820     }
3821
3822   if (skip_past_comma (&str) == FAIL
3823       || fp_op2 (&str) == FAIL)
3824     {
3825       if (! inst.error)
3826         inst.error = BAD_ARGS;
3827       return;
3828     }
3829
3830   inst.instruction |= flags;
3831   end_of_line (str);
3832   return;
3833 }
3834
3835 static void
3836 do_fp_monadic (str, flags)
3837      char *        str;
3838      unsigned long flags;
3839 {
3840   skip_whitespace (str);
3841
3842   switch (inst.suffix)
3843     {
3844     case SUFF_S:
3845       break;
3846     case SUFF_D:
3847       inst.instruction |= 0x00000080;
3848       break;
3849     case SUFF_E:
3850       inst.instruction |= 0x00080000;
3851       break;
3852     default:
3853       abort ();
3854     }
3855
3856   if (fp_reg_required_here (&str, 12) == FAIL)
3857     {
3858       if (! inst.error)
3859         inst.error = BAD_ARGS;
3860       return;
3861     }
3862
3863   if (skip_past_comma (&str) == FAIL
3864       || fp_op2 (&str) == FAIL)
3865     {
3866       if (! inst.error)
3867         inst.error = BAD_ARGS;
3868       return;
3869     }
3870
3871   inst.instruction |= flags;
3872   end_of_line (str);
3873   return;
3874 }
3875
3876 static void
3877 do_fp_cmp (str, flags)
3878      char *        str;
3879      unsigned long flags;
3880 {
3881   skip_whitespace (str);
3882
3883   if (fp_reg_required_here (&str, 16) == FAIL)
3884     {
3885       if (! inst.error)
3886         inst.error = BAD_ARGS;
3887       return;
3888     }
3889
3890   if (skip_past_comma (&str) == FAIL
3891       || fp_op2 (&str) == FAIL)
3892     {
3893       if (! inst.error)
3894         inst.error = BAD_ARGS;
3895       return;
3896     }
3897
3898   inst.instruction |= flags;
3899   end_of_line (str);
3900   return;
3901 }
3902
3903 static void
3904 do_fp_from_reg (str, flags)
3905      char *        str;
3906      unsigned long flags;
3907 {
3908   skip_whitespace (str);
3909
3910   switch (inst.suffix)
3911     {
3912     case SUFF_S:
3913       break;
3914     case SUFF_D:
3915       inst.instruction |= 0x00000080;
3916       break;
3917     case SUFF_E:
3918       inst.instruction |= 0x00080000;
3919       break;
3920     default:
3921       abort ();
3922     }
3923
3924   if (fp_reg_required_here (&str, 16) == FAIL)
3925     {
3926       if (! inst.error)
3927         inst.error = BAD_ARGS;
3928       return;
3929     }
3930
3931   if (skip_past_comma (&str) == FAIL
3932       || reg_required_here (&str, 12) == FAIL)
3933     {
3934       if (! inst.error)
3935         inst.error = BAD_ARGS;
3936       return;
3937     }
3938
3939   inst.instruction |= flags;
3940   end_of_line (str);
3941   return;
3942 }
3943
3944 static void
3945 do_fp_to_reg (str, flags)
3946      char *        str;
3947      unsigned long flags;
3948 {
3949   skip_whitespace (str);
3950
3951   if (reg_required_here (&str, 12) == FAIL)
3952     return;
3953
3954   if (skip_past_comma (&str) == FAIL
3955       || fp_reg_required_here (&str, 0) == FAIL)
3956     {
3957       if (! inst.error)
3958         inst.error = BAD_ARGS;
3959       return;
3960     }
3961
3962   inst.instruction |= flags;
3963   end_of_line (str);
3964   return;
3965 }
3966
3967 /* Thumb specific routines */
3968
3969 /* Parse and validate that a register is of the right form, this saves
3970    repeated checking of this information in many similar cases. 
3971    Unlike the 32-bit case we do not insert the register into the opcode 
3972    here, since the position is often unknown until the full instruction 
3973    has been parsed.  */
3974 static int
3975 thumb_reg (strp, hi_lo)
3976      char ** strp;
3977      int     hi_lo;
3978 {
3979   int reg;
3980
3981   if ((reg = reg_required_here (strp, -1)) == FAIL)
3982     return FAIL;
3983
3984   switch (hi_lo)
3985     {
3986     case THUMB_REG_LO:
3987       if (reg > 7)
3988         {
3989           inst.error = _("lo register required");
3990           return FAIL;
3991         }
3992       break;
3993
3994     case THUMB_REG_HI:
3995       if (reg < 8)
3996         {
3997           inst.error = _("hi register required");
3998           return FAIL;
3999         }
4000       break;
4001
4002     default:
4003       break;
4004     }
4005
4006   return reg;
4007 }
4008
4009 /* Parse an add or subtract instruction, SUBTRACT is non-zero if the opcode
4010    was SUB.  */
4011 static void
4012 thumb_add_sub (str, subtract)
4013      char * str;
4014      int    subtract;
4015 {
4016   int Rd, Rs, Rn = FAIL;
4017
4018   skip_whitespace (str);
4019
4020   if ((Rd = thumb_reg (&str, THUMB_REG_ANY)) == FAIL
4021       || skip_past_comma (&str) == FAIL)
4022     {
4023       if (! inst.error)
4024         inst.error = BAD_ARGS;
4025       return;
4026     }
4027
4028   if (is_immediate_prefix (*str))
4029     {
4030       Rs = Rd;
4031       str++;
4032       if (my_get_expression (&inst.reloc.exp, &str))
4033         return;
4034     }
4035   else
4036     {
4037       if ((Rs = thumb_reg (&str, THUMB_REG_ANY)) == FAIL)
4038         return;
4039
4040       if (skip_past_comma (&str) == FAIL)
4041         {
4042           /* Two operand format, shuffle the registers and pretend there 
4043              are 3 */
4044           Rn = Rs;
4045           Rs = Rd;
4046         }
4047       else if (is_immediate_prefix (*str))
4048         {
4049           str++;
4050           if (my_get_expression (&inst.reloc.exp, &str))
4051             return;
4052         }
4053       else if ((Rn = thumb_reg (&str, THUMB_REG_ANY)) == FAIL)
4054         return;
4055     }
4056
4057   /* We now have Rd and Rs set to registers, and Rn set to a register or FAIL;
4058      for the latter case, EXPR contains the immediate that was found. */
4059   if (Rn != FAIL)
4060     {
4061       /* All register format.  */
4062       if (Rd > 7 || Rs > 7 || Rn > 7)
4063         {
4064           if (Rs != Rd)
4065             {
4066               inst.error = _("dest and source1 must be the same register");
4067               return;
4068             }
4069
4070           /* Can't do this for SUB */
4071           if (subtract)
4072             {
4073               inst.error = _("subtract valid only on lo regs");
4074               return;
4075             }
4076
4077           inst.instruction = (T_OPCODE_ADD_HI
4078                               | (Rd > 7 ? THUMB_H1 : 0)
4079                               | (Rn > 7 ? THUMB_H2 : 0));
4080           inst.instruction |= (Rd & 7) | ((Rn & 7) << 3);
4081         }
4082       else
4083         {
4084           inst.instruction = subtract ? T_OPCODE_SUB_R3 : T_OPCODE_ADD_R3;
4085           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
4086         }
4087     }
4088   else
4089     {
4090       /* Immediate expression, now things start to get nasty.  */
4091
4092       /* First deal with HI regs, only very restricted cases allowed:
4093          Adjusting SP, and using PC or SP to get an address.  */
4094       if ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
4095           || (Rs > 7 && Rs != REG_SP && Rs != REG_PC))
4096         {
4097           inst.error = _("invalid Hi register with immediate");
4098           return;
4099         }
4100
4101       if (inst.reloc.exp.X_op != O_constant)
4102         {
4103           /* Value isn't known yet, all we can do is store all the fragments
4104              we know about in the instruction and let the reloc hacking 
4105              work it all out.  */
4106           inst.instruction = (subtract ? 0x8000 : 0) | (Rd << 4) | Rs;
4107           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
4108         }
4109       else
4110         {
4111           int offset = inst.reloc.exp.X_add_number;
4112
4113           if (subtract)
4114             offset = -offset;
4115
4116           if (offset < 0)
4117             {
4118               offset = -offset;
4119               subtract = 1;
4120
4121               /* Quick check, in case offset is MIN_INT */
4122               if (offset < 0)
4123                 {
4124                   inst.error = _("immediate value out of range");
4125                   return;
4126                 }
4127             }
4128           else
4129             subtract = 0;
4130
4131           if (Rd == REG_SP)
4132             {
4133               if (offset & ~0x1fc)
4134                 {
4135                   inst.error = _("invalid immediate value for stack adjust");
4136                   return;
4137                 }
4138               inst.instruction = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
4139               inst.instruction |= offset >> 2;
4140             }
4141           else if (Rs == REG_PC || Rs == REG_SP)
4142             {
4143               if (subtract
4144                   || (offset & ~0x3fc))
4145                 {
4146                   inst.error = _("invalid immediate for address calculation");
4147                   return;
4148                 }
4149               inst.instruction = (Rs == REG_PC ? T_OPCODE_ADD_PC
4150                                   : T_OPCODE_ADD_SP);
4151               inst.instruction |= (Rd << 8) | (offset >> 2);
4152             }
4153           else if (Rs == Rd)
4154             {
4155               if (offset & ~0xff)
4156                 {
4157                   inst.error = _("immediate value out of range");
4158                   return;
4159                 }
4160               inst.instruction = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
4161               inst.instruction |= (Rd << 8) | offset;
4162             }
4163           else
4164             {
4165               if (offset & ~0x7)
4166                 {
4167                   inst.error = _("immediate value out of range");
4168                   return;
4169                 }
4170               inst.instruction = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
4171               inst.instruction |= Rd | (Rs << 3) | (offset << 6);
4172             }
4173         }
4174     }
4175   
4176   end_of_line (str);
4177 }
4178
4179 static void
4180 thumb_shift (str, shift)
4181      char * str;
4182      int    shift;
4183 {
4184   int Rd, Rs, Rn = FAIL;
4185
4186   skip_whitespace (str);
4187
4188   if ((Rd = thumb_reg (&str, THUMB_REG_LO)) == FAIL
4189       || skip_past_comma (&str) == FAIL)
4190     {
4191       if (! inst.error)
4192         inst.error = BAD_ARGS;
4193       return;
4194     }
4195
4196   if (is_immediate_prefix (*str))
4197     {
4198       /* Two operand immediate format, set Rs to Rd.  */
4199       Rs = Rd;
4200       str ++;
4201       if (my_get_expression (&inst.reloc.exp, &str))
4202         return;
4203     }
4204   else
4205     {
4206       if ((Rs =  thumb_reg (&str, THUMB_REG_LO)) == FAIL)
4207         return;
4208
4209       if (skip_past_comma (&str) == FAIL)
4210         {
4211           /* Two operand format, shuffle the registers and pretend there
4212              are 3 */
4213           Rn = Rs;
4214           Rs = Rd;
4215         }
4216       else if (is_immediate_prefix (*str))
4217         {
4218           str++;
4219           if (my_get_expression (&inst.reloc.exp, &str))
4220             return;
4221         }
4222       else if ((Rn = thumb_reg (&str, THUMB_REG_LO)) == FAIL)
4223         return;
4224     }
4225
4226   /* We now have Rd and Rs set to registers, and Rn set to a register or FAIL;
4227      for the latter case, EXPR contains the immediate that was found. */
4228
4229   if (Rn != FAIL)
4230     {
4231       if (Rs != Rd)
4232         {
4233           inst.error = _("source1 and dest must be same register");
4234           return;
4235         }
4236
4237       switch (shift)
4238         {
4239         case THUMB_ASR: inst.instruction = T_OPCODE_ASR_R; break;
4240         case THUMB_LSL: inst.instruction = T_OPCODE_LSL_R; break;
4241         case THUMB_LSR: inst.instruction = T_OPCODE_LSR_R; break;
4242         }
4243
4244       inst.instruction |= Rd | (Rn << 3);
4245     }
4246   else
4247     {
4248       switch (shift)
4249         {
4250         case THUMB_ASR: inst.instruction = T_OPCODE_ASR_I; break;
4251         case THUMB_LSL: inst.instruction = T_OPCODE_LSL_I; break;
4252         case THUMB_LSR: inst.instruction = T_OPCODE_LSR_I; break;
4253         }
4254
4255       if (inst.reloc.exp.X_op != O_constant)
4256         {
4257           /* Value isn't known yet, create a dummy reloc and let reloc
4258              hacking fix it up */
4259
4260           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
4261         }
4262       else
4263         {
4264           unsigned shift_value = inst.reloc.exp.X_add_number;
4265
4266           if (shift_value > 32 || (shift_value == 32 && shift == THUMB_LSL))
4267             {
4268               inst.error = _("Invalid immediate for shift");
4269               return;
4270             }
4271
4272           /* Shifts of zero are handled by converting to LSL */
4273           if (shift_value == 0)
4274             inst.instruction = T_OPCODE_LSL_I;
4275
4276           /* Shifts of 32 are encoded as a shift of zero */
4277           if (shift_value == 32)
4278             shift_value = 0;
4279
4280           inst.instruction |= shift_value << 6;
4281         }
4282
4283       inst.instruction |= Rd | (Rs << 3);
4284     }
4285   
4286   end_of_line (str);
4287 }
4288
4289 static void
4290 thumb_mov_compare (str, move)
4291      char * str;
4292      int    move;
4293 {
4294   int Rd, Rs = FAIL;
4295
4296   skip_whitespace (str);
4297
4298   if ((Rd = thumb_reg (&str, THUMB_REG_ANY)) == FAIL
4299       || skip_past_comma (&str) == FAIL)
4300     {
4301       if (! inst.error)
4302         inst.error = BAD_ARGS;
4303       return;
4304     }
4305
4306   if (is_immediate_prefix (*str))
4307     {
4308       str++;
4309       if (my_get_expression (&inst.reloc.exp, &str))
4310         return;
4311     }
4312   else if ((Rs = thumb_reg (&str, THUMB_REG_ANY)) == FAIL)
4313     return;
4314
4315   if (Rs != FAIL)
4316     {
4317       if (Rs < 8 && Rd < 8)
4318         {
4319           if (move == THUMB_MOVE)
4320             /* A move of two lowregs is encoded as ADD Rd, Rs, #0
4321                since a MOV instruction produces unpredictable results */
4322             inst.instruction = T_OPCODE_ADD_I3;
4323           else
4324             inst.instruction = T_OPCODE_CMP_LR;
4325           inst.instruction |= Rd | (Rs << 3);
4326         }
4327       else
4328         {
4329           if (move == THUMB_MOVE)
4330             inst.instruction = T_OPCODE_MOV_HR;
4331           else
4332             inst.instruction = T_OPCODE_CMP_HR;
4333
4334           if (Rd > 7)
4335             inst.instruction |= THUMB_H1;
4336
4337           if (Rs > 7)
4338             inst.instruction |= THUMB_H2;
4339
4340           inst.instruction |= (Rd & 7) | ((Rs & 7) << 3);
4341         }
4342     }
4343   else
4344     {
4345       if (Rd > 7)
4346         {
4347           inst.error = _("only lo regs allowed with immediate");
4348           return;
4349         }
4350
4351       if (move == THUMB_MOVE)
4352         inst.instruction = T_OPCODE_MOV_I8;
4353       else
4354         inst.instruction = T_OPCODE_CMP_I8;
4355
4356       inst.instruction |= Rd << 8;
4357
4358       if (inst.reloc.exp.X_op != O_constant)
4359         inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
4360       else
4361         {
4362           unsigned value = inst.reloc.exp.X_add_number;
4363
4364           if (value > 255)
4365             {
4366               inst.error = _("invalid immediate");
4367               return;
4368             }
4369
4370           inst.instruction |= value;
4371         }
4372     }
4373
4374   end_of_line (str);
4375 }
4376
4377 static void
4378 thumb_load_store (str, load_store, size)
4379      char * str;
4380      int    load_store;
4381      int    size;
4382 {
4383   int Rd, Rb, Ro = FAIL;
4384
4385   skip_whitespace (str);
4386
4387   if ((Rd = thumb_reg (&str, THUMB_REG_LO)) == FAIL
4388       || skip_past_comma (&str) == FAIL)
4389     {
4390       if (! inst.error)
4391         inst.error = BAD_ARGS;
4392       return;
4393     }
4394
4395   if (*str == '[')
4396     {
4397       str++;
4398       if ((Rb = thumb_reg (&str, THUMB_REG_ANY)) == FAIL)
4399         return;
4400
4401       if (skip_past_comma (&str) != FAIL)
4402         {
4403           if (is_immediate_prefix (*str))
4404             {
4405               str++;
4406               if (my_get_expression (&inst.reloc.exp, &str))
4407                 return;
4408             }
4409           else if ((Ro = thumb_reg (&str, THUMB_REG_LO)) == FAIL)
4410             return;
4411         }
4412       else
4413         {
4414           inst.reloc.exp.X_op = O_constant;
4415           inst.reloc.exp.X_add_number = 0;
4416         }
4417
4418       if (*str != ']')
4419         {
4420           inst.error = _("expected ']'");
4421           return;
4422         }
4423       str++;
4424     }
4425   else if (*str == '=')
4426     {
4427       /* Parse an "ldr Rd, =expr" instruction; this is another pseudo op */
4428       str++;
4429
4430       skip_whitespace (str);
4431
4432       if (my_get_expression (& inst.reloc.exp, & str))
4433         return;
4434
4435       end_of_line (str);
4436       
4437       if (   inst.reloc.exp.X_op != O_constant
4438           && inst.reloc.exp.X_op != O_symbol)
4439         {
4440           inst.error = "Constant expression expected";
4441           return;
4442         }
4443
4444       if (inst.reloc.exp.X_op == O_constant
4445           && ((inst.reloc.exp.X_add_number & ~0xFF) == 0))
4446         {
4447           /* This can be done with a mov instruction */
4448
4449           inst.instruction  = T_OPCODE_MOV_I8 | (Rd << 8);
4450           inst.instruction |= inst.reloc.exp.X_add_number;
4451           return; 
4452         }
4453
4454       /* Insert into literal pool */     
4455       if (add_to_lit_pool () == FAIL)
4456         {
4457           if (!inst.error)
4458             inst.error = "literal pool insertion failed"; 
4459           return;
4460         }
4461
4462       inst.reloc.type   = BFD_RELOC_ARM_THUMB_OFFSET;
4463       inst.reloc.pc_rel = 1;
4464       inst.instruction  = T_OPCODE_LDR_PC | (Rd << 8);
4465       inst.reloc.exp.X_add_number += 4; /* Adjust ARM pipeline offset to Thumb */
4466
4467       return;
4468     }
4469   else
4470     {
4471       if (my_get_expression (&inst.reloc.exp, &str))
4472         return;
4473
4474       inst.instruction = T_OPCODE_LDR_PC | (Rd << 8);
4475       inst.reloc.pc_rel = 1;
4476       inst.reloc.exp.X_add_number -= 4; /* Pipeline offset */
4477       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
4478       end_of_line (str);
4479       return;
4480     }
4481
4482   if (Rb == REG_PC || Rb == REG_SP)
4483     {
4484       if (size != THUMB_WORD)
4485         {
4486           inst.error = _("byte or halfword not valid for base register");
4487           return;
4488         }
4489       else if (Rb == REG_PC && load_store != THUMB_LOAD)
4490         {
4491           inst.error = _("R15 based store not allowed");
4492           return;
4493         }
4494       else if (Ro != FAIL)
4495         {
4496           inst.error = _("Invalid base register for register offset");
4497           return;
4498         }
4499
4500       if (Rb == REG_PC)
4501         inst.instruction = T_OPCODE_LDR_PC;
4502       else if (load_store == THUMB_LOAD)
4503         inst.instruction = T_OPCODE_LDR_SP;
4504       else
4505         inst.instruction = T_OPCODE_STR_SP;
4506
4507       inst.instruction |= Rd << 8;
4508       if (inst.reloc.exp.X_op == O_constant)
4509         {
4510           unsigned offset = inst.reloc.exp.X_add_number;
4511
4512           if (offset & ~0x3fc)
4513             {
4514               inst.error = _("invalid offset");
4515               return;
4516             }
4517
4518           inst.instruction |= offset >> 2;
4519         }
4520       else
4521         inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
4522     }
4523   else if (Rb > 7)
4524     {
4525       inst.error = _("invalid base register in load/store");
4526       return;
4527     }
4528   else if (Ro == FAIL)
4529     {
4530       /* Immediate offset */
4531       if (size == THUMB_WORD)
4532         inst.instruction = (load_store == THUMB_LOAD
4533                             ? T_OPCODE_LDR_IW : T_OPCODE_STR_IW);
4534       else if (size == THUMB_HALFWORD)
4535         inst.instruction = (load_store == THUMB_LOAD
4536                             ? T_OPCODE_LDR_IH : T_OPCODE_STR_IH);
4537       else
4538         inst.instruction = (load_store == THUMB_LOAD
4539                             ? T_OPCODE_LDR_IB : T_OPCODE_STR_IB);
4540
4541       inst.instruction |= Rd | (Rb << 3);
4542
4543       if (inst.reloc.exp.X_op == O_constant)
4544         {
4545           unsigned offset = inst.reloc.exp.X_add_number;
4546           
4547           if (offset & ~(0x1f << size))
4548             {
4549               inst.error = _("Invalid offset");
4550               return;
4551             }
4552           inst.instruction |= (offset >> size) << 6;
4553         }
4554       else
4555         inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
4556     }
4557   else
4558     {
4559       /* Register offset */
4560       if (size == THUMB_WORD)
4561         inst.instruction = (load_store == THUMB_LOAD
4562                             ? T_OPCODE_LDR_RW : T_OPCODE_STR_RW);
4563       else if (size == THUMB_HALFWORD)
4564         inst.instruction = (load_store == THUMB_LOAD
4565                             ? T_OPCODE_LDR_RH : T_OPCODE_STR_RH);
4566       else
4567         inst.instruction = (load_store == THUMB_LOAD
4568                             ? T_OPCODE_LDR_RB : T_OPCODE_STR_RB);
4569
4570       inst.instruction |= Rd | (Rb << 3) | (Ro << 6);
4571     }
4572
4573   end_of_line (str);
4574 }
4575
4576 static void
4577 do_t_nop (str)
4578      char * str;
4579 {
4580   /* Do nothing */
4581   end_of_line (str);
4582   return;
4583 }
4584
4585 /* Handle the Format 4 instructions that do not have equivalents in other 
4586    formats.  That is, ADC, AND, EOR, SBC, ROR, TST, NEG, CMN, ORR, MUL,
4587    BIC and MVN.  */
4588 static void
4589 do_t_arit (str)
4590      char * str;
4591 {
4592   int Rd, Rs, Rn;
4593
4594   skip_whitespace (str);
4595
4596   if ((Rd = thumb_reg (&str, THUMB_REG_LO)) == FAIL
4597       || skip_past_comma (&str) == FAIL
4598       || (Rs = thumb_reg (&str, THUMB_REG_LO)) == FAIL)
4599     {
4600         inst.error = BAD_ARGS;
4601         return;
4602     }
4603
4604   if (skip_past_comma (&str) != FAIL)
4605     {
4606       /* Three operand format not allowed for TST, CMN, NEG and MVN.
4607          (It isn't allowed for CMP either, but that isn't handled by this
4608          function.)  */
4609       if (inst.instruction == T_OPCODE_TST
4610           || inst.instruction == T_OPCODE_CMN
4611           || inst.instruction == T_OPCODE_NEG
4612           || inst.instruction == T_OPCODE_MVN)
4613         {
4614           inst.error = BAD_ARGS;
4615           return;
4616         }
4617
4618       if ((Rn = thumb_reg (&str, THUMB_REG_LO)) == FAIL)
4619         return;
4620
4621       if (Rs != Rd)
4622         {
4623           inst.error = _("dest and source1 one must be the same register");
4624           return;
4625         }
4626       Rs = Rn;
4627     }
4628
4629   if (inst.instruction == T_OPCODE_MUL
4630       && Rs == Rd)
4631     as_tsktsk (_("Rs and Rd must be different in MUL"));
4632
4633   inst.instruction |= Rd | (Rs << 3);
4634   end_of_line (str);
4635 }
4636
4637 static void
4638 do_t_add (str)
4639      char * str;
4640 {
4641   thumb_add_sub (str, 0);
4642 }
4643
4644 static void
4645 do_t_asr (str)
4646      char * str;
4647 {
4648   thumb_shift (str, THUMB_ASR);
4649 }
4650
4651 static void
4652 do_t_branch9 (str)
4653      char * str;
4654 {
4655   if (my_get_expression (&inst.reloc.exp, &str))
4656     return;
4657   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH9;
4658   inst.reloc.pc_rel = 1;
4659   end_of_line (str);
4660 }
4661
4662 static void
4663 do_t_branch12 (str)
4664      char * str;
4665 {
4666   if (my_get_expression (&inst.reloc.exp, &str))
4667     return;
4668   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH12;
4669   inst.reloc.pc_rel = 1;
4670   end_of_line (str);
4671 }
4672
4673 /* Find the real, Thumb encoded start of a Thumb function.  */
4674
4675 static symbolS *
4676 find_real_start (symbolP)
4677      symbolS * symbolP;
4678 {
4679   char *       real_start;
4680   const char * name = S_GET_NAME (symbolP);
4681   symbolS *    new_target;
4682
4683   /* This definiton must agree with the one in gcc/config/arm/thumb.c */
4684 #define STUB_NAME ".real_start_of"
4685
4686   if (name == NULL)
4687     abort();
4688
4689   /* Names that start with '.' are local labels, not function entry points.
4690      The compiler may generate BL instructions to these labels because it
4691      needs to perform a branch to a far away location.  */
4692   if (name[0] == '.')
4693     return symbolP;
4694   
4695   real_start = malloc (strlen (name) + strlen (STUB_NAME) + 1);
4696   sprintf (real_start, "%s%s", STUB_NAME, name);
4697
4698   new_target = symbol_find (real_start);
4699   
4700   if (new_target == NULL)
4701     {
4702       as_warn ("Failed to find real start of function: %s\n", name);
4703       new_target = symbolP;
4704     }
4705
4706   free (real_start);
4707
4708   return new_target;
4709 }
4710
4711
4712 static void
4713 do_t_branch23 (str)
4714      char * str;
4715 {
4716   if (my_get_expression (& inst.reloc.exp, & str))
4717     return;
4718   
4719   inst.reloc.type   = BFD_RELOC_THUMB_PCREL_BRANCH23;
4720   inst.reloc.pc_rel = 1;
4721   end_of_line (str);
4722
4723   /* If the destination of the branch is a defined symbol which does not have
4724      the THUMB_FUNC attribute, then we must be calling a function which has
4725      the (interfacearm) attribute.  We look for the Thumb entry point to that
4726      function and change the branch to refer to that function instead.  */
4727   if (   inst.reloc.exp.X_op == O_symbol
4728       && inst.reloc.exp.X_add_symbol != NULL
4729       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
4730       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
4731     inst.reloc.exp.X_add_symbol = find_real_start (inst.reloc.exp.X_add_symbol);
4732 }
4733
4734 static void
4735 do_t_bx (str)
4736      char * str;
4737 {
4738   int reg;
4739
4740   skip_whitespace (str);
4741
4742   if ((reg = thumb_reg (&str, THUMB_REG_ANY)) == FAIL)
4743     return;
4744
4745   /* This sets THUMB_H2 from the top bit of reg.  */
4746   inst.instruction |= reg << 3;
4747
4748   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
4749      should cause the alignment to be checked once it is known.  This is
4750      because BX PC only works if the instruction is word aligned.  */
4751
4752   end_of_line (str);
4753 }
4754
4755 static void
4756 do_t_compare (str)
4757      char * str;
4758 {
4759   thumb_mov_compare (str, THUMB_COMPARE);
4760 }
4761
4762 static void
4763 do_t_ldmstm (str)
4764      char * str;
4765 {
4766   int Rb;
4767   long range;
4768
4769   skip_whitespace (str);
4770
4771   if ((Rb = thumb_reg (&str, THUMB_REG_LO)) == FAIL)
4772     return;
4773
4774   if (*str != '!')
4775     as_warn (_("Inserted missing '!': load/store multiple always writes back base register"));
4776   else
4777     str++;
4778
4779   if (skip_past_comma (&str) == FAIL
4780       || (range = reg_list (&str)) == FAIL)
4781     {
4782       if (! inst.error)
4783         inst.error = BAD_ARGS;
4784       return;
4785     }
4786
4787   if (inst.reloc.type != BFD_RELOC_NONE)
4788     {
4789       /* This really doesn't seem worth it. */
4790       inst.reloc.type = BFD_RELOC_NONE;
4791       inst.error = _("Expression too complex");
4792       return;
4793     }
4794
4795   if (range & ~0xff)
4796     {
4797       inst.error = _("only lo-regs valid in load/store multiple");
4798       return;
4799     }
4800
4801   inst.instruction |= (Rb << 8) | range;
4802   end_of_line (str);
4803 }
4804
4805 static void
4806 do_t_ldr (str)
4807      char * str;
4808 {
4809   thumb_load_store (str, THUMB_LOAD, THUMB_WORD);
4810 }
4811
4812 static void
4813 do_t_ldrb (str)
4814      char * str;
4815 {
4816   thumb_load_store (str, THUMB_LOAD, THUMB_BYTE);
4817 }
4818
4819 static void
4820 do_t_ldrh (str)
4821      char * str;
4822 {
4823   thumb_load_store (str, THUMB_LOAD, THUMB_HALFWORD);
4824 }
4825
4826 static void
4827 do_t_lds (str)
4828      char * str;
4829 {
4830   int Rd, Rb, Ro;
4831
4832   skip_whitespace (str);
4833
4834   if ((Rd = thumb_reg (&str, THUMB_REG_LO)) == FAIL
4835       || skip_past_comma (&str) == FAIL
4836       || *str++ != '['
4837       || (Rb = thumb_reg (&str, THUMB_REG_LO)) == FAIL
4838       || skip_past_comma (&str) == FAIL
4839       || (Ro = thumb_reg (&str, THUMB_REG_LO)) == FAIL
4840       || *str++ != ']')
4841     {
4842       if (! inst.error)
4843         inst.error = _("Syntax: ldrs[b] Rd, [Rb, Ro]");
4844       return;
4845     }
4846
4847   inst.instruction |= Rd | (Rb << 3) | (Ro << 6);
4848   end_of_line (str);
4849 }
4850
4851 static void
4852 do_t_lsl (str)
4853      char * str;
4854 {
4855   thumb_shift (str, THUMB_LSL);
4856 }
4857
4858 static void
4859 do_t_lsr (str)
4860      char * str;
4861 {
4862   thumb_shift (str, THUMB_LSR);
4863 }
4864
4865 static void
4866 do_t_mov (str)
4867      char * str;
4868 {
4869   thumb_mov_compare (str, THUMB_MOVE);
4870 }
4871
4872 static void
4873 do_t_push_pop (str)
4874      char * str;
4875 {
4876   long range;
4877
4878   skip_whitespace (str);
4879
4880   if ((range = reg_list (&str)) == FAIL)
4881     {
4882       if (! inst.error)
4883         inst.error = BAD_ARGS;
4884       return;
4885     }
4886
4887   if (inst.reloc.type != BFD_RELOC_NONE)
4888     {
4889       /* This really doesn't seem worth it. */
4890       inst.reloc.type = BFD_RELOC_NONE;
4891       inst.error = _("Expression too complex");
4892       return;
4893     }
4894
4895   if (range & ~0xff)
4896     {
4897       if ((inst.instruction == T_OPCODE_PUSH
4898            && (range & ~0xff) == 1 << REG_LR)
4899           || (inst.instruction == T_OPCODE_POP
4900               && (range & ~0xff) == 1 << REG_PC))
4901         {
4902           inst.instruction |= THUMB_PP_PC_LR;
4903           range &= 0xff;
4904         }
4905       else
4906         {
4907           inst.error = _("invalid register list to push/pop instruction");
4908           return;
4909         }
4910     }
4911
4912   inst.instruction |= range;
4913   end_of_line (str);
4914 }
4915
4916 static void
4917 do_t_str (str)
4918      char * str;
4919 {
4920   thumb_load_store (str, THUMB_STORE, THUMB_WORD);
4921 }
4922
4923 static void
4924 do_t_strb (str)
4925      char * str;
4926 {
4927   thumb_load_store (str, THUMB_STORE, THUMB_BYTE);
4928 }
4929
4930 static void
4931 do_t_strh (str)
4932      char * str;
4933 {
4934   thumb_load_store (str, THUMB_STORE, THUMB_HALFWORD);
4935 }
4936
4937 static void
4938 do_t_sub (str)
4939      char * str;
4940 {
4941   thumb_add_sub (str, 1);
4942 }
4943
4944 static void
4945 do_t_swi (str)
4946      char * str;
4947 {
4948   skip_whitespace (str);
4949
4950   if (my_get_expression (&inst.reloc.exp, &str))
4951     return;
4952
4953   inst.reloc.type = BFD_RELOC_ARM_SWI;
4954   end_of_line (str);
4955   return;
4956 }
4957
4958 static void
4959 do_t_adr (str)
4960      char * str;
4961 {
4962   int reg;
4963
4964   /* This is a pseudo-op of the form "adr rd, label" to be converted
4965      into a relative address of the form "add rd, pc, #label-.-4".  */
4966   skip_whitespace (str);
4967
4968   /* Store Rd in temporary location inside instruction.  */
4969   if ((reg = reg_required_here (&str, 4)) == FAIL
4970       || (reg > 7)  /* For Thumb reg must be r0..r7.  */
4971       || skip_past_comma (&str) == FAIL
4972       || my_get_expression (&inst.reloc.exp, &str))
4973     {
4974       if (!inst.error)
4975         inst.error = BAD_ARGS;
4976       return;
4977     }
4978
4979   inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
4980   inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
4981   inst.reloc.pc_rel = 1;
4982   inst.instruction |= REG_PC; /* Rd is already placed into the instruction.  */
4983   
4984   end_of_line (str);
4985 }
4986
4987 static void
4988 insert_reg (entry)
4989      int entry;
4990 {
4991   int    len = strlen (reg_table[entry].name) + 2;
4992   char * buf = (char *) xmalloc (len);
4993   char * buf2 = (char *) xmalloc (len);
4994   int    i = 0;
4995
4996 #ifdef REGISTER_PREFIX
4997   buf[i++] = REGISTER_PREFIX;
4998 #endif
4999
5000   strcpy (buf + i, reg_table[entry].name);
5001
5002   for (i = 0; buf[i]; i++)
5003     buf2[i] = islower (buf[i]) ? toupper (buf[i]) : buf[i];
5004
5005   buf2[i] = '\0';
5006
5007   hash_insert (arm_reg_hsh, buf, (PTR) &reg_table[entry]);
5008   hash_insert (arm_reg_hsh, buf2, (PTR) &reg_table[entry]);
5009 }
5010
5011 static void
5012 insert_reg_alias (str, regnum)
5013      char *str;
5014      int regnum;
5015 {
5016   struct reg_entry *new =
5017     (struct reg_entry *)xmalloc (sizeof (struct reg_entry));
5018   char *name = xmalloc (strlen (str) + 1);
5019   strcpy (name, str);
5020
5021   new->name = name;
5022   new->number = regnum;
5023
5024   hash_insert (arm_reg_hsh, name, (PTR) new);
5025 }
5026
5027 static void
5028 set_constant_flonums ()
5029 {
5030   int i;
5031
5032   for (i = 0; i < NUM_FLOAT_VALS; i++)
5033     if (atof_ieee ((char *)fp_const[i], 'x', fp_values[i]) == NULL)
5034       abort ();
5035 }
5036
5037 void
5038 md_begin ()
5039 {
5040   unsigned mach;
5041   unsigned int i;
5042   
5043   if (   (arm_ops_hsh = hash_new ()) == NULL
5044       || (arm_tops_hsh = hash_new ()) == NULL
5045       || (arm_cond_hsh = hash_new ()) == NULL
5046       || (arm_shift_hsh = hash_new ()) == NULL
5047       || (arm_reg_hsh = hash_new ()) == NULL
5048       || (arm_psr_hsh = hash_new ()) == NULL)
5049     as_fatal (_("Virtual memory exhausted"));
5050     
5051   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
5052     hash_insert (arm_ops_hsh, insns[i].template, (PTR) (insns + i));
5053   for (i = 0; i < sizeof (tinsns) / sizeof (struct thumb_opcode); i++)
5054     hash_insert (arm_tops_hsh, tinsns[i].template, (PTR) (tinsns + i));
5055   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
5056     hash_insert (arm_cond_hsh, conds[i].template, (PTR) (conds + i));
5057   for (i = 0; i < sizeof (shift) / sizeof (struct asm_shift); i++)
5058     hash_insert (arm_shift_hsh, shift[i].template, (PTR) (shift + i));
5059   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
5060     hash_insert (arm_psr_hsh, psrs[i].template, (PTR) (psrs + i));
5061
5062   for (i = 0; reg_table[i].name; i++)
5063     insert_reg (i);
5064
5065   set_constant_flonums ();
5066
5067 #if defined OBJ_COFF || defined OBJ_ELF
5068   {
5069     unsigned int flags = 0;
5070     
5071     /* Set the flags in the private structure.  */
5072     if (uses_apcs_26)      flags |= F_APCS26;
5073     if (support_interwork) flags |= F_INTERWORK;
5074     if (uses_apcs_float)   flags |= F_APCS_FLOAT;
5075     if (pic_code)          flags |= F_PIC;
5076     if ((cpu_variant & FPU_ALL) == FPU_NONE) flags |= F_SOFT_FLOAT;
5077
5078     bfd_set_private_flags (stdoutput, flags);
5079   }
5080 #endif
5081   
5082   /* Record the CPU type as well.  */
5083   switch (cpu_variant & ARM_CPU_MASK)
5084     {
5085     case ARM_2:
5086       mach = bfd_mach_arm_2;
5087       break;
5088       
5089     case ARM_3:                 /* Also ARM_250.  */
5090       mach = bfd_mach_arm_2a;
5091       break;
5092       
5093     default:
5094     case ARM_6 | ARM_3 | ARM_2: /* Actually no CPU type defined.  */
5095       mach = bfd_mach_arm_4;
5096       break;
5097       
5098     case ARM_7:                 /* Also ARM_6.  */
5099       mach = bfd_mach_arm_3;
5100       break;
5101     }
5102   
5103   /* Catch special cases.  */
5104   if (cpu_variant != (FPU_DEFAULT | CPU_DEFAULT))
5105     {
5106       if (cpu_variant & (ARM_EXT_V5 & ARM_THUMB))
5107         mach = bfd_mach_arm_5T;
5108       else if (cpu_variant & ARM_EXT_V5)
5109         mach = bfd_mach_arm_5;
5110       else if (cpu_variant & ARM_THUMB)
5111         mach = bfd_mach_arm_4T;
5112       else if ((cpu_variant & ARM_ARCH_V4) == ARM_ARCH_V4)
5113         mach = bfd_mach_arm_4;
5114       else if (cpu_variant & ARM_LONGMUL)
5115         mach = bfd_mach_arm_3M;
5116     }
5117   
5118   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
5119 }
5120
5121 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
5122    for use in the a.out file, and stores them in the array pointed to by buf.
5123    This knows about the endian-ness of the target machine and does
5124    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
5125    2 (short) and 4 (long)  Floating numbers are put out as a series of
5126    LITTLENUMS (shorts, here at least).  */
5127 void
5128 md_number_to_chars (buf, val, n)
5129      char * buf;
5130      valueT val;
5131      int    n;
5132 {
5133   if (target_big_endian)
5134     number_to_chars_bigendian (buf, val, n);
5135   else
5136     number_to_chars_littleendian (buf, val, n);
5137 }
5138
5139 static valueT 
5140 md_chars_to_number (buf, n)
5141      char * buf;
5142      int n;
5143 {
5144   valueT result = 0;
5145   unsigned char * where = (unsigned char *) buf;
5146
5147   if (target_big_endian)
5148     {
5149       while (n--)
5150         {
5151           result <<= 8;
5152           result |= (*where++ & 255);
5153         }
5154     }
5155   else
5156     {
5157       while (n--)
5158         {
5159           result <<= 8;
5160           result |= (where[n] & 255);
5161         }
5162     }
5163
5164   return result;
5165 }
5166
5167 /* Turn a string in input_line_pointer into a floating point constant
5168    of type TYPE, and store the appropriate bytes in *litP.  The number
5169    of LITTLENUMS emitted is stored in *sizeP .  An error message is
5170    returned, or NULL on OK.
5171
5172    Note that fp constants aren't represent in the normal way on the ARM.
5173    In big endian mode, things are as expected.  However, in little endian
5174    mode fp constants are big-endian word-wise, and little-endian byte-wise
5175    within the words.  For example, (double) 1.1 in big endian mode is
5176    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
5177    the byte sequence 99 99 f1 3f 9a 99 99 99.
5178
5179    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
5180
5181 char *
5182 md_atof (type, litP, sizeP)
5183      char   type;
5184      char * litP;
5185      int *  sizeP;
5186 {
5187   int prec;
5188   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5189   char *t;
5190   int i;
5191
5192   switch (type)
5193     {
5194     case 'f':
5195     case 'F':
5196     case 's':
5197     case 'S':
5198       prec = 2;
5199       break;
5200
5201     case 'd':
5202     case 'D':
5203     case 'r':
5204     case 'R':
5205       prec = 4;
5206       break;
5207
5208     case 'x':
5209     case 'X':
5210       prec = 6;
5211       break;
5212
5213     case 'p':
5214     case 'P':
5215       prec = 6;
5216       break;
5217
5218     default:
5219       *sizeP = 0;
5220       return _("Bad call to MD_ATOF()");
5221     }
5222
5223   t = atof_ieee (input_line_pointer, type, words);
5224   if (t)
5225     input_line_pointer = t;
5226   *sizeP = prec * 2;
5227
5228   if (target_big_endian)
5229     {
5230       for (i = 0; i < prec; i++)
5231         {
5232           md_number_to_chars (litP, (valueT) words[i], 2);
5233           litP += 2;
5234         }
5235     }
5236   else
5237     {
5238       /* For a 4 byte float the order of elements in `words' is 1 0.  For an
5239          8 byte float the order is 1 0 3 2.  */
5240       for (i = 0; i < prec; i += 2)
5241         {
5242           md_number_to_chars (litP, (valueT) words[i + 1], 2);
5243           md_number_to_chars (litP + 2, (valueT) words[i], 2);
5244           litP += 4;
5245         }
5246     }
5247
5248   return 0;
5249 }
5250
5251 /* The knowledge of the PC's pipeline offset is built into the insns themselves.  */ 
5252 long
5253 md_pcrel_from (fixP)
5254      fixS * fixP;
5255 {
5256   if (   fixP->fx_addsy
5257       && S_GET_SEGMENT (fixP->fx_addsy) == undefined_section
5258       && fixP->fx_subsy == NULL)
5259     return 0;
5260   
5261   if (fixP->fx_pcrel && (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_ADD))
5262     {
5263       /* PC relative addressing on the Thumb is slightly odd
5264          as the bottom two bits of the PC are forced to zero
5265          for the calculation.  */
5266       return (fixP->fx_where + fixP->fx_frag->fr_address) & ~3;
5267     }
5268
5269 #ifdef TE_WINCE
5270   /* The pattern was adjusted to accomodate CE's off-by-one fixups,
5271      so we un-adjust here to compensate for the accomodation.  */
5272   return fixP->fx_where + fixP->fx_frag->fr_address + 8;
5273 #else
5274   return fixP->fx_where + fixP->fx_frag->fr_address;
5275 #endif
5276 }
5277
5278 /* Round up a section size to the appropriate boundary. */
5279 valueT
5280 md_section_align (segment, size)
5281      segT   segment ATTRIBUTE_UNUSED;
5282      valueT size;
5283 {
5284 #ifdef OBJ_ELF
5285   return size;
5286 #else
5287   /* Round all sects to multiple of 4 */
5288   return (size + 3) & ~3;
5289 #endif
5290 }
5291
5292 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.  Otherwise 
5293    we have no need to default values of symbols.  */
5294
5295 /* ARGSUSED */
5296 symbolS *
5297 md_undefined_symbol (name)
5298      char * name ATTRIBUTE_UNUSED;
5299 {
5300 #ifdef OBJ_ELF
5301   if (name[0] == '_' && name[1] == 'G'
5302       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
5303     {
5304       if (!GOT_symbol)
5305         {
5306           if (symbol_find (name))
5307             as_bad ("GOT already in the symbol table");
5308           
5309           GOT_symbol = symbol_new (name, undefined_section,
5310                                    (valueT)0, & zero_address_frag);
5311         }
5312       
5313       return GOT_symbol;
5314     }
5315 #endif
5316   
5317   return 0;
5318 }
5319
5320 /* arm_reg_parse () := if it looks like a register, return its token and 
5321    advance the pointer. */
5322
5323 static int
5324 arm_reg_parse (ccp)
5325      register char ** ccp;
5326 {
5327   char * start = * ccp;
5328   char   c;
5329   char * p;
5330   struct reg_entry * reg;
5331
5332 #ifdef REGISTER_PREFIX
5333   if (*start != REGISTER_PREFIX)
5334     return FAIL;
5335   p = start + 1;
5336 #else
5337   p = start;
5338 #ifdef OPTIONAL_REGISTER_PREFIX
5339   if (*p == OPTIONAL_REGISTER_PREFIX)
5340     p++, start++;
5341 #endif
5342 #endif
5343   if (!isalpha (*p) || !is_name_beginner (*p))
5344     return FAIL;
5345
5346   c = *p++;
5347   while (isalpha (c) || isdigit (c) || c == '_')
5348     c = *p++;
5349
5350   *--p = 0;
5351   reg = (struct reg_entry *) hash_find (arm_reg_hsh, start);
5352   *p = c;
5353   
5354   if (reg)
5355     {
5356       *ccp = p;
5357       return reg->number;
5358     }
5359
5360   return FAIL;
5361 }
5362
5363 int
5364 md_apply_fix3 (fixP, val, seg)
5365      fixS *      fixP;
5366      valueT *    val;
5367      segT        seg;
5368 {
5369   offsetT        value = * val;
5370   offsetT        newval;
5371   unsigned int   newimm;
5372   unsigned long  temp;
5373   int            sign;
5374   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
5375   arm_fix_data * arm_data = (arm_fix_data *) fixP->tc_fix_data;
5376
5377   assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
5378
5379   /* Note whether this will delete the relocation.  */
5380 #if 0 /* patch from REarnshaw to JDavis (disabled for the moment, since it doesn't work fully) */
5381   if ((fixP->fx_addsy == 0 || symbol_constant_p (fixP->fx_addsy))
5382       && !fixP->fx_pcrel)
5383 #else
5384   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
5385 #endif
5386     fixP->fx_done = 1;
5387
5388   /* If this symbol is in a different section then we need to leave it for
5389      the linker to deal with.  Unfortunately, md_pcrel_from can't tell,
5390      so we have to undo it's effects here.  */
5391   if (fixP->fx_pcrel)
5392     {
5393       if (fixP->fx_addsy != NULL
5394           && S_IS_DEFINED (fixP->fx_addsy)
5395           && S_GET_SEGMENT (fixP->fx_addsy) != seg)
5396         {
5397           if (target_oabi
5398               && (fixP->fx_r_type == BFD_RELOC_ARM_PCREL_BRANCH
5399                 || fixP->fx_r_type == BFD_RELOC_ARM_PCREL_BLX
5400                 ))
5401             value = 0;
5402           else
5403             value += md_pcrel_from (fixP);
5404         }
5405     }
5406
5407   fixP->fx_addnumber = value;   /* Remember value for emit_reloc.  */
5408
5409   switch (fixP->fx_r_type)
5410     {
5411     case BFD_RELOC_ARM_IMMEDIATE:
5412       newimm = validate_immediate (value);
5413       temp = md_chars_to_number (buf, INSN_SIZE);
5414
5415       /* If the instruction will fail, see if we can fix things up by
5416          changing the opcode.  */
5417       if (newimm == (unsigned int) FAIL
5418           && (newimm = negate_data_op (&temp, value)) == (unsigned int) FAIL)
5419         {
5420           as_bad_where (fixP->fx_file, fixP->fx_line,
5421                         _("invalid constant (%lx) after fixup"),
5422                         (unsigned long) value);
5423           break;
5424         }
5425
5426       newimm |= (temp & 0xfffff000);
5427       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
5428       break;
5429
5430     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
5431       {
5432         unsigned int highpart = 0;
5433         unsigned int newinsn  = 0xe1a00000; /* nop */
5434         newimm = validate_immediate (value);
5435         temp = md_chars_to_number (buf, INSN_SIZE);
5436
5437         /* If the instruction will fail, see if we can fix things up by
5438            changing the opcode.  */
5439         if (newimm == (unsigned int) FAIL
5440             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
5441           {
5442             /* No ?  OK - try using two ADD instructions to generate the value.  */
5443             newimm = validate_immediate_twopart (value, & highpart);
5444
5445             /* Yes - then make sure that the second instruction is also an add.  */
5446             if (newimm != (unsigned int) FAIL)
5447               newinsn = temp;
5448             /* Still No ?  Try using a negated value.  */
5449             else if (validate_immediate_twopart (- value, & highpart) != (unsigned int) FAIL)
5450                 temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
5451             /* Otherwise - give up.  */
5452             else
5453               {
5454                 as_bad_where (fixP->fx_file, fixP->fx_line,
5455                               _("Unable to compute ADRL instructions for PC offset of 0x%x"), value);
5456                 break;
5457               }
5458
5459             /* Replace the first operand in the 2nd instruction (which is the PC)
5460                with the destination register.  We have already added in the PC in the
5461                first instruction and we do not want to do it again.  */
5462             newinsn &= ~ 0xf0000;
5463             newinsn |= ((newinsn & 0x0f000) << 4);
5464           }
5465
5466         newimm |= (temp & 0xfffff000);
5467         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
5468
5469         highpart |= (newinsn & 0xfffff000);
5470         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
5471       }
5472       break;
5473
5474     case BFD_RELOC_ARM_OFFSET_IMM:
5475       sign = value >= 0;
5476       
5477       if (value < 0)
5478         value = - value;
5479       
5480       if (validate_offset_imm (value, 0) == FAIL)
5481         {
5482           as_bad_where (fixP->fx_file, fixP->fx_line, 
5483                         _("bad immediate value for offset (%ld)"), (long) value);
5484           break;
5485         }
5486
5487       newval = md_chars_to_number (buf, INSN_SIZE);
5488       newval &= 0xff7ff000;
5489       newval |= value | (sign ? INDEX_UP : 0);
5490       md_number_to_chars (buf, newval, INSN_SIZE);
5491       break;
5492
5493      case BFD_RELOC_ARM_OFFSET_IMM8:
5494      case BFD_RELOC_ARM_HWLITERAL:
5495       sign = value >= 0;
5496       
5497       if (value < 0)
5498         value = - value;
5499
5500       if (validate_offset_imm (value, 1) == FAIL)
5501         {
5502           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
5503             as_bad_where (fixP->fx_file, fixP->fx_line, 
5504                         _("invalid literal constant: pool needs to be closer"));
5505           else
5506             as_bad (_("bad immediate value for half-word offset (%ld)"),
5507                     (long) value);
5508           break;
5509         }
5510
5511       newval = md_chars_to_number (buf, INSN_SIZE);
5512       newval &= 0xff7ff0f0;
5513       newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
5514       md_number_to_chars (buf, newval, INSN_SIZE);
5515       break;
5516
5517     case BFD_RELOC_ARM_LITERAL:
5518       sign = value >= 0;
5519       
5520       if (value < 0)
5521         value = - value;
5522
5523       if (validate_offset_imm (value, 0) == FAIL)
5524         {
5525           as_bad_where (fixP->fx_file, fixP->fx_line, 
5526                         _("invalid literal constant: pool needs to be closer"));
5527           break;
5528         }
5529
5530       newval = md_chars_to_number (buf, INSN_SIZE);
5531       newval &= 0xff7ff000;
5532       newval |= value | (sign ? INDEX_UP : 0);
5533       md_number_to_chars (buf, newval, INSN_SIZE);
5534       break;
5535
5536     case BFD_RELOC_ARM_SHIFT_IMM:
5537       newval = md_chars_to_number (buf, INSN_SIZE);
5538       if (((unsigned long) value) > 32
5539           || (value == 32 
5540               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
5541         {
5542           as_bad_where (fixP->fx_file, fixP->fx_line,
5543                         _("shift expression is too large"));
5544           break;
5545         }
5546
5547       if (value == 0)
5548         newval &= ~0x60;        /* Shifts of zero must be done as lsl */
5549       else if (value == 32)
5550         value = 0;
5551       newval &= 0xfffff07f;
5552       newval |= (value & 0x1f) << 7;
5553       md_number_to_chars (buf, newval , INSN_SIZE);
5554       break;
5555
5556     case BFD_RELOC_ARM_SWI:
5557       if (arm_data->thumb_mode)
5558         {
5559           if (((unsigned long) value) > 0xff)
5560             as_bad_where (fixP->fx_file, fixP->fx_line,
5561                           _("Invalid swi expression"));
5562           newval = md_chars_to_number (buf, THUMB_SIZE) & 0xff00;
5563           newval |= value;
5564           md_number_to_chars (buf, newval, THUMB_SIZE);
5565         }
5566       else
5567         {
5568           if (((unsigned long) value) > 0x00ffffff)
5569             as_bad_where (fixP->fx_file, fixP->fx_line, 
5570                           _("Invalid swi expression"));
5571           newval = md_chars_to_number (buf, INSN_SIZE) & 0xff000000;
5572           newval |= value;
5573           md_number_to_chars (buf, newval , INSN_SIZE);
5574         }
5575       break;
5576
5577     case BFD_RELOC_ARM_MULTI:
5578       if (((unsigned long) value) > 0xffff)
5579         as_bad_where (fixP->fx_file, fixP->fx_line,
5580                       _("Invalid expression in load/store multiple"));
5581       newval = value | md_chars_to_number (buf, INSN_SIZE);
5582       md_number_to_chars (buf, newval, INSN_SIZE);
5583       break;
5584
5585     case BFD_RELOC_ARM_PCREL_BRANCH:
5586       newval = md_chars_to_number (buf, INSN_SIZE);
5587
5588       /* Sign-extend a 24-bit number.  */
5589 #define SEXT24(x)       ((((x) & 0xffffff) ^ (~ 0x7fffff)) + 0x800000)
5590
5591 #ifdef OBJ_ELF
5592       if (! target_oabi)
5593         value = fixP->fx_offset;
5594 #endif
5595
5596       /* We are going to store value (shifted right by two) in the
5597          instruction, in a 24 bit, signed field.  Thus we need to check
5598          that none of the top 8 bits of the shifted value (top 7 bits of
5599          the unshifted, unsigned value) are set, or that they are all set.  */
5600       if ((value & ~ ((offsetT) 0x1ffffff)) != 0
5601           && ((value & ~ ((offsetT) 0x1ffffff)) != ~ ((offsetT) 0x1ffffff)))
5602         {
5603 #ifdef OBJ_ELF
5604           /* Normally we would be stuck at this point, since we cannot store
5605              the absolute address that is the destination of the branch in the
5606              24 bits of the branch instruction.  If however, we happen to know
5607              that the destination of the branch is in the same section as the
5608              branch instruciton itself, then we can compute the relocation for
5609              ourselves and not have to bother the linker with it.
5610              
5611              FIXME: The tests for OBJ_ELF and ! target_oabi are only here
5612              because I have not worked out how to do this for OBJ_COFF or
5613              target_oabi.  */
5614           if (! target_oabi
5615               && fixP->fx_addsy != NULL
5616               && S_IS_DEFINED (fixP->fx_addsy)
5617               && S_GET_SEGMENT (fixP->fx_addsy) == seg)
5618             {
5619               /* Get pc relative value to go into the branch.  */
5620               value = * val;
5621
5622               /* Permit a backward branch provided that enough bits are set.
5623                  Allow a forwards branch, provided that enough bits are clear.  */
5624               if ((value & ~ ((offsetT) 0x1ffffff)) == ~ ((offsetT) 0x1ffffff)
5625                   || (value & ~ ((offsetT) 0x1ffffff)) == 0)
5626                 fixP->fx_done = 1;
5627             }
5628           
5629           if (! fixP->fx_done)
5630 #endif
5631             as_bad_where (fixP->fx_file, fixP->fx_line,
5632                           _("gas can't handle same-section branch dest >= 0x04000000"));
5633         }
5634
5635       value >>= 2;
5636       value += SEXT24 (newval);
5637       
5638       if ((value & ~ ((offsetT) 0xffffff)) != 0
5639           && ((value & ~ ((offsetT) 0xffffff)) != ~ ((offsetT) 0xffffff)))
5640         as_bad_where (fixP->fx_file, fixP->fx_line,
5641                       _("out of range branch"));
5642       
5643       newval = (value & 0x00ffffff) | (newval & 0xff000000);
5644       md_number_to_chars (buf, newval, INSN_SIZE);
5645       break;
5646
5647     case BFD_RELOC_ARM_PCREL_BLX:
5648       {
5649         offsetT hbit;
5650         newval = md_chars_to_number (buf, INSN_SIZE);
5651
5652 #ifdef OBJ_ELF
5653         if (! target_oabi)
5654            value = fixP->fx_offset;
5655 #endif
5656         hbit   = (value >> 1) & 1;
5657         value  = (value >> 2) & 0x00ffffff;
5658         value  = (value + (newval & 0x00ffffff)) & 0x00ffffff;
5659         newval = value | (newval & 0xfe000000) | (hbit << 24);
5660         md_number_to_chars (buf, newval, INSN_SIZE);
5661       }
5662       break;
5663
5664     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* conditional branch */
5665       newval = md_chars_to_number (buf, THUMB_SIZE);
5666       {
5667         addressT diff = (newval & 0xff) << 1;
5668         if (diff & 0x100)
5669          diff |= ~0xff;
5670
5671         value += diff;
5672         if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
5673          as_bad_where (fixP->fx_file, fixP->fx_line,
5674                        _("Branch out of range"));
5675         newval = (newval & 0xff00) | ((value & 0x1ff) >> 1);
5676       }
5677       md_number_to_chars (buf, newval, THUMB_SIZE);
5678       break;
5679
5680     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* unconditional branch */
5681       newval = md_chars_to_number (buf, THUMB_SIZE);
5682       {
5683         addressT diff = (newval & 0x7ff) << 1;
5684         if (diff & 0x800)
5685          diff |= ~0x7ff;
5686
5687         value += diff;
5688         if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
5689          as_bad_where (fixP->fx_file, fixP->fx_line,
5690                        _("Branch out of range"));
5691         newval = (newval & 0xf800) | ((value & 0xfff) >> 1);
5692       }
5693       md_number_to_chars (buf, newval, THUMB_SIZE);
5694       break;
5695
5696     case BFD_RELOC_THUMB_PCREL_BLX:
5697     case BFD_RELOC_THUMB_PCREL_BRANCH23:
5698       {
5699         offsetT newval2;
5700         addressT diff;
5701
5702         newval  = md_chars_to_number (buf, THUMB_SIZE);
5703         newval2 = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
5704         diff = ((newval & 0x7ff) << 12) | ((newval2 & 0x7ff) << 1);
5705         if (diff & 0x400000)
5706           diff |= ~0x3fffff;
5707 #ifdef OBJ_ELF
5708         value = fixP->fx_offset;
5709 #endif
5710         value += diff;
5711         if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
5712           as_bad_where (fixP->fx_file, fixP->fx_line,
5713                         _("Branch with link out of range"));
5714
5715         newval  = (newval  & 0xf800) | ((value & 0x7fffff) >> 12);
5716         newval2 = (newval2 & 0xf800) | ((value & 0xfff) >> 1);
5717         md_number_to_chars (buf, newval, THUMB_SIZE);
5718         md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
5719       }
5720       break;
5721
5722     case BFD_RELOC_8:
5723       if (fixP->fx_done || fixP->fx_pcrel)
5724         md_number_to_chars (buf, value, 1);
5725 #ifdef OBJ_ELF
5726       else if (!target_oabi)
5727         {
5728           value = fixP->fx_offset;
5729           md_number_to_chars (buf, value, 1);
5730         }
5731 #endif
5732       break;
5733
5734     case BFD_RELOC_16:
5735       if (fixP->fx_done || fixP->fx_pcrel)
5736         md_number_to_chars (buf, value, 2);
5737 #ifdef OBJ_ELF
5738       else if (!target_oabi)
5739         {
5740           value = fixP->fx_offset;
5741           md_number_to_chars (buf, value, 2);
5742         }
5743 #endif
5744       break;
5745
5746 #ifdef OBJ_ELF
5747     case BFD_RELOC_ARM_GOT32:
5748     case BFD_RELOC_ARM_GOTOFF:
5749         md_number_to_chars (buf, 0, 4);
5750         break;
5751 #endif
5752
5753     case BFD_RELOC_RVA:
5754     case BFD_RELOC_32:
5755       if (fixP->fx_done || fixP->fx_pcrel)
5756         md_number_to_chars (buf, value, 4);
5757 #ifdef OBJ_ELF
5758       else if (!target_oabi)
5759         {
5760           value = fixP->fx_offset;
5761           md_number_to_chars (buf, value, 4);
5762         }
5763 #endif
5764       break;
5765
5766 #ifdef OBJ_ELF
5767     case BFD_RELOC_ARM_PLT32:
5768       /* It appears the instruction is fully prepared at this point. */
5769       break;
5770 #endif
5771
5772     case BFD_RELOC_ARM_GOTPC:
5773       md_number_to_chars (buf, value, 4);
5774       break;
5775       
5776     case BFD_RELOC_ARM_CP_OFF_IMM:
5777       sign = value >= 0;
5778       if (value < -1023 || value > 1023 || (value & 3))
5779         as_bad_where (fixP->fx_file, fixP->fx_line,
5780                       _("Illegal value for co-processor offset"));
5781       if (value < 0)
5782         value = -value;
5783       newval = md_chars_to_number (buf, INSN_SIZE) & 0xff7fff00;
5784       newval |= (value >> 2) | (sign ?  INDEX_UP : 0);
5785       md_number_to_chars (buf, newval , INSN_SIZE);
5786       break;
5787
5788     case BFD_RELOC_ARM_THUMB_OFFSET:
5789       newval = md_chars_to_number (buf, THUMB_SIZE);
5790       /* Exactly what ranges, and where the offset is inserted depends on
5791          the type of instruction, we can establish this from the top 4 bits */
5792       switch (newval >> 12)
5793         {
5794         case 4: /* PC load */
5795           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
5796              forced to zero for these loads, so we will need to round
5797              up the offset if the instruction address is not word
5798              aligned (since the final address produced must be, and
5799              we can only describe word-aligned immediate offsets).  */
5800
5801           if ((fixP->fx_frag->fr_address + fixP->fx_where + value) & 3)
5802             as_bad_where (fixP->fx_file, fixP->fx_line,
5803                           _("Invalid offset, target not word aligned (0x%08X)"),
5804                           (unsigned int)(fixP->fx_frag->fr_address + fixP->fx_where + value));
5805
5806           if ((value + 2) & ~0x3fe)
5807             as_bad_where (fixP->fx_file, fixP->fx_line,
5808                           _("Invalid offset, value too big (0x%08X)"), value);
5809
5810           /* Round up, since pc will be rounded down.  */
5811           newval |= (value + 2) >> 2;
5812           break;
5813
5814         case 9: /* SP load/store */
5815           if (value & ~0x3fc)
5816             as_bad_where (fixP->fx_file, fixP->fx_line,
5817                           _("Invalid offset, value too big (0x%08X)"), value);
5818           newval |= value >> 2;
5819           break;
5820
5821         case 6: /* Word load/store */
5822           if (value & ~0x7c)
5823             as_bad_where (fixP->fx_file, fixP->fx_line,
5824                           _("Invalid offset, value too big (0x%08X)"), value);
5825           newval |= value << 4; /* 6 - 2 */
5826           break;
5827
5828         case 7: /* Byte load/store */
5829           if (value & ~0x1f)
5830             as_bad_where (fixP->fx_file, fixP->fx_line,
5831                           _("Invalid offset, value too big (0x%08X)"), value);
5832           newval |= value << 6;
5833           break;
5834
5835         case 8: /* Halfword load/store */
5836           if (value & ~0x3e)
5837             as_bad_where (fixP->fx_file, fixP->fx_line,
5838                           _("Invalid offset, value too big (0x%08X)"), value);
5839           newval |= value << 5; /* 6 - 1 */
5840           break;
5841
5842         default:
5843           as_bad_where (fixP->fx_file, fixP->fx_line,
5844                         "Unable to process relocation for thumb opcode: %lx",
5845                         (unsigned long) newval);
5846           break;
5847         }
5848       md_number_to_chars (buf, newval, THUMB_SIZE);
5849       break;
5850
5851     case BFD_RELOC_ARM_THUMB_ADD:
5852       /* This is a complicated relocation, since we use it for all of
5853          the following immediate relocations:
5854             3bit ADD/SUB
5855             8bit ADD/SUB
5856             9bit ADD/SUB SP word-aligned
5857            10bit ADD PC/SP word-aligned
5858
5859          The type of instruction being processed is encoded in the
5860          instruction field:
5861            0x8000  SUB
5862            0x00F0  Rd
5863            0x000F  Rs
5864       */
5865       newval = md_chars_to_number (buf, THUMB_SIZE);
5866       {
5867         int rd = (newval >> 4) & 0xf;
5868         int rs = newval & 0xf;
5869         int subtract = newval & 0x8000;
5870
5871         if (rd == REG_SP)
5872           {
5873             if (value & ~0x1fc)
5874               as_bad_where (fixP->fx_file, fixP->fx_line,
5875                             _("Invalid immediate for stack address calculation"));
5876             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
5877             newval |= value >> 2;
5878           }
5879         else if (rs == REG_PC || rs == REG_SP)
5880           {
5881             if (subtract ||
5882                 value & ~0x3fc)
5883               as_bad_where (fixP->fx_file, fixP->fx_line,
5884                             _("Invalid immediate for address calculation (value = 0x%08lX)"),
5885                             (unsigned long) value);
5886             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
5887             newval |= rd << 8;
5888             newval |= value >> 2;
5889           }
5890         else if (rs == rd)
5891           {
5892             if (value & ~0xff)
5893               as_bad_where (fixP->fx_file, fixP->fx_line,
5894                             _("Invalid 8bit immediate"));
5895             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
5896             newval |= (rd << 8) | value;
5897           }
5898         else
5899           {
5900             if (value & ~0x7)
5901               as_bad_where (fixP->fx_file, fixP->fx_line,
5902                             _("Invalid 3bit immediate"));
5903             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
5904             newval |= rd | (rs << 3) | (value << 6);
5905           }
5906       }
5907       md_number_to_chars (buf, newval , THUMB_SIZE);
5908       break;
5909
5910     case BFD_RELOC_ARM_THUMB_IMM:
5911       newval = md_chars_to_number (buf, THUMB_SIZE);
5912       switch (newval >> 11)
5913         {
5914         case 0x04: /* 8bit immediate MOV */
5915         case 0x05: /* 8bit immediate CMP */
5916           if (value < 0 || value > 255)
5917             as_bad_where (fixP->fx_file, fixP->fx_line,
5918                           _("Invalid immediate: %ld is too large"),
5919                           (long) value);
5920           newval |= value;
5921           break;
5922
5923         default:
5924           abort ();
5925         }
5926       md_number_to_chars (buf, newval , THUMB_SIZE);
5927       break;
5928
5929     case BFD_RELOC_ARM_THUMB_SHIFT:
5930       /* 5bit shift value (0..31) */
5931       if (value < 0 || value > 31)
5932         as_bad_where (fixP->fx_file, fixP->fx_line,
5933                       _("Illegal Thumb shift value: %ld"), (long) value);
5934       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf03f;
5935       newval |= value << 6;
5936       md_number_to_chars (buf, newval , THUMB_SIZE);
5937       break;
5938
5939     case BFD_RELOC_VTABLE_INHERIT:
5940     case BFD_RELOC_VTABLE_ENTRY:
5941       fixP->fx_done = 0;
5942       return 1;
5943
5944     case BFD_RELOC_NONE:
5945     default:
5946       as_bad_where (fixP->fx_file, fixP->fx_line,
5947                     _("Bad relocation fixup type (%d)"), fixP->fx_r_type);
5948     }
5949
5950   return 1;
5951 }
5952
5953 /* Translate internal representation of relocation info to BFD target
5954    format.  */
5955 arelent *
5956 tc_gen_reloc (section, fixp)
5957      asection * section ATTRIBUTE_UNUSED;
5958      fixS * fixp;
5959 {
5960   arelent * reloc;
5961   bfd_reloc_code_real_type code;
5962
5963   reloc = (arelent *) xmalloc (sizeof (arelent));
5964
5965   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5966   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5967   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5968
5969   /* @@ Why fx_addnumber sometimes and fx_offset other times?  */
5970 #ifndef OBJ_ELF
5971   if (fixp->fx_pcrel == 0)
5972     reloc->addend = fixp->fx_offset;
5973   else
5974     reloc->addend = fixp->fx_offset = reloc->address;
5975 #else  /* OBJ_ELF */
5976   reloc->addend = fixp->fx_offset;
5977 #endif
5978
5979   switch (fixp->fx_r_type)
5980     {
5981     case BFD_RELOC_8:
5982       if (fixp->fx_pcrel)
5983         {
5984           code = BFD_RELOC_8_PCREL;
5985           break;
5986         }
5987
5988     case BFD_RELOC_16:
5989       if (fixp->fx_pcrel)
5990         {
5991           code = BFD_RELOC_16_PCREL;
5992           break;
5993         }
5994
5995     case BFD_RELOC_32:
5996       if (fixp->fx_pcrel)
5997         {
5998           code = BFD_RELOC_32_PCREL;
5999           break;
6000         }
6001
6002     case BFD_RELOC_ARM_PCREL_BRANCH:
6003     case BFD_RELOC_ARM_PCREL_BLX:
6004     case BFD_RELOC_RVA:      
6005     case BFD_RELOC_THUMB_PCREL_BRANCH9:
6006     case BFD_RELOC_THUMB_PCREL_BRANCH12:
6007     case BFD_RELOC_THUMB_PCREL_BRANCH23:
6008     case BFD_RELOC_THUMB_PCREL_BLX:
6009     case BFD_RELOC_VTABLE_ENTRY:
6010     case BFD_RELOC_VTABLE_INHERIT:
6011       code = fixp->fx_r_type;
6012       break;
6013
6014     case BFD_RELOC_ARM_LITERAL:
6015     case BFD_RELOC_ARM_HWLITERAL:
6016       /* If this is called then the a literal has been referenced across
6017          a section boundary - possibly due to an implicit dump */
6018       as_bad_where (fixp->fx_file, fixp->fx_line,
6019                     _("Literal referenced across section boundary (Implicit dump?)"));
6020       return NULL;
6021
6022 #ifdef OBJ_ELF
6023     case BFD_RELOC_ARM_GOT32:
6024     case BFD_RELOC_ARM_GOTOFF:
6025     case BFD_RELOC_ARM_PLT32:
6026        code = fixp->fx_r_type;
6027     break;
6028 #endif
6029
6030     case BFD_RELOC_ARM_IMMEDIATE:
6031       as_bad_where (fixp->fx_file, fixp->fx_line,
6032                     _("Internal_relocation (type %d) not fixed up (IMMEDIATE)"),
6033                     fixp->fx_r_type);
6034       return NULL;
6035
6036     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
6037       as_bad_where (fixp->fx_file, fixp->fx_line,
6038                     _("ADRL used for a symbol not defined in the same file"),
6039                     fixp->fx_r_type);
6040       return NULL;
6041
6042     case BFD_RELOC_ARM_OFFSET_IMM:
6043       as_bad_where (fixp->fx_file, fixp->fx_line,
6044                     _("Internal_relocation (type %d) not fixed up (OFFSET_IMM)"),
6045                     fixp->fx_r_type);
6046       return NULL;
6047
6048     default:
6049       {
6050         char * type;
6051         switch (fixp->fx_r_type)
6052           {
6053           case BFD_RELOC_ARM_IMMEDIATE:    type = "IMMEDIATE";    break;
6054           case BFD_RELOC_ARM_OFFSET_IMM:   type = "OFFSET_IMM";   break;
6055           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
6056           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
6057           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
6058           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
6059           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
6060           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
6061           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
6062           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
6063           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
6064           default:                         type = _("<unknown>"); break;
6065           }
6066         as_bad_where (fixp->fx_file, fixp->fx_line,
6067                       _("Can not represent %s relocation in this object file format (%d)"),
6068                       type, fixp->fx_pcrel);
6069         return NULL;
6070       }
6071     }
6072
6073 #ifdef OBJ_ELF
6074  if (code == BFD_RELOC_32_PCREL
6075      && GOT_symbol
6076      && fixp->fx_addsy == GOT_symbol)
6077    {
6078      code = BFD_RELOC_ARM_GOTPC;
6079      reloc->addend = fixp->fx_offset = reloc->address;
6080    }
6081 #endif
6082    
6083   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
6084
6085   if (reloc->howto == NULL)
6086     {
6087       as_bad_where (fixp->fx_file, fixp->fx_line,
6088                     _("Can not represent %s relocation in this object file format"),
6089                     bfd_get_reloc_code_name (code));
6090       return NULL;
6091     }
6092
6093    /* HACK: Since arm ELF uses Rel instead of Rela, encode the
6094       vtable entry to be used in the relocation's section offset.  */
6095    if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
6096      reloc->address = fixp->fx_offset;
6097
6098   return reloc;
6099 }
6100
6101 int
6102 md_estimate_size_before_relax (fragP, segtype)
6103      fragS * fragP ATTRIBUTE_UNUSED;
6104      segT    segtype ATTRIBUTE_UNUSED;
6105 {
6106   as_fatal (_("md_estimate_size_before_relax\n"));
6107   return 1;
6108 }
6109
6110 static void
6111 output_inst PARAMS ((void))
6112 {
6113   char * to = NULL;
6114     
6115   if (inst.error)
6116     {
6117       as_bad (inst.error);
6118       return;
6119     }
6120
6121   to = frag_more (inst.size);
6122   
6123   if (thumb_mode && (inst.size > THUMB_SIZE))
6124     {
6125       assert (inst.size == (2 * THUMB_SIZE));
6126       md_number_to_chars (to, inst.instruction >> 16, THUMB_SIZE);
6127       md_number_to_chars (to + THUMB_SIZE, inst.instruction, THUMB_SIZE);
6128     }
6129   else if (inst.size > INSN_SIZE)
6130     {
6131       assert (inst.size == (2 * INSN_SIZE));
6132       md_number_to_chars (to, inst.instruction, INSN_SIZE);
6133       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
6134     }
6135   else
6136     md_number_to_chars (to, inst.instruction, inst.size);
6137
6138   if (inst.reloc.type != BFD_RELOC_NONE)
6139     fix_new_arm (frag_now, to - frag_now->fr_literal,
6140                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
6141                  inst.reloc.type);
6142
6143   return;
6144 }
6145
6146 void
6147 md_assemble (str)
6148      char * str;
6149 {
6150   char   c;
6151   char * p;
6152   char * q;
6153   char * start;
6154
6155   /* Align the instruction.
6156      This may not be the right thing to do but ... */
6157   /* arm_align (2, 0); */
6158   listing_prev_line (); /* Defined in listing.h */
6159
6160   /* Align the previous label if needed.  */
6161   if (last_label_seen != NULL)
6162     {
6163       symbol_set_frag (last_label_seen, frag_now);
6164       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
6165       S_SET_SEGMENT (last_label_seen, now_seg);
6166     }
6167
6168   memset (&inst, '\0', sizeof (inst));
6169   inst.reloc.type = BFD_RELOC_NONE;
6170
6171   skip_whitespace (str);
6172   
6173   /* Scan up to the end of the op-code, which must end in white space or
6174      end of string.  */
6175   for (start = p = str; *p != '\0'; p++)
6176     if (*p == ' ')
6177       break;
6178     
6179   if (p == str)
6180     {
6181       as_bad (_("No operator -- statement `%s'\n"), str);
6182       return;
6183     }
6184
6185   if (thumb_mode)
6186     {
6187       CONST struct thumb_opcode * opcode;
6188
6189       c = *p;
6190       *p = '\0';
6191       opcode = (CONST struct thumb_opcode *) hash_find (arm_tops_hsh, str);
6192       *p = c;
6193       
6194       if (opcode)
6195         {
6196           /* Check that this instruction is supported for this CPU.  */
6197           if (thumb_mode == 1 && (opcode->variants & cpu_variant) == 0)
6198              {
6199                 as_bad (_("selected processor does not support this opcode"));
6200                 return;
6201              }
6202
6203           inst.instruction = opcode->value;
6204           inst.size = opcode->size;
6205           (*opcode->parms)(p);
6206           output_inst ();
6207           return;
6208         }
6209     }
6210   else
6211     {
6212       CONST struct asm_opcode * opcode;
6213       unsigned long cond_code;
6214
6215       inst.size = INSN_SIZE;
6216       /* p now points to the end of the opcode, probably white space, but we
6217          have to break the opcode up in case it contains condionals and flags;
6218          keep trying with progressively smaller basic instructions until one
6219          matches, or we run out of opcode.  */
6220       q = (p - str > LONGEST_INST) ? str + LONGEST_INST : p;
6221
6222       for (; q != str; q--)
6223         {
6224           c = *q;
6225           *q = '\0';
6226
6227           opcode = (CONST struct asm_opcode *) hash_find (arm_ops_hsh, str);
6228           *q = c;
6229           
6230           if (opcode && opcode->template)
6231             {
6232               unsigned long flag_bits = 0;
6233               char * r;
6234
6235               /* Check that this instruction is supported for this CPU.  */
6236               if ((opcode->variants & cpu_variant) == 0)
6237                 goto try_shorter;
6238
6239               inst.instruction = opcode->value;
6240               if (q == p)               /* Just a simple opcode.  */
6241                 {
6242                   if (opcode->comp_suffix)
6243                     {
6244                        if (*opcode->comp_suffix != '\0')
6245                          as_bad (_("Opcode `%s' must have suffix from list: <%s>"),
6246                              str, opcode->comp_suffix);
6247                        else
6248                          /* Not a conditional instruction. */
6249                          (*opcode->parms)(q, 0);
6250                     }
6251                   else
6252                     {
6253                       /* A conditional instruction with default condition. */
6254                       inst.instruction |= COND_ALWAYS;
6255                       (*opcode->parms)(q, 0);
6256                     }
6257                   output_inst ();
6258                   return;
6259                 }
6260
6261               /* Not just a simple opcode.  Check if extra is a conditional. */
6262               r = q;
6263               if (p - r >= 2)
6264                 {
6265                   CONST struct asm_cond *cond;
6266                   char d = *(r + 2);
6267
6268                   *(r + 2) = '\0';
6269                   cond = (CONST struct asm_cond *) hash_find (arm_cond_hsh, r);
6270                   *(r + 2) = d;
6271                   if (cond)
6272                     {
6273                       if (cond->value == 0xf0000000)
6274                         as_tsktsk (
6275 _("Warning: Use of the 'nv' conditional is deprecated\n"));
6276
6277                       cond_code = cond->value;
6278                       r += 2;
6279                     }
6280                   else
6281                     cond_code = COND_ALWAYS;
6282                 }
6283               else
6284                 cond_code = COND_ALWAYS;
6285
6286               /* Apply the conditional, or complain it's not allowed. */
6287               if (opcode->comp_suffix && *opcode->comp_suffix == '\0')
6288                 {
6289                    /* Instruction isn't conditional */
6290                    if (cond_code != COND_ALWAYS)
6291                      {
6292                        as_bad (_("Opcode `%s' is unconditional\n"), str);
6293                        return;
6294                      }
6295                 }
6296               else
6297                 /* Instruction is conditional: set the condition into it. */
6298                 inst.instruction |= cond_code;       
6299
6300
6301               /* If there is a compulsory suffix, it should come here, before
6302                  any optional flags.  */
6303               if (opcode->comp_suffix && *opcode->comp_suffix != '\0')
6304                 {
6305                   CONST char *s = opcode->comp_suffix;
6306
6307                   while (*s)
6308                     {
6309                       inst.suffix++;
6310                       if (*r == *s)
6311                         break;
6312                       s++;
6313                     }
6314
6315                   if (*s == '\0')
6316                     {
6317                       as_bad (_("Opcode `%s' must have suffix from <%s>\n"), str,
6318                               opcode->comp_suffix);
6319                       return;
6320                     }
6321
6322                   r++;
6323                 }
6324
6325               /* The remainder, if any should now be flags for the instruction;
6326                  Scan these checking each one found with the opcode.  */
6327               if (r != p)
6328                 {
6329                   char d;
6330                   CONST struct asm_flg *flag = opcode->flags;
6331
6332                   if (flag)
6333                     {
6334                       int flagno;
6335
6336                       d = *p;
6337                       *p = '\0';
6338
6339                       for (flagno = 0; flag[flagno].template; flagno++)
6340                         {
6341                           if (streq (r, flag[flagno].template))
6342                             {
6343                               flag_bits |= flag[flagno].set_bits;
6344                               break;
6345                             }
6346                         }
6347
6348                       *p = d;
6349                       if (! flag[flagno].template)
6350                         goto try_shorter;
6351                     }
6352                   else
6353                     goto try_shorter;
6354                 }
6355
6356               (*opcode->parms) (p, flag_bits);
6357               output_inst ();
6358               return;
6359             }
6360
6361         try_shorter:
6362           ;
6363         }
6364     }
6365
6366   /* It wasn't an instruction, but it might be a register alias of the form
6367      alias .req reg */
6368   q = p;
6369   skip_whitespace (q);
6370
6371   c = *p;
6372   *p = '\0';
6373     
6374   if (*q && !strncmp (q, ".req ", 4))
6375     {
6376       int    reg;
6377       char * copy_of_str = str;
6378       char * r;
6379       
6380       q += 4;
6381       skip_whitespace (q);
6382
6383       for (r = q; *r != '\0'; r++)
6384         if (*r == ' ')
6385           break;
6386       
6387       if (r != q)
6388         {
6389           int regnum;
6390           char d = *r;
6391
6392           *r = '\0';
6393           regnum = arm_reg_parse (& q);
6394           *r = d;
6395
6396           reg = arm_reg_parse (& str);
6397           
6398           if (reg == FAIL)
6399             {
6400               if (regnum != FAIL)
6401                 insert_reg_alias (str, regnum);
6402               else
6403                 as_warn (_("register '%s' does not exist\n"), q);
6404             }
6405           else if (regnum != FAIL)
6406             {
6407               if (reg != regnum)
6408                 as_warn (_("ignoring redefinition of register alias '%s'"),
6409                          copy_of_str );
6410               
6411               /* Do not warn about redefinitions to the same alias.  */
6412             }
6413           else
6414             as_warn (_("ignoring redefinition of register alias '%s' to non-existant register '%s'"),
6415                      copy_of_str, q);
6416         }
6417       else
6418         as_warn (_("ignoring incomplete .req pseuso op"));
6419       
6420       *p = c;
6421       return;
6422     }
6423
6424   *p = c;
6425   as_bad (_("bad instruction `%s'"), start);
6426 }
6427
6428 /*
6429  * md_parse_option
6430  *    Invocation line includes a switch not recognized by the base assembler.
6431  *    See if it's a processor-specific option.  These are:
6432  *    Cpu variants, the arm part is optional:
6433  *            -m[arm]1                Currently not supported.
6434  *            -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
6435  *            -m[arm]3                Arm 3 processor
6436  *            -m[arm]6[xx],           Arm 6 processors
6437  *            -m[arm]7[xx][t][[d]m]   Arm 7 processors
6438  *            -m[arm]8[10]            Arm 8 processors
6439  *            -m[arm]9[20][tdmi]      Arm 9 processors
6440  *            -mstrongarm[110[0]]     StrongARM processors
6441  *            -m[arm]v[2345[t]]       Arm architectures
6442  *            -mall                   All (except the ARM1)
6443  *    FP variants:
6444  *            -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
6445  *            -mfpe-old               (No float load/store multiples)
6446  *            -mno-fpu                Disable all floating point instructions
6447  *    Run-time endian selection:
6448  *            -EB                     big endian cpu
6449  *            -EL                     little endian cpu
6450  *    ARM Procedure Calling Standard:
6451  *            -mapcs-32               32 bit APCS
6452  *            -mapcs-26               26 bit APCS
6453  *            -mapcs-float            Pass floats in float regs
6454  *            -mapcs-reentrant        Position independent code
6455  *            -mthumb-interwork       Code supports Arm/Thumb interworking
6456  *            -moabi                  Old ELF ABI
6457  */
6458
6459 CONST char * md_shortopts = "m:k";
6460 struct option md_longopts[] =
6461 {
6462 #ifdef ARM_BI_ENDIAN
6463 #define OPTION_EB (OPTION_MD_BASE + 0)
6464   {"EB", no_argument, NULL, OPTION_EB},
6465 #define OPTION_EL (OPTION_MD_BASE + 1)
6466   {"EL", no_argument, NULL, OPTION_EL},
6467 #ifdef OBJ_ELF
6468 #define OPTION_OABI (OPTION_MD_BASE +2)
6469   {"oabi", no_argument, NULL, OPTION_OABI},
6470 #endif
6471 #endif
6472   {NULL, no_argument, NULL, 0}
6473 };
6474 size_t md_longopts_size = sizeof (md_longopts);
6475
6476 int
6477 md_parse_option (c, arg)
6478      int    c;
6479      char * arg;
6480 {
6481   char * str = arg;
6482
6483   switch (c)
6484     {
6485 #ifdef ARM_BI_ENDIAN
6486     case OPTION_EB:
6487       target_big_endian = 1;
6488       break;
6489     case OPTION_EL:
6490       target_big_endian = 0;
6491       break;
6492 #endif
6493
6494     case 'm':
6495       switch (*str)
6496         {
6497         case 'f':
6498           if (streq (str, "fpa10"))
6499             cpu_variant = (cpu_variant & ~FPU_ALL) | FPU_FPA10;
6500           else if (streq (str, "fpa11"))
6501             cpu_variant = (cpu_variant & ~FPU_ALL) | FPU_FPA11;
6502           else if (streq (str, "fpe-old"))
6503             cpu_variant = (cpu_variant & ~FPU_ALL) | FPU_CORE;
6504           else
6505             goto bad;
6506           break;
6507
6508         case 'n':
6509           if (streq (str, "no-fpu"))
6510             cpu_variant &= ~FPU_ALL;
6511           break;
6512
6513 #ifdef OBJ_ELF
6514         case 'o':
6515           if (streq (str, "oabi"))
6516             target_oabi = true;
6517           break;
6518 #endif
6519           
6520         case 't':
6521           /* Limit assembler to generating only Thumb instructions: */
6522           if (streq (str, "thumb"))
6523             {
6524               cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_THUMB;
6525               cpu_variant = (cpu_variant & ~FPU_ALL) | FPU_NONE;
6526               thumb_mode = 1;
6527             }
6528           else if (streq (str, "thumb-interwork"))
6529             {
6530               if ((cpu_variant & ARM_THUMB) == 0)
6531                 cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_ARCH_V4T;
6532 #if defined OBJ_COFF || defined OBJ_ELF
6533               support_interwork = true;
6534 #endif
6535             }
6536           else
6537             goto bad;
6538           break;
6539
6540         default:
6541           if (streq (str, "all"))
6542             {
6543               cpu_variant = ARM_ALL | FPU_ALL;
6544               return 1;
6545             }
6546 #if defined OBJ_COFF || defined OBJ_ELF
6547           if (! strncmp (str, "apcs-", 5))
6548             {
6549               /* GCC passes on all command line options starting "-mapcs-..."
6550                  to us, so we must parse them here.  */
6551
6552               str += 5;
6553               
6554               if (streq (str, "32"))
6555                 {
6556                   uses_apcs_26 = false;
6557                   return 1;
6558                 }
6559               else if (streq (str, "26"))
6560                 {
6561                   uses_apcs_26 = true;
6562                   return 1;
6563                 }
6564               else if (streq (str, "frame"))
6565                 {
6566                   /* Stack frames are being generated - does not affect
6567                      linkage of code.  */
6568                   return 1;
6569                 }
6570               else if (streq (str, "stack-check"))
6571                 {
6572                   /* Stack checking is being performed - does not affect
6573                      linkage, but does require that the functions
6574                      __rt_stkovf_split_small and __rt_stkovf_split_big be
6575                      present in the final link.  */
6576
6577                   return 1;
6578                 }
6579               else if (streq (str, "float"))
6580                 {
6581                   /* Floating point arguments are being passed in the floating
6582                      point registers.  This does affect linking, since this
6583                      version of the APCS is incompatible with the version that
6584                      passes floating points in the integer registers.  */
6585
6586                   uses_apcs_float = true;
6587                   return 1;
6588                 }
6589               else if (streq (str, "reentrant"))
6590                 {
6591                   /* Reentrant code has been generated.  This does affect
6592                      linking, since there is no point in linking reentrant/
6593                      position independent code with absolute position code. */
6594                   pic_code = true;
6595                   return 1;
6596                 }
6597               
6598               as_bad (_("Unrecognised APCS switch -m%s"), arg);
6599               return 0;
6600             }
6601 #endif
6602           /* Strip off optional "arm" */
6603           if (! strncmp (str, "arm", 3))
6604             str += 3;
6605
6606           switch (*str)
6607             {
6608             case '1':
6609               if (streq (str, "1"))
6610                 cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_1;
6611               else
6612                 goto bad;
6613               break;
6614
6615             case '2':
6616               if (streq (str, "2"))
6617                 cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_2;
6618               else if (streq (str, "250"))
6619                 cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_250;
6620               else
6621                 goto bad;
6622               break;
6623
6624             case '3':
6625               if (streq (str, "3"))
6626                 cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_3;
6627               else
6628                 goto bad;
6629               break;
6630
6631             case '6':
6632               switch (strtol (str, NULL, 10))
6633                 {
6634                 case 6:
6635                 case 60:
6636                 case 600:
6637                 case 610:
6638                 case 620:
6639                   cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_6;
6640                   break;
6641                 default:
6642                   goto bad;
6643                 }
6644               break;
6645
6646             case '7':
6647               switch (strtol (str, & str, 10))  /* Eat the processor name */
6648                 {
6649                 case 7:
6650                 case 70:
6651                 case 700:
6652                 case 710:
6653                 case 720:
6654                 case 7100:
6655                 case 7500:
6656                   break;
6657                 default:
6658                   goto bad;
6659                 }
6660               cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_7;
6661               for (; *str; str++)
6662                 {
6663                 switch (* str)
6664                   {
6665                   case 't':
6666                     cpu_variant |= (ARM_THUMB | ARM_ARCH_V4);
6667                     break;
6668
6669                   case 'm':
6670                     cpu_variant |= ARM_LONGMUL;
6671                     break;
6672
6673                   case 'f': /* fe => fp enabled cpu.  */
6674                     if (str[1] == 'e')
6675                       ++ str;
6676                     else
6677                       goto bad;
6678                     
6679                   case 'c': /* Left over from 710c processor name.  */
6680                   case 'd': /* Debug */
6681                   case 'i': /* Embedded ICE */
6682                     /* Included for completeness in ARM processor naming. */
6683                     break;
6684
6685                   default:
6686                     goto bad;
6687                   }
6688                 }
6689               break;
6690
6691             case '8':
6692               if (streq (str, "8") || streq (str, "810"))
6693                 cpu_variant = (cpu_variant & ~ARM_ANY)
6694                   | ARM_8 | ARM_ARCH_V4 | ARM_LONGMUL;
6695               else
6696                 goto bad;
6697               break;
6698               
6699             case '9':
6700               if (streq (str, "9"))
6701                 cpu_variant = (cpu_variant & ~ARM_ANY)
6702                   | ARM_9 | ARM_ARCH_V4 | ARM_LONGMUL | ARM_THUMB;
6703               else if (streq (str, "920"))
6704                 cpu_variant = (cpu_variant & ~ARM_ANY)
6705                   | ARM_9 | ARM_ARCH_V4 | ARM_LONGMUL;
6706               else if (streq (str, "920t"))
6707                 cpu_variant = (cpu_variant & ~ARM_ANY)
6708                   | ARM_9 | ARM_ARCH_V4 | ARM_LONGMUL | ARM_THUMB;
6709               else if (streq (str, "9tdmi"))
6710                 cpu_variant = (cpu_variant & ~ARM_ANY)
6711                   | ARM_9 | ARM_ARCH_V4 | ARM_LONGMUL | ARM_THUMB;
6712               else
6713                 goto bad;
6714               break;
6715
6716               
6717             case 's':
6718               if (streq (str, "strongarm")
6719                   || streq (str, "strongarm110")
6720                   || streq (str, "strongarm1100"))
6721                 cpu_variant = (cpu_variant & ~ARM_ANY)
6722                   | ARM_8 | ARM_ARCH_V4 | ARM_LONGMUL;
6723               else
6724                 goto bad;
6725               break;
6726                 
6727             case 'v':
6728               /* Select variant based on architecture rather than processor.  */
6729               switch (*++str)
6730                 {
6731                 case '2':
6732                   switch (*++str)
6733                     {
6734                     case 'a':
6735                       cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_3;
6736                       break;
6737                     case 0:
6738                       cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_2;
6739                       break;
6740                     default:
6741                       as_bad (_("Invalid architecture variant -m%s"), arg);
6742                       break;
6743                     }
6744                   break;
6745                   
6746                 case '3':
6747                     cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_7;
6748                     
6749                   switch (*++str)
6750                     {
6751                     case 'm': cpu_variant |= ARM_LONGMUL; break;
6752                     case 0:   break;
6753                     default:
6754                       as_bad (_("Invalid architecture variant -m%s"), arg);
6755                       break;
6756                     }
6757                   break;
6758                   
6759                 case '4':
6760                   cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_ARCH_V4;
6761                   
6762                   switch (*++str)
6763                     {
6764                     case 't': cpu_variant |= ARM_THUMB; break;
6765                     case 0:   break;
6766                     default:
6767                       as_bad (_("Invalid architecture variant -m%s"), arg);
6768                       break;
6769                     }
6770                   break;
6771
6772                 case '5':
6773                   cpu_variant = (cpu_variant & ~ARM_ANY) | ARM_ARCH_V5;
6774                   switch (*++str)
6775                     {
6776                     case 't': cpu_variant |= ARM_THUMB; break;
6777                     case 0:   break;
6778                     default:
6779                       as_bad (_("Invalid architecture variant -m%s"), arg);
6780                       break;
6781                     }
6782                   break;
6783                   
6784                 default:
6785                   as_bad (_("Invalid architecture variant -m%s"), arg);
6786                   break;
6787                 }
6788               break;
6789               
6790             default:
6791             bad:
6792               as_bad (_("Invalid processor variant -m%s"), arg);
6793               return 0;
6794             }
6795         }
6796       break;
6797
6798 #if defined OBJ_ELF || defined OBJ_COFF
6799     case 'k':
6800       pic_code = 1;
6801       break;
6802 #endif
6803       
6804     default:
6805       return 0;
6806     }
6807
6808    return 1;
6809 }
6810
6811 void
6812 md_show_usage (fp)
6813      FILE * fp;
6814 {
6815   fprintf (fp, _("\
6816  ARM Specific Assembler Options:\n\
6817   -m[arm][<processor name>] select processor variant\n\
6818   -m[arm]v[2|2a|3|3m|4|4t|5[t][e]] select architecture variant\n\
6819   -mthumb                   only allow Thumb instructions\n\
6820   -mthumb-interwork         mark the assembled code as supporting interworking\n\
6821   -mall                     allow any instruction\n\
6822   -mfpa10, -mfpa11          select floating point architecture\n\
6823   -mfpe-old                 don't allow floating-point multiple instructions\n\
6824   -mno-fpu                  don't allow any floating-point instructions.\n\
6825   -k                        generate PIC code.\n"));
6826 #if defined OBJ_COFF || defined OBJ_ELF
6827   fprintf (fp, _("\
6828   -mapcs-32, -mapcs-26      specify which ARM Procedure Calling Standard to use\n\
6829   -mapcs-float              floating point args are passed in FP regs\n\
6830   -mapcs-reentrant          the code is position independent/reentrant\n"));
6831   #endif
6832 #ifdef OBJ_ELF
6833   fprintf (fp, _("\
6834   -moabi                    support the old ELF ABI\n"));
6835 #endif
6836 #ifdef ARM_BI_ENDIAN
6837   fprintf (fp, _("\
6838   -EB                       assemble code for a big endian cpu\n\
6839   -EL                       assemble code for a little endian cpu\n"));
6840 #endif
6841 }
6842
6843 /* We need to be able to fix up arbitrary expressions in some statements.
6844    This is so that we can handle symbols that are an arbitrary distance from
6845    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
6846    which returns part of an address in a form which will be valid for
6847    a data instruction.  We do this by pushing the expression into a symbol
6848    in the expr_section, and creating a fix for that.  */
6849
6850 static void
6851 fix_new_arm (frag, where, size, exp, pc_rel, reloc)
6852      fragS *       frag;
6853      int           where;
6854      short int     size;
6855      expressionS * exp;
6856      int           pc_rel;
6857      int           reloc;
6858 {
6859   fixS *         new_fix;
6860   arm_fix_data * arm_data;
6861
6862   switch (exp->X_op)
6863     {
6864     case O_constant:
6865     case O_symbol:
6866     case O_add:
6867     case O_subtract:
6868       new_fix = fix_new_exp (frag, where, size, exp, pc_rel, reloc);
6869       break;
6870
6871     default:
6872       new_fix = fix_new (frag, where, size, make_expr_symbol (exp), 0,
6873                          pc_rel, reloc);
6874       break;
6875     }
6876
6877   /* Mark whether the fix is to a THUMB instruction, or an ARM instruction */
6878   arm_data = (arm_fix_data *) obstack_alloc (& notes, sizeof (arm_fix_data));
6879   new_fix->tc_fix_data = (PTR) arm_data;
6880   arm_data->thumb_mode = thumb_mode;
6881
6882   return;
6883 }
6884
6885
6886 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
6887 void
6888 cons_fix_new_arm (frag, where, size, exp)
6889      fragS *       frag;
6890      int           where;
6891      int           size;
6892      expressionS * exp;
6893 {
6894   bfd_reloc_code_real_type type;
6895   int pcrel = 0;
6896
6897   /* Pick a reloc.
6898      FIXME: @@ Should look at CPU word size.  */
6899   switch (size) 
6900     {
6901     case 1:
6902       type = BFD_RELOC_8;
6903       break;
6904     case 2:
6905       type = BFD_RELOC_16;
6906       break;
6907     case 4:
6908     default:
6909       type = BFD_RELOC_32;
6910       break;
6911     case 8:
6912       type = BFD_RELOC_64;
6913       break;
6914     }
6915   
6916   fix_new_exp (frag, where, (int) size, exp, pcrel, type);
6917 }
6918
6919 /* A good place to do this, although this was probably not intended
6920    for this kind of use.  We need to dump the literal pool before
6921    references are made to a null symbol pointer.  */
6922 void
6923 arm_cleanup ()
6924 {
6925   if (current_poolP == NULL)
6926     return;
6927   
6928   subseg_set (text_section, 0); /* Put it at the end of text section.  */
6929   s_ltorg (0);
6930   listing_prev_line ();
6931 }
6932
6933 void
6934 arm_start_line_hook ()
6935 {
6936   last_label_seen = NULL;
6937 }
6938
6939 void
6940 arm_frob_label (sym)
6941      symbolS * sym;
6942 {
6943   last_label_seen = sym;
6944   
6945   ARM_SET_THUMB (sym, thumb_mode);
6946   
6947 #if defined OBJ_COFF || defined OBJ_ELF
6948   ARM_SET_INTERWORK (sym, support_interwork);
6949 #endif
6950   
6951   if (label_is_thumb_function_name)
6952     {
6953       /* When the address of a Thumb function is taken the bottom
6954          bit of that address should be set.  This will allow
6955          interworking between Arm and Thumb functions to work
6956          correctly.  */
6957
6958       THUMB_SET_FUNC (sym, 1);
6959       
6960       label_is_thumb_function_name = false;
6961     }
6962 }
6963
6964 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
6965    ARM ones.  */
6966
6967 void
6968 arm_adjust_symtab ()
6969 {
6970 #ifdef OBJ_COFF
6971   symbolS * sym;
6972
6973   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
6974     {
6975       if (ARM_IS_THUMB (sym))
6976         {
6977           if (THUMB_IS_FUNC (sym))
6978             {
6979               /* Mark the symbol as a Thumb function.  */
6980               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
6981                   || S_GET_STORAGE_CLASS (sym) == C_LABEL) /* This can happen! */
6982                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
6983
6984               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
6985                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
6986               else
6987                 as_bad (_("%s: unexpected function type: %d"),
6988                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
6989             }
6990           else switch (S_GET_STORAGE_CLASS (sym))
6991             {
6992               case C_EXT:
6993                 S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
6994                 break;
6995               case C_STAT:
6996                 S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
6997                 break;
6998               case C_LABEL:
6999                 S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
7000                 break;
7001               default: /* do nothing */ 
7002                 break;
7003             }
7004         }
7005
7006       if (ARM_IS_INTERWORK (sym))
7007         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
7008     }
7009 #endif
7010 #ifdef OBJ_ELF
7011   symbolS *         sym;
7012   char              bind;
7013
7014   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
7015     {
7016       if (ARM_IS_THUMB (sym))
7017         {
7018           elf_symbol_type * elf_sym;
7019           
7020           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
7021           bind = ELF_ST_BIND (elf_sym);
7022           
7023           /* If it's a .thumb_func, declare it as so,
7024              otherwise tag label as .code 16.  */
7025           if (THUMB_IS_FUNC (sym))
7026             elf_sym->internal_elf_sym.st_info =
7027               ELF_ST_INFO (bind, STT_ARM_TFUNC);
7028           else
7029             elf_sym->internal_elf_sym.st_info =
7030               ELF_ST_INFO (bind, STT_ARM_16BIT);
7031          }
7032      }
7033 #endif
7034 }
7035
7036 int
7037 arm_data_in_code ()
7038 {
7039   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
7040     {
7041       *input_line_pointer = '/';
7042       input_line_pointer += 5;
7043       *input_line_pointer = 0;
7044       return 1;
7045     }
7046   
7047   return 0;
7048 }
7049
7050 char *
7051 arm_canonicalize_symbol_name (name)
7052      char * name;
7053 {
7054   int len;
7055
7056   if (thumb_mode && (len = strlen (name)) > 5
7057       && streq (name + len - 5, "/data"))
7058     *(name + len - 5) = 0;
7059
7060   return name;
7061 }
7062
7063 boolean
7064 arm_validate_fix (fixP)
7065      fixS * fixP;
7066 {
7067   /* If the destination of the branch is a defined symbol which does not have
7068      the THUMB_FUNC attribute, then we must be calling a function which has
7069      the (interfacearm) attribute.  We look for the Thumb entry point to that
7070      function and change the branch to refer to that function instead.  */
7071   if (   fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
7072       && fixP->fx_addsy != NULL
7073       && S_IS_DEFINED (fixP->fx_addsy)
7074       && ! THUMB_IS_FUNC (fixP->fx_addsy))
7075     {
7076       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
7077       return true;
7078     }
7079
7080   return false;
7081 }
7082
7083 #ifdef OBJ_ELF
7084 /* Relocations against Thumb function names must be left unadjusted,
7085    so that the linker can use this information to correctly set the
7086    bottom bit of their addresses.  The MIPS version of this function
7087    also prevents relocations that are mips-16 specific, but I do not
7088    know why it does this.
7089
7090    FIXME:
7091    There is one other problem that ought to be addressed here, but
7092    which currently is not:  Taking the address of a label (rather
7093    than a function) and then later jumping to that address.  Such
7094    addresses also ought to have their bottom bit set (assuming that
7095    they reside in Thumb code), but at the moment they will not.  */
7096    
7097 boolean
7098 arm_fix_adjustable (fixP)
7099    fixS * fixP;
7100 {
7101   if (fixP->fx_addsy == NULL)
7102     return 1;
7103   
7104   /* Prevent all adjustments to global symbols. */
7105   if (S_IS_EXTERN (fixP->fx_addsy))
7106     return 0;
7107   
7108   if (S_IS_WEAK (fixP->fx_addsy))
7109     return 0;
7110
7111   if (THUMB_IS_FUNC (fixP->fx_addsy)
7112       && fixP->fx_subsy == NULL)
7113     return 0;
7114   
7115   /* We need the symbol name for the VTABLE entries */
7116   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
7117       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
7118     return 0;
7119
7120   return 1;
7121 }
7122
7123 const char *
7124 elf32_arm_target_format ()
7125 {
7126   if (target_big_endian)
7127     if (target_oabi)
7128       return "elf32-bigarm-oabi";
7129     else
7130       return "elf32-bigarm";
7131   else
7132     if (target_oabi)
7133       return "elf32-littlearm-oabi";
7134     else
7135       return "elf32-littlearm";
7136 }
7137
7138 void
7139 armelf_frob_symbol (symp, puntp)
7140      symbolS * symp;
7141      int * puntp;
7142 {
7143   elf_frob_symbol (symp, puntp);
7144
7145
7146 int
7147 arm_force_relocation (fixp)
7148      struct fix * fixp;
7149 {
7150   if (   fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
7151       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
7152       || fixp->fx_r_type == BFD_RELOC_ARM_PCREL_BRANCH
7153       || fixp->fx_r_type == BFD_RELOC_ARM_PCREL_BLX
7154       || fixp->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX
7155       || fixp->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23)    
7156     return 1;
7157   
7158   return 0;
7159 }
7160
7161 static bfd_reloc_code_real_type
7162 arm_parse_reloc ()
7163 {
7164   char   id[16];
7165   char * ip;
7166   unsigned int i;
7167   static struct
7168   {
7169     char * str;
7170     int    len;
7171     bfd_reloc_code_real_type reloc;
7172   }
7173   reloc_map[] =
7174   {
7175 #define MAP(str,reloc) { str, sizeof (str)-1, reloc }
7176     MAP ("(got)",    BFD_RELOC_ARM_GOT32),
7177     MAP ("(gotoff)", BFD_RELOC_ARM_GOTOFF),
7178     /* ScottB: Jan 30, 1998 */
7179     /* Added support for parsing "var(PLT)" branch instructions */
7180     /* generated by GCC for PLT relocs */
7181     MAP ("(plt)",    BFD_RELOC_ARM_PLT32),
7182     { NULL, 0,         BFD_RELOC_UNUSED }
7183 #undef MAP    
7184   };
7185
7186   for (i = 0, ip = input_line_pointer;
7187        i < sizeof (id) && (isalnum (*ip) || ispunct (*ip));
7188        i++, ip++)
7189     id[i] = tolower (*ip);
7190   
7191   for (i = 0; reloc_map[i].str; i++)
7192     if (strncmp (id, reloc_map[i].str, reloc_map[i].len) == 0)
7193       break;
7194   
7195   input_line_pointer += reloc_map[i].len;
7196   
7197   return reloc_map[i].reloc;
7198 }
7199
7200 static void
7201 s_arm_elf_cons (nbytes)
7202      int nbytes;
7203 {
7204   expressionS exp;
7205
7206 #ifdef md_flush_pending_output
7207   md_flush_pending_output ();
7208 #endif
7209
7210   if (is_it_end_of_statement ())
7211     {
7212       demand_empty_rest_of_line ();
7213       return;
7214     }
7215
7216 #ifdef md_cons_align
7217   md_cons_align (nbytes);
7218 #endif
7219
7220   do
7221     {
7222       bfd_reloc_code_real_type reloc;
7223       
7224       expression (& exp);
7225
7226       if (exp.X_op == O_symbol
7227           && * input_line_pointer == '('
7228           && (reloc = arm_parse_reloc()) != BFD_RELOC_UNUSED)
7229         {
7230           reloc_howto_type * howto = bfd_reloc_type_lookup (stdoutput, reloc);
7231           int size = bfd_get_reloc_size (howto);
7232
7233           if (size > nbytes)
7234             as_bad ("%s relocations do not fit in %d bytes",
7235                     howto->name, nbytes);
7236           else
7237             {
7238               register char * p = frag_more ((int) nbytes);
7239               int offset = nbytes - size;
7240
7241               fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size,
7242                            & exp, 0, reloc);
7243             }
7244         }
7245       else
7246         emit_expr (& exp, (unsigned int) nbytes);
7247     }
7248   while (*input_line_pointer++ == ',');
7249
7250   input_line_pointer--;         /* Put terminator back into stream.  */
7251   demand_empty_rest_of_line ();
7252 }
7253
7254 #endif /* OBJ_ELF */