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