Home | History | Annotate | Line # | Download | only in config
tc-d10v.c revision 1.10
      1 /* tc-d10v.c -- Assembler code for the Mitsubishi D10V
      2    Copyright (C) 1996-2025 Free Software Foundation, Inc.
      3 
      4    This file is part of GAS, the GNU Assembler.
      5 
      6    GAS is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3, or (at your option)
      9    any later version.
     10 
     11    GAS is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with GAS; see the file COPYING.  If not, write to
     18    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
     19    Boston, MA 02110-1301, USA.  */
     20 
     21 #include "as.h"
     22 #include "safe-ctype.h"
     23 #include "subsegs.h"
     24 #include "opcode/d10v.h"
     25 #include "elf/ppc.h"
     26 #include "dwarf2dbg.h"
     27 
     28 const char comment_chars[]        = ";";
     29 const char line_comment_chars[]   = "#";
     30 const char line_separator_chars[] = "";
     31 const char md_shortopts[]         = "O";
     32 const char EXP_CHARS[]            = "eE";
     33 const char FLT_CHARS[]            = "dD";
     34 
     35 int Optimizing = 0;
     36 
     37 #define AT_WORD_P(X) ((X)->X_op == O_right_shift \
     38 		      && (X)->X_op_symbol != NULL \
     39 		      && symbol_constant_p ((X)->X_op_symbol) \
     40 		      && S_GET_VALUE ((X)->X_op_symbol) == AT_WORD_RIGHT_SHIFT)
     41 #define AT_WORD_RIGHT_SHIFT 2
     42 
     43 /* Fixups.  */
     44 #define MAX_INSN_FIXUPS  5
     45 
     46 struct d10v_fixup
     47 {
     48   expressionS exp;
     49   int operand;
     50   int pcrel;
     51   int size;
     52   bfd_reloc_code_real_type reloc;
     53 };
     54 
     55 typedef struct _fixups
     56 {
     57   int fc;
     58   struct d10v_fixup fix[MAX_INSN_FIXUPS];
     59   struct _fixups *next;
     60 } Fixups;
     61 
     62 static Fixups FixUps[2];
     63 static Fixups *fixups;
     64 
     65 static int do_not_ignore_hash = 0;
     66 
     67 typedef int packing_type;
     68 #define PACK_UNSPEC 	(0)	/* Packing order not specified.  */
     69 #define PACK_PARALLEL	(1)	/* "||"  */
     70 #define PACK_LEFT_RIGHT (2)	/* "->"  */
     71 #define PACK_RIGHT_LEFT (3)	/* "<-"  */
     72 static packing_type etype = PACK_UNSPEC; /* Used by d10v_cleanup.  */
     73 
     74 /* TRUE if instruction swapping warnings should be inhibited.
     75    --nowarnswap.  */
     76 static bool flag_warn_suppress_instructionswap;
     77 
     78 /* TRUE if instruction packing should be performed when --gstabs is specified.
     79    --gstabs-packing, --no-gstabs-packing.  */
     80 static bool flag_allow_gstabs_packing = 1;
     81 
     82 /* Local functions.  */
     83 
     84 enum options
     85 {
     86   OPTION_NOWARNSWAP = OPTION_MD_BASE,
     87   OPTION_GSTABSPACKING,
     88   OPTION_NOGSTABSPACKING
     89 };
     90 
     91 const struct option md_longopts[] =
     92 {
     93   {"nowarnswap", no_argument, NULL, OPTION_NOWARNSWAP},
     94   {"gstabspacking",  no_argument, NULL, OPTION_GSTABSPACKING},
     95   {"gstabs-packing", no_argument, NULL, OPTION_GSTABSPACKING},
     96   {"nogstabspacking",   no_argument, NULL, OPTION_NOGSTABSPACKING},
     97   {"no-gstabs-packing", no_argument, NULL, OPTION_NOGSTABSPACKING},
     98   {NULL, no_argument, NULL, 0}
     99 };
    100 
    101 const size_t md_longopts_size = sizeof (md_longopts);
    102 
    103 /* Opcode hash table.  */
    104 static htab_t d10v_hash;
    105 
    106 /* Do a binary search of the d10v_predefined_registers array to see if
    107    NAME is a valid register name.  Return the register number from the
    108    array on success, or -1 on failure.  */
    109 
    110 static int
    111 reg_name_search (char *name)
    112 {
    113   int middle, low, high;
    114   int cmp;
    115 
    116   low = 0;
    117   high = d10v_reg_name_cnt () - 1;
    118 
    119   do
    120     {
    121       middle = (low + high) / 2;
    122       cmp = strcasecmp (name, d10v_predefined_registers[middle].name);
    123       if (cmp < 0)
    124 	high = middle - 1;
    125       else if (cmp > 0)
    126 	low = middle + 1;
    127       else
    128 	return d10v_predefined_registers[middle].value;
    129     }
    130   while (low <= high);
    131   return -1;
    132 }
    133 
    134 /* Check the string at input_line_pointer
    135    to see if it is a valid register name.  */
    136 
    137 static int
    138 register_name (expressionS *expressionP)
    139 {
    140   int reg_number;
    141   char c, *p = input_line_pointer;
    142 
    143   while (!is_end_of_stmt (*p) && *p != ',' && !is_whitespace (*p) && *p != ')')
    144     p++;
    145 
    146   c = *p;
    147   if (c)
    148     *p++ = 0;
    149 
    150   /* Look to see if it's in the register table.  */
    151   reg_number = reg_name_search (input_line_pointer);
    152   if (reg_number >= 0)
    153     {
    154       expressionP->X_op = O_register;
    155       /* Temporarily store a pointer to the string here.  */
    156       expressionP->X_op_symbol = (symbolS *) input_line_pointer;
    157       expressionP->X_add_number = reg_number;
    158       input_line_pointer = p;
    159       return 1;
    160     }
    161   if (c)
    162     *(p - 1) = c;
    163   return 0;
    164 }
    165 
    166 static int
    167 check_range (unsigned long num, int bits, int flags)
    168 {
    169   long min, max;
    170   int retval = 0;
    171 
    172   /* Don't bother checking 16-bit values.  */
    173   if (bits == 16)
    174     return 0;
    175 
    176   if (flags & OPERAND_SHIFT)
    177     {
    178       /* All special shift operands are unsigned and <= 16.
    179 	 We allow 0 for now.  */
    180       if (num > 16)
    181 	return 1;
    182       else
    183 	return 0;
    184     }
    185 
    186   if (flags & OPERAND_SIGNED)
    187     {
    188       /* Signed 3-bit integers are restricted to the (-2, 3) range.  */
    189       if (flags & RESTRICTED_NUM3)
    190 	{
    191 	  if ((long) num < -2 || (long) num > 3)
    192 	    retval = 1;
    193 	}
    194       else
    195 	{
    196 	  max = (1 << (bits - 1)) - 1;
    197 	  min = - (1 << (bits - 1));
    198 	  if (((long) num > max) || ((long) num < min))
    199 	    retval = 1;
    200 	}
    201     }
    202   else
    203     {
    204       max = (1 << bits) - 1;
    205       min = 0;
    206       if (((long) num > max) || ((long) num < min))
    207 	retval = 1;
    208     }
    209   return retval;
    210 }
    211 
    212 void
    213 md_show_usage (FILE *stream)
    214 {
    215   fprintf (stream, _("D10V options:\n\
    216 -O                      Optimize.  Will do some operations in parallel.\n\
    217 --gstabs-packing        Pack adjacent short instructions together even\n\
    218                         when --gstabs is specified.  On by default.\n\
    219 --no-gstabs-packing     If --gstabs is specified, do not pack adjacent\n\
    220                         instructions together.\n"));
    221 }
    222 
    223 int
    224 md_parse_option (int c, const char *arg ATTRIBUTE_UNUSED)
    225 {
    226   switch (c)
    227     {
    228     case 'O':
    229       /* Optimize. Will attempt to parallelize operations.  */
    230       Optimizing = 1;
    231       break;
    232     case OPTION_NOWARNSWAP:
    233       flag_warn_suppress_instructionswap = 1;
    234       break;
    235     case OPTION_GSTABSPACKING:
    236       flag_allow_gstabs_packing = 1;
    237       break;
    238     case OPTION_NOGSTABSPACKING:
    239       flag_allow_gstabs_packing = 0;
    240       break;
    241     default:
    242       return 0;
    243     }
    244   return 1;
    245 }
    246 
    247 symbolS *
    248 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
    249 {
    250   return 0;
    251 }
    252 
    253 const char *
    254 md_atof (int type, char *litP, int *sizeP)
    255 {
    256   return ieee_md_atof (type, litP, sizeP, true);
    257 }
    258 
    259 void
    260 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
    261 		 asection *sec ATTRIBUTE_UNUSED,
    262 		 fragS *fragP ATTRIBUTE_UNUSED)
    263 {
    264   abort ();
    265 }
    266 
    267 valueT
    268 md_section_align (asection *seg, valueT addr)
    269 {
    270   int align = bfd_section_alignment (seg);
    271   return ((addr + (1 << align) - 1) & -(1 << align));
    272 }
    273 
    274 void
    275 md_begin (void)
    276 {
    277   const char *prev_name = "";
    278   const struct d10v_opcode *opcode;
    279   d10v_hash = str_htab_create ();
    280 
    281   /* Insert unique names into hash table.  The D10v instruction set
    282      has many identical opcode names that have different opcodes based
    283      on the operands.  This hash table then provides a quick index to
    284      the first opcode with a particular name in the opcode table.  */
    285 
    286   for (opcode = d10v_opcodes; opcode->name; opcode++)
    287     {
    288       if (strcmp (prev_name, opcode->name))
    289 	{
    290 	  prev_name = opcode->name;
    291 	  str_hash_insert (d10v_hash, opcode->name, opcode, 0);
    292 	}
    293     }
    294 
    295   fixups = &FixUps[0];
    296   FixUps[0].next = &FixUps[1];
    297   FixUps[1].next = &FixUps[0];
    298 }
    299 
    300 /* Remove the postincrement or postdecrement operator ( '+' or '-' )
    301    from an expression.  */
    302 
    303 static int
    304 postfix (char *p)
    305 {
    306   while (*p != '-' && *p != '+')
    307     {
    308       if (*p == 0 || *p == '\n' || *p == '\r')
    309 	break;
    310       p++;
    311     }
    312 
    313   if (*p == '-')
    314     {
    315       *p = ' ';
    316       return -1;
    317     }
    318   if (*p == '+')
    319     {
    320       *p = ' ';
    321       return 1;
    322     }
    323 
    324   return 0;
    325 }
    326 
    327 static bfd_reloc_code_real_type
    328 get_reloc (const struct d10v_operand *op)
    329 {
    330   int bits = op->bits;
    331 
    332   if (bits <= 4)
    333     return 0;
    334 
    335   if (op->flags & OPERAND_ADDR)
    336     {
    337       if (bits == 8)
    338 	return BFD_RELOC_D10V_10_PCREL_R;
    339       else
    340 	return BFD_RELOC_D10V_18_PCREL;
    341     }
    342 
    343   return BFD_RELOC_16;
    344 }
    345 
    346 /* Parse a string of operands.  Return an array of expressions.  */
    347 
    348 static int
    349 get_operands (expressionS exp[])
    350 {
    351   char *p = input_line_pointer;
    352   int numops = 0;
    353   int post = 0;
    354   int uses_at = 0;
    355 
    356   while (*p)
    357     {
    358       while (is_whitespace (*p) || *p == ',')
    359 	p++;
    360       if (is_end_of_stmt (*p))
    361 	break;
    362 
    363       if (*p == '@')
    364 	{
    365 	  uses_at = 1;
    366 
    367 	  p++;
    368 	  exp[numops].X_op = O_absent;
    369 	  if (*p == '(')
    370 	    {
    371 	      p++;
    372 	      exp[numops].X_add_number = OPERAND_ATPAR;
    373 	    }
    374 	  else if (*p == '-')
    375 	    {
    376 	      p++;
    377 	      exp[numops].X_add_number = OPERAND_ATMINUS;
    378 	    }
    379 	  else
    380 	    {
    381 	      exp[numops].X_add_number = OPERAND_ATSIGN;
    382 	      if (*p == '+')
    383 		{
    384 		  numops++;
    385 		  exp[numops].X_op = O_absent;
    386 		  exp[numops].X_add_number = OPERAND_PLUS;
    387 		  p++;
    388 		}
    389 	      post = postfix (p);
    390 	    }
    391 	  numops++;
    392 	  continue;
    393 	}
    394 
    395       if (*p == ')')
    396 	{
    397 	  /* Just skip the trailing paren.  */
    398 	  p++;
    399 	  continue;
    400 	}
    401 
    402       input_line_pointer = p;
    403 
    404       /* Check to see if it might be a register name.  */
    405       if (!register_name (&exp[numops]))
    406 	{
    407 	  /* Parse as an expression.  */
    408 	  if (uses_at)
    409 	    {
    410 	      /* Any expression that involves the indirect addressing
    411 		 cannot also involve immediate addressing.  Therefore
    412 		 the use of the hash character is illegal.  */
    413 	      int save = do_not_ignore_hash;
    414 	      do_not_ignore_hash = 1;
    415 
    416 	      expression (&exp[numops]);
    417 
    418 	      do_not_ignore_hash = save;
    419 	    }
    420 	  else
    421 	    expression (&exp[numops]);
    422 	}
    423 
    424       if (strncasecmp (input_line_pointer, "@word", 5) == 0)
    425 	{
    426 	  input_line_pointer += 5;
    427 	  if (exp[numops].X_op == O_register)
    428 	    {
    429 	      /* If it looked like a register name but was followed by
    430                  "@word" then it was really a symbol, so change it to
    431                  one.  */
    432 	      exp[numops].X_op = O_symbol;
    433 	      exp[numops].X_add_symbol =
    434 		symbol_find_or_make ((char *) exp[numops].X_op_symbol);
    435 	    }
    436 
    437 	  /* Check for identifier@word+constant.  */
    438 	  if (*input_line_pointer == '-' || *input_line_pointer == '+')
    439 	    {
    440 	      expressionS new_exp;
    441 	      expression (&new_exp);
    442 	      exp[numops].X_add_number = new_exp.X_add_number;
    443 	    }
    444 
    445 	  /* Convert expr into a right shift by AT_WORD_RIGHT_SHIFT.  */
    446 	  {
    447 	    expressionS new_exp;
    448 	    memset (&new_exp, 0, sizeof new_exp);
    449 	    new_exp.X_add_number = AT_WORD_RIGHT_SHIFT;
    450 	    new_exp.X_op = O_constant;
    451 	    new_exp.X_unsigned = 1;
    452 	    exp[numops].X_op_symbol = make_expr_symbol (&new_exp);
    453 	    exp[numops].X_op = O_right_shift;
    454 	  }
    455 
    456 	  know (AT_WORD_P (&exp[numops]));
    457 	}
    458 
    459       if (exp[numops].X_op == O_illegal)
    460 	as_bad (_("illegal operand"));
    461       else if (exp[numops].X_op == O_absent)
    462 	as_bad (_("missing operand"));
    463 
    464       numops++;
    465       p = input_line_pointer;
    466     }
    467 
    468   switch (post)
    469     {
    470     case -1:	/* Postdecrement mode.  */
    471       exp[numops].X_op = O_absent;
    472       exp[numops++].X_add_number = OPERAND_MINUS;
    473       break;
    474     case 1:	/* Postincrement mode.  */
    475       exp[numops].X_op = O_absent;
    476       exp[numops++].X_add_number = OPERAND_PLUS;
    477       break;
    478     }
    479 
    480   exp[numops].X_op = 0;
    481   return numops;
    482 }
    483 
    484 static unsigned long
    485 d10v_insert_operand (unsigned long insn,
    486 		     int op_type,
    487 		     offsetT value,
    488 		     int left,
    489 		     fixS *fix)
    490 {
    491   int shift, bits;
    492 
    493   shift = d10v_operands[op_type].shift;
    494   if (left)
    495     shift += 15;
    496 
    497   bits = d10v_operands[op_type].bits;
    498 
    499   /* Truncate to the proper number of bits.  */
    500   if (check_range (value, bits, d10v_operands[op_type].flags))
    501     as_bad_where (fix->fx_file, fix->fx_line,
    502 		  _("operand out of range: %ld"), (long) value);
    503 
    504   value &= 0x7FFFFFFF >> (31 - bits);
    505   insn |= (value << shift);
    506 
    507   return insn;
    508 }
    509 
    510 /* Take a pointer to the opcode entry in the opcode table and the
    511    array of operand expressions.  Return the instruction.  */
    512 
    513 static unsigned long
    514 build_insn (struct d10v_opcode *opcode,
    515 	    expressionS *opers,
    516 	    unsigned long insn)
    517 {
    518   int i, bits, shift, flags, format;
    519   unsigned long number;
    520 
    521   /* The insn argument is only used for the DIVS kludge.  */
    522   if (insn)
    523     format = LONG_R;
    524   else
    525     {
    526       insn = opcode->opcode;
    527       format = opcode->format;
    528     }
    529 
    530   for (i = 0; opcode->operands[i]; i++)
    531     {
    532       flags = d10v_operands[opcode->operands[i]].flags;
    533       bits = d10v_operands[opcode->operands[i]].bits;
    534       shift = d10v_operands[opcode->operands[i]].shift;
    535       number = opers[i].X_add_number;
    536 
    537       if (flags & OPERAND_REG)
    538 	{
    539 	  number &= REGISTER_MASK;
    540 	  if (format == LONG_L)
    541 	    shift += 15;
    542 	}
    543 
    544       if (opers[i].X_op != O_register && opers[i].X_op != O_constant)
    545 	{
    546 	  /* Now create a fixup.  */
    547 
    548 	  if (fixups->fc >= MAX_INSN_FIXUPS)
    549 	    as_fatal (_("too many fixups"));
    550 
    551 	  if (AT_WORD_P (&opers[i]))
    552 	    {
    553 	      /* Recognize XXX>>1+N aka XXX@word+N as special (AT_WORD).  */
    554 	      fixups->fix[fixups->fc].reloc = BFD_RELOC_D10V_18;
    555 	      opers[i].X_op = O_symbol;
    556 	      opers[i].X_op_symbol = NULL; /* Should free it.  */
    557 	      /* number is left shifted by AT_WORD_RIGHT_SHIFT so
    558                  that, it is aligned with the symbol's value.  Later,
    559                  BFD_RELOC_D10V_18 will right shift (symbol_value +
    560                  X_add_number).  */
    561 	      number <<= AT_WORD_RIGHT_SHIFT;
    562 	      opers[i].X_add_number = number;
    563 	    }
    564 	  else
    565 	    {
    566 	      fixups->fix[fixups->fc].reloc =
    567 		get_reloc (&d10v_operands[opcode->operands[i]]);
    568 
    569 	      /* Check that an immediate was passed to ops that expect one.  */
    570 	      if ((flags & OPERAND_NUM)
    571 		  && (fixups->fix[fixups->fc].reloc == 0))
    572 		as_bad (_("operand is not an immediate"));
    573 	    }
    574 
    575 	  if (fixups->fix[fixups->fc].reloc == BFD_RELOC_16 ||
    576 	      fixups->fix[fixups->fc].reloc == BFD_RELOC_D10V_18)
    577 	    fixups->fix[fixups->fc].size = 2;
    578 	  else
    579 	    fixups->fix[fixups->fc].size = 4;
    580 
    581 	  fixups->fix[fixups->fc].exp = opers[i];
    582 	  fixups->fix[fixups->fc].operand = opcode->operands[i];
    583 	  fixups->fix[fixups->fc].pcrel = (flags & OPERAND_ADDR) != 0;
    584 	  (fixups->fc)++;
    585 	}
    586 
    587       /* Truncate to the proper number of bits.  */
    588       if ((opers[i].X_op == O_constant) && check_range (number, bits, flags))
    589 	as_bad (_("operand out of range: %lu"), number);
    590       number &= 0x7FFFFFFF >> (31 - bits);
    591       insn = insn | (number << shift);
    592     }
    593 
    594   /* kludge: for DIVS, we need to put the operands in twice on the second
    595      pass, format is changed to LONG_R to force the second set of operands
    596      to not be shifted over 15.  */
    597   if ((opcode->opcode == OPCODE_DIVS) && (format == LONG_L))
    598     insn = build_insn (opcode, opers, insn);
    599 
    600   return insn;
    601 }
    602 
    603 /* Write out a long form instruction.  */
    604 
    605 static void
    606 write_long (unsigned long insn, Fixups *fx)
    607 {
    608   int i, where;
    609   char *f = frag_more (4);
    610 
    611   dwarf2_emit_insn (4);
    612   insn |= FM11;
    613   number_to_chars_bigendian (f, insn, 4);
    614 
    615   for (i = 0; i < fx->fc; i++)
    616     {
    617       if (fx->fix[i].reloc)
    618 	{
    619 	  where = f - frag_now->fr_literal;
    620 	  if (fx->fix[i].size == 2)
    621 	    where += 2;
    622 
    623 	  if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
    624 	    fx->fix[i].operand |= 4096;
    625 
    626 	  fix_new_exp (frag_now,
    627 		       where,
    628 		       fx->fix[i].size,
    629 		       &(fx->fix[i].exp),
    630 		       fx->fix[i].pcrel,
    631 		       fx->fix[i].operand|2048);
    632 	}
    633     }
    634   fx->fc = 0;
    635 }
    636 
    637 /* Write out a short form instruction by itself.  */
    638 
    639 static void
    640 write_1_short (struct d10v_opcode *opcode,
    641 	       unsigned long insn,
    642 	       Fixups *fx)
    643 {
    644   char *f = frag_more (4);
    645   int i, where;
    646 
    647   dwarf2_emit_insn (4);
    648   if (opcode->exec_type & PARONLY)
    649     as_fatal (_("Instruction must be executed in parallel with another instruction."));
    650 
    651   /* The other container needs to be NOP.
    652      According to 4.3.1: for FM=00, sub-instructions performed only by IU
    653      cannot be encoded in L-container.  */
    654   if (opcode->unit == IU)
    655     insn |= FM00 | (NOP << 15);		/* Right container.  */
    656   else
    657     insn = FM00 | (insn << 15) | NOP;	/* Left container.  */
    658 
    659   number_to_chars_bigendian (f, insn, 4);
    660   for (i = 0; i < fx->fc; i++)
    661     {
    662       if (fx->fix[i].reloc)
    663 	{
    664 	  where = f - frag_now->fr_literal;
    665 	  if (fx->fix[i].size == 2)
    666 	    where += 2;
    667 
    668 	  if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
    669 	    fx->fix[i].operand |= 4096;
    670 
    671 	  /* If it's an R reloc, we may have to switch it to L.  */
    672 	  if ((fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R)
    673 	      && (opcode->unit != IU))
    674 	    fx->fix[i].operand |= 1024;
    675 
    676 	  fix_new_exp (frag_now,
    677 		       where,
    678 		       fx->fix[i].size,
    679 		       &(fx->fix[i].exp),
    680 		       fx->fix[i].pcrel,
    681 		       fx->fix[i].operand|2048);
    682 	}
    683     }
    684   fx->fc = 0;
    685 }
    686 
    687 /* Determine if there are any resource conflicts among two manually
    688    parallelized instructions.  Some of this was lifted from parallel_ok.  */
    689 
    690 static void
    691 check_resource_conflict (struct d10v_opcode *op1,
    692 			 unsigned long insn1,
    693 			 struct d10v_opcode *op2,
    694 			 unsigned long insn2)
    695 {
    696   int i, j, flags, mask, shift, regno;
    697   unsigned long ins, mod[2];
    698   struct d10v_opcode *op;
    699 
    700   if ((op1->exec_type & SEQ)
    701       || ! ((op1->exec_type & PAR) || (op1->exec_type & PARONLY)))
    702     {
    703       as_warn (_("packing conflict: %s must dispatch sequentially"),
    704 	      op1->name);
    705       return;
    706     }
    707 
    708   if ((op2->exec_type & SEQ)
    709       || ! ((op2->exec_type & PAR) || (op2->exec_type & PARONLY)))
    710     {
    711       as_warn (_("packing conflict: %s must dispatch sequentially"),
    712 	      op2->name);
    713       return;
    714     }
    715 
    716    /* See if both instructions write to the same resource.
    717 
    718       The idea here is to create two sets of bitmasks (mod and used) which
    719       indicate which registers are modified or used by each instruction.
    720       The operation can only be done in parallel if neither instruction
    721       modifies the same register. Accesses to control registers and memory
    722       are treated as accesses to a single register. So if both instructions
    723       write memory or if the first instruction writes memory and the second
    724       reads, then they cannot be done in parallel. We treat reads to the PSW
    725       (which includes C, F0, and F1) in isolation. So simultaneously writing
    726       C and F0 in two different sub-instructions is permitted.  */
    727 
    728   /* The bitmasks (mod and used) look like this (bit 31 = MSB).
    729      r0-r15	  0-15
    730      a0-a1	  16-17
    731      cr (not psw) 18
    732      psw(other)   19
    733      mem	  20
    734      psw(C flag)  21
    735      psw(F0 flag) 22  */
    736 
    737   for (j = 0; j < 2; j++)
    738     {
    739       if (j == 0)
    740 	{
    741 	  op = op1;
    742 	  ins = insn1;
    743 	}
    744       else
    745 	{
    746 	  op = op2;
    747 	  ins = insn2;
    748 	}
    749       mod[j] = 0;
    750       if (op->exec_type & BRANCH_LINK)
    751 	mod[j] |= 1 << 13;
    752 
    753       for (i = 0; op->operands[i]; i++)
    754 	{
    755 	  flags = d10v_operands[op->operands[i]].flags;
    756 	  shift = d10v_operands[op->operands[i]].shift;
    757 	  mask = 0x7FFFFFFF >> (31 - d10v_operands[op->operands[i]].bits);
    758 	  if (flags & OPERAND_REG)
    759 	    {
    760 	      regno = (ins >> shift) & mask;
    761 	      if (flags & (OPERAND_ACC0 | OPERAND_ACC1))
    762 		regno += 16;
    763 	      else if (flags & OPERAND_CONTROL)	/* mvtc or mvfc */
    764 		{
    765 		  if (regno == 0)
    766 		    regno = 19;
    767 		  else
    768 		    regno = 18;
    769 		}
    770 	      else if (flags & OPERAND_FFLAG)
    771 		regno = 22;
    772 	      else if (flags & OPERAND_CFLAG)
    773 		regno = 21;
    774 
    775 	      if (flags & OPERAND_DEST
    776 		  /* Auto inc/dec also modifies the register.  */
    777 		  || (op->operands[i + 1] != 0
    778 		      && (d10v_operands[op->operands[i + 1]].flags
    779 			  & (OPERAND_PLUS | OPERAND_MINUS)) != 0))
    780 		{
    781 		  mod[j] |= 1 << regno;
    782 		  if (flags & OPERAND_EVEN)
    783 		    mod[j] |= 1 << (regno + 1);
    784 		}
    785 	    }
    786 	  else if (flags & OPERAND_ATMINUS)
    787 	    {
    788 	      /* SP implicitly used/modified.  */
    789 	      mod[j] |= 1 << 15;
    790 	    }
    791 	}
    792 
    793       if (op->exec_type & WMEM)
    794 	mod[j] |= 1 << 20;
    795       else if (op->exec_type & WF0)
    796 	mod[j] |= 1 << 22;
    797       else if (op->exec_type & WCAR)
    798 	mod[j] |= 1 << 21;
    799     }
    800 
    801   if ((mod[0] & mod[1]) == 0)
    802     return;
    803   else
    804     {
    805       unsigned long x;
    806       x = mod[0] & mod[1];
    807 
    808       for (j = 0; j <= 15; j++)
    809 	if (x & (1 << j))
    810 	  as_warn (_("resource conflict (R%d)"), j);
    811       for (j = 16; j <= 17; j++)
    812 	if (x & (1 << j))
    813 	  as_warn (_("resource conflict (A%d)"), j - 16);
    814       if (x & (1 << 19))
    815 	as_warn (_("resource conflict (PSW)"));
    816       if (x & (1 << 21))
    817 	as_warn (_("resource conflict (C flag)"));
    818       if (x & (1 << 22))
    819 	as_warn (_("resource conflict (F flag)"));
    820     }
    821 }
    822 
    823 /* Check 2 instructions and determine if they can be safely
    824    executed in parallel.  Return 1 if they can be.  */
    825 
    826 static int
    827 parallel_ok (struct d10v_opcode *op1,
    828 	     unsigned long insn1,
    829 	     struct d10v_opcode *op2,
    830 	     unsigned long insn2,
    831 	     packing_type exec_type)
    832 {
    833   int i, j, flags, mask, shift, regno;
    834   unsigned long ins, mod[2], used[2];
    835   struct d10v_opcode *op;
    836 
    837   if ((op1->exec_type & SEQ) != 0 || (op2->exec_type & SEQ) != 0
    838       || (op1->exec_type & PAR) == 0 || (op2->exec_type & PAR) == 0
    839       || (op1->unit == BOTH) || (op2->unit == BOTH)
    840       || (op1->unit == IU && op2->unit == IU)
    841       || (op1->unit == MU && op2->unit == MU))
    842     return 0;
    843 
    844   /* If this is auto parallelization, and the first instruction is a
    845      branch or should not be packed, then don't parallelize.  */
    846   if (exec_type == PACK_UNSPEC
    847       && (op1->exec_type & (ALONE | BRANCH)))
    848     return 0;
    849 
    850   /* The idea here is to create two sets of bitmasks (mod and used)
    851      which indicate which registers are modified or used by each
    852      instruction.  The operation can only be done in parallel if
    853      instruction 1 and instruction 2 modify different registers, and
    854      the first instruction does not modify registers that the second
    855      is using (The second instruction can modify registers that the
    856      first is using as they are only written back after the first
    857      instruction has completed).  Accesses to control registers, PSW,
    858      and memory are treated as accesses to a single register.  So if
    859      both instructions write memory or if the first instruction writes
    860      memory and the second reads, then they cannot be done in
    861      parallel.  Likewise, if the first instruction mucks with the psw
    862      and the second reads the PSW (which includes C, F0, and F1), then
    863      they cannot operate safely in parallel.  */
    864 
    865   /* The bitmasks (mod and used) look like this (bit 31 = MSB).
    866      r0-r15	  0-15
    867      a0-a1	  16-17
    868      cr (not psw) 18
    869      psw	  19
    870      mem	  20  */
    871 
    872   for (j = 0; j < 2; j++)
    873     {
    874       if (j == 0)
    875 	{
    876 	  op = op1;
    877 	  ins = insn1;
    878 	}
    879       else
    880 	{
    881 	  op = op2;
    882 	  ins = insn2;
    883 	}
    884       mod[j] = used[j] = 0;
    885       if (op->exec_type & BRANCH_LINK)
    886 	mod[j] |= 1 << 13;
    887 
    888       for (i = 0; op->operands[i]; i++)
    889 	{
    890 	  flags = d10v_operands[op->operands[i]].flags;
    891 	  shift = d10v_operands[op->operands[i]].shift;
    892 	  mask = 0x7FFFFFFF >> (31 - d10v_operands[op->operands[i]].bits);
    893 	  if (flags & OPERAND_REG)
    894 	    {
    895 	      regno = (ins >> shift) & mask;
    896 	      if (flags & (OPERAND_ACC0 | OPERAND_ACC1))
    897 		regno += 16;
    898 	      else if (flags & OPERAND_CONTROL)	/* mvtc or mvfc.  */
    899 		{
    900 		  if (regno == 0)
    901 		    regno = 19;
    902 		  else
    903 		    regno = 18;
    904 		}
    905 	      else if (flags & (OPERAND_FFLAG | OPERAND_CFLAG))
    906 		regno = 19;
    907 
    908 	      if (flags & OPERAND_DEST)
    909 		{
    910 		  mod[j] |= 1 << regno;
    911 		  if (flags & OPERAND_EVEN)
    912 		    mod[j] |= 1 << (regno + 1);
    913 		}
    914 	      else
    915 		{
    916 		  used[j] |= 1 << regno;
    917 		  if (flags & OPERAND_EVEN)
    918 		    used[j] |= 1 << (regno + 1);
    919 
    920 		  /* Auto inc/dec also modifies the register.  */
    921 		  if (op->operands[i + 1] != 0
    922 		      && (d10v_operands[op->operands[i + 1]].flags
    923 			  & (OPERAND_PLUS | OPERAND_MINUS)) != 0)
    924 		    mod[j] |= 1 << regno;
    925 		}
    926 	    }
    927 	  else if (flags & OPERAND_ATMINUS)
    928 	    {
    929 	      /* SP implicitly used/modified.  */
    930 	      mod[j] |= 1 << 15;
    931 	      used[j] |= 1 << 15;
    932 	    }
    933 	}
    934       if (op->exec_type & RMEM)
    935 	used[j] |= 1 << 20;
    936       else if (op->exec_type & WMEM)
    937 	mod[j] |= 1 << 20;
    938       else if (op->exec_type & RF0)
    939 	used[j] |= 1 << 19;
    940       else if (op->exec_type & WF0)
    941 	mod[j] |= 1 << 19;
    942       else if (op->exec_type & WCAR)
    943 	mod[j] |= 1 << 19;
    944     }
    945   if ((mod[0] & mod[1]) == 0 && (mod[0] & used[1]) == 0)
    946     return 1;
    947   return 0;
    948 }
    949 
    950 /* Expects two short instructions.
    951    If possible, writes out both as a single packed instruction.
    952    Otherwise, writes out the first one, packed with a NOP.
    953    Returns number of instructions not written out.  */
    954 
    955 static int
    956 write_2_short (struct d10v_opcode *opcode1,
    957 	       unsigned long insn1,
    958 	       struct d10v_opcode *opcode2,
    959 	       unsigned long insn2,
    960 	       packing_type exec_type,
    961 	       Fixups *fx)
    962 {
    963   unsigned long insn;
    964   char *f;
    965   int i, j, where;
    966 
    967   if ((exec_type != PACK_PARALLEL)
    968       && ((opcode1->exec_type & PARONLY) || (opcode2->exec_type & PARONLY)))
    969     as_fatal (_("Instruction must be executed in parallel"));
    970 
    971   if ((opcode1->format & LONG_OPCODE) || (opcode2->format & LONG_OPCODE))
    972     as_fatal (_("Long instructions may not be combined."));
    973 
    974   switch (exec_type)
    975     {
    976     case PACK_UNSPEC:	/* Order not specified.  */
    977       if (opcode1->exec_type & ALONE)
    978 	{
    979 	  /* Case of a short branch on a separate GAS line.  Pack with NOP.  */
    980 	  write_1_short (opcode1, insn1, fx->next);
    981 	  return 1;
    982 	}
    983       if (Optimizing
    984 	  && parallel_ok (opcode1, insn1, opcode2, insn2, exec_type))
    985 	{
    986 	  /* Parallel.  */
    987 	  if (opcode1->unit == IU)
    988 	    insn = FM00 | (insn2 << 15) | insn1;
    989 	  else if (opcode2->unit == MU)
    990 	    insn = FM00 | (insn2 << 15) | insn1;
    991 	  else
    992 	    insn = FM00 | (insn1 << 15) | insn2;
    993 	}
    994       else if (opcode1->unit == IU)
    995 	/* Reverse sequential with IU opcode1 on right and done first.  */
    996 	insn = FM10 | (insn2 << 15) | insn1;
    997       else
    998 	/* Sequential with non-IU opcode1 on left and done first.  */
    999 	insn = FM01 | (insn1 << 15) | insn2;
   1000       break;
   1001 
   1002     case PACK_PARALLEL:
   1003       if (opcode1->exec_type & SEQ || opcode2->exec_type & SEQ)
   1004 	as_fatal
   1005 	  (_("One of these instructions may not be executed in parallel."));
   1006       if (opcode1->unit == IU)
   1007 	{
   1008 	  if (opcode2->unit == IU)
   1009 	    as_fatal (_("Two IU instructions may not be executed in parallel"));
   1010 	  if (!flag_warn_suppress_instructionswap)
   1011 	    as_warn (_("Swapping instruction order"));
   1012 	  insn = FM00 | (insn2 << 15) | insn1;
   1013 	}
   1014       else if (opcode2->unit == MU)
   1015 	{
   1016 	  if (opcode1->unit == MU)
   1017 	    as_fatal (_("Two MU instructions may not be executed in parallel"));
   1018 	  if (!flag_warn_suppress_instructionswap)
   1019 	    as_warn (_("Swapping instruction order"));
   1020 	  insn = FM00 | (insn2 << 15) | insn1;
   1021 	}
   1022       else
   1023 	insn = FM00 | (insn1 << 15) | insn2;
   1024       check_resource_conflict (opcode1, insn1, opcode2, insn2);
   1025       break;
   1026 
   1027     case PACK_LEFT_RIGHT:
   1028       if (opcode1->unit != IU)
   1029 	insn = FM01 | (insn1 << 15) | insn2;
   1030       else if (opcode2->unit == MU || opcode2->unit == EITHER)
   1031 	{
   1032 	  if (!flag_warn_suppress_instructionswap)
   1033 	    as_warn (_("Swapping instruction order"));
   1034 	  insn = FM10 | (insn2 << 15) | insn1;
   1035 	}
   1036       else
   1037 	as_fatal (_("IU instruction may not be in the left container"));
   1038       if (opcode1->exec_type & ALONE)
   1039 	as_warn (_("Instruction in R container is squashed by flow control instruction in L container."));
   1040       break;
   1041 
   1042     case PACK_RIGHT_LEFT:
   1043       if (opcode2->unit != MU)
   1044 	insn = FM10 | (insn1 << 15) | insn2;
   1045       else if (opcode1->unit == IU || opcode1->unit == EITHER)
   1046 	{
   1047 	  if (!flag_warn_suppress_instructionswap)
   1048 	    as_warn (_("Swapping instruction order"));
   1049 	  insn = FM01 | (insn2 << 15) | insn1;
   1050 	}
   1051       else
   1052 	as_fatal (_("MU instruction may not be in the right container"));
   1053       if (opcode2->exec_type & ALONE)
   1054 	as_warn (_("Instruction in R container is squashed by flow control instruction in L container."));
   1055       break;
   1056 
   1057     default:
   1058       as_fatal (_("unknown execution type passed to write_2_short()"));
   1059     }
   1060 
   1061   f = frag_more (4);
   1062   dwarf2_emit_insn (4);
   1063   number_to_chars_bigendian (f, insn, 4);
   1064 
   1065   /* Process fixup chains.  fx refers to insn2 when j == 0, and to
   1066      insn1 when j == 1.  Yes, it's reversed.  */
   1067 
   1068   for (j = 0; j < 2; j++)
   1069     {
   1070       for (i = 0; i < fx->fc; i++)
   1071 	{
   1072 	  if (fx->fix[i].reloc)
   1073 	    {
   1074 	      where = f - frag_now->fr_literal;
   1075 	      if (fx->fix[i].size == 2)
   1076 		where += 2;
   1077 
   1078 	      if (fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R
   1079 		  /* A BFD_RELOC_D10V_10_PCREL_R relocation applied to
   1080 		     the instruction in the L container has to be
   1081 		     adjusted to BDF_RELOC_D10V_10_PCREL_L.  When
   1082 		     j==0, we're processing insn2's operands, so we
   1083 		     want to mark the operand if insn2 is *not* in the
   1084 		     R container.  When j==1, we're processing insn1's
   1085 		     operands, so we want to mark the operand if insn2
   1086 		     *is* in the R container.  Note that, if two
   1087 		     instructions are identical, we're never going to
   1088 		     swap them, so the test is safe.  */
   1089 		  && j == ((insn & 0x7fff) == insn2))
   1090 		fx->fix[i].operand |= 1024;
   1091 
   1092 	      if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
   1093 		fx->fix[i].operand |= 4096;
   1094 
   1095 	      fix_new_exp (frag_now,
   1096 			   where,
   1097 			   fx->fix[i].size,
   1098 			   &(fx->fix[i].exp),
   1099 			   fx->fix[i].pcrel,
   1100 			   fx->fix[i].operand|2048);
   1101 	    }
   1102 	}
   1103       fx->fc = 0;
   1104       fx = fx->next;
   1105     }
   1106   return 0;
   1107 }
   1108 
   1109 /* This is the main entry point for the machine-dependent assembler.
   1110    str points to a machine-dependent instruction.  This function is
   1111    supposed to emit the frags/bytes it assembles to.  For the D10V, it
   1112    mostly handles the special VLIW parsing and packing and leaves the
   1113    difficult stuff to do_assemble().  */
   1114 
   1115 static unsigned long prev_insn;
   1116 static struct d10v_opcode *prev_opcode = 0;
   1117 static subsegT prev_subseg;
   1118 static segT prev_seg = 0;
   1119 
   1120 /* Find the symbol which has the same name as the register in exp.  */
   1121 
   1122 static symbolS *
   1123 find_symbol_matching_register (expressionS *exp)
   1124 {
   1125   int i;
   1126 
   1127   if (exp->X_op != O_register)
   1128     return NULL;
   1129 
   1130   /* Find the name of the register.  */
   1131   for (i = d10v_reg_name_cnt (); i--;)
   1132     if (d10v_predefined_registers[i].value == exp->X_add_number)
   1133       break;
   1134 
   1135   if (i < 0)
   1136     abort ();
   1137 
   1138   /* Now see if a symbol has been defined with the same name.  */
   1139   return symbol_find (d10v_predefined_registers[i].name);
   1140 }
   1141 
   1142 /* Get a pointer to an entry in the opcode table.
   1143    The function must look at all opcodes with the same name and use
   1144    the operands to choose the correct opcode.  */
   1145 
   1146 static struct d10v_opcode *
   1147 find_opcode (struct d10v_opcode *opcode, expressionS myops[])
   1148 {
   1149   int i, match;
   1150   struct d10v_opcode *next_opcode;
   1151 
   1152   /* Get all the operands and save them as expressions.  */
   1153   get_operands (myops);
   1154 
   1155   /* Now see if the operand is a fake.  If so, find the correct size
   1156      instruction, if possible.  */
   1157   if (opcode->format == OPCODE_FAKE)
   1158     {
   1159       int opnum = opcode->operands[0];
   1160       int flags;
   1161 
   1162       if (myops[opnum].X_op == O_register)
   1163 	{
   1164 	  myops[opnum].X_op = O_symbol;
   1165 	  myops[opnum].X_add_symbol =
   1166 	    symbol_find_or_make ((char *) myops[opnum].X_op_symbol);
   1167 	  myops[opnum].X_add_number = 0;
   1168 	  myops[opnum].X_op_symbol = NULL;
   1169 	}
   1170 
   1171       next_opcode = opcode + 1;
   1172 
   1173       /* If the first operand is supposed to be a register, make sure
   1174 	 we got a valid one.  */
   1175       flags = d10v_operands[next_opcode->operands[0]].flags;
   1176       if (flags & OPERAND_REG)
   1177 	{
   1178 	  int X_op = myops[0].X_op;
   1179 	  int num = myops[0].X_add_number;
   1180 
   1181 	  if (X_op != O_register
   1182 	      || (num & ~flags
   1183 		  & (OPERAND_GPR | OPERAND_ACC0 | OPERAND_ACC1
   1184 		     | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL))
   1185 	      || ((flags & OPERAND_SP) && ! (num & OPERAND_SP)))
   1186 	    {
   1187 	      as_bad (_("bad opcode or operands"));
   1188 	      return 0;
   1189 	    }
   1190 	}
   1191 
   1192       if (myops[opnum].X_op == O_constant
   1193 	  || (myops[opnum].X_op == O_symbol
   1194 	      && S_IS_DEFINED (myops[opnum].X_add_symbol)
   1195 	      && (S_GET_SEGMENT (myops[opnum].X_add_symbol) == now_seg)))
   1196 	{
   1197 	  for (i = 0; opcode->operands[i + 1]; i++)
   1198 	    {
   1199 	      int bits = d10v_operands[next_opcode->operands[opnum]].bits;
   1200 
   1201 	      flags = d10v_operands[next_opcode->operands[opnum]].flags;
   1202 
   1203 	      if (flags & OPERAND_ADDR)
   1204 		bits += 2;
   1205 
   1206 	      if (myops[opnum].X_op == O_constant)
   1207 		{
   1208 		  if (!check_range (myops[opnum].X_add_number, bits, flags))
   1209 		    break;
   1210 		}
   1211 	      else
   1212 		{
   1213 		  fragS *sym_frag;
   1214 		  fragS *f;
   1215 		  unsigned long current_position;
   1216 		  unsigned long symbol_position;
   1217 		  unsigned long value;
   1218 		  bool found_symbol;
   1219 
   1220 		  /* Calculate the address of the current instruction
   1221 		     and the address of the symbol.  Do this by summing
   1222 		     the offsets of previous frags until we reach the
   1223 		     frag containing the symbol, and the current frag.  */
   1224 		  sym_frag = symbol_get_frag (myops[opnum].X_add_symbol);
   1225 		  found_symbol = false;
   1226 
   1227 		  current_position = frag_now_fix_octets ();
   1228 		  symbol_position = S_GET_VALUE (myops[opnum].X_add_symbol);
   1229 
   1230 		  for (f = frchain_now->frch_root; f; f = f->fr_next)
   1231 		    {
   1232 		      current_position += f->fr_fix + f->fr_offset;
   1233 
   1234 		      if (f == sym_frag)
   1235 			found_symbol = true;
   1236 
   1237 		      if (! found_symbol)
   1238 			symbol_position += f->fr_fix + f->fr_offset;
   1239 		    }
   1240 
   1241 		  value = symbol_position;
   1242 
   1243 		  if (flags & OPERAND_ADDR)
   1244 		    value -= current_position;
   1245 
   1246 		  if (AT_WORD_P (&myops[opnum]))
   1247 		    {
   1248 		      if (bits > 4)
   1249 			{
   1250 			  bits += 2;
   1251 			  if (!check_range (value, bits, flags))
   1252 			    break;
   1253 			}
   1254 		    }
   1255 		  else if (!check_range (value, bits, flags))
   1256 		    break;
   1257 		}
   1258 	      next_opcode++;
   1259 	    }
   1260 
   1261 	  if (opcode->operands [i + 1] == 0)
   1262 	    as_fatal (_("value out of range"));
   1263 	  else
   1264 	    opcode = next_opcode;
   1265 	}
   1266       else
   1267 	/* Not a constant, so use a long instruction.  */
   1268 	opcode += 2;
   1269     }
   1270 
   1271   match = 0;
   1272 
   1273   /* Now search the opcode table table for one with operands
   1274      that matches what we've got.  */
   1275   while (!match)
   1276     {
   1277       match = 1;
   1278       for (i = 0; opcode->operands[i]; i++)
   1279 	{
   1280 	  int flags = d10v_operands[opcode->operands[i]].flags;
   1281 	  int X_op = myops[i].X_op;
   1282 	  int num = myops[i].X_add_number;
   1283 
   1284 	  if (X_op == 0)
   1285 	    {
   1286 	      match = 0;
   1287 	      break;
   1288 	    }
   1289 
   1290 	  if (flags & OPERAND_REG)
   1291 	    {
   1292 	      if ((X_op != O_register)
   1293 		  || (num & ~flags
   1294 		      & (OPERAND_GPR | OPERAND_ACC0 | OPERAND_ACC1
   1295 			 | OPERAND_FFLAG | OPERAND_CFLAG
   1296 			 | OPERAND_CONTROL))
   1297 		  || ((flags & OPERAND_SP) && ! (num & OPERAND_SP)))
   1298 		{
   1299 		  match = 0;
   1300 		  break;
   1301 		}
   1302 	    }
   1303 
   1304 	  if (((flags & OPERAND_MINUS)   && ((X_op != O_absent) || (num != OPERAND_MINUS))) ||
   1305 	      ((flags & OPERAND_PLUS)    && ((X_op != O_absent) || (num != OPERAND_PLUS))) ||
   1306 	      ((flags & OPERAND_ATMINUS) && ((X_op != O_absent) || (num != OPERAND_ATMINUS))) ||
   1307 	      ((flags & OPERAND_ATPAR)   && ((X_op != O_absent) || (num != OPERAND_ATPAR))) ||
   1308 	      ((flags & OPERAND_ATSIGN)  && ((X_op != O_absent) || ((num != OPERAND_ATSIGN) && (num != OPERAND_ATPAR)))))
   1309 	    {
   1310 	      match = 0;
   1311 	      break;
   1312 	    }
   1313 
   1314 	  /* Unfortunately, for the indirect operand in instructions such
   1315 	     as ``ldb r1, @(c,r14)'' this function can be passed
   1316 	     X_op == O_register (because 'c' is a valid register name).
   1317 	     However we cannot just ignore the case when X_op == O_register
   1318 	     but flags & OPERAND_REG is null, so we check to see if a symbol
   1319 	     of the same name as the register exists.  If the symbol does
   1320 	     exist, then the parser was unable to distinguish the two cases
   1321 	     and we fix things here. (Ref: PR14826)  */
   1322 
   1323 	  if (!(flags & OPERAND_REG) && (X_op == O_register))
   1324 	    {
   1325 	      symbolS * sym;
   1326 
   1327 	      sym = find_symbol_matching_register (& myops[i]);
   1328 
   1329 	      if (sym != NULL)
   1330 		{
   1331 		  myops[i].X_op = X_op = O_symbol;
   1332 		  myops[i].X_add_symbol = sym;
   1333 		}
   1334 	      else
   1335 		as_bad
   1336 		  (_("illegal operand - register name found where none expected"));
   1337 	    }
   1338 	}
   1339 
   1340       /* We're only done if the operands matched so far AND there
   1341 	     are no more to check.  */
   1342       if (match && myops[i].X_op == 0)
   1343 	break;
   1344       else
   1345 	match = 0;
   1346 
   1347       next_opcode = opcode + 1;
   1348 
   1349       if (next_opcode->opcode == 0)
   1350 	break;
   1351 
   1352       if (strcmp (next_opcode->name, opcode->name))
   1353 	break;
   1354 
   1355       opcode = next_opcode;
   1356     }
   1357 
   1358   if (!match)
   1359     {
   1360       as_bad (_("bad opcode or operands"));
   1361       return 0;
   1362     }
   1363 
   1364   /* Check that all registers that are required to be even are.
   1365      Also, if any operands were marked as registers, but were really symbols,
   1366      fix that here.  */
   1367   for (i = 0; opcode->operands[i]; i++)
   1368     {
   1369       if ((d10v_operands[opcode->operands[i]].flags & OPERAND_EVEN) &&
   1370 	  (myops[i].X_add_number & 1))
   1371 	as_fatal (_("Register number must be EVEN"));
   1372       if ((d10v_operands[opcode->operands[i]].flags & OPERAND_NOSP)
   1373 	  && (myops[i].X_add_number & OPERAND_SP))
   1374 	as_bad (_("Unsupported use of sp"));
   1375       if (myops[i].X_op == O_register)
   1376 	{
   1377 	  if (!(d10v_operands[opcode->operands[i]].flags & OPERAND_REG))
   1378 	    {
   1379 	      myops[i].X_op = O_symbol;
   1380 	      myops[i].X_add_symbol =
   1381 		symbol_find_or_make ((char *) myops[i].X_op_symbol);
   1382 	      myops[i].X_add_number = 0;
   1383 	      myops[i].X_op_symbol = NULL;
   1384 	    }
   1385 	}
   1386       if ((d10v_operands[opcode->operands[i]].flags & OPERAND_CONTROL)
   1387 	  && (myops[i].X_add_number == OPERAND_CONTROL + 4
   1388 	      || myops[i].X_add_number == OPERAND_CONTROL + 5
   1389 	      || myops[i].X_add_number == OPERAND_CONTROL + 6
   1390 	      || myops[i].X_add_number == OPERAND_CONTROL + 12
   1391 	      || myops[i].X_add_number == OPERAND_CONTROL + 13
   1392 	      || myops[i].X_add_number == OPERAND_CONTROL + 15))
   1393 	as_warn (_("cr%d is a reserved control register"),
   1394 		 (int) myops[i].X_add_number - OPERAND_CONTROL);
   1395     }
   1396   return opcode;
   1397 }
   1398 
   1399 /* Assemble a single instruction.
   1400    Return an opcode, or -1 (an invalid opcode) on error.  */
   1401 
   1402 static unsigned long
   1403 do_assemble (char *str, struct d10v_opcode **opcode)
   1404 {
   1405   unsigned char *op_start, *op_end;
   1406   char *save;
   1407   char name[20];
   1408   int nlen = 0;
   1409   expressionS myops[6];
   1410 
   1411   /* Drop leading whitespace.  */
   1412   while (is_whitespace (*str))
   1413     str++;
   1414 
   1415   /* Find the opcode end.  */
   1416   for (op_start = op_end = (unsigned char *) str;
   1417        !is_end_of_stmt (*op_end) && !is_whitespace (*op_end);
   1418        op_end++)
   1419     {
   1420       name[nlen] = TOLOWER (op_start[nlen]);
   1421       nlen++;
   1422       if (nlen == sizeof (name) - 1)
   1423 	break;
   1424     }
   1425   name[nlen] = 0;
   1426 
   1427   if (nlen == 0)
   1428     return -1;
   1429 
   1430   /* Find the first opcode with the proper name.  */
   1431   *opcode = str_hash_find (d10v_hash, name);
   1432   if (*opcode == NULL)
   1433     return -1;
   1434 
   1435   save = input_line_pointer;
   1436   input_line_pointer = (char *) op_end;
   1437   *opcode = find_opcode (*opcode, myops);
   1438   if (*opcode == 0)
   1439     return -1;
   1440   input_line_pointer = save;
   1441 
   1442   return build_insn ((*opcode), myops, 0);
   1443 }
   1444 
   1445 /* If while processing a fixup, a reloc really needs to be created.
   1446    Then it is done here.  */
   1447 
   1448 arelent *
   1449 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
   1450 {
   1451   arelent *reloc;
   1452   reloc = notes_alloc (sizeof (arelent));
   1453   reloc->sym_ptr_ptr = notes_alloc (sizeof (asymbol *));
   1454   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
   1455   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
   1456   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
   1457   if (reloc->howto == NULL)
   1458     {
   1459       as_bad_where (fixp->fx_file, fixp->fx_line,
   1460 		    _("reloc %d not supported by object file format"),
   1461 		    (int) fixp->fx_r_type);
   1462       return NULL;
   1463     }
   1464 
   1465   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
   1466     reloc->address = fixp->fx_offset;
   1467 
   1468   reloc->addend = 0;
   1469 
   1470   return reloc;
   1471 }
   1472 
   1473 int
   1474 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
   1475 			       asection *seg ATTRIBUTE_UNUSED)
   1476 {
   1477   abort ();
   1478   return 0;
   1479 }
   1480 
   1481 long
   1482 md_pcrel_from_section (fixS *fixp, segT sec)
   1483 {
   1484   if (fixp->fx_addsy != NULL
   1485       && (!S_IS_DEFINED (fixp->fx_addsy)
   1486 	  || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
   1487     return 0;
   1488   return fixp->fx_frag->fr_address + fixp->fx_where;
   1489 }
   1490 
   1491 void
   1492 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
   1493 {
   1494   char *where;
   1495   unsigned long insn;
   1496   long value = *valP;
   1497   int op_type;
   1498   int left = 0;
   1499 
   1500   if (fixP->fx_addsy == NULL)
   1501     fixP->fx_done = 1;
   1502 
   1503   /* We don't actually support subtracting a symbol.  */
   1504   if (fixP->fx_subsy != NULL)
   1505     as_bad_subtract (fixP);
   1506 
   1507   op_type = fixP->fx_r_type;
   1508   if (op_type & 2048)
   1509     {
   1510       op_type -= 2048;
   1511       if (op_type & 1024)
   1512 	{
   1513 	  op_type -= 1024;
   1514 	  fixP->fx_r_type = BFD_RELOC_D10V_10_PCREL_L;
   1515 	  left = 1;
   1516 	}
   1517       else if (op_type & 4096)
   1518 	{
   1519 	  op_type -= 4096;
   1520 	  fixP->fx_r_type = BFD_RELOC_D10V_18;
   1521 	}
   1522       else
   1523 	fixP->fx_r_type =
   1524 	  get_reloc (&d10v_operands[op_type]);
   1525     }
   1526 
   1527   /* Fetch the instruction, insert the fully resolved operand
   1528      value, and stuff the instruction back again.  */
   1529   where = fixP->fx_frag->fr_literal + fixP->fx_where;
   1530   insn = bfd_getb32 (where);
   1531 
   1532   switch (fixP->fx_r_type)
   1533     {
   1534     case BFD_RELOC_D10V_10_PCREL_L:
   1535     case BFD_RELOC_D10V_10_PCREL_R:
   1536     case BFD_RELOC_D10V_18_PCREL:
   1537       /* If the fix is relative to a global symbol, not a section
   1538 	 symbol, then ignore the offset.
   1539          XXX - Do we have to worry about branches to a symbol + offset ?  */
   1540       if (fixP->fx_addsy != NULL
   1541 	  && S_IS_EXTERNAL (fixP->fx_addsy) )
   1542         {
   1543           segT fseg = S_GET_SEGMENT (fixP->fx_addsy);
   1544           segment_info_type *segf = seg_info(fseg);
   1545 
   1546 	  if ( segf && segf->sym != fixP->fx_addsy)
   1547 	    value = 0;
   1548         }
   1549       /* Fall through.  */
   1550     case BFD_RELOC_D10V_18:
   1551       /* Instruction addresses are always right-shifted by 2.  */
   1552       value >>= AT_WORD_RIGHT_SHIFT;
   1553       if (fixP->fx_size == 2)
   1554 	bfd_putb16 (value, where);
   1555       else
   1556 	{
   1557 	  struct d10v_opcode *rep, *repi;
   1558 
   1559 	  rep = str_hash_find (d10v_hash, "rep");
   1560 	  repi = str_hash_find (d10v_hash, "repi");
   1561 	  if ((insn & FM11) == FM11
   1562 	      && ((repi != NULL
   1563 		   && (insn & repi->mask) == (unsigned) repi->opcode)
   1564 		  || (rep != NULL
   1565 		      && (insn & rep->mask) == (unsigned) rep->opcode))
   1566 	      && value < 4)
   1567 	    as_fatal
   1568 	      (_("line %d: rep or repi must include at least 4 instructions"),
   1569 	       fixP->fx_line);
   1570 	  insn =
   1571 	    d10v_insert_operand (insn, op_type, (offsetT) value, left, fixP);
   1572 	  bfd_putb32 (insn, where);
   1573 	}
   1574       break;
   1575     case BFD_RELOC_32:
   1576       bfd_putb32 (value, where);
   1577       break;
   1578     case BFD_RELOC_16:
   1579       bfd_putb16 (value, where);
   1580       break;
   1581     case BFD_RELOC_8:
   1582       *where = value;
   1583       break;
   1584 
   1585     case BFD_RELOC_VTABLE_INHERIT:
   1586     case BFD_RELOC_VTABLE_ENTRY:
   1587       fixP->fx_done = 0;
   1588       return;
   1589 
   1590     default:
   1591       as_fatal (_("line %d: unknown relocation type: 0x%x"),
   1592 		fixP->fx_line, fixP->fx_r_type);
   1593     }
   1594 }
   1595 
   1596 /* d10v_cleanup() is called after the assembler has finished parsing
   1597    the input file, when a label is read from the input file, or when a
   1598    stab directive is output.  Because the D10V assembler sometimes
   1599    saves short instructions to see if it can package them with the
   1600    next instruction, there may be a short instruction that still needs
   1601    to be written.
   1602 
   1603    NOTE: accesses a global, etype.
   1604    NOTE: invoked by various macros such as md_cleanup: see.  */
   1605 
   1606 int
   1607 d10v_cleanup (void)
   1608 {
   1609   segT seg;
   1610   subsegT subseg;
   1611 
   1612   /* If cleanup was invoked because the assembler encountered, e.g., a
   1613      user label, we write out the pending instruction, if any.  If it
   1614      was invoked because the assembler is outputting a piece of line
   1615      debugging information, though, we write out the pending
   1616      instruction only if the --no-gstabs-packing command line switch
   1617      has been specified.  */
   1618   if (prev_opcode
   1619       && etype == PACK_UNSPEC
   1620       && (! outputting_stabs_line_debug || ! flag_allow_gstabs_packing))
   1621     {
   1622       seg = now_seg;
   1623       subseg = now_subseg;
   1624 
   1625       if (prev_seg)
   1626 	subseg_set (prev_seg, prev_subseg);
   1627 
   1628       write_1_short (prev_opcode, prev_insn, fixups->next);
   1629       subseg_set (seg, subseg);
   1630       prev_opcode = NULL;
   1631     }
   1632   return 1;
   1633 }
   1634 
   1635 void
   1636 d10v_frob_label (symbolS *lab)
   1637 {
   1638   d10v_cleanup ();
   1639   symbol_set_frag (lab, frag_now);
   1640   S_SET_VALUE (lab, (valueT) frag_now_fix ());
   1641   dwarf2_emit_label (lab);
   1642 }
   1643 
   1644 /* Like normal .word, except support @word.
   1645    Clobbers input_line_pointer, checks end-of-line.  */
   1646 
   1647 static void
   1648 d10v_dot_word (int dummy ATTRIBUTE_UNUSED)
   1649 {
   1650   expressionS exp;
   1651   char *p;
   1652 
   1653   if (is_it_end_of_statement ())
   1654     {
   1655       demand_empty_rest_of_line ();
   1656       return;
   1657     }
   1658 
   1659   do
   1660     {
   1661       expression (&exp);
   1662       if (!strncasecmp (input_line_pointer, "@word", 5))
   1663 	{
   1664 	  exp.X_add_number = 0;
   1665 	  input_line_pointer += 5;
   1666 
   1667 	  p = frag_more (2);
   1668 	  fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
   1669 		       &exp, 0, BFD_RELOC_D10V_18);
   1670 	}
   1671       else
   1672 	emit_expr (&exp, 2);
   1673     }
   1674   while (*input_line_pointer++ == ',');
   1675 
   1676   input_line_pointer--;		/* Put terminator back into stream.  */
   1677   demand_empty_rest_of_line ();
   1678 }
   1679 
   1680 /* Mitsubishi asked that we support some old syntax that apparently
   1681    had immediate operands starting with '#'.  This is in some of their
   1682    sample code but is not documented (although it appears in some
   1683    examples in their assembler manual). For now, we'll solve this
   1684    compatibility problem by simply ignoring any '#' at the beginning
   1685    of an operand.  */
   1686 
   1687 /* Operands that begin with '#' should fall through to here.
   1688    From expr.c.  */
   1689 
   1690 void
   1691 md_operand (expressionS *expressionP)
   1692 {
   1693   if (*input_line_pointer == '#' && ! do_not_ignore_hash)
   1694     {
   1695       input_line_pointer++;
   1696       expression (expressionP);
   1697     }
   1698 }
   1699 
   1700 bool
   1701 d10v_fix_adjustable (fixS *fixP)
   1702 {
   1703   /* We need the symbol name for the VTABLE entries.  */
   1704   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
   1705       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
   1706     return 0;
   1707 
   1708   return 1;
   1709 }
   1710 
   1711 /* The target specific pseudo-ops which we support.  */
   1712 const pseudo_typeS md_pseudo_table[] =
   1713 {
   1714   { "word",	d10v_dot_word,	2 },
   1715   { NULL,       NULL,           0 }
   1716 };
   1717 
   1718 void
   1719 md_assemble (char *str)
   1720 {
   1721   /* etype is saved extype.  For multi-line instructions.  */
   1722   packing_type extype = PACK_UNSPEC;		/* Parallel, etc.  */
   1723   struct d10v_opcode *opcode;
   1724   unsigned long insn;
   1725   char *str2;
   1726 
   1727   if (etype == PACK_UNSPEC)
   1728     {
   1729       /* Look for the special multiple instruction separators.  */
   1730       str2 = strstr (str, "||");
   1731       if (str2)
   1732 	extype = PACK_PARALLEL;
   1733       else
   1734 	{
   1735 	  str2 = strstr (str, "->");
   1736 	  if (str2)
   1737 	    extype = PACK_LEFT_RIGHT;
   1738 	  else
   1739 	    {
   1740 	      str2 = strstr (str, "<-");
   1741 	      if (str2)
   1742 		extype = PACK_RIGHT_LEFT;
   1743 	    }
   1744 	}
   1745 
   1746       /* str2 points to the separator, if there is one.  */
   1747       if (str2)
   1748 	{
   1749 	  *str2 = 0;
   1750 
   1751 	  /* If two instructions are present and we already have one saved,
   1752 	     then first write out the saved one.  */
   1753 	  d10v_cleanup ();
   1754 
   1755 	  /* Assemble first instruction and save it.  */
   1756 	  prev_insn = do_assemble (str, &prev_opcode);
   1757 	  prev_seg = now_seg;
   1758 	  prev_subseg = now_subseg;
   1759 	  if (prev_insn == (unsigned long) -1)
   1760 	    as_fatal (_("can't find previous opcode "));
   1761 	  fixups = fixups->next;
   1762 	  str = str2 + 2;
   1763 	}
   1764     }
   1765 
   1766   insn = do_assemble (str, &opcode);
   1767   if (insn == (unsigned long) -1)
   1768     {
   1769       if (extype != PACK_UNSPEC)
   1770 	etype = extype;
   1771       else
   1772 	as_bad (_("could not assemble: %s"), str);
   1773       return;
   1774     }
   1775 
   1776   if (etype != PACK_UNSPEC)
   1777     {
   1778       extype = etype;
   1779       etype = PACK_UNSPEC;
   1780     }
   1781 
   1782   /* If this is a long instruction, write it and any previous short
   1783      instruction.  */
   1784   if (opcode->format & LONG_OPCODE)
   1785     {
   1786       if (extype != PACK_UNSPEC)
   1787 	as_fatal (_("Unable to mix instructions as specified"));
   1788       d10v_cleanup ();
   1789       write_long (insn, fixups);
   1790       prev_opcode = NULL;
   1791       return;
   1792     }
   1793 
   1794   if (prev_opcode
   1795       && prev_seg
   1796       && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
   1797     d10v_cleanup ();
   1798 
   1799   if (prev_opcode
   1800       && (0 == write_2_short (prev_opcode, prev_insn, opcode, insn, extype,
   1801 			      fixups)))
   1802     {
   1803       /* No instructions saved.  */
   1804       prev_opcode = NULL;
   1805     }
   1806   else
   1807     {
   1808       if (extype != PACK_UNSPEC)
   1809 	as_fatal (_("Unable to mix instructions as specified"));
   1810       /* Save last instruction so it may be packed on next pass.  */
   1811       prev_opcode = opcode;
   1812       prev_insn = insn;
   1813       prev_seg = now_seg;
   1814       prev_subseg = now_subseg;
   1815       fixups = fixups->next;
   1816     }
   1817 }
   1818 
   1819