]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/binutils/gas/config/tc-arc.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / binutils / gas / config / tc-arc.c
1 /* tc-arc.c -- Assembler for the ARC
2    Copyright 1994, 1995, 1997, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by Doug Evans (dje@cygnus.com).
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 <stdio.h>
24 #include "libiberty.h"
25 #include "as.h"
26 #include "struc-symbol.h"
27 #include "safe-ctype.h"
28 #include "subsegs.h"
29 #include "opcode/arc.h"
30 #include "../opcodes/arc-ext.h"
31 #include "elf/arc.h"
32 #include "dwarf2dbg.h"
33
34 extern int arc_get_mach PARAMS ((char *));
35 extern int arc_operand_type PARAMS ((int));
36 extern int arc_insn_not_jl PARAMS ((arc_insn));
37 extern int arc_limm_fixup_adjust PARAMS ((arc_insn));
38 extern int arc_get_noshortcut_flag PARAMS ((void));
39 extern int arc_set_ext_seg PARAMS ((void));
40 extern void arc_code_symbol PARAMS ((expressionS *));
41
42 static arc_insn arc_insert_operand PARAMS ((arc_insn,
43                                             const struct arc_operand *, int,
44                                             const struct arc_operand_value *,
45                                             offsetT, char *, unsigned int));
46 static void arc_common PARAMS ((int));
47 static void arc_extinst PARAMS ((int));
48 static void arc_extoper PARAMS ((int));
49 static void arc_option PARAMS ((int));
50 static int  get_arc_exp_reloc_type PARAMS ((int, int, expressionS *,
51                                            expressionS *));
52
53 static void init_opcode_tables PARAMS ((int));
54
55 const struct suffix_classes {
56   char *name;
57   int  len;
58 } suffixclass[] = {
59   { "SUFFIX_COND|SUFFIX_FLAG",23 },
60   { "SUFFIX_FLAG", 11 },
61   { "SUFFIX_COND", 11 },
62   { "SUFFIX_NONE", 11 }
63 };
64
65 #define MAXSUFFIXCLASS (sizeof (suffixclass) / sizeof (struct suffix_classes))
66
67 const struct syntax_classes {
68   char *name;
69   int  len;
70   int  class;
71 } syntaxclass[] = {
72   { "SYNTAX_3OP|OP1_MUST_BE_IMM", 26, SYNTAX_3OP|OP1_MUST_BE_IMM|SYNTAX_VALID },
73   { "OP1_MUST_BE_IMM|SYNTAX_3OP", 26, OP1_MUST_BE_IMM|SYNTAX_3OP|SYNTAX_VALID },
74   { "SYNTAX_2OP|OP1_IMM_IMPLIED", 26, SYNTAX_2OP|OP1_IMM_IMPLIED|SYNTAX_VALID },
75   { "OP1_IMM_IMPLIED|SYNTAX_2OP", 26, OP1_IMM_IMPLIED|SYNTAX_2OP|SYNTAX_VALID },
76   { "SYNTAX_3OP",                 10, SYNTAX_3OP|SYNTAX_VALID },
77   { "SYNTAX_2OP",                 10, SYNTAX_2OP|SYNTAX_VALID }
78 };
79
80 #define MAXSYNTAXCLASS (sizeof (syntaxclass) / sizeof (struct syntax_classes))
81
82 const pseudo_typeS md_pseudo_table[] = {
83   { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0).  */
84   { "comm", arc_common, 0 },
85   { "common", arc_common, 0 },
86   { "lcomm", arc_common, 1 },
87   { "lcommon", arc_common, 1 },
88   { "2byte", cons, 2 },
89   { "half", cons, 2 },
90   { "short", cons, 2 },
91   { "3byte", cons, 3 },
92   { "4byte", cons, 4 },
93   { "word", cons, 4 },
94   { "option", arc_option, 0 },
95   { "cpu", arc_option, 0 },
96   { "block", s_space, 0 },
97   { "extcondcode", arc_extoper, 0 },
98   { "extcoreregister", arc_extoper, 1 },
99   { "extauxregister", arc_extoper, 2 },
100   { "extinstruction", arc_extinst, 0 },
101   { NULL, 0, 0 },
102 };
103
104 /* This array holds the chars that always start a comment.  If the
105    pre-processor is disabled, these aren't very useful.  */
106 const char comment_chars[] = "#;";
107
108 /* This array holds the chars that only start a comment at the beginning of
109    a line.  If the line seems to have the form '# 123 filename'
110    .line and .file directives will appear in the pre-processed output */
111 /* Note that input_file.c hand checks for '#' at the beginning of the
112    first line of the input file.  This is because the compiler outputs
113    #NO_APP at the beginning of its output.  */
114 /* Also note that comments started like this one will always
115    work if '/' isn't otherwise defined.  */
116 const char line_comment_chars[] = "#";
117
118 const char line_separator_chars[] = "";
119
120 /* Chars that can be used to separate mant from exp in floating point nums.  */
121 const char EXP_CHARS[] = "eE";
122
123 /* Chars that mean this number is a floating point constant
124    As in 0f12.456 or 0d1.2345e12.  */
125 const char FLT_CHARS[] = "rRsSfFdD";
126
127 /* Byte order.  */
128 extern int target_big_endian;
129 const char *arc_target_format = DEFAULT_TARGET_FORMAT;
130 static int byte_order = DEFAULT_BYTE_ORDER;
131
132 static segT arcext_section;
133
134 /* One of bfd_mach_arc_n.  */
135 static int arc_mach_type = bfd_mach_arc_6;
136
137 /* Non-zero if the cpu type has been explicitly specified.  */
138 static int mach_type_specified_p = 0;
139
140 /* Non-zero if opcode tables have been initialized.
141    A .option command must appear before any instructions.  */
142 static int cpu_tables_init_p = 0;
143
144 static struct hash_control *arc_suffix_hash = NULL;
145 \f
146 const char *md_shortopts = "";
147 struct option md_longopts[] = {
148 #define OPTION_EB (OPTION_MD_BASE + 0)
149   { "EB", no_argument, NULL, OPTION_EB },
150 #define OPTION_EL (OPTION_MD_BASE + 1)
151   { "EL", no_argument, NULL, OPTION_EL },
152 #define OPTION_ARC5 (OPTION_MD_BASE + 2)
153   { "marc5", no_argument, NULL, OPTION_ARC5 },
154   { "pre-v6", no_argument, NULL, OPTION_ARC5 },
155 #define OPTION_ARC6 (OPTION_MD_BASE + 3)
156   { "marc6", no_argument, NULL, OPTION_ARC6 },
157 #define OPTION_ARC7 (OPTION_MD_BASE + 4)
158   { "marc7", no_argument, NULL, OPTION_ARC7 },
159 #define OPTION_ARC8 (OPTION_MD_BASE + 5)
160   { "marc8", no_argument, NULL, OPTION_ARC8 },
161 #define OPTION_ARC (OPTION_MD_BASE + 6)
162   { "marc", no_argument, NULL, OPTION_ARC },
163   { NULL, no_argument, NULL, 0 }
164 };
165 size_t md_longopts_size = sizeof (md_longopts);
166
167 #define IS_SYMBOL_OPERAND(o) \
168  ((o) == 'b' || (o) == 'c' || (o) == 's' || (o) == 'o' || (o) == 'O')
169
170 struct arc_operand_value *get_ext_suffix (char *s);
171
172 /* Invocation line includes a switch not recognized by the base assembler.
173    See if it's a processor-specific option.  */
174
175 int
176 md_parse_option (c, arg)
177      int c;
178      char *arg ATTRIBUTE_UNUSED;
179 {
180   switch (c)
181     {
182     case OPTION_ARC5:
183       arc_mach_type = bfd_mach_arc_5;
184       break;
185     case OPTION_ARC:
186     case OPTION_ARC6:
187       arc_mach_type = bfd_mach_arc_6;
188       break;
189     case OPTION_ARC7:
190       arc_mach_type = bfd_mach_arc_7;
191       break;
192     case OPTION_ARC8:
193       arc_mach_type = bfd_mach_arc_8;
194       break;
195     case OPTION_EB:
196       byte_order = BIG_ENDIAN;
197       arc_target_format = "elf32-bigarc";
198       break;
199     case OPTION_EL:
200       byte_order = LITTLE_ENDIAN;
201       arc_target_format = "elf32-littlearc";
202       break;
203     default:
204       return 0;
205     }
206   return 1;
207 }
208
209 void
210 md_show_usage (stream)
211      FILE *stream;
212 {
213   fprintf (stream, "\
214 ARC Options:\n\
215   -marc[5|6|7|8]          select processor variant (default arc%d)\n\
216   -EB                     assemble code for a big endian cpu\n\
217   -EL                     assemble code for a little endian cpu\n", arc_mach_type + 5);
218 }
219
220 /* This function is called once, at assembler startup time.  It should
221    set up all the tables, etc. that the MD part of the assembler will need.
222    Opcode selection is deferred until later because we might see a .option
223    command.  */
224
225 void
226 md_begin ()
227 {
228   /* The endianness can be chosen "at the factory".  */
229   target_big_endian = byte_order == BIG_ENDIAN;
230
231   if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, arc_mach_type))
232     as_warn ("could not set architecture and machine");
233
234   /* This call is necessary because we need to initialize `arc_operand_map'
235      which may be needed before we see the first insn.  */
236   arc_opcode_init_tables (arc_get_opcode_mach (arc_mach_type,
237                                                target_big_endian));
238 }
239
240 /* Initialize the various opcode and operand tables.
241    MACH is one of bfd_mach_arc_xxx.  */
242 static void
243 init_opcode_tables (mach)
244      int mach;
245 {
246   int i;
247   char *last;
248
249   if ((arc_suffix_hash = hash_new ()) == NULL)
250     as_fatal ("virtual memory exhausted");
251
252   if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
253     as_warn ("could not set architecture and machine");
254
255   /* This initializes a few things in arc-opc.c that we need.
256      This must be called before the various arc_xxx_supported fns.  */
257   arc_opcode_init_tables (arc_get_opcode_mach (mach, target_big_endian));
258
259   /* Only put the first entry of each equivalently named suffix in the
260      table.  */
261   last = "";
262   for (i = 0; i < arc_suffixes_count; i++)
263     {
264       if (strcmp (arc_suffixes[i].name, last) != 0)
265         hash_insert (arc_suffix_hash, arc_suffixes[i].name, (PTR) (arc_suffixes + i));
266       last = arc_suffixes[i].name;
267     }
268
269   /* Since registers don't have a prefix, we put them in the symbol table so
270      they can't be used as symbols.  This also simplifies argument parsing as
271      we can let gas parse registers for us.  The recorded register number is
272      the address of the register's entry in arc_reg_names.
273
274      If the register name is already in the table, then the existing
275      definition is assumed to be from an .ExtCoreRegister pseudo-op.  */
276
277   for (i = 0; i < arc_reg_names_count; i++)
278     {
279       if (symbol_find (arc_reg_names[i].name))
280         continue;
281       /* Use symbol_create here instead of symbol_new so we don't try to
282          output registers into the object file's symbol table.  */
283       symbol_table_insert (symbol_create (arc_reg_names[i].name,
284                                           reg_section,
285                                           (int) &arc_reg_names[i],
286                                           &zero_address_frag));
287     }
288
289   /* Tell `.option' it's too late.  */
290   cpu_tables_init_p = 1;
291 }
292 \f
293 /* Insert an operand value into an instruction.
294    If REG is non-NULL, it is a register number and ignore VAL.  */
295
296 static arc_insn
297 arc_insert_operand (insn, operand, mods, reg, val, file, line)
298      arc_insn insn;
299      const struct arc_operand *operand;
300      int mods;
301      const struct arc_operand_value *reg;
302      offsetT val;
303      char *file;
304      unsigned int line;
305 {
306   if (operand->bits != 32)
307     {
308       long min, max;
309       offsetT test;
310
311       if ((operand->flags & ARC_OPERAND_SIGNED) != 0)
312         {
313           if ((operand->flags & ARC_OPERAND_SIGNOPT) != 0)
314             max = (1 << operand->bits) - 1;
315           else
316             max = (1 << (operand->bits - 1)) - 1;
317           min = - (1 << (operand->bits - 1));
318         }
319       else
320         {
321           max = (1 << operand->bits) - 1;
322           min = 0;
323         }
324
325       if ((operand->flags & ARC_OPERAND_NEGATIVE) != 0)
326         test = - val;
327       else
328         test = val;
329
330       if (test < (offsetT) min || test > (offsetT) max)
331         {
332           const char *err =
333             "operand out of range (%s not between %ld and %ld)";
334           char buf[100];
335
336           sprint_value (buf, test);
337           if (file == (char *) NULL)
338             as_warn (err, buf, min, max);
339           else
340             as_warn_where (file, line, err, buf, min, max);
341         }
342     }
343
344   if (operand->insert)
345     {
346       const char *errmsg;
347
348       errmsg = NULL;
349       insn = (*operand->insert) (insn, operand, mods, reg, (long) val, &errmsg);
350       if (errmsg != (const char *) NULL)
351         as_warn (errmsg);
352     }
353   else
354     insn |= (((long) val & ((1 << operand->bits) - 1))
355              << operand->shift);
356
357   return insn;
358 }
359
360 /* We need to keep a list of fixups.  We can't simply generate them as
361    we go, because that would require us to first create the frag, and
362    that would screw up references to ``.''.  */
363
364 struct arc_fixup {
365   /* index into `arc_operands'  */
366   int opindex;
367   expressionS exp;
368 };
369
370 #define MAX_FIXUPS 5
371
372 #define MAX_SUFFIXES 5
373
374 /* This routine is called for each instruction to be assembled.  */
375
376 void
377 md_assemble (str)
378      char *str;
379 {
380   const struct arc_opcode *opcode;
381   const struct arc_opcode *std_opcode;
382   struct arc_opcode *ext_opcode;
383   char *start;
384   const char *last_errmsg = 0;
385   arc_insn insn;
386   static int init_tables_p = 0;
387
388   /* Opcode table initialization is deferred until here because we have to
389      wait for a possible .option command.  */
390   if (!init_tables_p)
391     {
392       init_opcode_tables (arc_mach_type);
393       init_tables_p = 1;
394     }
395
396   /* Skip leading white space.  */
397   while (ISSPACE (*str))
398     str++;
399
400   /* The instructions are stored in lists hashed by the first letter (though
401      we needn't care how they're hashed).  Get the first in the list.  */
402
403   ext_opcode = arc_ext_opcodes;
404   std_opcode = arc_opcode_lookup_asm (str);
405
406   /* Keep looking until we find a match.  */
407
408   start = str;
409   for (opcode = (ext_opcode ? ext_opcode : std_opcode);
410        opcode != NULL;
411        opcode = (ARC_OPCODE_NEXT_ASM (opcode)
412                  ? ARC_OPCODE_NEXT_ASM (opcode)
413                  : (ext_opcode ? ext_opcode = NULL, std_opcode : NULL)))
414     {
415       int past_opcode_p, fc, num_suffixes;
416       int fix_up_at = 0;
417       char *syn;
418       struct arc_fixup fixups[MAX_FIXUPS];
419       /* Used as a sanity check.  If we need a limm reloc, make sure we ask
420          for an extra 4 bytes from frag_more.  */
421       int limm_reloc_p;
422       int ext_suffix_p;
423       const struct arc_operand_value *insn_suffixes[MAX_SUFFIXES];
424
425       /* Is this opcode supported by the selected cpu?  */
426       if (! arc_opcode_supported (opcode))
427         continue;
428
429       /* Scan the syntax string.  If it doesn't match, try the next one.  */
430
431       arc_opcode_init_insert ();
432       insn = opcode->value;
433       fc = 0;
434       past_opcode_p = 0;
435       num_suffixes = 0;
436       limm_reloc_p = 0;
437       ext_suffix_p = 0;
438
439       /* We don't check for (*str != '\0') here because we want to parse
440          any trailing fake arguments in the syntax string.  */
441       for (str = start, syn = opcode->syntax; *syn != '\0';)
442         {
443           int mods;
444           const struct arc_operand *operand;
445
446           /* Non operand chars must match exactly.  */
447           if (*syn != '%' || *++syn == '%')
448             {
449               /* Handle '+' specially as we want to allow "ld r0,[sp-4]".  */
450               /* ??? The syntax has changed to [sp,-4].  */
451               if (0 && *syn == '+' && *str == '-')
452                 {
453                   /* Skip over syn's +, but leave str's - alone.
454                      That makes the case identical to "ld r0,[sp+-4]".  */
455                   ++syn;
456                 }
457               else if (*str == *syn)
458                 {
459                   if (*syn == ' ')
460                     past_opcode_p = 1;
461                   ++syn;
462                   ++str;
463                 }
464               else
465                 break;
466               continue;
467             }
468
469           /* We have an operand.  Pick out any modifiers.  */
470           mods = 0;
471           while (ARC_MOD_P (arc_operands[arc_operand_map[(int) *syn]].flags))
472             {
473               mods |= arc_operands[arc_operand_map[(int) *syn]].flags & ARC_MOD_BITS;
474               ++syn;
475             }
476           operand = arc_operands + arc_operand_map[(int) *syn];
477           if (operand->fmt == 0)
478             as_fatal ("unknown syntax format character `%c'", *syn);
479
480           if (operand->flags & ARC_OPERAND_FAKE)
481             {
482               const char *errmsg = NULL;
483               if (operand->insert)
484                 {
485                   insn = (*operand->insert) (insn, operand, mods, NULL, 0, &errmsg);
486                   if (errmsg != (const char *) NULL)
487                     {
488                       last_errmsg = errmsg;
489                       if (operand->flags & ARC_OPERAND_ERROR)
490                         {
491                           as_bad (errmsg);
492                           return;
493                         }
494                       else if (operand->flags & ARC_OPERAND_WARN)
495                         as_warn (errmsg);
496                       break;
497                     }
498                   if (limm_reloc_p
499                       && (operand->flags && operand->flags & ARC_OPERAND_LIMM)
500                       && (operand->flags &
501                           (ARC_OPERAND_ABSOLUTE_BRANCH | ARC_OPERAND_ADDRESS)))
502                     {
503                       fixups[fix_up_at].opindex = arc_operand_map[operand->fmt];
504                     }
505                 }
506               ++syn;
507             }
508           /* Are we finished with suffixes?  */
509           else if (!past_opcode_p)
510             {
511               int found;
512               char c;
513               char *s, *t;
514               const struct arc_operand_value *suf, *suffix_end;
515               const struct arc_operand_value *suffix = NULL;
516
517               if (!(operand->flags & ARC_OPERAND_SUFFIX))
518                 abort ();
519
520               /* If we're at a space in the input string, we want to skip the
521                  remaining suffixes.  There may be some fake ones though, so
522                  just go on to try the next one.  */
523               if (*str == ' ')
524                 {
525                   ++syn;
526                   continue;
527                 }
528
529               s = str;
530               if (mods & ARC_MOD_DOT)
531                 {
532                   if (*s != '.')
533                     break;
534                   ++s;
535                 }
536               else
537                 {
538                   /* This can happen in "b.nd foo" and we're currently looking
539                      for "%q" (ie: a condition code suffix).  */
540                   if (*s == '.')
541                     {
542                       ++syn;
543                       continue;
544                     }
545                 }
546
547               /* Pick the suffix out and look it up via the hash table.  */
548               for (t = s; *t && ISALNUM (*t); ++t)
549                 continue;
550               c = *t;
551               *t = '\0';
552               if ((suf = get_ext_suffix (s)))
553                 ext_suffix_p = 1;
554               else
555                 suf = hash_find (arc_suffix_hash, s);
556               if (!suf)
557                 {
558                   /* This can happen in "blle foo" and we're currently using
559                      the template "b%q%.n %j".  The "bl" insn occurs later in
560                      the table so "lle" isn't an illegal suffix.  */
561                   *t = c;
562                   break;
563                 }
564
565               /* Is it the right type?  Note that the same character is used
566                  several times, so we have to examine all of them.  This is
567                  relatively efficient as equivalent entries are kept
568                  together.  If it's not the right type, don't increment `str'
569                  so we try the next one in the series.  */
570               found = 0;
571               if (ext_suffix_p && arc_operands[suf->type].fmt == *syn)
572                 {
573                   /* Insert the suffix's value into the insn.  */
574                   *t = c;
575                   if (operand->insert)
576                     insn = (*operand->insert) (insn, operand,
577                                                mods, NULL, suf->value,
578                                                NULL);
579                   else
580                     insn |= suf->value << operand->shift;
581
582                   str = t;
583                   found = 1;
584                 }
585               else
586                 {
587                   *t = c;
588                   suffix_end = arc_suffixes + arc_suffixes_count;
589                   for (suffix = suf;
590                        suffix < suffix_end && strcmp (suffix->name, suf->name) == 0;
591                        ++suffix)
592                     {
593                       if (arc_operands[suffix->type].fmt == *syn)
594                         {
595                           /* Insert the suffix's value into the insn.  */
596                           if (operand->insert)
597                             insn = (*operand->insert) (insn, operand,
598                                                        mods, NULL, suffix->value,
599                                                        NULL);
600                           else
601                             insn |= suffix->value << operand->shift;
602
603                           str = t;
604                           found = 1;
605                           break;
606                         }
607                     }
608                 }
609               ++syn;
610               if (!found)
611                 /* Wrong type.  Just go on to try next insn entry.  */
612                 ;
613               else
614                 {
615                   if (num_suffixes == MAX_SUFFIXES)
616                     as_bad ("too many suffixes");
617                   else
618                     insn_suffixes[num_suffixes++] = suffix;
619                 }
620             }
621           else
622             /* This is either a register or an expression of some kind.  */
623             {
624               char *hold;
625               const struct arc_operand_value *reg = NULL;
626               long value = 0;
627               expressionS exp;
628
629               if (operand->flags & ARC_OPERAND_SUFFIX)
630                 abort ();
631
632               /* Is there anything left to parse?
633                  We don't check for this at the top because we want to parse
634                  any trailing fake arguments in the syntax string.  */
635               if (is_end_of_line[(unsigned char) *str])
636                 break;
637
638               /* Parse the operand.  */
639               hold = input_line_pointer;
640               input_line_pointer = str;
641               expression (&exp);
642               str = input_line_pointer;
643               input_line_pointer = hold;
644
645               if (exp.X_op == O_illegal)
646                 as_bad ("illegal operand");
647               else if (exp.X_op == O_absent)
648                 as_bad ("missing operand");
649               else if (exp.X_op == O_constant)
650                 {
651                   value = exp.X_add_number;
652                 }
653               else if (exp.X_op == O_register)
654                 {
655                   reg = (struct arc_operand_value *) exp.X_add_number;
656                 }
657 #define IS_REG_DEST_OPERAND(o) ((o) == 'a')
658               else if (IS_REG_DEST_OPERAND (*syn))
659                 as_bad ("symbol as destination register");
660               else
661                 {
662                   if (!strncmp (str, "@h30", 4))
663                     {
664                       arc_code_symbol (&exp);
665                       str += 4;
666                     }
667                   /* We need to generate a fixup for this expression.  */
668                   if (fc >= MAX_FIXUPS)
669                     as_fatal ("too many fixups");
670                   fixups[fc].exp = exp;
671                   /* We don't support shimm relocs. break here to force
672                      the assembler to output a limm.  */
673 #define IS_REG_SHIMM_OFFSET(o) ((o) == 'd')
674                   if (IS_REG_SHIMM_OFFSET (*syn))
675                     break;
676                   /* If this is a register constant (IE: one whose
677                      register value gets stored as 61-63) then this
678                      must be a limm.  */
679                   /* ??? This bit could use some cleaning up.
680                      Referencing the format chars like this goes
681                      against style.  */
682                   if (IS_SYMBOL_OPERAND (*syn))
683                     {
684                       const char *junk;
685                       limm_reloc_p = 1;
686                       /* Save this, we don't yet know what reloc to use.  */
687                       fix_up_at = fc;
688                       /* Tell insert_reg we need a limm.  This is
689                          needed because the value at this point is
690                          zero, a shimm.  */
691                       /* ??? We need a cleaner interface than this.  */
692                       (*arc_operands[arc_operand_map['Q']].insert)
693                         (insn, operand, mods, reg, 0L, &junk);
694                     }
695                   else
696                     fixups[fc].opindex = arc_operand_map[(int) *syn];
697                   ++fc;
698                   value = 0;
699                 }
700
701               /* Insert the register or expression into the instruction.  */
702               if (operand->insert)
703                 {
704                   const char *errmsg = NULL;
705                   insn = (*operand->insert) (insn, operand, mods,
706                                              reg, (long) value, &errmsg);
707                   if (errmsg != (const char *) NULL)
708                     {
709                       last_errmsg = errmsg;
710                       if (operand->flags & ARC_OPERAND_ERROR)
711                         {
712                           as_bad (errmsg);
713                           return;
714                         }
715                       else if (operand->flags & ARC_OPERAND_WARN)
716                         as_warn (errmsg);
717                       break;
718                     }
719                 }
720               else
721                 insn |= (value & ((1 << operand->bits) - 1)) << operand->shift;
722
723               ++syn;
724             }
725         }
726
727       /* If we're at the end of the syntax string, we're done.  */
728       /* FIXME: try to move this to a separate function.  */
729       if (*syn == '\0')
730         {
731           int i;
732           char *f;
733           long limm, limm_p;
734
735           /* For the moment we assume a valid `str' can only contain blanks
736              now.  IE: We needn't try again with a longer version of the
737              insn and it is assumed that longer versions of insns appear
738              before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
739
740           while (ISSPACE (*str))
741             ++str;
742
743           if (!is_end_of_line[(unsigned char) *str])
744             as_bad ("junk at end of line: `%s'", str);
745
746           /* Is there a limm value?  */
747           limm_p = arc_opcode_limm_p (&limm);
748
749           /* Perform various error and warning tests.  */
750
751           {
752             static int in_delay_slot_p = 0;
753             static int prev_insn_needs_cc_nop_p = 0;
754             /* delay slot type seen */
755             int delay_slot_type = ARC_DELAY_NONE;
756             /* conditional execution flag seen */
757             int conditional = 0;
758             /* 1 if condition codes are being set */
759             int cc_set_p = 0;
760             /* 1 if conditional branch, including `b' "branch always" */
761             int cond_branch_p = opcode->flags & ARC_OPCODE_COND_BRANCH;
762
763             for (i = 0; i < num_suffixes; ++i)
764               {
765                 switch (arc_operands[insn_suffixes[i]->type].fmt)
766                   {
767                   case 'n':
768                     delay_slot_type = insn_suffixes[i]->value;
769                     break;
770                   case 'q':
771                     conditional = insn_suffixes[i]->value;
772                     break;
773                   case 'f':
774                     cc_set_p = 1;
775                     break;
776                   }
777               }
778
779             /* Putting an insn with a limm value in a delay slot is supposed to
780                be legal, but let's warn the user anyway.  Ditto for 8 byte
781                jumps with delay slots.  */
782             if (in_delay_slot_p && limm_p)
783               as_warn ("8 byte instruction in delay slot");
784             if (delay_slot_type != ARC_DELAY_NONE
785                 && limm_p && arc_insn_not_jl (insn)) /* except for jl  addr */
786               as_warn ("8 byte jump instruction with delay slot");
787             in_delay_slot_p = (delay_slot_type != ARC_DELAY_NONE) && !limm_p;
788
789             /* Warn when a conditional branch immediately follows a set of
790                the condition codes.  Note that this needn't be done if the
791                insn that sets the condition codes uses a limm.  */
792             if (cond_branch_p && conditional != 0 /* 0 = "always" */
793                 && prev_insn_needs_cc_nop_p && arc_mach_type == bfd_mach_arc_5)
794               as_warn ("conditional branch follows set of flags");
795             prev_insn_needs_cc_nop_p =
796               /* FIXME: ??? not required:
797                  (delay_slot_type != ARC_DELAY_NONE) &&  */
798               cc_set_p && !limm_p;
799           }
800
801           /* Write out the instruction.
802              It is important to fetch enough space in one call to `frag_more'.
803              We use (f - frag_now->fr_literal) to compute where we are and we
804              don't want frag_now to change between calls.  */
805           if (limm_p)
806             {
807               f = frag_more (8);
808               md_number_to_chars (f, insn, 4);
809               md_number_to_chars (f + 4, limm, 4);
810               dwarf2_emit_insn (8);
811             }
812           else if (limm_reloc_p)
813             {
814               /* We need a limm reloc, but the tables think we don't.  */
815               abort ();
816             }
817           else
818             {
819               f = frag_more (4);
820               md_number_to_chars (f, insn, 4);
821               dwarf2_emit_insn (4);
822             }
823
824           /* Create any fixups.  */
825           for (i = 0; i < fc; ++i)
826             {
827               int op_type, reloc_type;
828               expressionS exptmp;
829               const struct arc_operand *operand;
830
831               /* Create a fixup for this operand.
832                  At this point we do not use a bfd_reloc_code_real_type for
833                  operands residing in the insn, but instead just use the
834                  operand index.  This lets us easily handle fixups for any
835                  operand type, although that is admittedly not a very exciting
836                  feature.  We pick a BFD reloc type in md_apply_fix3.
837
838                  Limm values (4 byte immediate "constants") must be treated
839                  normally because they're not part of the actual insn word
840                  and thus the insertion routines don't handle them.  */
841
842               if (arc_operands[fixups[i].opindex].flags & ARC_OPERAND_LIMM)
843                 {
844                   /* Modify the fixup addend as required by the cpu.  */
845                   fixups[i].exp.X_add_number += arc_limm_fixup_adjust (insn);
846                   op_type = fixups[i].opindex;
847                   /* FIXME: can we add this data to the operand table?  */
848                   if (op_type == arc_operand_map['L']
849                       || op_type == arc_operand_map['s']
850                       || op_type == arc_operand_map['o']
851                       || op_type == arc_operand_map['O'])
852                     reloc_type = BFD_RELOC_32;
853                   else if (op_type == arc_operand_map['J'])
854                     reloc_type = BFD_RELOC_ARC_B26;
855                   else
856                     abort ();
857                   reloc_type = get_arc_exp_reloc_type (1, reloc_type,
858                                                        &fixups[i].exp,
859                                                        &exptmp);
860                 }
861               else
862                 {
863                   op_type = get_arc_exp_reloc_type (0, fixups[i].opindex,
864                                                     &fixups[i].exp, &exptmp);
865                   reloc_type = op_type + (int) BFD_RELOC_UNUSED;
866                 }
867               operand = &arc_operands[op_type];
868               fix_new_exp (frag_now,
869                            ((f - frag_now->fr_literal)
870                             + (operand->flags & ARC_OPERAND_LIMM ? 4 : 0)), 4,
871                            &exptmp,
872                            (operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0,
873                            (bfd_reloc_code_real_type) reloc_type);
874             }
875
876           /* All done.  */
877           return;
878         }
879
880       /* Try the next entry.  */
881     }
882
883   if (NULL == last_errmsg)
884     as_bad ("bad instruction `%s'", start);
885   else
886     as_bad (last_errmsg);
887 }
888 \f
889 static void
890 arc_extoper (opertype)
891      int opertype;
892 {
893   char *name;
894   char *mode;
895   char c;
896   char *p;
897   int imode = 0;
898   int number;
899   struct arc_ext_operand_value *ext_oper;
900   symbolS *symbolP;
901
902   segT old_sec;
903   int old_subsec;
904
905   name = input_line_pointer;
906   c = get_symbol_end ();
907   name = xstrdup (name);
908
909   p = name;
910   while (*p)
911     {
912       *p = TOLOWER (*p);
913       p++;
914     }
915
916   /* just after name is now '\0'  */
917   p = input_line_pointer;
918   *p = c;
919   SKIP_WHITESPACE ();
920
921   if (*input_line_pointer != ',')
922     {
923       as_bad ("expected comma after operand name");
924       ignore_rest_of_line ();
925       free (name);
926       return;
927     }
928
929   input_line_pointer++;         /* skip ','  */
930   number = get_absolute_expression ();
931
932   if (number < 0)
933     {
934       as_bad ("negative operand number %d", number);
935       ignore_rest_of_line ();
936       free (name);
937       return;
938     }
939
940   if (opertype)
941     {
942       SKIP_WHITESPACE ();
943
944       if (*input_line_pointer != ',')
945         {
946           as_bad ("expected comma after register-number");
947           ignore_rest_of_line ();
948           free (name);
949           return;
950         }
951
952       input_line_pointer++;             /* skip ','  */
953       mode = input_line_pointer;
954
955       if (!strncmp (mode, "r|w", 3))
956         {
957           imode = 0;
958           input_line_pointer += 3;
959         }
960       else
961         {
962           if (!strncmp (mode, "r", 1))
963             {
964               imode = ARC_REGISTER_READONLY;
965               input_line_pointer += 1;
966             }
967           else
968             {
969               if (strncmp (mode, "w", 1))
970                 {
971                   as_bad ("invalid mode");
972                   ignore_rest_of_line ();
973                   free (name);
974                   return;
975                 }
976               else
977                 {
978                   imode = ARC_REGISTER_WRITEONLY;
979                   input_line_pointer += 1;
980                 }
981             }
982         }
983       SKIP_WHITESPACE ();
984       if (1 == opertype)
985         {
986           if (*input_line_pointer != ',')
987             {
988               as_bad ("expected comma after register-mode");
989               ignore_rest_of_line ();
990               free (name);
991               return;
992             }
993
994           input_line_pointer++;         /* skip ','  */
995
996           if (!strncmp (input_line_pointer, "cannot_shortcut", 15))
997             {
998               imode |= arc_get_noshortcut_flag ();
999               input_line_pointer += 15;
1000             }
1001           else
1002             {
1003               if (strncmp (input_line_pointer, "can_shortcut", 12))
1004                 {
1005                   as_bad ("shortcut designator invalid");
1006                   ignore_rest_of_line ();
1007                   free (name);
1008                   return;
1009                 }
1010               else
1011                 {
1012                   input_line_pointer += 12;
1013                 }
1014             }
1015         }
1016     }
1017
1018   if ((opertype == 1) && number > 60)
1019     {
1020       as_bad ("core register value (%d) too large", number);
1021       ignore_rest_of_line ();
1022       free (name);
1023       return;
1024     }
1025
1026   if ((opertype == 0) && number > 31)
1027     {
1028       as_bad ("condition code value (%d) too large", number);
1029       ignore_rest_of_line ();
1030       free (name);
1031       return;
1032     }
1033
1034   ext_oper = (struct arc_ext_operand_value *) \
1035     xmalloc (sizeof (struct arc_ext_operand_value));
1036
1037   if (opertype)
1038     {
1039       /* If the symbol already exists, point it at the new definition.  */
1040       if ((symbolP = symbol_find (name)))
1041         {
1042           if (S_GET_SEGMENT (symbolP) == reg_section)
1043             S_SET_VALUE (symbolP, (int) &ext_oper->operand);
1044           else
1045             {
1046               as_bad ("attempt to override symbol: %s", name);
1047               ignore_rest_of_line ();
1048               free (name);
1049               free (ext_oper);
1050               return;
1051             }
1052         }
1053       else
1054         {
1055           /* If its not there, add it.  */
1056           symbol_table_insert (symbol_create (name, reg_section,
1057                                               (int) &ext_oper->operand, &zero_address_frag));
1058         }
1059     }
1060
1061   ext_oper->operand.name  = name;
1062   ext_oper->operand.value = number;
1063   ext_oper->operand.type  = arc_operand_type (opertype);
1064   ext_oper->operand.flags = imode;
1065
1066   ext_oper->next = arc_ext_operands;
1067   arc_ext_operands = ext_oper;
1068
1069   /* OK, now that we know what this operand is, put a description in
1070      the arc extension section of the output file.  */
1071
1072   old_sec    = now_seg;
1073   old_subsec = now_subseg;
1074
1075   arc_set_ext_seg ();
1076
1077   switch (opertype)
1078     {
1079     case 0:
1080       p = frag_more (1);
1081       *p = 3 + strlen (name) + 1;
1082       p = frag_more (1);
1083       *p = EXT_COND_CODE;
1084       p = frag_more (1);
1085       *p = number;
1086       p = frag_more (strlen (name) + 1);
1087       strcpy (p, name);
1088       break;
1089     case 1:
1090       p = frag_more (1);
1091       *p = 3 + strlen (name) + 1;
1092       p = frag_more (1);
1093       *p = EXT_CORE_REGISTER;
1094       p = frag_more (1);
1095       *p = number;
1096       p = frag_more (strlen (name) + 1);
1097       strcpy (p, name);
1098       break;
1099     case 2:
1100       p = frag_more (1);
1101       *p = 6 + strlen (name) + 1;
1102       p = frag_more (1);
1103       *p = EXT_AUX_REGISTER;
1104       p = frag_more (1);
1105       *p = number >> 24 & 0xff;
1106       p = frag_more (1);
1107       *p = number >> 16 & 0xff;
1108       p = frag_more (1);
1109       *p = number >>  8 & 0xff;
1110       p = frag_more (1);
1111       *p = number       & 0xff;
1112       p = frag_more (strlen (name) + 1);
1113       strcpy (p, name);
1114       break;
1115     default:
1116       as_bad ("invalid opertype");
1117       ignore_rest_of_line ();
1118       free (name);
1119       return;
1120       break;
1121     }
1122
1123   subseg_set (old_sec, old_subsec);
1124
1125   /* Enter all registers into the symbol table.  */
1126
1127   demand_empty_rest_of_line ();
1128 }
1129
1130 static void
1131 arc_extinst (ignore)
1132      int ignore ATTRIBUTE_UNUSED;
1133 {
1134   unsigned char syntax[129];
1135   char *name;
1136   char *p;
1137   char c;
1138   int suffixcode = -1;
1139   int opcode, subopcode;
1140   int i;
1141   int class = 0;
1142   int name_len;
1143   struct arc_opcode *ext_op;
1144
1145   segT old_sec;
1146   int old_subsec;
1147
1148   name = input_line_pointer;
1149   c = get_symbol_end ();
1150   name = xstrdup (name);
1151   strcpy (syntax, name);
1152   name_len = strlen (name);
1153
1154   /* just after name is now '\0'  */
1155   p = input_line_pointer;
1156   *p = c;
1157
1158   SKIP_WHITESPACE ();
1159
1160   if (*input_line_pointer != ',')
1161     {
1162       as_bad ("expected comma after operand name");
1163       ignore_rest_of_line ();
1164       return;
1165     }
1166
1167   input_line_pointer++;         /* skip ','  */
1168   opcode = get_absolute_expression ();
1169
1170   SKIP_WHITESPACE ();
1171
1172   if (*input_line_pointer != ',')
1173     {
1174       as_bad ("expected comma after opcode");
1175       ignore_rest_of_line ();
1176       return;
1177     }
1178
1179   input_line_pointer++;         /* skip ','  */
1180   subopcode = get_absolute_expression ();
1181
1182   if (subopcode < 0)
1183     {
1184       as_bad ("negative subopcode %d", subopcode);
1185       ignore_rest_of_line ();
1186       return;
1187     }
1188
1189   if (subopcode)
1190     {
1191       if (3 != opcode)
1192         {
1193           as_bad ("subcode value found when opcode not equal 0x03");
1194           ignore_rest_of_line ();
1195           return;
1196         }
1197       else
1198         {
1199           if (subopcode < 0x09 || subopcode == 0x3f)
1200             {
1201               as_bad ("invalid subopcode %d", subopcode);
1202               ignore_rest_of_line ();
1203               return;
1204             }
1205         }
1206     }
1207
1208   SKIP_WHITESPACE ();
1209
1210   if (*input_line_pointer != ',')
1211     {
1212       as_bad ("expected comma after subopcode");
1213       ignore_rest_of_line ();
1214       return;
1215     }
1216
1217   input_line_pointer++;         /* skip ','  */
1218
1219   for (i = 0; i < (int) MAXSUFFIXCLASS; i++)
1220     {
1221       if (!strncmp (suffixclass[i].name,input_line_pointer, suffixclass[i].len))
1222         {
1223           suffixcode = i;
1224           input_line_pointer += suffixclass[i].len;
1225           break;
1226         }
1227     }
1228
1229   if (-1 == suffixcode)
1230     {
1231       as_bad ("invalid suffix class");
1232       ignore_rest_of_line ();
1233       return;
1234     }
1235
1236   SKIP_WHITESPACE ();
1237
1238   if (*input_line_pointer != ',')
1239     {
1240       as_bad ("expected comma after suffix class");
1241       ignore_rest_of_line ();
1242       return;
1243     }
1244
1245   input_line_pointer++;         /* skip ','  */
1246
1247   for (i = 0; i < (int) MAXSYNTAXCLASS; i++)
1248     {
1249       if (!strncmp (syntaxclass[i].name,input_line_pointer, syntaxclass[i].len))
1250         {
1251           class = syntaxclass[i].class;
1252           input_line_pointer += syntaxclass[i].len;
1253           break;
1254         }
1255     }
1256
1257   if (0 == (SYNTAX_VALID & class))
1258     {
1259       as_bad ("invalid syntax class");
1260       ignore_rest_of_line ();
1261       return;
1262     }
1263
1264   if ((0x3 == opcode) & (class & SYNTAX_3OP))
1265     {
1266       as_bad ("opcode 0x3 and SYNTAX_3OP invalid");
1267       ignore_rest_of_line ();
1268       return;
1269     }
1270
1271   switch (suffixcode)
1272     {
1273     case 0:
1274       strcat (syntax, "%.q%.f ");
1275       break;
1276     case 1:
1277       strcat (syntax, "%.f ");
1278       break;
1279     case 2:
1280       strcat (syntax, "%.q ");
1281       break;
1282     case 3:
1283       strcat (syntax, " ");
1284       break;
1285     default:
1286       as_bad ("unknown suffix class");
1287       ignore_rest_of_line ();
1288       return;
1289       break;
1290     };
1291
1292   strcat (syntax, ((opcode == 0x3) ? "%a,%b" : ((class & SYNTAX_3OP) ? "%a,%b,%c" : "%b,%c")));
1293   if (suffixcode < 2)
1294     strcat (syntax, "%F");
1295   strcat (syntax, "%S%L");
1296
1297   ext_op = (struct arc_opcode *) xmalloc (sizeof (struct arc_opcode));
1298   ext_op->syntax = xstrdup (syntax);
1299
1300   ext_op->mask  = I (-1) | ((0x3 == opcode) ? C (-1) : 0);
1301   ext_op->value = I (opcode) | ((0x3 == opcode) ? C (subopcode) : 0);
1302   ext_op->flags = class;
1303   ext_op->next_asm = arc_ext_opcodes;
1304   ext_op->next_dis = arc_ext_opcodes;
1305   arc_ext_opcodes = ext_op;
1306
1307   /* OK, now that we know what this inst is, put a description in the
1308      arc extension section of the output file.  */
1309
1310   old_sec    = now_seg;
1311   old_subsec = now_subseg;
1312
1313   arc_set_ext_seg ();
1314
1315   p = frag_more (1);
1316   *p = 5 + name_len + 1;
1317   p = frag_more (1);
1318   *p = EXT_INSTRUCTION;
1319   p = frag_more (1);
1320   *p = opcode;
1321   p = frag_more (1);
1322   *p = subopcode;
1323   p = frag_more (1);
1324   *p = (class & (OP1_MUST_BE_IMM | OP1_IMM_IMPLIED) ? IGNORE_FIRST_OPD : 0);
1325   p = frag_more (name_len);
1326   strncpy (p, syntax, name_len);
1327   p = frag_more (1);
1328   *p = '\0';
1329
1330   subseg_set (old_sec, old_subsec);
1331
1332   demand_empty_rest_of_line ();
1333 }
1334
1335 int
1336 arc_set_ext_seg ()
1337 {
1338   if (!arcext_section)
1339     {
1340       arcext_section = subseg_new (".arcextmap", 0);
1341       bfd_set_section_flags (stdoutput, arcext_section,
1342                              SEC_READONLY | SEC_HAS_CONTENTS);
1343     }
1344   else
1345     subseg_set (arcext_section, 0);
1346   return 1;
1347 }
1348
1349 static void
1350 arc_common (localScope)
1351      int localScope;
1352 {
1353   char *name;
1354   char c;
1355   char *p;
1356   int align, size;
1357   symbolS *symbolP;
1358
1359   name = input_line_pointer;
1360   c = get_symbol_end ();
1361   /* just after name is now '\0'  */
1362   p = input_line_pointer;
1363   *p = c;
1364   SKIP_WHITESPACE ();
1365
1366   if (*input_line_pointer != ',')
1367     {
1368       as_bad ("expected comma after symbol name");
1369       ignore_rest_of_line ();
1370       return;
1371     }
1372
1373   input_line_pointer++;         /* skip ','  */
1374   size = get_absolute_expression ();
1375
1376   if (size < 0)
1377     {
1378       as_bad ("negative symbol length");
1379       ignore_rest_of_line ();
1380       return;
1381     }
1382
1383   *p = 0;
1384   symbolP = symbol_find_or_make (name);
1385   *p = c;
1386
1387   if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
1388     {
1389       as_bad ("ignoring attempt to re-define symbol");
1390       ignore_rest_of_line ();
1391       return;
1392     }
1393   if (((int) S_GET_VALUE (symbolP) != 0) \
1394       && ((int) S_GET_VALUE (symbolP) != size))
1395     {
1396       as_warn ("length of symbol \"%s\" already %ld, ignoring %d",
1397                S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
1398     }
1399   assert (symbolP->sy_frag == &zero_address_frag);
1400
1401   /* Now parse the alignment field.  This field is optional for
1402      local and global symbols. Default alignment is zero.  */
1403   if (*input_line_pointer == ',')
1404     {
1405       input_line_pointer++;
1406       align = get_absolute_expression ();
1407       if (align < 0)
1408         {
1409           align = 0;
1410           as_warn ("assuming symbol alignment of zero");
1411         }
1412     }
1413   else
1414     align = 0;
1415
1416   if (localScope != 0)
1417     {
1418       segT old_sec;
1419       int old_subsec;
1420       char *pfrag;
1421
1422       old_sec    = now_seg;
1423       old_subsec = now_subseg;
1424       record_alignment (bss_section, align);
1425       subseg_set (bss_section, 0);  /* ??? subseg_set (bss_section, 1); ???  */
1426
1427       if (align)
1428         /* Do alignment.  */
1429         frag_align (align, 0, 0);
1430
1431       /* Detach from old frag.  */
1432       if (S_GET_SEGMENT (symbolP) == bss_section)
1433         symbolP->sy_frag->fr_symbol = NULL;
1434
1435       symbolP->sy_frag = frag_now;
1436       pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
1437                         (offsetT) size, (char *) 0);
1438       *pfrag = 0;
1439
1440       S_SET_SIZE       (symbolP, size);
1441       S_SET_SEGMENT    (symbolP, bss_section);
1442       S_CLEAR_EXTERNAL (symbolP);
1443       symbolP->local = 1;
1444       subseg_set (old_sec, old_subsec);
1445     }
1446   else
1447     {
1448       S_SET_VALUE    (symbolP, (valueT) size);
1449       S_SET_ALIGN    (symbolP, align);
1450       S_SET_EXTERNAL (symbolP);
1451       S_SET_SEGMENT  (symbolP, bfd_com_section_ptr);
1452     }
1453
1454   symbolP->bsym->flags |= BSF_OBJECT;
1455
1456   demand_empty_rest_of_line ();
1457 }
1458 \f
1459 /* Select the cpu we're assembling for.  */
1460
1461 static void
1462 arc_option (ignore)
1463      int ignore ATTRIBUTE_UNUSED;
1464 {
1465   int mach;
1466   char c;
1467   char *cpu;
1468
1469   cpu = input_line_pointer;
1470   c = get_symbol_end ();
1471   mach = arc_get_mach (cpu);
1472   *input_line_pointer = c;
1473
1474   /* If an instruction has already been seen, it's too late.  */
1475   if (cpu_tables_init_p)
1476     {
1477       as_bad ("\".option\" directive must appear before any instructions");
1478       ignore_rest_of_line ();
1479       return;
1480     }
1481
1482   if (mach == -1)
1483     goto bad_cpu;
1484
1485   if (mach_type_specified_p && mach != arc_mach_type)
1486     {
1487       as_bad ("\".option\" directive conflicts with initial definition");
1488       ignore_rest_of_line ();
1489       return;
1490     }
1491   else
1492     {
1493       /* The cpu may have been selected on the command line.  */
1494       if (mach != arc_mach_type)
1495         as_warn ("\".option\" directive overrides command-line (default) value");
1496       arc_mach_type = mach;
1497       if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
1498         as_fatal ("could not set architecture and machine");
1499       mach_type_specified_p = 1;
1500     }
1501   demand_empty_rest_of_line ();
1502   return;
1503
1504  bad_cpu:
1505   as_bad ("invalid identifier for \".option\"");
1506   ignore_rest_of_line ();
1507 }
1508 \f
1509 /* Turn a string in input_line_pointer into a floating point constant
1510    of type TYPE, and store the appropriate bytes in *LITP.  The number
1511    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1512    returned, or NULL on OK.  */
1513
1514 /* Equal to MAX_PRECISION in atof-ieee.c  */
1515 #define MAX_LITTLENUMS 6
1516
1517 char *
1518 md_atof (type, litP, sizeP)
1519      int type;
1520      char *litP;
1521      int *sizeP;
1522 {
1523   int prec;
1524   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1525   LITTLENUM_TYPE *wordP;
1526   char *t;
1527
1528   switch (type)
1529     {
1530     case 'f':
1531     case 'F':
1532       prec = 2;
1533       break;
1534
1535     case 'd':
1536     case 'D':
1537       prec = 4;
1538       break;
1539
1540     default:
1541       *sizeP = 0;
1542       return "bad call to md_atof";
1543     }
1544
1545   t = atof_ieee (input_line_pointer, type, words);
1546   if (t)
1547     input_line_pointer = t;
1548   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1549   for (wordP = words; prec--;)
1550     {
1551       md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
1552       litP += sizeof (LITTLENUM_TYPE);
1553     }
1554
1555   return NULL;
1556 }
1557
1558 /* Write a value out to the object file, using the appropriate
1559    endianness.  */
1560
1561 void
1562 md_number_to_chars (buf, val, n)
1563      char *buf;
1564      valueT val;
1565      int n;
1566 {
1567   if (target_big_endian)
1568     number_to_chars_bigendian (buf, val, n);
1569   else
1570     number_to_chars_littleendian (buf, val, n);
1571 }
1572
1573 /* Round up a section size to the appropriate boundary.  */
1574
1575 valueT
1576 md_section_align (segment, size)
1577      segT segment;
1578      valueT size;
1579 {
1580   int align = bfd_get_section_alignment (stdoutput, segment);
1581
1582   return ((size + (1 << align) - 1) & (-1 << align));
1583 }
1584
1585 /* We don't have any form of relaxing.  */
1586
1587 int
1588 md_estimate_size_before_relax (fragp, seg)
1589      fragS *fragp ATTRIBUTE_UNUSED;
1590      asection *seg ATTRIBUTE_UNUSED;
1591 {
1592   as_fatal (_("md_estimate_size_before_relax\n"));
1593   return 1;
1594 }
1595
1596 /* Convert a machine dependent frag.  We never generate these.  */
1597
1598 void
1599 md_convert_frag (abfd, sec, fragp)
1600      bfd *abfd ATTRIBUTE_UNUSED;
1601      asection *sec ATTRIBUTE_UNUSED;
1602      fragS *fragp ATTRIBUTE_UNUSED;
1603 {
1604   as_fatal (_("md_convert_frag\n"));
1605 }
1606
1607 void
1608 arc_code_symbol (expressionP)
1609      expressionS *expressionP;
1610 {
1611   if (expressionP->X_op == O_symbol && expressionP->X_add_number == 0)
1612     {
1613       expressionS two;
1614       expressionP->X_op = O_right_shift;
1615       expressionP->X_add_symbol->sy_value.X_op = O_constant;
1616       two.X_op = O_constant;
1617       two.X_add_symbol = two.X_op_symbol = NULL;
1618       two.X_add_number = 2;
1619       expressionP->X_op_symbol = make_expr_symbol (&two);
1620     }
1621   /* Allow %st(sym1-sym2)  */
1622   else if (expressionP->X_op == O_subtract
1623            && expressionP->X_add_symbol != NULL
1624            && expressionP->X_op_symbol != NULL
1625            && expressionP->X_add_number == 0)
1626     {
1627       expressionS two;
1628       expressionP->X_add_symbol = make_expr_symbol (expressionP);
1629       expressionP->X_op = O_right_shift;
1630       two.X_op = O_constant;
1631       two.X_add_symbol = two.X_op_symbol = NULL;
1632       two.X_add_number = 2;
1633       expressionP->X_op_symbol = make_expr_symbol (&two);
1634     }
1635   else
1636     {
1637       as_bad ("expression too complex code symbol");
1638       return;
1639     }
1640 }
1641
1642 /* Parse an operand that is machine-specific.
1643
1644    The ARC has a special %-op to adjust addresses so they're usable in
1645    branches.  The "st" is short for the STatus register.
1646    ??? Later expand this to take a flags value too.
1647
1648    ??? We can't create new expression types so we map the %-op's onto the
1649    existing syntax.  This means that the user could use the chosen syntax
1650    to achieve the same effect.  */
1651
1652 void
1653 md_operand (expressionP)
1654      expressionS *expressionP;
1655 {
1656   char *p = input_line_pointer;
1657
1658   if (*p == '%')
1659     if (strncmp (p, "%st(", 4) == 0)
1660       {
1661         input_line_pointer += 4;
1662         expression (expressionP);
1663         if (*input_line_pointer != ')')
1664           {
1665             as_bad ("missing ')' in %%-op");
1666             return;
1667           }
1668         ++input_line_pointer;
1669         arc_code_symbol (expressionP);
1670       }
1671     else
1672       {
1673         /* It could be a register.  */
1674         int i, l;
1675         struct arc_ext_operand_value *ext_oper = arc_ext_operands;
1676         p++;
1677
1678         while (ext_oper)
1679           {
1680             l = strlen (ext_oper->operand.name);
1681             if (!strncmp (p, ext_oper->operand.name, l) && !ISALNUM (*(p + l)))
1682               {
1683                 input_line_pointer += l + 1;
1684                 expressionP->X_op = O_register;
1685                 expressionP->X_add_number = (int) &ext_oper->operand;
1686                 return;
1687               }
1688             ext_oper = ext_oper->next;
1689           }
1690         for (i = 0; i < arc_reg_names_count; i++)
1691           {
1692             l = strlen (arc_reg_names[i].name);
1693             if (!strncmp (p, arc_reg_names[i].name, l) && !ISALNUM (*(p + l)))
1694               {
1695                 input_line_pointer += l + 1;
1696                 expressionP->X_op = O_register;
1697                 expressionP->X_add_number = (int) &arc_reg_names[i];
1698                 break;
1699               }
1700           }
1701       }
1702 }
1703
1704 /* We have no need to default values of symbols.
1705    We could catch register names here, but that is handled by inserting
1706    them all in the symbol table to begin with.  */
1707
1708 symbolS *
1709 md_undefined_symbol (name)
1710      char *name ATTRIBUTE_UNUSED;
1711 {
1712   return 0;
1713 }
1714 \f
1715 /* Functions concerning expressions.  */
1716
1717 /* Parse a .byte, .word, etc. expression.
1718
1719    Values for the status register are specified with %st(label).
1720    `label' will be right shifted by 2.  */
1721
1722 void
1723 arc_parse_cons_expression (exp, nbytes)
1724      expressionS *exp;
1725      unsigned int nbytes ATTRIBUTE_UNUSED;
1726 {
1727   char *p = input_line_pointer;
1728   int code_symbol_fix = 0;
1729
1730   for (; ! is_end_of_line[(unsigned char) *p]; p++)
1731     if (*p == '@' && !strncmp (p, "@h30", 4))
1732       {
1733         code_symbol_fix = 1;
1734         strcpy (p, ";   ");
1735       }
1736   expr (0, exp);
1737   if (code_symbol_fix)
1738     {
1739       arc_code_symbol (exp);
1740       input_line_pointer = p;
1741     }
1742 }
1743
1744 /* Record a fixup for a cons expression.  */
1745
1746 void
1747 arc_cons_fix_new (frag, where, nbytes, exp)
1748      fragS *frag;
1749      int where;
1750      int nbytes;
1751      expressionS *exp;
1752 {
1753   if (nbytes == 4)
1754     {
1755       int reloc_type;
1756       expressionS exptmp;
1757
1758       /* This may be a special ARC reloc (eg: %st()).  */
1759       reloc_type = get_arc_exp_reloc_type (1, BFD_RELOC_32, exp, &exptmp);
1760       fix_new_exp (frag, where, nbytes, &exptmp, 0, reloc_type);
1761     }
1762   else
1763     {
1764       fix_new_exp (frag, where, nbytes, exp, 0,
1765                    nbytes == 2 ? BFD_RELOC_16
1766                    : nbytes == 8 ? BFD_RELOC_64
1767                    : BFD_RELOC_32);
1768     }
1769 }
1770 \f
1771 /* Functions concerning relocs.  */
1772
1773 /* The location from which a PC relative jump should be calculated,
1774    given a PC relative reloc.  */
1775
1776 long
1777 md_pcrel_from (fixP)
1778      fixS *fixP;
1779 {
1780   /* Return the address of the delay slot.  */
1781   return fixP->fx_frag->fr_address + fixP->fx_where + fixP->fx_size;
1782 }
1783
1784 /* Compute the reloc type of an expression.
1785    The possibly modified expression is stored in EXPNEW.
1786
1787    This is used to convert the expressions generated by the %-op's into
1788    the appropriate operand type.  It is called for both data in instructions
1789    (operands) and data outside instructions (variables, debugging info, etc.).
1790
1791    Currently supported %-ops:
1792
1793    %st(symbol): represented as "symbol >> 2"
1794                 "st" is short for STatus as in the status register (pc)
1795
1796    DEFAULT_TYPE is the type to use if no special processing is required.
1797
1798    DATA_P is non-zero for data or limm values, zero for insn operands.
1799    Remember that the opcode "insertion fns" cannot be used on data, they're
1800    only for inserting operands into insns.  They also can't be used for limm
1801    values as the insertion routines don't handle limm values.  When called for
1802    insns we return fudged reloc types (real_value - BFD_RELOC_UNUSED).  When
1803    called for data or limm values we use real reloc types.  */
1804
1805 static int
1806 get_arc_exp_reloc_type (data_p, default_type, exp, expnew)
1807      int data_p;
1808      int default_type;
1809      expressionS *exp;
1810      expressionS *expnew;
1811 {
1812   /* If the expression is "symbol >> 2" we must change it to just "symbol",
1813      as fix_new_exp can't handle it.  Similarly for (symbol - symbol) >> 2.
1814      That's ok though.  What's really going on here is that we're using
1815      ">> 2" as a special syntax for specifying BFD_RELOC_ARC_B26.  */
1816
1817   if (exp->X_op == O_right_shift
1818       && exp->X_op_symbol != NULL
1819       && exp->X_op_symbol->sy_value.X_op == O_constant
1820       && exp->X_op_symbol->sy_value.X_add_number == 2
1821       && exp->X_add_number == 0)
1822     {
1823       if (exp->X_add_symbol != NULL
1824           && (exp->X_add_symbol->sy_value.X_op == O_constant
1825               || exp->X_add_symbol->sy_value.X_op == O_symbol))
1826         {
1827           *expnew = *exp;
1828           expnew->X_op = O_symbol;
1829           expnew->X_op_symbol = NULL;
1830           return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
1831         }
1832       else if (exp->X_add_symbol != NULL
1833                && exp->X_add_symbol->sy_value.X_op == O_subtract)
1834         {
1835           *expnew = exp->X_add_symbol->sy_value;
1836           return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
1837         }
1838     }
1839
1840   *expnew = *exp;
1841   return default_type;
1842 }
1843
1844 /* Apply a fixup to the object code.  This is called for all the
1845    fixups we generated by the call to fix_new_exp, above.  In the call
1846    above we used a reloc code which was the largest legal reloc code
1847    plus the operand index.  Here we undo that to recover the operand
1848    index.  At this point all symbol values should be fully resolved,
1849    and we attempt to completely resolve the reloc.  If we can not do
1850    that, we determine the correct reloc code and put it back in the fixup.  */
1851
1852 void
1853 md_apply_fix3 (fixP, valP, seg)
1854      fixS *fixP;
1855      valueT * valP;
1856      segT seg;
1857 {
1858 #if 0
1859   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1860 #endif
1861   valueT value = * valP;
1862
1863   if (fixP->fx_addsy == (symbolS *) NULL)
1864     fixP->fx_done = 1;
1865
1866   else if (fixP->fx_pcrel)
1867     {
1868       /* Hack around bfd_install_relocation brain damage.  */
1869       if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
1870         value += md_pcrel_from (fixP);
1871     }
1872
1873   /* We can't actually support subtracting a symbol.  */
1874   if (fixP->fx_subsy != NULL)
1875     as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1876
1877   if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
1878     {
1879       int opindex;
1880       const struct arc_operand *operand;
1881       char *where;
1882       arc_insn insn;
1883
1884       opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
1885
1886       operand = &arc_operands[opindex];
1887
1888       /* Fetch the instruction, insert the fully resolved operand
1889          value, and stuff the instruction back again.  */
1890       where = fixP->fx_frag->fr_literal + fixP->fx_where;
1891       if (target_big_endian)
1892         insn = bfd_getb32 ((unsigned char *) where);
1893       else
1894         insn = bfd_getl32 ((unsigned char *) where);
1895       insn = arc_insert_operand (insn, operand, -1, NULL, (offsetT) value,
1896                                  fixP->fx_file, fixP->fx_line);
1897       if (target_big_endian)
1898         bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1899       else
1900         bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
1901
1902       if (fixP->fx_done)
1903         {
1904           /* Nothing else to do here.  */
1905           return;
1906         }
1907
1908       /* Determine a BFD reloc value based on the operand information.
1909          We are only prepared to turn a few of the operands into relocs.
1910          !!! Note that we can't handle limm values here.  Since we're using
1911          implicit addends the addend must be inserted into the instruction,
1912          however, the opcode insertion routines currently do nothing with
1913          limm values.  */
1914       if (operand->fmt == 'B')
1915         {
1916           assert ((operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0
1917                   && operand->bits == 20
1918                   && operand->shift == 7);
1919           fixP->fx_r_type = BFD_RELOC_ARC_B22_PCREL;
1920         }
1921       else if (operand->fmt == 'J')
1922         {
1923           assert ((operand->flags & ARC_OPERAND_ABSOLUTE_BRANCH) != 0
1924                   && operand->bits == 24
1925                   && operand->shift == 32);
1926           fixP->fx_r_type = BFD_RELOC_ARC_B26;
1927         }
1928       else if (operand->fmt == 'L')
1929         {
1930           assert ((operand->flags & ARC_OPERAND_LIMM) != 0
1931                   && operand->bits == 32
1932                   && operand->shift == 32);
1933           fixP->fx_r_type = BFD_RELOC_32;
1934         }
1935       else
1936         {
1937           as_bad_where (fixP->fx_file, fixP->fx_line,
1938                         "unresolved expression that must be resolved");
1939           fixP->fx_done = 1;
1940           return;
1941         }
1942     }
1943   else
1944     {
1945       switch (fixP->fx_r_type)
1946         {
1947         case BFD_RELOC_8:
1948           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1949                               value, 1);
1950           break;
1951         case BFD_RELOC_16:
1952           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1953                               value, 2);
1954           break;
1955         case BFD_RELOC_32:
1956           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1957                               value, 4);
1958           break;
1959 #if 0
1960         case BFD_RELOC_64:
1961           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1962                               value, 8);
1963           break;
1964 #endif
1965         case BFD_RELOC_ARC_B26:
1966           /* If !fixP->fx_done then `value' is an implicit addend.
1967              We must shift it right by 2 in this case as well because the
1968              linker performs the relocation and then adds this in (as opposed
1969              to adding this in and then shifting right by 2).  */
1970           value >>= 2;
1971           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1972                               value, 4);
1973           break;
1974         default:
1975           abort ();
1976         }
1977     }
1978 }
1979
1980 /* Translate internal representation of relocation info to BFD target
1981    format.  */
1982
1983 arelent *
1984 tc_gen_reloc (section, fixP)
1985      asection *section ATTRIBUTE_UNUSED;
1986      fixS *fixP;
1987 {
1988   arelent *reloc;
1989
1990   reloc = (arelent *) xmalloc (sizeof (arelent));
1991
1992   reloc->sym_ptr_ptr = &fixP->fx_addsy->bsym;
1993   reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
1994   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1995   if (reloc->howto == (reloc_howto_type *) NULL)
1996     {
1997       as_bad_where (fixP->fx_file, fixP->fx_line,
1998                     "internal error: can't export reloc type %d (`%s')",
1999                     fixP->fx_r_type,
2000                     bfd_get_reloc_code_name (fixP->fx_r_type));
2001       return NULL;
2002     }
2003
2004   assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
2005
2006   /* Set addend to account for PC being advanced one insn before the
2007      target address is computed.  */
2008
2009   reloc->addend = (fixP->fx_pcrel ? -4 : 0);
2010
2011   return reloc;
2012 }