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