Home | History | Annotate | Line # | Download | only in config
      1       1.1     skrll /* tc-d30v.c -- Assembler code for the Mitsubishi D30V
      2  1.1.1.10  christos    Copyright (C) 1997-2026 Free Software Foundation, Inc.
      3       1.1     skrll 
      4       1.1     skrll    This file is part of GAS, the GNU Assembler.
      5       1.1     skrll 
      6       1.1     skrll    GAS is free software; you can redistribute it and/or modify
      7       1.1     skrll    it under the terms of the GNU General Public License as published by
      8       1.1     skrll    the Free Software Foundation; either version 3, or (at your option)
      9       1.1     skrll    any later version.
     10       1.1     skrll 
     11       1.1     skrll    GAS is distributed in the hope that it will be useful,
     12       1.1     skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13       1.1     skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14       1.1     skrll    GNU General Public License for more details.
     15       1.1     skrll 
     16       1.1     skrll    You should have received a copy of the GNU General Public License
     17       1.1     skrll    along with GAS; see the file COPYING.  If not, write to
     18       1.1     skrll    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
     19       1.1     skrll    Boston, MA 02110-1301, USA.  */
     20       1.1     skrll 
     21       1.1     skrll #include "as.h"
     22       1.1     skrll #include "safe-ctype.h"
     23       1.1     skrll #include "subsegs.h"
     24       1.1     skrll #include "opcode/d30v.h"
     25   1.1.1.2  christos #include "dwarf2dbg.h"
     26       1.1     skrll 
     27       1.1     skrll const char comment_chars[]        = ";";
     28       1.1     skrll const char line_comment_chars[]   = "#";
     29       1.1     skrll const char line_separator_chars[] = "";
     30  1.1.1.10  christos /* Must do this if we want VLIW instruction with "->" or "<-".  */
     31  1.1.1.10  christos const char d30v_symbol_chars[]    = "-";
     32   1.1.1.9  christos const char md_shortopts[]         = "OnNcC";
     33       1.1     skrll const char EXP_CHARS[]            = "eE";
     34       1.1     skrll const char FLT_CHARS[]            = "dD";
     35       1.1     skrll 
     36       1.1     skrll #include <limits.h>
     37       1.1     skrll #ifndef CHAR_BIT
     38       1.1     skrll #define CHAR_BIT 8
     39       1.1     skrll #endif
     40       1.1     skrll 
     41       1.1     skrll #define NOP_MULTIPLY 1
     42       1.1     skrll #define NOP_ALL 2
     43       1.1     skrll static int warn_nops = 0;
     44       1.1     skrll static int Optimizing = 0;
     45       1.1     skrll static int warn_register_name_conflicts = 1;
     46       1.1     skrll 
     47       1.1     skrll #define FORCE_SHORT	1
     48       1.1     skrll #define FORCE_LONG	2
     49       1.1     skrll 
     50       1.1     skrll /* EXEC types.  */
     51       1.1     skrll typedef enum _exec_type
     52       1.1     skrll {
     53       1.1     skrll   EXEC_UNKNOWN,			/* No order specified.  */
     54       1.1     skrll   EXEC_PARALLEL,		/* Done in parallel (FM=00).  */
     55       1.1     skrll   EXEC_SEQ,			/* Sequential (FM=01).  */
     56       1.1     skrll   EXEC_REVSEQ			/* Reverse sequential (FM=10).  */
     57       1.1     skrll } exec_type_enum;
     58       1.1     skrll 
     59       1.1     skrll /* Fixups.  */
     60       1.1     skrll #define MAX_INSN_FIXUPS  5
     61       1.1     skrll 
     62       1.1     skrll struct d30v_fixup
     63       1.1     skrll {
     64       1.1     skrll   expressionS exp;
     65       1.1     skrll   int operand;
     66       1.1     skrll   int pcrel;
     67       1.1     skrll   int size;
     68       1.1     skrll   bfd_reloc_code_real_type reloc;
     69       1.1     skrll };
     70       1.1     skrll 
     71       1.1     skrll typedef struct _fixups
     72       1.1     skrll {
     73       1.1     skrll   int fc;
     74       1.1     skrll   struct d30v_fixup fix[MAX_INSN_FIXUPS];
     75       1.1     skrll   struct _fixups *next;
     76       1.1     skrll } Fixups;
     77       1.1     skrll 
     78       1.1     skrll static Fixups FixUps[2];
     79       1.1     skrll static Fixups *fixups;
     80       1.1     skrll 
     81       1.1     skrll /* Whether current and previous instruction are word multiply insns.  */
     82       1.1     skrll static int cur_mul32_p = 0;
     83       1.1     skrll static int prev_mul32_p = 0;
     84       1.1     skrll 
     85       1.1     skrll /*  The flag_explicitly_parallel is true iff the instruction being assembled
     86       1.1     skrll     has been explicitly written as a parallel short-instruction pair by the
     87       1.1     skrll     human programmer.  It is used in parallel_ok () to distinguish between
     88       1.1     skrll     those dangerous parallelizations attempted by the human, which are to be
     89       1.1     skrll     allowed, and those attempted by the assembler, which are not.  It is set
     90       1.1     skrll     from md_assemble ().  */
     91       1.1     skrll static int flag_explicitly_parallel = 0;
     92       1.1     skrll static int flag_xp_state = 0;
     93       1.1     skrll 
     94       1.1     skrll /* Whether current and previous left sub-instruction disables
     95       1.1     skrll    execution of right sub-instruction.  */
     96       1.1     skrll static int cur_left_kills_right_p = 0;
     97       1.1     skrll static int prev_left_kills_right_p = 0;
     98       1.1     skrll 
     99       1.1     skrll /* The known current alignment of the current section.  */
    100       1.1     skrll static int d30v_current_align;
    101       1.1     skrll static segT d30v_current_align_seg;
    102       1.1     skrll 
    103       1.1     skrll /* The last seen label in the current section.  This is used to auto-align
    104       1.1     skrll    labels preceding instructions.  */
    105       1.1     skrll static symbolS *d30v_last_label;
    106       1.1     skrll 
    107       1.1     skrll /* Two nops.  */
    108       1.1     skrll #define NOP_LEFT   ((long long) NOP << 32)
    109       1.1     skrll #define NOP_RIGHT  ((long long) NOP)
    110       1.1     skrll #define NOP2 (FM00 | NOP_LEFT | NOP_RIGHT)
    111       1.1     skrll 
    112   1.1.1.9  christos const struct option md_longopts[] =
    113       1.1     skrll {
    114       1.1     skrll   {NULL, no_argument, NULL, 0}
    115       1.1     skrll };
    116       1.1     skrll 
    117   1.1.1.9  christos const size_t md_longopts_size = sizeof (md_longopts);
    118       1.1     skrll 
    119       1.1     skrll /* Opcode hash table.  */
    120   1.1.1.7  christos static htab_t d30v_hash;
    121       1.1     skrll 
    122       1.1     skrll /* Do a binary search of the pre_defined_registers array to see if
    123   1.1.1.5  christos    NAME is a valid register name.  Return the register number from the
    124       1.1     skrll    array on success, or -1 on failure.  */
    125       1.1     skrll 
    126       1.1     skrll static int
    127       1.1     skrll reg_name_search (char *name)
    128       1.1     skrll {
    129       1.1     skrll   int middle, low, high;
    130       1.1     skrll   int cmp;
    131       1.1     skrll 
    132       1.1     skrll   low = 0;
    133       1.1     skrll   high = reg_name_cnt () - 1;
    134       1.1     skrll 
    135       1.1     skrll   do
    136       1.1     skrll     {
    137       1.1     skrll       middle = (low + high) / 2;
    138       1.1     skrll       cmp = strcasecmp (name, pre_defined_registers[middle].name);
    139       1.1     skrll       if (cmp < 0)
    140       1.1     skrll 	high = middle - 1;
    141       1.1     skrll       else if (cmp > 0)
    142       1.1     skrll 	low = middle + 1;
    143       1.1     skrll       else
    144       1.1     skrll 	{
    145       1.1     skrll 	  if (symbol_find (name) != NULL)
    146       1.1     skrll 	    {
    147       1.1     skrll 	      if (warn_register_name_conflicts)
    148       1.1     skrll 		as_warn (_("Register name %s conflicts with symbol of the same name"),
    149       1.1     skrll 			 name);
    150       1.1     skrll 	    }
    151       1.1     skrll 
    152       1.1     skrll 	  return pre_defined_registers[middle].value;
    153       1.1     skrll 	}
    154       1.1     skrll     }
    155       1.1     skrll   while (low <= high);
    156       1.1     skrll 
    157       1.1     skrll   return -1;
    158       1.1     skrll }
    159       1.1     skrll 
    160       1.1     skrll /* Check the string at input_line_pointer to see if it is a valid
    161       1.1     skrll    register name.  */
    162       1.1     skrll 
    163       1.1     skrll static int
    164       1.1     skrll register_name (expressionS *expressionP)
    165       1.1     skrll {
    166       1.1     skrll   int reg_number;
    167       1.1     skrll   char c, *p = input_line_pointer;
    168       1.1     skrll 
    169   1.1.1.9  christos   while (!is_end_of_stmt (*p) && *p != ',' && !is_whitespace (*p) && *p != ')')
    170       1.1     skrll     p++;
    171       1.1     skrll 
    172       1.1     skrll   c = *p;
    173       1.1     skrll   if (c)
    174       1.1     skrll     *p++ = 0;
    175       1.1     skrll 
    176       1.1     skrll   /* Look to see if it's in the register table.  */
    177       1.1     skrll   reg_number = reg_name_search (input_line_pointer);
    178       1.1     skrll   if (reg_number >= 0)
    179       1.1     skrll     {
    180       1.1     skrll       expressionP->X_op = O_register;
    181       1.1     skrll       /* Temporarily store a pointer to the string here.  */
    182       1.1     skrll       expressionP->X_op_symbol = (symbolS *) input_line_pointer;
    183       1.1     skrll       expressionP->X_add_number = reg_number;
    184       1.1     skrll       input_line_pointer = p;
    185       1.1     skrll       return 1;
    186       1.1     skrll     }
    187       1.1     skrll   if (c)
    188       1.1     skrll     *(p - 1) = c;
    189       1.1     skrll   return 0;
    190       1.1     skrll }
    191       1.1     skrll 
    192       1.1     skrll static int
    193       1.1     skrll check_range (unsigned long num, int bits, int flags)
    194       1.1     skrll {
    195       1.1     skrll   long min, max;
    196       1.1     skrll 
    197       1.1     skrll   /* Don't bother checking 32-bit values.  */
    198       1.1     skrll   if (bits == 32)
    199       1.1     skrll     {
    200       1.1     skrll       if (sizeof (unsigned long) * CHAR_BIT == 32)
    201       1.1     skrll 	return 0;
    202       1.1     skrll 
    203       1.1     skrll       /* We don't record signed or unsigned for 32-bit quantities.
    204       1.1     skrll 	 Allow either.  */
    205       1.1     skrll       min = -((unsigned long) 1 << (bits - 1));
    206       1.1     skrll       max = ((unsigned long) 1 << bits) - 1;
    207       1.1     skrll       return (long) num < min || (long) num > max;
    208       1.1     skrll     }
    209       1.1     skrll 
    210       1.1     skrll   if (flags & OPERAND_SHIFT)
    211       1.1     skrll     {
    212       1.1     skrll       /* We know that all shifts are right by three bits.  */
    213       1.1     skrll       num >>= 3;
    214       1.1     skrll 
    215       1.1     skrll       if (flags & OPERAND_SIGNED)
    216       1.1     skrll 	{
    217       1.1     skrll 	  unsigned long sign_bit = ((unsigned long) -1L >> 4) + 1;
    218       1.1     skrll 	  num = (num ^ sign_bit) - sign_bit;
    219       1.1     skrll 	}
    220       1.1     skrll     }
    221       1.1     skrll 
    222       1.1     skrll   if (flags & OPERAND_SIGNED)
    223       1.1     skrll     {
    224       1.1     skrll       max = ((unsigned long) 1 << (bits - 1)) - 1;
    225       1.1     skrll       min = - ((unsigned long) 1 << (bits - 1));
    226       1.1     skrll       return (long) num > max || (long) num < min;
    227       1.1     skrll     }
    228       1.1     skrll   else
    229       1.1     skrll     {
    230       1.1     skrll       max = ((unsigned long) 1 << bits) - 1;
    231       1.1     skrll       return num > (unsigned long) max;
    232       1.1     skrll     }
    233       1.1     skrll }
    234       1.1     skrll 
    235       1.1     skrll void
    236       1.1     skrll md_show_usage (FILE *stream)
    237       1.1     skrll {
    238       1.1     skrll   fprintf (stream, _("\nD30V options:\n\
    239       1.1     skrll -O                      Make adjacent short instructions parallel if possible.\n\
    240       1.1     skrll -n                      Warn about all NOPs inserted by the assembler.\n\
    241   1.1.1.5  christos -N                      Warn about NOPs inserted after word multiplies.\n\
    242   1.1.1.5  christos -c                      Warn about symbols whose names match register names.\n\
    243       1.1     skrll -C                      Opposite of -C.  -c is the default.\n"));
    244       1.1     skrll }
    245       1.1     skrll 
    246       1.1     skrll int
    247   1.1.1.4  christos md_parse_option (int c, const char *arg ATTRIBUTE_UNUSED)
    248       1.1     skrll {
    249       1.1     skrll   switch (c)
    250       1.1     skrll     {
    251       1.1     skrll       /* Optimize.  Will attempt to parallelize operations.  */
    252       1.1     skrll     case 'O':
    253       1.1     skrll       Optimizing = 1;
    254       1.1     skrll       break;
    255       1.1     skrll 
    256       1.1     skrll       /* Warn about all NOPS that the assembler inserts.  */
    257       1.1     skrll     case 'n':
    258       1.1     skrll       warn_nops = NOP_ALL;
    259       1.1     skrll       break;
    260       1.1     skrll 
    261       1.1     skrll       /* Warn about the NOPS that the assembler inserts because of the
    262       1.1     skrll 	 multiply hazard.  */
    263       1.1     skrll     case 'N':
    264       1.1     skrll       warn_nops = NOP_MULTIPLY;
    265       1.1     skrll       break;
    266       1.1     skrll 
    267       1.1     skrll     case 'c':
    268       1.1     skrll       warn_register_name_conflicts = 1;
    269       1.1     skrll       break;
    270       1.1     skrll 
    271       1.1     skrll     case 'C':
    272       1.1     skrll       warn_register_name_conflicts = 0;
    273       1.1     skrll       break;
    274       1.1     skrll 
    275       1.1     skrll     default:
    276       1.1     skrll       return 0;
    277       1.1     skrll     }
    278       1.1     skrll   return 1;
    279       1.1     skrll }
    280       1.1     skrll 
    281       1.1     skrll symbolS *
    282       1.1     skrll md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
    283       1.1     skrll {
    284       1.1     skrll   return 0;
    285       1.1     skrll }
    286       1.1     skrll 
    287   1.1.1.4  christos const char *
    288       1.1     skrll md_atof (int type, char *litP, int *sizeP)
    289       1.1     skrll {
    290   1.1.1.7  christos   return ieee_md_atof (type, litP, sizeP, true);
    291       1.1     skrll }
    292       1.1     skrll 
    293       1.1     skrll void
    294       1.1     skrll md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
    295       1.1     skrll 		 asection *sec ATTRIBUTE_UNUSED,
    296       1.1     skrll 		 fragS *fragP ATTRIBUTE_UNUSED)
    297       1.1     skrll {
    298       1.1     skrll   abort ();
    299       1.1     skrll }
    300       1.1     skrll 
    301       1.1     skrll valueT
    302       1.1     skrll md_section_align (asection *seg, valueT addr)
    303       1.1     skrll {
    304   1.1.1.6  christos   int align = bfd_section_alignment (seg);
    305   1.1.1.3  christos   return ((addr + (1 << align) - 1) & -(1 << align));
    306       1.1     skrll }
    307       1.1     skrll 
    308       1.1     skrll void
    309       1.1     skrll md_begin (void)
    310       1.1     skrll {
    311   1.1.1.9  christos   const struct d30v_opcode *opcode;
    312   1.1.1.7  christos   d30v_hash = str_htab_create ();
    313       1.1     skrll 
    314       1.1     skrll   /* Insert opcode names into a hash table.  */
    315   1.1.1.9  christos   for (opcode = d30v_opcode_table; opcode->name; opcode++)
    316   1.1.1.7  christos       str_hash_insert (d30v_hash, opcode->name, opcode, 0);
    317       1.1     skrll 
    318       1.1     skrll   fixups = &FixUps[0];
    319       1.1     skrll   FixUps[0].next = &FixUps[1];
    320       1.1     skrll   FixUps[1].next = &FixUps[0];
    321       1.1     skrll 
    322       1.1     skrll   d30v_current_align_seg = now_seg;
    323       1.1     skrll }
    324       1.1     skrll 
    325       1.1     skrll /* Remove the postincrement or postdecrement operator ( '+' or '-' )
    326       1.1     skrll    from an expression.  */
    327       1.1     skrll 
    328       1.1     skrll static int
    329       1.1     skrll postfix (char *p)
    330       1.1     skrll {
    331       1.1     skrll   while (*p != '-' && *p != '+')
    332       1.1     skrll     {
    333   1.1.1.9  christos       if (is_end_of_stmt (*p) || is_whitespace (*p) || *p == ',')
    334       1.1     skrll 	break;
    335       1.1     skrll       p++;
    336       1.1     skrll     }
    337       1.1     skrll 
    338       1.1     skrll   if (*p == '-')
    339       1.1     skrll     {
    340       1.1     skrll       *p = ' ';
    341       1.1     skrll       return -1;
    342       1.1     skrll     }
    343       1.1     skrll 
    344       1.1     skrll   if (*p == '+')
    345       1.1     skrll     {
    346       1.1     skrll       *p = ' ';
    347       1.1     skrll       return 1;
    348       1.1     skrll     }
    349       1.1     skrll 
    350       1.1     skrll   return 0;
    351       1.1     skrll }
    352       1.1     skrll 
    353       1.1     skrll static bfd_reloc_code_real_type
    354       1.1     skrll get_reloc (const struct d30v_operand *op, int rel_flag)
    355       1.1     skrll {
    356       1.1     skrll   switch (op->bits)
    357       1.1     skrll     {
    358       1.1     skrll     case 6:
    359       1.1     skrll       if (op->flags & OPERAND_SHIFT)
    360       1.1     skrll 	return BFD_RELOC_D30V_9_PCREL;
    361       1.1     skrll       else
    362       1.1     skrll 	return BFD_RELOC_D30V_6;
    363       1.1     skrll       break;
    364       1.1     skrll     case 12:
    365       1.1     skrll       if (!(op->flags & OPERAND_SHIFT))
    366       1.1     skrll 	as_warn (_("unexpected 12-bit reloc type"));
    367       1.1     skrll       if (rel_flag == RELOC_PCREL)
    368       1.1     skrll 	return BFD_RELOC_D30V_15_PCREL;
    369       1.1     skrll       else
    370       1.1     skrll 	return BFD_RELOC_D30V_15;
    371       1.1     skrll     case 18:
    372       1.1     skrll       if (!(op->flags & OPERAND_SHIFT))
    373       1.1     skrll 	as_warn (_("unexpected 18-bit reloc type"));
    374       1.1     skrll       if (rel_flag == RELOC_PCREL)
    375       1.1     skrll 	return BFD_RELOC_D30V_21_PCREL;
    376       1.1     skrll       else
    377       1.1     skrll 	return BFD_RELOC_D30V_21;
    378       1.1     skrll     case 32:
    379       1.1     skrll       if (rel_flag == RELOC_PCREL)
    380       1.1     skrll 	return BFD_RELOC_D30V_32_PCREL;
    381       1.1     skrll       else
    382       1.1     skrll 	return BFD_RELOC_D30V_32;
    383       1.1     skrll     default:
    384       1.1     skrll       return 0;
    385       1.1     skrll     }
    386       1.1     skrll }
    387       1.1     skrll 
    388       1.1     skrll /* Parse a string of operands and return an array of expressions.  */
    389       1.1     skrll 
    390       1.1     skrll static int
    391       1.1     skrll get_operands (expressionS exp[], int cmp_hack)
    392       1.1     skrll {
    393       1.1     skrll   char *p = input_line_pointer;
    394       1.1     skrll   int numops = 0;
    395       1.1     skrll   int post = 0;
    396       1.1     skrll 
    397       1.1     skrll   if (cmp_hack)
    398       1.1     skrll     {
    399       1.1     skrll       exp[numops].X_op = O_absent;
    400       1.1     skrll       exp[numops++].X_add_number = cmp_hack - 1;
    401       1.1     skrll     }
    402       1.1     skrll 
    403       1.1     skrll   while (*p)
    404       1.1     skrll     {
    405   1.1.1.9  christos       while (is_whitespace (*p) || *p == ',')
    406       1.1     skrll 	p++;
    407       1.1     skrll 
    408       1.1     skrll       if (*p == 0 || *p == '\n' || *p == '\r')
    409       1.1     skrll 	break;
    410       1.1     skrll 
    411       1.1     skrll       if (*p == '@')
    412       1.1     skrll 	{
    413       1.1     skrll 	  p++;
    414       1.1     skrll 	  exp[numops].X_op = O_absent;
    415       1.1     skrll 	  if (*p == '(')
    416       1.1     skrll 	    {
    417       1.1     skrll 	      p++;
    418       1.1     skrll 	      exp[numops].X_add_number = OPERAND_ATPAR;
    419       1.1     skrll 	      post = postfix (p);
    420       1.1     skrll 	    }
    421       1.1     skrll 	  else if (*p == '-')
    422       1.1     skrll 	    {
    423       1.1     skrll 	      p++;
    424       1.1     skrll 	      exp[numops].X_add_number = OPERAND_ATMINUS;
    425       1.1     skrll 	    }
    426       1.1     skrll 	  else
    427       1.1     skrll 	    {
    428       1.1     skrll 	      exp[numops].X_add_number = OPERAND_ATSIGN;
    429       1.1     skrll 	      post = postfix (p);
    430       1.1     skrll 	    }
    431       1.1     skrll 	  numops++;
    432       1.1     skrll 	  continue;
    433       1.1     skrll 	}
    434       1.1     skrll 
    435       1.1     skrll       if (*p == ')')
    436       1.1     skrll 	{
    437       1.1     skrll 	  /* Just skip the trailing paren.  */
    438       1.1     skrll 	  p++;
    439       1.1     skrll 	  continue;
    440       1.1     skrll 	}
    441       1.1     skrll 
    442       1.1     skrll       input_line_pointer = p;
    443       1.1     skrll 
    444       1.1     skrll       /* Check to see if it might be a register name.  */
    445       1.1     skrll       if (!register_name (&exp[numops]))
    446       1.1     skrll 	{
    447       1.1     skrll 	  /* Parse as an expression.  */
    448       1.1     skrll 	  expression (&exp[numops]);
    449       1.1     skrll 	}
    450       1.1     skrll 
    451       1.1     skrll       if (exp[numops].X_op == O_illegal)
    452       1.1     skrll 	as_bad (_("illegal operand"));
    453       1.1     skrll       else if (exp[numops].X_op == O_absent)
    454       1.1     skrll 	as_bad (_("missing operand"));
    455       1.1     skrll 
    456       1.1     skrll       numops++;
    457       1.1     skrll       p = input_line_pointer;
    458       1.1     skrll 
    459       1.1     skrll       switch (post)
    460       1.1     skrll 	{
    461       1.1     skrll 	case -1:
    462       1.1     skrll 	  /* Postdecrement mode.  */
    463       1.1     skrll 	  exp[numops].X_op = O_absent;
    464       1.1     skrll 	  exp[numops++].X_add_number = OPERAND_MINUS;
    465       1.1     skrll 	  break;
    466       1.1     skrll 	case 1:
    467       1.1     skrll 	  /* Postincrement mode.  */
    468       1.1     skrll 	  exp[numops].X_op = O_absent;
    469       1.1     skrll 	  exp[numops++].X_add_number = OPERAND_PLUS;
    470       1.1     skrll 	  break;
    471       1.1     skrll 	}
    472       1.1     skrll       post = 0;
    473       1.1     skrll     }
    474       1.1     skrll 
    475       1.1     skrll   exp[numops].X_op = 0;
    476       1.1     skrll 
    477       1.1     skrll   return numops;
    478       1.1     skrll }
    479       1.1     skrll 
    480       1.1     skrll /* Generate the instruction.
    481       1.1     skrll    It does everything but write the FM bits.  */
    482       1.1     skrll 
    483       1.1     skrll static long long
    484       1.1     skrll build_insn (struct d30v_insn *opcode, expressionS *opers)
    485       1.1     skrll {
    486   1.1.1.2  christos   int i, bits, shift, flags;
    487       1.1     skrll   unsigned long number, id = 0;
    488       1.1     skrll   long long insn;
    489   1.1.1.9  christos   const struct d30v_opcode *op = opcode->op;
    490   1.1.1.9  christos   const struct d30v_format *form = opcode->form;
    491       1.1     skrll 
    492       1.1     skrll   insn =
    493       1.1     skrll     opcode->ecc << 28 | op->op1 << 25 | op->op2 << 20 | form->modifier << 18;
    494       1.1     skrll 
    495       1.1     skrll   for (i = 0; form->operands[i]; i++)
    496       1.1     skrll     {
    497       1.1     skrll       flags = d30v_operand_table[form->operands[i]].flags;
    498       1.1     skrll 
    499       1.1     skrll       /* Must be a register or number.  */
    500       1.1     skrll       if (!(flags & OPERAND_REG) && !(flags & OPERAND_NUM)
    501       1.1     skrll 	  && !(flags & OPERAND_NAME) && !(flags & OPERAND_SPECIAL))
    502       1.1     skrll 	continue;
    503       1.1     skrll 
    504       1.1     skrll       bits = d30v_operand_table[form->operands[i]].bits;
    505       1.1     skrll       if (flags & OPERAND_SHIFT)
    506       1.1     skrll 	bits += 3;
    507       1.1     skrll 
    508       1.1     skrll       shift = 12 - d30v_operand_table[form->operands[i]].position;
    509       1.1     skrll       if (opers[i].X_op != O_symbol)
    510       1.1     skrll 	number = opers[i].X_add_number;
    511       1.1     skrll       else
    512       1.1     skrll 	number = 0;
    513       1.1     skrll       if (flags & OPERAND_REG)
    514       1.1     skrll 	{
    515       1.1     skrll 	  /* Check for mvfsys or mvtsys control registers.  */
    516       1.1     skrll 	  if (flags & OPERAND_CONTROL && (number & 0x7f) > MAX_CONTROL_REG)
    517       1.1     skrll 	    {
    518       1.1     skrll 	      /* PSWL or PSWH.  */
    519       1.1     skrll 	      id = (number & 0x7f) - MAX_CONTROL_REG;
    520       1.1     skrll 	      number = 0;
    521       1.1     skrll 	    }
    522       1.1     skrll 	  else if (number & OPERAND_FLAG)
    523       1.1     skrll 	    /* NUMBER is a flag register.  */
    524       1.1     skrll 	    id = 3;
    525       1.1     skrll 
    526       1.1     skrll 	  number &= 0x7F;
    527       1.1     skrll 	}
    528       1.1     skrll       else if (flags & OPERAND_SPECIAL)
    529       1.1     skrll 	number = id;
    530       1.1     skrll 
    531       1.1     skrll       if (opers[i].X_op != O_register && opers[i].X_op != O_constant
    532       1.1     skrll 	  && !(flags & OPERAND_NAME))
    533       1.1     skrll 	{
    534       1.1     skrll 	  /* Now create a fixup.  */
    535       1.1     skrll 	  if (fixups->fc >= MAX_INSN_FIXUPS)
    536       1.1     skrll 	    as_fatal (_("too many fixups"));
    537       1.1     skrll 
    538       1.1     skrll 	  fixups->fix[fixups->fc].reloc =
    539       1.1     skrll 	    get_reloc (d30v_operand_table + form->operands[i], op->reloc_flag);
    540       1.1     skrll 	  fixups->fix[fixups->fc].size = 4;
    541       1.1     skrll 	  fixups->fix[fixups->fc].exp = opers[i];
    542       1.1     skrll 	  fixups->fix[fixups->fc].operand = form->operands[i];
    543       1.1     skrll 	  if (fixups->fix[fixups->fc].reloc == BFD_RELOC_D30V_9_PCREL)
    544       1.1     skrll 	    fixups->fix[fixups->fc].pcrel = RELOC_PCREL;
    545       1.1     skrll 	  else
    546       1.1     skrll 	    fixups->fix[fixups->fc].pcrel = op->reloc_flag;
    547       1.1     skrll 	  (fixups->fc)++;
    548       1.1     skrll 	}
    549       1.1     skrll 
    550       1.1     skrll       /* Truncate to the proper number of bits.  */
    551       1.1     skrll       if ((opers[i].X_op == O_constant) && check_range (number, bits, flags))
    552       1.1     skrll 	as_bad (_("operand out of range: %ld"), number);
    553       1.1     skrll       if (bits < 31)
    554       1.1     skrll 	number &= 0x7FFFFFFF >> (31 - bits);
    555       1.1     skrll       if (flags & OPERAND_SHIFT)
    556       1.1     skrll 	number >>= 3;
    557       1.1     skrll       if (bits == 32)
    558       1.1     skrll 	{
    559       1.1     skrll 	  /* It's a LONG instruction.  */
    560       1.1     skrll 	  insn |= ((number & 0xffffffff) >> 26);	/* Top 6 bits.  */
    561       1.1     skrll 	  insn <<= 32;			/* Shift the first word over.  */
    562       1.1     skrll 	  insn |= ((number & 0x03FC0000) << 2);		/* Next 8 bits.  */
    563       1.1     skrll 	  insn |= number & 0x0003FFFF;			/* Bottom 18 bits.  */
    564       1.1     skrll 	}
    565       1.1     skrll       else
    566       1.1     skrll 	insn |= number << shift;
    567       1.1     skrll     }
    568       1.1     skrll 
    569       1.1     skrll   return insn;
    570       1.1     skrll }
    571       1.1     skrll 
    572       1.1     skrll static void
    573       1.1     skrll d30v_number_to_chars (char *buf,	/* Return 'nbytes' of chars here.  */
    574       1.1     skrll 		      long long value,	/* The value of the bits.  */
    575       1.1     skrll 		      int n)		/* Number of bytes in the output.  */
    576       1.1     skrll {
    577       1.1     skrll   while (n--)
    578       1.1     skrll     {
    579       1.1     skrll       buf[n] = value & 0xff;
    580       1.1     skrll       value >>= 8;
    581       1.1     skrll     }
    582       1.1     skrll }
    583       1.1     skrll 
    584       1.1     skrll /* Write out a long form instruction.  */
    585       1.1     skrll 
    586       1.1     skrll static void
    587       1.1     skrll write_long (struct d30v_insn *opcode ATTRIBUTE_UNUSED,
    588       1.1     skrll 	    long long insn,
    589       1.1     skrll 	    Fixups *fx)
    590       1.1     skrll {
    591       1.1     skrll   int i, where;
    592       1.1     skrll   char *f = frag_more (8);
    593       1.1     skrll 
    594   1.1.1.2  christos   dwarf2_emit_insn (8);
    595       1.1     skrll   insn |= FM11;
    596       1.1     skrll   d30v_number_to_chars (f, insn, 8);
    597       1.1     skrll 
    598       1.1     skrll   for (i = 0; i < fx->fc; i++)
    599       1.1     skrll     {
    600       1.1     skrll       if (fx->fix[i].reloc)
    601       1.1     skrll 	{
    602       1.1     skrll 	  where = f - frag_now->fr_literal;
    603       1.1     skrll 	  fix_new_exp (frag_now, where, fx->fix[i].size, &(fx->fix[i].exp),
    604       1.1     skrll 		       fx->fix[i].pcrel, fx->fix[i].reloc);
    605       1.1     skrll 	}
    606       1.1     skrll     }
    607       1.1     skrll 
    608       1.1     skrll   fx->fc = 0;
    609       1.1     skrll }
    610       1.1     skrll 
    611       1.1     skrll /* Write out a short form instruction by itself.  */
    612       1.1     skrll 
    613       1.1     skrll static void
    614       1.1     skrll write_1_short (struct d30v_insn *opcode,
    615       1.1     skrll 	       long long insn,
    616       1.1     skrll 	       Fixups *fx,
    617       1.1     skrll 	       int use_sequential)
    618       1.1     skrll {
    619       1.1     skrll   char *f = frag_more (8);
    620       1.1     skrll   int i, where;
    621       1.1     skrll 
    622   1.1.1.2  christos   dwarf2_emit_insn (8);
    623       1.1     skrll   if (warn_nops == NOP_ALL)
    624       1.1     skrll     as_warn (_("%s NOP inserted"), use_sequential ?
    625       1.1     skrll 	     _("sequential") : _("parallel"));
    626       1.1     skrll 
    627       1.1     skrll   /* The other container needs to be NOP.  */
    628       1.1     skrll   if (use_sequential)
    629       1.1     skrll     {
    630       1.1     skrll       /* Use a sequential NOP rather than a parallel one,
    631       1.1     skrll 	 as the current instruction is a FLAG_MUL32 type one
    632       1.1     skrll 	 and the next instruction is a load.  */
    633       1.1     skrll 
    634       1.1     skrll       /* According to 4.3.1: for FM=01, sub-instructions performed
    635       1.1     skrll 	 only by IU cannot be encoded in L-container.  */
    636       1.1     skrll       if (opcode->op->unit == IU)
    637       1.1     skrll 	/* Right then left.  */
    638       1.1     skrll 	insn |= FM10 | NOP_LEFT;
    639       1.1     skrll       else
    640       1.1     skrll 	/* Left then right.  */
    641       1.1     skrll 	insn = FM01 | (insn << 32) | NOP_RIGHT;
    642       1.1     skrll     }
    643       1.1     skrll   else
    644       1.1     skrll     {
    645       1.1     skrll       /* According to 4.3.1: for FM=00, sub-instructions performed
    646       1.1     skrll 	 only by IU cannot be encoded in L-container.  */
    647       1.1     skrll       if (opcode->op->unit == IU)
    648       1.1     skrll 	/* Right container.  */
    649       1.1     skrll 	insn |= FM00 | NOP_LEFT;
    650       1.1     skrll       else
    651       1.1     skrll 	/* Left container.  */
    652       1.1     skrll 	insn = FM00 | (insn << 32) | NOP_RIGHT;
    653       1.1     skrll     }
    654       1.1     skrll 
    655       1.1     skrll   d30v_number_to_chars (f, insn, 8);
    656       1.1     skrll 
    657       1.1     skrll   for (i = 0; i < fx->fc; i++)
    658       1.1     skrll     {
    659       1.1     skrll       if (fx->fix[i].reloc)
    660       1.1     skrll 	{
    661       1.1     skrll 	  where = f - frag_now->fr_literal;
    662       1.1     skrll 	  fix_new_exp (frag_now,
    663       1.1     skrll 		       where,
    664       1.1     skrll 		       fx->fix[i].size,
    665       1.1     skrll 		       &(fx->fix[i].exp),
    666       1.1     skrll 		       fx->fix[i].pcrel,
    667       1.1     skrll 		       fx->fix[i].reloc);
    668       1.1     skrll 	}
    669       1.1     skrll     }
    670       1.1     skrll 
    671       1.1     skrll   fx->fc = 0;
    672       1.1     skrll }
    673       1.1     skrll 
    674       1.1     skrll /* Check 2 instructions and determine if they can be safely
    675       1.1     skrll    executed in parallel.  Return 1 if they can be.  */
    676       1.1     skrll 
    677       1.1     skrll static int
    678       1.1     skrll parallel_ok (struct d30v_insn *op1,
    679       1.1     skrll 	     unsigned long insn1,
    680       1.1     skrll 	     struct d30v_insn *op2,
    681       1.1     skrll 	     unsigned long insn2,
    682       1.1     skrll 	     exec_type_enum exec_type)
    683       1.1     skrll {
    684       1.1     skrll   int i, j, shift, regno, bits, ecc;
    685       1.1     skrll   unsigned long flags, mask, flags_set1, flags_set2, flags_used1, flags_used2;
    686       1.1     skrll   unsigned long ins, mod_reg[2][3], used_reg[2][3], flag_reg[2];
    687   1.1.1.9  christos   const struct d30v_format *f;
    688   1.1.1.9  christos   const struct d30v_opcode *op;
    689       1.1     skrll 
    690       1.1     skrll   /* Section 4.3: Both instructions must not be IU or MU only.  */
    691       1.1     skrll   if ((op1->op->unit == IU && op2->op->unit == IU)
    692       1.1     skrll       || (op1->op->unit == MU && op2->op->unit == MU))
    693       1.1     skrll     return 0;
    694       1.1     skrll 
    695       1.1     skrll   /* First instruction must not be a jump to safely optimize, unless this
    696       1.1     skrll      is an explicit parallel operation.  */
    697       1.1     skrll   if (exec_type != EXEC_PARALLEL
    698       1.1     skrll       && (op1->op->flags_used & (FLAG_JMP | FLAG_JSR)))
    699       1.1     skrll     return 0;
    700       1.1     skrll 
    701       1.1     skrll   /* If one instruction is /TX or /XT and the other is /FX or /XF respectively,
    702       1.1     skrll      then it is safe to allow the two to be done as parallel ops, since only
    703       1.1     skrll      one will ever be executed at a time.  */
    704       1.1     skrll   if ((op1->ecc == ECC_TX && op2->ecc == ECC_FX)
    705       1.1     skrll       || (op1->ecc == ECC_FX && op2->ecc == ECC_TX)
    706       1.1     skrll       || (op1->ecc == ECC_XT && op2->ecc == ECC_XF)
    707       1.1     skrll       || (op1->ecc == ECC_XF && op2->ecc == ECC_XT))
    708       1.1     skrll     return 1;
    709       1.1     skrll 
    710       1.1     skrll   /* [0] r0-r31
    711       1.1     skrll      [1] r32-r63
    712       1.1     skrll      [2] a0, a1, flag registers.  */
    713       1.1     skrll   for (j = 0; j < 2; j++)
    714       1.1     skrll     {
    715       1.1     skrll       if (j == 0)
    716       1.1     skrll 	{
    717       1.1     skrll 	  f = op1->form;
    718       1.1     skrll 	  op = op1->op;
    719       1.1     skrll 	  ecc = op1->ecc;
    720       1.1     skrll 	  ins = insn1;
    721       1.1     skrll 	}
    722       1.1     skrll       else
    723       1.1     skrll 	{
    724       1.1     skrll 	  f = op2->form;
    725       1.1     skrll 	  op = op2->op;
    726       1.1     skrll 	  ecc = op2->ecc;
    727       1.1     skrll 	  ins = insn2;
    728       1.1     skrll 	}
    729       1.1     skrll 
    730       1.1     skrll       flag_reg[j] = 0;
    731       1.1     skrll       mod_reg[j][0] = mod_reg[j][1] = 0;
    732       1.1     skrll       used_reg[j][0] = used_reg[j][1] = 0;
    733       1.1     skrll 
    734       1.1     skrll       if (flag_explicitly_parallel)
    735       1.1     skrll 	{
    736       1.1     skrll 	  /* For human specified parallel instructions we have been asked
    737       1.1     skrll 	     to ignore the possibility that both instructions could modify
    738       1.1     skrll 	     bits in the PSW, so we initialise the mod & used arrays to 0.
    739       1.1     skrll 	     We have been asked, however, to refuse to allow parallel
    740       1.1     skrll 	     instructions which explicitly set the same flag register,
    741       1.1     skrll 	     eg "cmpne f0,r1,0x10 || cmpeq f0, r5, 0x2", so further on we test
    742       1.1     skrll 	     for the use of a flag register and set a bit in the mod or used
    743       1.1     skrll 	     array appropriately.  */
    744       1.1     skrll 	  mod_reg[j][2]  = 0;
    745       1.1     skrll 	  used_reg[j][2] = 0;
    746       1.1     skrll 	}
    747       1.1     skrll       else
    748       1.1     skrll 	{
    749       1.1     skrll 	  mod_reg[j][2] = (op->flags_set & FLAG_ALL);
    750       1.1     skrll 	  used_reg[j][2] = (op->flags_used & FLAG_ALL);
    751       1.1     skrll 	}
    752       1.1     skrll 
    753       1.1     skrll       /* BSR/JSR always sets R62.  */
    754       1.1     skrll       if (op->flags_used & FLAG_JSR)
    755       1.1     skrll 	mod_reg[j][1] = (1L << (62 - 32));
    756       1.1     skrll 
    757       1.1     skrll       /* Conditional execution affects the flags_used.  */
    758       1.1     skrll       switch (ecc)
    759       1.1     skrll 	{
    760       1.1     skrll 	case ECC_TX:
    761       1.1     skrll 	case ECC_FX:
    762       1.1     skrll 	  used_reg[j][2] |= flag_reg[j] = FLAG_0;
    763       1.1     skrll 	  break;
    764       1.1     skrll 
    765       1.1     skrll 	case ECC_XT:
    766       1.1     skrll 	case ECC_XF:
    767       1.1     skrll 	  used_reg[j][2] |= flag_reg[j] = FLAG_1;
    768       1.1     skrll 	  break;
    769       1.1     skrll 
    770       1.1     skrll 	case ECC_TT:
    771       1.1     skrll 	case ECC_TF:
    772       1.1     skrll 	  used_reg[j][2] |= flag_reg[j] = (FLAG_0 | FLAG_1);
    773       1.1     skrll 	  break;
    774       1.1     skrll 	}
    775       1.1     skrll 
    776       1.1     skrll       for (i = 0; f->operands[i]; i++)
    777       1.1     skrll 	{
    778       1.1     skrll 	  flags = d30v_operand_table[f->operands[i]].flags;
    779       1.1     skrll 	  shift = 12 - d30v_operand_table[f->operands[i]].position;
    780       1.1     skrll 	  bits = d30v_operand_table[f->operands[i]].bits;
    781       1.1     skrll 	  if (bits == 32)
    782       1.1     skrll 	    mask = 0xffffffff;
    783       1.1     skrll 	  else
    784       1.1     skrll 	    mask = 0x7FFFFFFF >> (31 - bits);
    785       1.1     skrll 
    786       1.1     skrll 	  if ((flags & OPERAND_PLUS) || (flags & OPERAND_MINUS))
    787       1.1     skrll 	    {
    788       1.1     skrll 	      /* This is a post-increment or post-decrement.
    789       1.1     skrll 		 The previous register needs to be marked as modified.  */
    790       1.1     skrll 	      shift = 12 - d30v_operand_table[f->operands[i - 1]].position;
    791       1.1     skrll 	      regno = (ins >> shift) & 0x3f;
    792       1.1     skrll 	      if (regno >= 32)
    793       1.1     skrll 		mod_reg[j][1] |= 1L << (regno - 32);
    794       1.1     skrll 	      else
    795       1.1     skrll 		mod_reg[j][0] |= 1L << regno;
    796       1.1     skrll 	    }
    797       1.1     skrll 	  else if (flags & OPERAND_REG)
    798       1.1     skrll 	    {
    799       1.1     skrll 	      regno = (ins >> shift) & mask;
    800       1.1     skrll 	      /* The memory write functions don't have a destination
    801       1.1     skrll                  register.  */
    802       1.1     skrll 	      if ((flags & OPERAND_DEST) && !(op->flags_set & FLAG_MEM))
    803       1.1     skrll 		{
    804       1.1     skrll 		  /* MODIFIED registers and flags.  */
    805       1.1     skrll 		  if (flags & OPERAND_ACC)
    806       1.1     skrll 		    {
    807       1.1     skrll 		      if (regno == 0)
    808       1.1     skrll 			mod_reg[j][2] |= FLAG_A0;
    809       1.1     skrll 		      else if (regno == 1)
    810       1.1     skrll 			mod_reg[j][2] |= FLAG_A1;
    811       1.1     skrll 		      else
    812       1.1     skrll 			abort ();
    813       1.1     skrll 		    }
    814       1.1     skrll 		  else if (flags & OPERAND_FLAG)
    815       1.1     skrll 		    mod_reg[j][2] |= 1L << regno;
    816       1.1     skrll 		  else if (!(flags & OPERAND_CONTROL))
    817       1.1     skrll 		    {
    818       1.1     skrll 		      int r, z;
    819       1.1     skrll 
    820       1.1     skrll 		      /* Need to check if there are two destination
    821       1.1     skrll 			 registers, for example ld2w.  */
    822       1.1     skrll 		      if (flags & OPERAND_2REG)
    823       1.1     skrll 			z = 1;
    824       1.1     skrll 		      else
    825       1.1     skrll 			z = 0;
    826       1.1     skrll 
    827       1.1     skrll 		      for (r = regno; r <= regno + z; r++)
    828       1.1     skrll 			{
    829       1.1     skrll 			  if (r >= 32)
    830       1.1     skrll 			    mod_reg[j][1] |= 1L << (r - 32);
    831       1.1     skrll 			  else
    832       1.1     skrll 			    mod_reg[j][0] |= 1L << r;
    833       1.1     skrll 			}
    834       1.1     skrll 		    }
    835       1.1     skrll 		}
    836       1.1     skrll 	      else
    837       1.1     skrll 		{
    838       1.1     skrll 		  /* USED, but not modified registers and flags.  */
    839       1.1     skrll 		  if (flags & OPERAND_ACC)
    840       1.1     skrll 		    {
    841       1.1     skrll 		      if (regno == 0)
    842       1.1     skrll 			used_reg[j][2] |= FLAG_A0;
    843       1.1     skrll 		      else if (regno == 1)
    844       1.1     skrll 			used_reg[j][2] |= FLAG_A1;
    845       1.1     skrll 		      else
    846       1.1     skrll 			abort ();
    847       1.1     skrll 		    }
    848       1.1     skrll 		  else if (flags & OPERAND_FLAG)
    849       1.1     skrll 		    used_reg[j][2] |= 1L << regno;
    850       1.1     skrll 		  else if (!(flags & OPERAND_CONTROL))
    851       1.1     skrll 		    {
    852       1.1     skrll 		      int r, z;
    853       1.1     skrll 
    854       1.1     skrll 		      /* Need to check if there are two source
    855       1.1     skrll 			 registers, for example st2w.  */
    856       1.1     skrll 		      if (flags & OPERAND_2REG)
    857       1.1     skrll 			z = 1;
    858       1.1     skrll 		      else
    859       1.1     skrll 			z = 0;
    860       1.1     skrll 
    861       1.1     skrll 		      for (r = regno; r <= regno + z; r++)
    862       1.1     skrll 			{
    863       1.1     skrll 			  if (r >= 32)
    864   1.1.1.7  christos 			    used_reg[j][1] |= 1UL << (r - 32);
    865       1.1     skrll 			  else
    866   1.1.1.7  christos 			    used_reg[j][0] |= 1UL << r;
    867       1.1     skrll 			}
    868       1.1     skrll 		    }
    869       1.1     skrll 		}
    870       1.1     skrll 	    }
    871       1.1     skrll 	}
    872       1.1     skrll     }
    873       1.1     skrll 
    874       1.1     skrll   flags_set1 = op1->op->flags_set;
    875       1.1     skrll   flags_set2 = op2->op->flags_set;
    876       1.1     skrll   flags_used1 = op1->op->flags_used;
    877       1.1     skrll   flags_used2 = op2->op->flags_used;
    878       1.1     skrll 
    879       1.1     skrll   /* Check for illegal combinations with ADDppp/SUBppp.  */
    880       1.1     skrll   if (((flags_set1 & FLAG_NOT_WITH_ADDSUBppp) != 0
    881       1.1     skrll        && (flags_used2 & FLAG_ADDSUBppp) != 0)
    882       1.1     skrll       || ((flags_set2 & FLAG_NOT_WITH_ADDSUBppp) != 0
    883       1.1     skrll 	  && (flags_used1 & FLAG_ADDSUBppp) != 0))
    884       1.1     skrll     return 0;
    885       1.1     skrll 
    886       1.1     skrll   /* Load instruction combined with half-word multiply is illegal.  */
    887       1.1     skrll   if (((flags_used1 & FLAG_MEM) != 0 && (flags_used2 & FLAG_MUL16))
    888       1.1     skrll       || ((flags_used2 & FLAG_MEM) != 0 && (flags_used1 & FLAG_MUL16)))
    889       1.1     skrll     return 0;
    890       1.1     skrll 
    891       1.1     skrll   /* Specifically allow add || add by removing carry, overflow bits dependency.
    892       1.1     skrll      This is safe, even if an addc follows since the IU takes the argument in
    893       1.1     skrll      the right container, and it writes its results last.
    894       1.1     skrll      However, don't paralellize add followed by addc or sub followed by
    895       1.1     skrll      subb.  */
    896       1.1     skrll   if (mod_reg[0][2] == FLAG_CVVA && mod_reg[1][2] == FLAG_CVVA
    897       1.1     skrll       && (used_reg[0][2] & ~flag_reg[0]) == 0
    898       1.1     skrll       && (used_reg[1][2] & ~flag_reg[1]) == 0
    899       1.1     skrll       && op1->op->unit == EITHER && op2->op->unit == EITHER)
    900       1.1     skrll     {
    901       1.1     skrll       mod_reg[0][2] = mod_reg[1][2] = 0;
    902       1.1     skrll     }
    903       1.1     skrll 
    904       1.1     skrll   for (j = 0; j < 3; j++)
    905       1.1     skrll     {
    906       1.1     skrll       /* If the second instruction depends on the first, we obviously
    907       1.1     skrll 	 cannot parallelize.  Note, the mod flag implies use, so
    908       1.1     skrll 	 check that as well.  */
    909       1.1     skrll       /* If flag_explicitly_parallel is set, then the case of the
    910       1.1     skrll 	 second instruction using a register the first instruction
    911       1.1     skrll 	 modifies is assumed to be okay; we trust the human.  We
    912       1.1     skrll 	 don't trust the human if both instructions modify the same
    913       1.1     skrll 	 register but we do trust the human if they modify the same
    914       1.1     skrll 	 flags.  */
    915       1.1     skrll       /* We have now been requested not to trust the human if the
    916       1.1     skrll 	 instructions modify the same flag registers either.  */
    917       1.1     skrll       if (flag_explicitly_parallel)
    918       1.1     skrll 	{
    919       1.1     skrll 	  if ((mod_reg[0][j] & mod_reg[1][j]) != 0)
    920       1.1     skrll 	    return 0;
    921       1.1     skrll 	}
    922       1.1     skrll       else
    923       1.1     skrll 	if ((mod_reg[0][j] & (mod_reg[1][j] | used_reg[1][j])) != 0)
    924       1.1     skrll 	  return 0;
    925       1.1     skrll     }
    926       1.1     skrll 
    927       1.1     skrll   return 1;
    928       1.1     skrll }
    929       1.1     skrll 
    930       1.1     skrll /* Write out a short form instruction if possible.
    931       1.1     skrll    Return number of instructions not written out.  */
    932       1.1     skrll 
    933       1.1     skrll static int
    934       1.1     skrll write_2_short (struct d30v_insn *opcode1,
    935       1.1     skrll 	       long long insn1,
    936       1.1     skrll 	       struct d30v_insn *opcode2,
    937       1.1     skrll 	       long long insn2,
    938       1.1     skrll 	       exec_type_enum exec_type,
    939       1.1     skrll 	       Fixups *fx)
    940       1.1     skrll {
    941       1.1     skrll   long long insn = NOP2;
    942       1.1     skrll   char *f;
    943       1.1     skrll   int i, j, where;
    944       1.1     skrll 
    945       1.1     skrll   if (exec_type == EXEC_SEQ
    946       1.1     skrll       && (opcode1->op->flags_used & (FLAG_JMP | FLAG_JSR))
    947       1.1     skrll       && ((opcode1->op->flags_used & FLAG_DELAY) == 0)
    948       1.1     skrll       && ((opcode1->ecc == ECC_AL) || ! Optimizing))
    949       1.1     skrll     {
    950       1.1     skrll       /* Unconditional, non-delayed branches kill instructions in
    951       1.1     skrll 	 the right bin.  Conditional branches don't always but if
    952       1.1     skrll 	 we are not optimizing, then we have been asked to produce
    953       1.1     skrll 	 an error about such constructs.  For the purposes of this
    954       1.1     skrll 	 test, subroutine calls are considered to be branches.  */
    955   1.1.1.7  christos       write_1_short (opcode1, insn1, fx->next, false);
    956       1.1     skrll       return 1;
    957       1.1     skrll     }
    958       1.1     skrll 
    959       1.1     skrll   /* Note: we do not have to worry about subroutine calls occurring
    960       1.1     skrll      in the right hand container.  The return address is always
    961       1.1     skrll      aligned to the next 64 bit boundary, be that 64 or 32 bit away.  */
    962       1.1     skrll   switch (exec_type)
    963       1.1     skrll     {
    964       1.1     skrll     case EXEC_UNKNOWN:	/* Order not specified.  */
    965       1.1     skrll       if (Optimizing
    966       1.1     skrll 	  && parallel_ok (opcode1, insn1, opcode2, insn2, exec_type)
    967       1.1     skrll 	  && ! (   (opcode1->op->unit == EITHER_BUT_PREFER_MU
    968       1.1     skrll 		 || opcode1->op->unit == MU)
    969       1.1     skrll 		&&
    970       1.1     skrll 		(   opcode2->op->unit == EITHER_BUT_PREFER_MU
    971       1.1     skrll 		 || opcode2->op->unit == MU)))
    972       1.1     skrll 	{
    973       1.1     skrll 	  /* Parallel.  */
    974       1.1     skrll 	  exec_type = EXEC_PARALLEL;
    975       1.1     skrll 
    976       1.1     skrll 	  if (opcode1->op->unit == IU
    977       1.1     skrll 	      || opcode2->op->unit == MU
    978       1.1     skrll 	      || opcode2->op->unit == EITHER_BUT_PREFER_MU)
    979       1.1     skrll 	    insn = FM00 | (insn2 << 32) | insn1;
    980       1.1     skrll 	  else
    981       1.1     skrll 	    {
    982       1.1     skrll 	      insn = FM00 | (insn1 << 32) | insn2;
    983       1.1     skrll 	      fx = fx->next;
    984       1.1     skrll 	    }
    985       1.1     skrll 	}
    986       1.1     skrll       else if ((opcode1->op->flags_used & (FLAG_JMP | FLAG_JSR)
    987       1.1     skrll 		&& ((opcode1->op->flags_used & FLAG_DELAY) == 0))
    988       1.1     skrll 	       || opcode1->op->flags_used & FLAG_RP)
    989       1.1     skrll 	{
    990       1.1     skrll 	  /* We must emit (non-delayed) branch type instructions
    991       1.1     skrll 	     on their own with nothing in the right container.  */
    992       1.1     skrll 	  /* We must treat repeat instructions likewise, since the
    993       1.1     skrll 	     following instruction has to be separate from the repeat
    994       1.1     skrll 	     in order to be repeated.  */
    995   1.1.1.7  christos 	  write_1_short (opcode1, insn1, fx->next, false);
    996       1.1     skrll 	  return 1;
    997       1.1     skrll 	}
    998       1.1     skrll       else if (prev_left_kills_right_p)
    999       1.1     skrll 	{
   1000   1.1.1.5  christos 	  /* The left instruction kills the right slot, so we
   1001       1.1     skrll 	     must leave it empty.  */
   1002   1.1.1.7  christos 	  write_1_short (opcode1, insn1, fx->next, false);
   1003       1.1     skrll 	  return 1;
   1004       1.1     skrll 	}
   1005       1.1     skrll       else if (opcode1->op->unit == IU)
   1006       1.1     skrll 	{
   1007       1.1     skrll 	  if (opcode2->op->unit == EITHER_BUT_PREFER_MU)
   1008       1.1     skrll 	    {
   1009       1.1     skrll 	      /* Case 103810 is a request from Mitsubishi that opcodes
   1010       1.1     skrll 		 with EITHER_BUT_PREFER_MU should not be executed in
   1011       1.1     skrll 		 reverse sequential order.  */
   1012   1.1.1.7  christos 	      write_1_short (opcode1, insn1, fx->next, false);
   1013       1.1     skrll 	      return 1;
   1014       1.1     skrll 	    }
   1015       1.1     skrll 
   1016       1.1     skrll 	  /* Reverse sequential.  */
   1017       1.1     skrll 	  insn = FM10 | (insn2 << 32) | insn1;
   1018       1.1     skrll 	  exec_type = EXEC_REVSEQ;
   1019       1.1     skrll 	}
   1020       1.1     skrll       else
   1021       1.1     skrll 	{
   1022       1.1     skrll 	  /* Sequential.  */
   1023       1.1     skrll 	  insn = FM01 | (insn1 << 32) | insn2;
   1024       1.1     skrll 	  fx = fx->next;
   1025       1.1     skrll 	  exec_type = EXEC_SEQ;
   1026       1.1     skrll 	}
   1027       1.1     skrll       break;
   1028       1.1     skrll 
   1029       1.1     skrll     case EXEC_PARALLEL:	/* Parallel.  */
   1030       1.1     skrll       flag_explicitly_parallel = flag_xp_state;
   1031       1.1     skrll       if (! parallel_ok (opcode1, insn1, opcode2, insn2, exec_type))
   1032       1.1     skrll 	as_bad (_("Instructions may not be executed in parallel"));
   1033       1.1     skrll       else if (opcode1->op->unit == IU)
   1034       1.1     skrll 	{
   1035       1.1     skrll 	  if (opcode2->op->unit == IU)
   1036       1.1     skrll 	    as_bad (_("Two IU instructions may not be executed in parallel"));
   1037       1.1     skrll 	  as_warn (_("Swapping instruction order"));
   1038       1.1     skrll 	  insn = FM00 | (insn2 << 32) | insn1;
   1039       1.1     skrll 	}
   1040       1.1     skrll       else if (opcode2->op->unit == MU)
   1041       1.1     skrll 	{
   1042       1.1     skrll 	  if (opcode1->op->unit == MU)
   1043       1.1     skrll 	    as_bad (_("Two MU instructions may not be executed in parallel"));
   1044       1.1     skrll 	  else if (opcode1->op->unit == EITHER_BUT_PREFER_MU)
   1045       1.1     skrll 	    as_warn (_("Executing %s in IU may not work"), opcode1->op->name);
   1046       1.1     skrll 	  as_warn (_("Swapping instruction order"));
   1047       1.1     skrll 	  insn = FM00 | (insn2 << 32) | insn1;
   1048       1.1     skrll 	}
   1049       1.1     skrll       else
   1050       1.1     skrll 	{
   1051       1.1     skrll 	  if (opcode2->op->unit == EITHER_BUT_PREFER_MU)
   1052       1.1     skrll 	    as_warn (_("Executing %s in IU may not work in parallel execution"),
   1053       1.1     skrll 		     opcode2->op->name);
   1054       1.1     skrll 
   1055       1.1     skrll 	  insn = FM00 | (insn1 << 32) | insn2;
   1056       1.1     skrll 	  fx = fx->next;
   1057       1.1     skrll 	}
   1058       1.1     skrll       flag_explicitly_parallel = 0;
   1059       1.1     skrll       break;
   1060       1.1     skrll 
   1061       1.1     skrll     case EXEC_SEQ:	/* Sequential.  */
   1062       1.1     skrll       if (opcode1->op->unit == IU)
   1063       1.1     skrll 	as_bad (_("IU instruction may not be in the left container"));
   1064       1.1     skrll       if (prev_left_kills_right_p)
   1065       1.1     skrll 	as_bad (_("special left instruction `%s' kills instruction "
   1066       1.1     skrll 		  "`%s' in right container"),
   1067       1.1     skrll 		opcode1->op->name, opcode2->op->name);
   1068       1.1     skrll       insn = FM01 | (insn1 << 32) | insn2;
   1069       1.1     skrll       fx = fx->next;
   1070       1.1     skrll       break;
   1071       1.1     skrll 
   1072       1.1     skrll     case EXEC_REVSEQ:	/* Reverse sequential.  */
   1073       1.1     skrll       if (opcode2->op->unit == MU)
   1074       1.1     skrll 	as_bad (_("MU instruction may not be in the right container"));
   1075       1.1     skrll       if (opcode1->op->unit == EITHER_BUT_PREFER_MU)
   1076       1.1     skrll 	as_warn (_("Executing %s in reverse serial with %s may not work"),
   1077       1.1     skrll 		 opcode1->op->name, opcode2->op->name);
   1078       1.1     skrll       else if (opcode2->op->unit == EITHER_BUT_PREFER_MU)
   1079       1.1     skrll 	as_warn (_("Executing %s in IU in reverse serial may not work"),
   1080       1.1     skrll 		 opcode2->op->name);
   1081       1.1     skrll       insn = FM10 | (insn1 << 32) | insn2;
   1082       1.1     skrll       fx = fx->next;
   1083       1.1     skrll       break;
   1084       1.1     skrll 
   1085       1.1     skrll     default:
   1086       1.1     skrll       as_fatal (_("unknown execution type passed to write_2_short()"));
   1087       1.1     skrll     }
   1088       1.1     skrll 
   1089       1.1     skrll   f = frag_more (8);
   1090   1.1.1.2  christos   dwarf2_emit_insn (8);
   1091       1.1     skrll   d30v_number_to_chars (f, insn, 8);
   1092       1.1     skrll 
   1093       1.1     skrll   /* If the previous instruction was a 32-bit multiply but it is put into a
   1094       1.1     skrll      parallel container, mark the current instruction as being a 32-bit
   1095       1.1     skrll      multiply.  */
   1096       1.1     skrll   if (prev_mul32_p && exec_type == EXEC_PARALLEL)
   1097       1.1     skrll     cur_mul32_p = 1;
   1098       1.1     skrll 
   1099       1.1     skrll   for (j = 0; j < 2; j++)
   1100       1.1     skrll     {
   1101       1.1     skrll       for (i = 0; i < fx->fc; i++)
   1102       1.1     skrll 	{
   1103       1.1     skrll 	  if (fx->fix[i].reloc)
   1104       1.1     skrll 	    {
   1105       1.1     skrll 	      where = (f - frag_now->fr_literal) + 4 * j;
   1106       1.1     skrll 
   1107       1.1     skrll 	      fix_new_exp (frag_now,
   1108       1.1     skrll 			   where,
   1109       1.1     skrll 			   fx->fix[i].size,
   1110       1.1     skrll 			   &(fx->fix[i].exp),
   1111       1.1     skrll 			   fx->fix[i].pcrel,
   1112       1.1     skrll 			   fx->fix[i].reloc);
   1113       1.1     skrll 	    }
   1114       1.1     skrll 	}
   1115       1.1     skrll 
   1116       1.1     skrll       fx->fc = 0;
   1117       1.1     skrll       fx = fx->next;
   1118       1.1     skrll     }
   1119       1.1     skrll 
   1120       1.1     skrll   return 0;
   1121       1.1     skrll }
   1122       1.1     skrll 
   1123       1.1     skrll /* Get a pointer to an entry in the format table.
   1124       1.1     skrll    It must look at all formats for an opcode and use the operands
   1125       1.1     skrll    to choose the correct one.  Return NULL on error.  */
   1126       1.1     skrll 
   1127   1.1.1.9  christos static const struct d30v_format *
   1128   1.1.1.9  christos find_format (const struct d30v_opcode *opcode,
   1129       1.1     skrll 	     expressionS myops[],
   1130       1.1     skrll 	     int fsize,
   1131       1.1     skrll 	     int cmp_hack)
   1132       1.1     skrll {
   1133   1.1.1.2  christos   int match, opcode_index, i = 0, j, k;
   1134   1.1.1.9  christos   const struct d30v_format *fm;
   1135       1.1     skrll 
   1136       1.1     skrll   if (opcode == NULL)
   1137       1.1     skrll     return NULL;
   1138       1.1     skrll 
   1139       1.1     skrll   /* Get all the operands and save them as expressions.  */
   1140   1.1.1.2  christos   get_operands (myops, cmp_hack);
   1141       1.1     skrll 
   1142   1.1.1.2  christos   while ((opcode_index = opcode->format[i++]) != 0)
   1143       1.1     skrll     {
   1144   1.1.1.2  christos       if (fsize == FORCE_SHORT && opcode_index >= LONG)
   1145       1.1     skrll 	continue;
   1146       1.1     skrll 
   1147   1.1.1.2  christos       if (fsize == FORCE_LONG && opcode_index < LONG)
   1148       1.1     skrll 	continue;
   1149       1.1     skrll 
   1150   1.1.1.9  christos       fm = &d30v_format_table[opcode_index];
   1151   1.1.1.2  christos       k = opcode_index;
   1152   1.1.1.2  christos       while (fm->form == opcode_index)
   1153       1.1     skrll 	{
   1154       1.1     skrll 	  match = 1;
   1155       1.1     skrll 	  /* Now check the operands for compatibility.  */
   1156       1.1     skrll 	  for (j = 0; match && fm->operands[j]; j++)
   1157       1.1     skrll 	    {
   1158       1.1     skrll 	      int flags = d30v_operand_table[fm->operands[j]].flags;
   1159       1.1     skrll 	      int bits = d30v_operand_table[fm->operands[j]].bits;
   1160   1.1.1.4  christos 	      operatorT X_op = myops[j].X_op;
   1161       1.1     skrll 	      int num = myops[j].X_add_number;
   1162       1.1     skrll 
   1163       1.1     skrll 	      if (flags & OPERAND_SPECIAL)
   1164       1.1     skrll 		break;
   1165       1.1     skrll 	      else if (X_op == O_illegal)
   1166       1.1     skrll 		match = 0;
   1167       1.1     skrll 	      else if (flags & OPERAND_REG)
   1168       1.1     skrll 		{
   1169       1.1     skrll 		  if (X_op != O_register
   1170       1.1     skrll 		      || ((flags & OPERAND_ACC) && !(num & OPERAND_ACC))
   1171       1.1     skrll 		      || (!(flags & OPERAND_ACC) && (num & OPERAND_ACC))
   1172       1.1     skrll 		      || ((flags & OPERAND_FLAG) && !(num & OPERAND_FLAG))
   1173       1.1     skrll 		      || (!(flags & (OPERAND_FLAG | OPERAND_CONTROL)) && (num & OPERAND_FLAG))
   1174       1.1     skrll 		      || ((flags & OPERAND_CONTROL)
   1175       1.1     skrll 			  && !(num & (OPERAND_CONTROL | OPERAND_FLAG))))
   1176       1.1     skrll 		    match = 0;
   1177       1.1     skrll 		}
   1178       1.1     skrll 	      else if (((flags & OPERAND_MINUS)
   1179       1.1     skrll 			&& (X_op != O_absent || num != OPERAND_MINUS))
   1180       1.1     skrll 		       || ((flags & OPERAND_PLUS)
   1181       1.1     skrll 			   && (X_op != O_absent || num != OPERAND_PLUS))
   1182       1.1     skrll 		       || ((flags & OPERAND_ATMINUS)
   1183       1.1     skrll 			   && (X_op != O_absent || num != OPERAND_ATMINUS))
   1184       1.1     skrll 		       || ((flags & OPERAND_ATPAR)
   1185       1.1     skrll 			   && (X_op != O_absent || num != OPERAND_ATPAR))
   1186       1.1     skrll 		       || ((flags & OPERAND_ATSIGN)
   1187       1.1     skrll 			   && (X_op != O_absent || num != OPERAND_ATSIGN)))
   1188       1.1     skrll 		match = 0;
   1189       1.1     skrll 	      else if (flags & OPERAND_NUM)
   1190       1.1     skrll 		{
   1191       1.1     skrll 		  /* A number can be a constant or symbol expression.  */
   1192       1.1     skrll 
   1193       1.1     skrll 		  /* If we have found a register name, but that name
   1194       1.1     skrll 		     also matches a symbol, then re-parse the name as
   1195       1.1     skrll 		     an expression.  */
   1196       1.1     skrll 		  if (X_op == O_register
   1197       1.1     skrll 		      && symbol_find ((char *) myops[j].X_op_symbol))
   1198       1.1     skrll 		    {
   1199       1.1     skrll 		      input_line_pointer = (char *) myops[j].X_op_symbol;
   1200       1.1     skrll 		      expression (&myops[j]);
   1201       1.1     skrll 		    }
   1202       1.1     skrll 
   1203       1.1     skrll 		  /* Turn an expression into a symbol for later resolution.  */
   1204       1.1     skrll 		  if (X_op != O_absent && X_op != O_constant
   1205       1.1     skrll 		      && X_op != O_symbol && X_op != O_register
   1206       1.1     skrll 		      && X_op != O_big)
   1207       1.1     skrll 		    {
   1208       1.1     skrll 		      symbolS *sym = make_expr_symbol (&myops[j]);
   1209       1.1     skrll 		      myops[j].X_op = X_op = O_symbol;
   1210       1.1     skrll 		      myops[j].X_add_symbol = sym;
   1211       1.1     skrll 		      myops[j].X_add_number = num = 0;
   1212       1.1     skrll 		    }
   1213       1.1     skrll 
   1214       1.1     skrll 		  if (fm->form >= LONG)
   1215       1.1     skrll 		    {
   1216       1.1     skrll 		      /* If we're testing for a LONG format, either fits.  */
   1217       1.1     skrll 		      if (X_op != O_constant && X_op != O_symbol)
   1218       1.1     skrll 			match = 0;
   1219       1.1     skrll 		    }
   1220       1.1     skrll 		  else if (fm->form < LONG
   1221       1.1     skrll 			   && ((fsize == FORCE_SHORT && X_op == O_symbol)
   1222       1.1     skrll 			       || (fm->form == SHORT_D2 && j == 0)))
   1223       1.1     skrll 		    match = 1;
   1224       1.1     skrll 
   1225       1.1     skrll 		  /* This is the tricky part.  Will the constant or symbol
   1226       1.1     skrll 		     fit into the space in the current format?  */
   1227       1.1     skrll 		  else if (X_op == O_constant)
   1228       1.1     skrll 		    {
   1229       1.1     skrll 		      if (check_range (num, bits, flags))
   1230       1.1     skrll 			match = 0;
   1231       1.1     skrll 		    }
   1232       1.1     skrll 		  else if (X_op == O_symbol
   1233       1.1     skrll 			   && S_IS_DEFINED (myops[j].X_add_symbol)
   1234       1.1     skrll 			   && S_GET_SEGMENT (myops[j].X_add_symbol) == now_seg
   1235       1.1     skrll 			   && opcode->reloc_flag == RELOC_PCREL)
   1236       1.1     skrll 		    {
   1237       1.1     skrll 		      /* If the symbol is defined, see if the value will fit
   1238       1.1     skrll 			 into the form we're considering.  */
   1239       1.1     skrll 		      fragS *f;
   1240       1.1     skrll 		      long value;
   1241       1.1     skrll 
   1242       1.1     skrll 		      /* Calculate the current address by running through the
   1243       1.1     skrll 			 previous frags and adding our current offset.  */
   1244   1.1.1.3  christos 		      value = frag_now_fix_octets ();
   1245       1.1     skrll 		      for (f = frchain_now->frch_root; f; f = f->fr_next)
   1246       1.1     skrll 			value += f->fr_fix + f->fr_offset;
   1247   1.1.1.3  christos 		      value = S_GET_VALUE (myops[j].X_add_symbol) - value;
   1248       1.1     skrll 		      if (check_range (value, bits, flags))
   1249       1.1     skrll 			match = 0;
   1250       1.1     skrll 		    }
   1251       1.1     skrll 		  else
   1252       1.1     skrll 		    match = 0;
   1253       1.1     skrll 		}
   1254       1.1     skrll 	    }
   1255       1.1     skrll 	  /* We're only done if the operands matched so far AND there
   1256       1.1     skrll 	     are no more to check.  */
   1257       1.1     skrll 	  if (match && myops[j].X_op == 0)
   1258       1.1     skrll 	    {
   1259       1.1     skrll 	      /* Final check - issue a warning if an odd numbered register
   1260       1.1     skrll 		 is used as the first register in an instruction that reads
   1261       1.1     skrll 		 or writes 2 registers.  */
   1262       1.1     skrll 
   1263       1.1     skrll 	      for (j = 0; fm->operands[j]; j++)
   1264       1.1     skrll 		if (myops[j].X_op == O_register
   1265       1.1     skrll 		    && (myops[j].X_add_number & 1)
   1266       1.1     skrll 		    && (d30v_operand_table[fm->operands[j]].flags & OPERAND_2REG))
   1267       1.1     skrll 		  as_warn (_("Odd numbered register used as target of multi-register instruction"));
   1268       1.1     skrll 
   1269       1.1     skrll 	      return fm;
   1270       1.1     skrll 	    }
   1271   1.1.1.9  christos 	  fm = &d30v_format_table[++k];
   1272       1.1     skrll 	}
   1273       1.1     skrll     }
   1274       1.1     skrll   return NULL;
   1275       1.1     skrll }
   1276       1.1     skrll 
   1277       1.1     skrll /* Assemble a single instruction and return an opcode.
   1278       1.1     skrll    Return -1 (an invalid opcode) on error.  */
   1279       1.1     skrll 
   1280       1.1     skrll #define NAME_BUF_LEN	20
   1281       1.1     skrll 
   1282       1.1     skrll static long long
   1283       1.1     skrll do_assemble (char *str,
   1284       1.1     skrll 	     struct d30v_insn *opcode,
   1285       1.1     skrll 	     int shortp,
   1286       1.1     skrll 	     int is_parallel)
   1287       1.1     skrll {
   1288       1.1     skrll   char *op_start;
   1289       1.1     skrll   char *save;
   1290       1.1     skrll   char *op_end;
   1291       1.1     skrll   char           name[NAME_BUF_LEN];
   1292       1.1     skrll   int            cmp_hack;
   1293       1.1     skrll   int            nlen = 0;
   1294       1.1     skrll   int            fsize = (shortp ? FORCE_SHORT : 0);
   1295       1.1     skrll   expressionS    myops[6];
   1296       1.1     skrll   long long      insn;
   1297       1.1     skrll 
   1298       1.1     skrll   /* Drop leading whitespace.  */
   1299   1.1.1.9  christos   while (is_whitespace (*str))
   1300       1.1     skrll     str++;
   1301       1.1     skrll 
   1302       1.1     skrll   /* Find the opcode end.  */
   1303       1.1     skrll   for (op_start = op_end = str;
   1304       1.1     skrll        *op_end
   1305       1.1     skrll        && nlen < (NAME_BUF_LEN - 1)
   1306       1.1     skrll        && *op_end != '/'
   1307   1.1.1.9  christos        && !is_end_of_stmt (*op_end) && !is_whitespace (*op_end);
   1308       1.1     skrll        op_end++)
   1309       1.1     skrll     {
   1310       1.1     skrll       name[nlen] = TOLOWER (op_start[nlen]);
   1311       1.1     skrll       nlen++;
   1312       1.1     skrll     }
   1313       1.1     skrll 
   1314       1.1     skrll   if (nlen == 0)
   1315       1.1     skrll     return -1;
   1316       1.1     skrll 
   1317       1.1     skrll   name[nlen] = 0;
   1318       1.1     skrll 
   1319       1.1     skrll   /* If there is an execution condition code, handle it.  */
   1320       1.1     skrll   if (*op_end == '/')
   1321       1.1     skrll     {
   1322       1.1     skrll       int i = 0;
   1323       1.1     skrll       while ((i < ECC_MAX) && strncasecmp (d30v_ecc_names[i], op_end + 1, 2))
   1324       1.1     skrll 	i++;
   1325       1.1     skrll 
   1326       1.1     skrll       if (i == ECC_MAX)
   1327       1.1     skrll 	{
   1328       1.1     skrll 	  char tmp[4];
   1329       1.1     skrll 	  strncpy (tmp, op_end + 1, 2);
   1330       1.1     skrll 	  tmp[2] = 0;
   1331       1.1     skrll 	  as_bad (_("unknown condition code: %s"), tmp);
   1332       1.1     skrll 	  return -1;
   1333       1.1     skrll 	}
   1334       1.1     skrll       opcode->ecc = i;
   1335       1.1     skrll       op_end += 3;
   1336       1.1     skrll     }
   1337       1.1     skrll   else
   1338       1.1     skrll     opcode->ecc = ECC_AL;
   1339       1.1     skrll 
   1340       1.1     skrll   /* CMP and CMPU change their name based on condition codes.  */
   1341   1.1.1.7  christos   if (startswith (name, "cmp"))
   1342       1.1     skrll     {
   1343       1.1     skrll       int p, i;
   1344   1.1.1.9  christos       const char **d30v_str = d30v_cc_names;
   1345   1.1.1.2  christos 
   1346       1.1     skrll       if (name[3] == 'u')
   1347       1.1     skrll 	p = 4;
   1348       1.1     skrll       else
   1349       1.1     skrll 	p = 3;
   1350       1.1     skrll 
   1351   1.1.1.2  christos       for (i = 1; *d30v_str && strncmp (*d30v_str, &name[p], 2); i++, d30v_str++)
   1352       1.1     skrll 	;
   1353       1.1     skrll 
   1354       1.1     skrll       /* cmpu only supports some condition codes.  */
   1355       1.1     skrll       if (p == 4)
   1356       1.1     skrll 	{
   1357       1.1     skrll 	  if (i < 3 || i > 6)
   1358       1.1     skrll 	    {
   1359       1.1     skrll 	      name[p + 2] = 0;
   1360       1.1     skrll 	      as_bad (_("cmpu doesn't support condition code %s"), &name[p]);
   1361       1.1     skrll 	    }
   1362       1.1     skrll 	}
   1363       1.1     skrll 
   1364   1.1.1.2  christos       if (!*d30v_str)
   1365       1.1     skrll 	{
   1366       1.1     skrll 	  name[p + 2] = 0;
   1367       1.1     skrll 	  as_bad (_("unknown condition code: %s"), &name[p]);
   1368       1.1     skrll 	}
   1369       1.1     skrll 
   1370       1.1     skrll       cmp_hack = i;
   1371       1.1     skrll       name[p] = 0;
   1372       1.1     skrll     }
   1373       1.1     skrll   else
   1374       1.1     skrll     cmp_hack = 0;
   1375       1.1     skrll 
   1376       1.1     skrll   /* Need to look for .s or .l.  */
   1377       1.1     skrll   if (name[nlen - 2] == '.')
   1378       1.1     skrll     {
   1379       1.1     skrll       switch (name[nlen - 1])
   1380       1.1     skrll 	{
   1381       1.1     skrll 	case 's':
   1382       1.1     skrll 	  fsize = FORCE_SHORT;
   1383       1.1     skrll 	  break;
   1384       1.1     skrll 	case 'l':
   1385       1.1     skrll 	  fsize = FORCE_LONG;
   1386       1.1     skrll 	  break;
   1387       1.1     skrll 	}
   1388       1.1     skrll       name[nlen - 2] = 0;
   1389       1.1     skrll     }
   1390       1.1     skrll 
   1391       1.1     skrll   /* Find the first opcode with the proper name.  */
   1392   1.1.1.9  christos   opcode->op = str_hash_find (d30v_hash, name);
   1393       1.1     skrll   if (opcode->op == NULL)
   1394       1.1     skrll     {
   1395       1.1     skrll       as_bad (_("unknown opcode: %s"), name);
   1396       1.1     skrll       return -1;
   1397       1.1     skrll     }
   1398       1.1     skrll 
   1399       1.1     skrll   save = input_line_pointer;
   1400       1.1     skrll   input_line_pointer = op_end;
   1401       1.1     skrll   while (!(opcode->form = find_format (opcode->op, myops, fsize, cmp_hack)))
   1402       1.1     skrll     {
   1403       1.1     skrll       opcode->op++;
   1404       1.1     skrll       if (opcode->op->name == NULL || strcmp (opcode->op->name, name))
   1405       1.1     skrll 	{
   1406       1.1     skrll 	  as_bad (_("operands for opcode `%s' do not match any valid format"),
   1407       1.1     skrll 		  name);
   1408       1.1     skrll 	  return -1;
   1409       1.1     skrll 	}
   1410       1.1     skrll     }
   1411       1.1     skrll   input_line_pointer = save;
   1412       1.1     skrll 
   1413       1.1     skrll   insn = build_insn (opcode, myops);
   1414       1.1     skrll 
   1415       1.1     skrll   /* Propagate multiply status.  */
   1416       1.1     skrll   if (insn != -1)
   1417       1.1     skrll     {
   1418       1.1     skrll       if (is_parallel && prev_mul32_p)
   1419       1.1     skrll 	cur_mul32_p = 1;
   1420       1.1     skrll       else
   1421       1.1     skrll 	{
   1422       1.1     skrll 	  prev_mul32_p = cur_mul32_p;
   1423       1.1     skrll 	  cur_mul32_p  = (opcode->op->flags_used & FLAG_MUL32) != 0;
   1424       1.1     skrll 	}
   1425       1.1     skrll     }
   1426       1.1     skrll 
   1427       1.1     skrll   /* Propagate left_kills_right status.  */
   1428       1.1     skrll   if (insn != -1)
   1429       1.1     skrll     {
   1430       1.1     skrll       prev_left_kills_right_p = cur_left_kills_right_p;
   1431       1.1     skrll 
   1432       1.1     skrll       if (opcode->op->flags_set & FLAG_LKR)
   1433       1.1     skrll 	{
   1434       1.1     skrll 	  cur_left_kills_right_p = 1;
   1435       1.1     skrll 
   1436       1.1     skrll 	  if (strcmp (opcode->op->name, "mvtsys") == 0)
   1437       1.1     skrll 	    {
   1438       1.1     skrll 	      /* Left kills right for only mvtsys only for
   1439       1.1     skrll                  PSW/PSWH/PSWL/flags target.  */
   1440       1.1     skrll 	      if ((myops[0].X_op == O_register) &&
   1441       1.1     skrll 		  ((myops[0].X_add_number == OPERAND_CONTROL) || /* psw */
   1442       1.1     skrll 		   (myops[0].X_add_number == OPERAND_CONTROL+MAX_CONTROL_REG+2) || /* pswh */
   1443       1.1     skrll 		   (myops[0].X_add_number == OPERAND_CONTROL+MAX_CONTROL_REG+1) || /* pswl */
   1444       1.1     skrll 		   (myops[0].X_add_number == OPERAND_FLAG+0) || /* f0 */
   1445       1.1     skrll 		   (myops[0].X_add_number == OPERAND_FLAG+1) || /* f1 */
   1446       1.1     skrll 		   (myops[0].X_add_number == OPERAND_FLAG+2) || /* f2 */
   1447       1.1     skrll 		   (myops[0].X_add_number == OPERAND_FLAG+3) || /* f3 */
   1448       1.1     skrll 		   (myops[0].X_add_number == OPERAND_FLAG+4) || /* f4 */
   1449       1.1     skrll 		   (myops[0].X_add_number == OPERAND_FLAG+5) || /* f5 */
   1450       1.1     skrll 		   (myops[0].X_add_number == OPERAND_FLAG+6) || /* f6 */
   1451       1.1     skrll 		   (myops[0].X_add_number == OPERAND_FLAG+7))) /* f7 */
   1452       1.1     skrll 		{
   1453       1.1     skrll 		  cur_left_kills_right_p = 1;
   1454       1.1     skrll 		}
   1455       1.1     skrll 	      else
   1456       1.1     skrll 		{
   1457       1.1     skrll 		  /* Other mvtsys target registers don't kill right
   1458       1.1     skrll                      instruction.  */
   1459       1.1     skrll 		  cur_left_kills_right_p = 0;
   1460       1.1     skrll 		}
   1461       1.1     skrll 	    } /* mvtsys */
   1462       1.1     skrll 	}
   1463       1.1     skrll       else
   1464       1.1     skrll 	cur_left_kills_right_p = 0;
   1465       1.1     skrll     }
   1466       1.1     skrll 
   1467       1.1     skrll   return insn;
   1468       1.1     skrll }
   1469       1.1     skrll 
   1470       1.1     skrll /* Called internally to handle all alignment needs.  This takes care
   1471       1.1     skrll    of eliding calls to frag_align if'n the cached current alignment
   1472       1.1     skrll    says we've already got it, as well as taking care of the auto-aligning
   1473       1.1     skrll    labels wrt code.  */
   1474       1.1     skrll 
   1475       1.1     skrll static void
   1476       1.1     skrll d30v_align (int n, char *pfill, symbolS *label)
   1477       1.1     skrll {
   1478       1.1     skrll   /* The front end is prone to changing segments out from under us
   1479       1.1     skrll      temporarily when -g is in effect.  */
   1480       1.1     skrll   int switched_seg_p = (d30v_current_align_seg != now_seg);
   1481       1.1     skrll 
   1482       1.1     skrll   /* Do not assume that if 'd30v_current_align >= n' and
   1483       1.1     skrll      '! switched_seg_p' that it is safe to avoid performing
   1484       1.1     skrll      this alignment request.  The alignment of the current frag
   1485       1.1     skrll      can be changed under our feet, for example by a .ascii
   1486       1.1     skrll      directive in the source code.  cf testsuite/gas/d30v/reloc.s  */
   1487   1.1.1.7  christos   d30v_cleanup (false);
   1488       1.1     skrll 
   1489       1.1     skrll   if (pfill == NULL)
   1490       1.1     skrll     {
   1491       1.1     skrll       if (n > 2
   1492   1.1.1.6  christos 	  && (bfd_section_flags (now_seg) & SEC_CODE) != 0)
   1493       1.1     skrll 	{
   1494       1.1     skrll 	  static char const nop[4] = { 0x00, 0xf0, 0x00, 0x00 };
   1495       1.1     skrll 
   1496       1.1     skrll 	  /* First, make sure we're on a four-byte boundary, in case
   1497       1.1     skrll 	     someone has been putting .byte values the text section.  */
   1498       1.1     skrll 	  if (d30v_current_align < 2 || switched_seg_p)
   1499       1.1     skrll 	    frag_align (2, 0, 0);
   1500       1.1     skrll 	  frag_align_pattern (n, nop, sizeof nop, 0);
   1501       1.1     skrll 	}
   1502       1.1     skrll       else
   1503       1.1     skrll 	frag_align (n, 0, 0);
   1504       1.1     skrll     }
   1505       1.1     skrll   else
   1506       1.1     skrll     frag_align (n, *pfill, 0);
   1507       1.1     skrll 
   1508       1.1     skrll   if (!switched_seg_p)
   1509       1.1     skrll     d30v_current_align = n;
   1510       1.1     skrll 
   1511       1.1     skrll   if (label != NULL)
   1512       1.1     skrll     {
   1513       1.1     skrll       symbolS     *sym;
   1514   1.1.1.7  christos       int          label_seen = false;
   1515       1.1     skrll       struct frag *old_frag;
   1516       1.1     skrll       valueT       old_value;
   1517       1.1     skrll       valueT       new_value;
   1518       1.1     skrll 
   1519   1.1.1.2  christos       gas_assert (S_GET_SEGMENT (label) == now_seg);
   1520       1.1     skrll 
   1521       1.1     skrll       old_frag  = symbol_get_frag (label);
   1522       1.1     skrll       old_value = S_GET_VALUE (label);
   1523       1.1     skrll       new_value = (valueT) frag_now_fix ();
   1524       1.1     skrll 
   1525       1.1     skrll       /* It is possible to have more than one label at a particular
   1526       1.1     skrll 	 address, especially if debugging is enabled, so we must
   1527       1.1     skrll 	 take care to adjust all the labels at this address in this
   1528       1.1     skrll 	 fragment.  To save time we search from the end of the symbol
   1529       1.1     skrll 	 list, backwards, since the symbols we are interested in are
   1530       1.1     skrll 	 almost certainly the ones that were most recently added.
   1531       1.1     skrll 	 Also to save time we stop searching once we have seen at least
   1532       1.1     skrll 	 one matching label, and we encounter a label that is no longer
   1533       1.1     skrll 	 in the target fragment.  Note, this search is guaranteed to
   1534       1.1     skrll 	 find at least one match when sym == label, so no special case
   1535       1.1     skrll 	 code is necessary.  */
   1536       1.1     skrll       for (sym = symbol_lastP; sym != NULL; sym = symbol_previous (sym))
   1537       1.1     skrll 	{
   1538       1.1     skrll 	  if (symbol_get_frag (sym) == old_frag
   1539       1.1     skrll 	      && S_GET_VALUE (sym) == old_value)
   1540       1.1     skrll 	    {
   1541   1.1.1.7  christos 	      label_seen = true;
   1542       1.1     skrll 	      symbol_set_frag (sym, frag_now);
   1543       1.1     skrll 	      S_SET_VALUE (sym, new_value);
   1544       1.1     skrll 	    }
   1545       1.1     skrll 	  else if (label_seen && symbol_get_frag (sym) != old_frag)
   1546       1.1     skrll 	    break;
   1547       1.1     skrll 	}
   1548       1.1     skrll     }
   1549       1.1     skrll 
   1550       1.1     skrll   record_alignment (now_seg, n);
   1551       1.1     skrll }
   1552       1.1     skrll 
   1553       1.1     skrll /* This is the main entry point for the machine-dependent assembler.
   1554       1.1     skrll    STR points to a machine-dependent instruction.  This function is
   1555       1.1     skrll    supposed to emit the frags/bytes it assembles to.  For the D30V, it
   1556       1.1     skrll    mostly handles the special VLIW parsing and packing and leaves the
   1557       1.1     skrll    difficult stuff to do_assemble ().  */
   1558       1.1     skrll 
   1559       1.1     skrll static long long prev_insn = -1;
   1560       1.1     skrll static struct d30v_insn prev_opcode;
   1561       1.1     skrll static subsegT prev_subseg;
   1562       1.1     skrll static segT prev_seg = 0;
   1563       1.1     skrll 
   1564       1.1     skrll void
   1565       1.1     skrll md_assemble (char *str)
   1566       1.1     skrll {
   1567       1.1     skrll   struct d30v_insn opcode;
   1568       1.1     skrll   long long insn;
   1569       1.1     skrll   /* Execution type; parallel, etc.  */
   1570       1.1     skrll   exec_type_enum extype = EXEC_UNKNOWN;
   1571       1.1     skrll   /* Saved extype.  Used for multiline instructions.  */
   1572       1.1     skrll   static exec_type_enum etype = EXEC_UNKNOWN;
   1573       1.1     skrll   char *str2;
   1574       1.1     skrll 
   1575       1.1     skrll   if ((prev_insn != -1) && prev_seg
   1576       1.1     skrll       && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
   1577   1.1.1.7  christos     d30v_cleanup (false);
   1578       1.1     skrll 
   1579       1.1     skrll   if (d30v_current_align < 3)
   1580       1.1     skrll     d30v_align (3, NULL, d30v_last_label);
   1581       1.1     skrll   else if (d30v_current_align > 3)
   1582       1.1     skrll     d30v_current_align = 3;
   1583       1.1     skrll   d30v_last_label = NULL;
   1584       1.1     skrll 
   1585       1.1     skrll   flag_explicitly_parallel = 0;
   1586       1.1     skrll   flag_xp_state = 0;
   1587       1.1     skrll   if (etype == EXEC_UNKNOWN)
   1588       1.1     skrll     {
   1589       1.1     skrll       /* Look for the special multiple instruction separators.  */
   1590       1.1     skrll       str2 = strstr (str, "||");
   1591       1.1     skrll       if (str2)
   1592       1.1     skrll 	{
   1593       1.1     skrll 	  extype = EXEC_PARALLEL;
   1594       1.1     skrll 	  flag_xp_state = 1;
   1595       1.1     skrll 	}
   1596       1.1     skrll       else
   1597       1.1     skrll 	{
   1598       1.1     skrll 	  str2 = strstr (str, "->");
   1599       1.1     skrll 	  if (str2)
   1600       1.1     skrll 	    extype = EXEC_SEQ;
   1601       1.1     skrll 	  else
   1602       1.1     skrll 	    {
   1603       1.1     skrll 	      str2 = strstr (str, "<-");
   1604       1.1     skrll 	      if (str2)
   1605       1.1     skrll 		extype = EXEC_REVSEQ;
   1606       1.1     skrll 	    }
   1607       1.1     skrll 	}
   1608       1.1     skrll 
   1609       1.1     skrll       /* STR2 points to the separator, if one.  */
   1610       1.1     skrll       if (str2)
   1611       1.1     skrll 	{
   1612       1.1     skrll 	  *str2 = 0;
   1613       1.1     skrll 
   1614       1.1     skrll 	  /* If two instructions are present and we already have one saved,
   1615       1.1     skrll 	     then first write it out.  */
   1616   1.1.1.7  christos 	  d30v_cleanup (false);
   1617       1.1     skrll 
   1618       1.1     skrll 	  /* Assemble first instruction and save it.  */
   1619       1.1     skrll 	  prev_insn = do_assemble (str, &prev_opcode, 1, 0);
   1620       1.1     skrll 	  if (prev_insn == -1)
   1621       1.1     skrll 	    as_bad (_("Cannot assemble instruction"));
   1622       1.1     skrll 	  if (prev_opcode.form != NULL && prev_opcode.form->form >= LONG)
   1623       1.1     skrll 	    as_bad (_("First opcode is long.  Unable to mix instructions as specified."));
   1624       1.1     skrll 	  fixups = fixups->next;
   1625       1.1     skrll 	  str = str2 + 2;
   1626       1.1     skrll 	  prev_seg = now_seg;
   1627       1.1     skrll 	  prev_subseg = now_subseg;
   1628       1.1     skrll 	}
   1629       1.1     skrll     }
   1630       1.1     skrll 
   1631       1.1     skrll   insn = do_assemble (str, &opcode,
   1632       1.1     skrll 		      (extype != EXEC_UNKNOWN || etype != EXEC_UNKNOWN),
   1633       1.1     skrll 		      extype == EXEC_PARALLEL);
   1634       1.1     skrll   if (insn == -1)
   1635       1.1     skrll     {
   1636       1.1     skrll       if (extype != EXEC_UNKNOWN)
   1637       1.1     skrll 	etype = extype;
   1638       1.1     skrll       as_bad (_("Cannot assemble instruction"));
   1639       1.1     skrll       return;
   1640       1.1     skrll     }
   1641       1.1     skrll 
   1642       1.1     skrll   if (etype != EXEC_UNKNOWN)
   1643       1.1     skrll     {
   1644       1.1     skrll       extype = etype;
   1645       1.1     skrll       etype = EXEC_UNKNOWN;
   1646       1.1     skrll     }
   1647       1.1     skrll 
   1648       1.1     skrll   /* Word multiply instructions must not be followed by either a load or a
   1649       1.1     skrll      16-bit multiply instruction in the next cycle.  */
   1650       1.1     skrll   if (   (extype != EXEC_REVSEQ)
   1651       1.1     skrll       && prev_mul32_p
   1652       1.1     skrll       && (opcode.op->flags_used & (FLAG_MEM | FLAG_MUL16)))
   1653       1.1     skrll     {
   1654       1.1     skrll       /* However, load and multiply should able to be combined in a parallel
   1655       1.1     skrll 	 operation, so check for that first.  */
   1656       1.1     skrll       if (prev_insn != -1
   1657       1.1     skrll 	  && (opcode.op->flags_used & FLAG_MEM)
   1658       1.1     skrll 	  && opcode.form->form < LONG
   1659       1.1     skrll 	  && (extype == EXEC_PARALLEL || (Optimizing && extype == EXEC_UNKNOWN))
   1660       1.1     skrll 	  && parallel_ok (&prev_opcode, (long) prev_insn,
   1661       1.1     skrll 			  &opcode, (long) insn, extype)
   1662       1.1     skrll 	  && write_2_short (&prev_opcode, (long) prev_insn,
   1663       1.1     skrll 			    &opcode, (long) insn, extype, fixups) == 0)
   1664       1.1     skrll 	{
   1665       1.1     skrll 	  /* No instructions saved.  */
   1666       1.1     skrll 	  prev_insn = -1;
   1667       1.1     skrll 	  return;
   1668       1.1     skrll 	}
   1669       1.1     skrll       else
   1670       1.1     skrll 	{
   1671       1.1     skrll 	  /* Can't parallelize, flush previous instruction and emit a
   1672       1.1     skrll 	     word of NOPS, unless the previous instruction is a NOP,
   1673       1.1     skrll 	     in which case just flush it, as this will generate a word
   1674       1.1     skrll 	     of NOPs for us.  */
   1675       1.1     skrll 
   1676       1.1     skrll 	  if (prev_insn != -1 && (strcmp (prev_opcode.op->name, "nop") == 0))
   1677   1.1.1.7  christos 	    d30v_cleanup (false);
   1678       1.1     skrll 	  else
   1679       1.1     skrll 	    {
   1680       1.1     skrll 	      char *f;
   1681       1.1     skrll 
   1682       1.1     skrll 	      if (prev_insn != -1)
   1683   1.1.1.7  christos 		d30v_cleanup (true);
   1684       1.1     skrll 	      else
   1685       1.1     skrll 		{
   1686       1.1     skrll 		  f = frag_more (8);
   1687   1.1.1.2  christos 		  dwarf2_emit_insn (8);
   1688       1.1     skrll 		  d30v_number_to_chars (f, NOP2, 8);
   1689       1.1     skrll 
   1690       1.1     skrll 		  if (warn_nops == NOP_ALL || warn_nops == NOP_MULTIPLY)
   1691       1.1     skrll 		    {
   1692       1.1     skrll 		      if (opcode.op->flags_used & FLAG_MEM)
   1693       1.1     skrll 			as_warn (_("word of NOPs added between word multiply and load"));
   1694       1.1     skrll 		      else
   1695       1.1     skrll 			as_warn (_("word of NOPs added between word multiply and 16-bit multiply"));
   1696       1.1     skrll 		    }
   1697       1.1     skrll 		}
   1698       1.1     skrll 	    }
   1699       1.1     skrll 
   1700       1.1     skrll 	  extype = EXEC_UNKNOWN;
   1701       1.1     skrll 	}
   1702       1.1     skrll     }
   1703       1.1     skrll   else if (   (extype == EXEC_REVSEQ)
   1704       1.1     skrll 	   && cur_mul32_p
   1705       1.1     skrll 	   && (prev_opcode.op->flags_used & (FLAG_MEM | FLAG_MUL16)))
   1706       1.1     skrll     {
   1707       1.1     skrll       /* Can't parallelize, flush current instruction and add a
   1708       1.1     skrll          sequential NOP.  */
   1709   1.1.1.7  christos       write_1_short (&opcode, (long) insn, fixups->next->next, true);
   1710       1.1     skrll 
   1711       1.1     skrll       /* Make the previous instruction the current one.  */
   1712       1.1     skrll       extype = EXEC_UNKNOWN;
   1713       1.1     skrll       insn = prev_insn;
   1714       1.1     skrll       now_seg = prev_seg;
   1715       1.1     skrll       now_subseg = prev_subseg;
   1716       1.1     skrll       prev_insn = -1;
   1717       1.1     skrll       cur_mul32_p = prev_mul32_p;
   1718       1.1     skrll       prev_mul32_p = 0;
   1719       1.1     skrll       memcpy (&opcode, &prev_opcode, sizeof (prev_opcode));
   1720       1.1     skrll     }
   1721       1.1     skrll 
   1722       1.1     skrll   /* If this is a long instruction, write it and any previous short
   1723       1.1     skrll      instruction.  */
   1724       1.1     skrll   if (opcode.form->form >= LONG)
   1725       1.1     skrll     {
   1726       1.1     skrll       if (extype != EXEC_UNKNOWN)
   1727       1.1     skrll 	as_bad (_("Instruction uses long version, so it cannot be mixed as specified"));
   1728   1.1.1.7  christos       d30v_cleanup (false);
   1729       1.1     skrll       write_long (&opcode, insn, fixups);
   1730       1.1     skrll       prev_insn = -1;
   1731       1.1     skrll     }
   1732       1.1     skrll   else if ((prev_insn != -1)
   1733       1.1     skrll 	   && (write_2_short
   1734       1.1     skrll 	       (&prev_opcode, (long) prev_insn, &opcode,
   1735       1.1     skrll 		(long) insn, extype, fixups) == 0))
   1736       1.1     skrll     {
   1737       1.1     skrll       /* No instructions saved.  */
   1738       1.1     skrll       prev_insn = -1;
   1739       1.1     skrll     }
   1740       1.1     skrll   else
   1741       1.1     skrll     {
   1742       1.1     skrll       if (extype != EXEC_UNKNOWN)
   1743       1.1     skrll 	as_bad (_("Unable to mix instructions as specified"));
   1744       1.1     skrll 
   1745       1.1     skrll       /* Save off last instruction so it may be packed on next pass.  */
   1746       1.1     skrll       memcpy (&prev_opcode, &opcode, sizeof (prev_opcode));
   1747       1.1     skrll       prev_insn = insn;
   1748       1.1     skrll       prev_seg = now_seg;
   1749       1.1     skrll       prev_subseg = now_subseg;
   1750       1.1     skrll       fixups = fixups->next;
   1751       1.1     skrll       prev_mul32_p = cur_mul32_p;
   1752       1.1     skrll     }
   1753       1.1     skrll }
   1754       1.1     skrll 
   1755       1.1     skrll /* If while processing a fixup, a reloc really needs to be created,
   1756       1.1     skrll    then it is done here.  */
   1757       1.1     skrll 
   1758       1.1     skrll arelent *
   1759       1.1     skrll tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
   1760       1.1     skrll {
   1761       1.1     skrll   arelent *reloc;
   1762   1.1.1.9  christos   reloc = notes_alloc (sizeof (arelent));
   1763   1.1.1.9  christos   reloc->sym_ptr_ptr = notes_alloc (sizeof (asymbol *));
   1764       1.1     skrll   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
   1765       1.1     skrll   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
   1766       1.1     skrll   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
   1767       1.1     skrll   if (reloc->howto == NULL)
   1768       1.1     skrll     {
   1769       1.1     skrll       as_bad_where (fixp->fx_file, fixp->fx_line,
   1770       1.1     skrll 		    _("reloc %d not supported by object file format"),
   1771       1.1     skrll 		    (int) fixp->fx_r_type);
   1772       1.1     skrll       return NULL;
   1773       1.1     skrll     }
   1774       1.1     skrll 
   1775       1.1     skrll   reloc->addend = 0;
   1776       1.1     skrll   return reloc;
   1777       1.1     skrll }
   1778       1.1     skrll 
   1779       1.1     skrll int
   1780       1.1     skrll md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
   1781       1.1     skrll 			       asection *seg ATTRIBUTE_UNUSED)
   1782       1.1     skrll {
   1783       1.1     skrll   abort ();
   1784       1.1     skrll   return 0;
   1785       1.1     skrll }
   1786       1.1     skrll 
   1787       1.1     skrll long
   1788       1.1     skrll md_pcrel_from_section (fixS *fixp, segT sec)
   1789       1.1     skrll {
   1790   1.1.1.9  christos   if (fixp->fx_addsy != NULL
   1791       1.1     skrll       && (!S_IS_DEFINED (fixp->fx_addsy)
   1792       1.1     skrll 	  || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
   1793       1.1     skrll     return 0;
   1794       1.1     skrll   return fixp->fx_frag->fr_address + fixp->fx_where;
   1795       1.1     skrll }
   1796       1.1     skrll 
   1797       1.1     skrll /* Called after the assembler has finished parsing the input file or
   1798       1.1     skrll    after a label is defined.  Because the D30V assembler sometimes
   1799       1.1     skrll    saves short instructions to see if it can package them with the
   1800       1.1     skrll    next instruction, there may be a short instruction that still needs
   1801       1.1     skrll    written.  */
   1802       1.1     skrll 
   1803       1.1     skrll int
   1804       1.1     skrll d30v_cleanup (int use_sequential)
   1805       1.1     skrll {
   1806       1.1     skrll   segT seg;
   1807       1.1     skrll   subsegT subseg;
   1808       1.1     skrll 
   1809       1.1     skrll   if (prev_insn != -1)
   1810       1.1     skrll     {
   1811       1.1     skrll       seg = now_seg;
   1812       1.1     skrll       subseg = now_subseg;
   1813       1.1     skrll       subseg_set (prev_seg, prev_subseg);
   1814       1.1     skrll       write_1_short (&prev_opcode, (long) prev_insn, fixups->next,
   1815       1.1     skrll 		     use_sequential);
   1816       1.1     skrll       subseg_set (seg, subseg);
   1817       1.1     skrll       prev_insn = -1;
   1818       1.1     skrll       if (use_sequential)
   1819   1.1.1.7  christos 	prev_mul32_p = false;
   1820       1.1     skrll     }
   1821       1.1     skrll 
   1822       1.1     skrll   return 1;
   1823       1.1     skrll }
   1824       1.1     skrll 
   1825       1.1     skrll /* This function is called at the start of every line.  It checks to
   1826       1.1     skrll    see if the first character is a '.', which indicates the start of a
   1827       1.1     skrll    pseudo-op.  If it is, then write out any unwritten instructions.  */
   1828       1.1     skrll 
   1829       1.1     skrll void
   1830       1.1     skrll d30v_start_line (void)
   1831       1.1     skrll {
   1832       1.1     skrll   char *c = input_line_pointer;
   1833       1.1     skrll 
   1834   1.1.1.9  christos   while (is_whitespace (*c))
   1835       1.1     skrll     c++;
   1836       1.1     skrll 
   1837       1.1     skrll   if (*c == '.')
   1838   1.1.1.7  christos     d30v_cleanup (false);
   1839       1.1     skrll }
   1840       1.1     skrll 
   1841       1.1     skrll static void
   1842   1.1.1.4  christos check_size (long value, int bits, const char *file, int line)
   1843       1.1     skrll {
   1844       1.1     skrll   int tmp, max;
   1845       1.1     skrll 
   1846       1.1     skrll   if (value < 0)
   1847       1.1     skrll     tmp = ~value;
   1848       1.1     skrll   else
   1849       1.1     skrll     tmp = value;
   1850       1.1     skrll 
   1851       1.1     skrll   max = (1 << (bits - 1)) - 1;
   1852       1.1     skrll 
   1853       1.1     skrll   if (tmp > max)
   1854       1.1     skrll     as_bad_where (file, line, _("value too large to fit in %d bits"), bits);
   1855       1.1     skrll }
   1856       1.1     skrll 
   1857       1.1     skrll /* d30v_frob_label() is called when after a label is recognized.  */
   1858       1.1     skrll 
   1859       1.1     skrll void
   1860       1.1     skrll d30v_frob_label (symbolS *lab)
   1861       1.1     skrll {
   1862       1.1     skrll   /* Emit any pending instructions.  */
   1863   1.1.1.7  christos   d30v_cleanup (false);
   1864       1.1     skrll 
   1865       1.1     skrll   /* Update the label's address with the current output pointer.  */
   1866       1.1     skrll   symbol_set_frag (lab, frag_now);
   1867       1.1     skrll   S_SET_VALUE (lab, (valueT) frag_now_fix ());
   1868       1.1     skrll 
   1869       1.1     skrll   /* Record this label for future adjustment after we find out what
   1870       1.1     skrll      kind of data it references, and the required alignment therewith.  */
   1871       1.1     skrll   d30v_last_label = lab;
   1872   1.1.1.2  christos 
   1873   1.1.1.2  christos   dwarf2_emit_label (lab);
   1874       1.1     skrll }
   1875       1.1     skrll 
   1876       1.1     skrll /* Hook into cons for capturing alignment changes.  */
   1877       1.1     skrll 
   1878       1.1     skrll void
   1879       1.1     skrll d30v_cons_align (int size)
   1880       1.1     skrll {
   1881       1.1     skrll   int log_size;
   1882       1.1     skrll 
   1883   1.1.1.2  christos   /* Don't specially align anything in debug sections.  */
   1884   1.1.1.2  christos   if ((now_seg->flags & SEC_ALLOC) == 0
   1885   1.1.1.2  christos       || strcmp (now_seg->name, ".eh_frame") == 0)
   1886   1.1.1.2  christos     return;
   1887   1.1.1.2  christos 
   1888       1.1     skrll   log_size = 0;
   1889       1.1     skrll   while ((size >>= 1) != 0)
   1890       1.1     skrll     ++log_size;
   1891       1.1     skrll 
   1892       1.1     skrll   if (d30v_current_align < log_size)
   1893   1.1.1.9  christos     d30v_align (log_size, NULL, NULL);
   1894       1.1     skrll   else if (d30v_current_align > log_size)
   1895       1.1     skrll     d30v_current_align = log_size;
   1896       1.1     skrll   d30v_last_label = NULL;
   1897       1.1     skrll }
   1898       1.1     skrll 
   1899       1.1     skrll void
   1900       1.1     skrll md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
   1901       1.1     skrll {
   1902       1.1     skrll   char *where;
   1903       1.1     skrll   unsigned long insn, insn2;
   1904       1.1     skrll   long value = *valP;
   1905       1.1     skrll 
   1906   1.1.1.9  christos   if (fixP->fx_addsy == NULL)
   1907       1.1     skrll     fixP->fx_done = 1;
   1908       1.1     skrll 
   1909       1.1     skrll   /* We don't support subtracting a symbol.  */
   1910   1.1.1.9  christos   if (fixP->fx_subsy != NULL)
   1911   1.1.1.7  christos     as_bad_subtract (fixP);
   1912       1.1     skrll 
   1913       1.1     skrll   /* Fetch the instruction, insert the fully resolved operand
   1914       1.1     skrll      value, and stuff the instruction back again.  */
   1915       1.1     skrll   where = fixP->fx_frag->fr_literal + fixP->fx_where;
   1916   1.1.1.9  christos   insn = bfd_getb32 (where);
   1917       1.1     skrll 
   1918       1.1     skrll   switch (fixP->fx_r_type)
   1919       1.1     skrll     {
   1920   1.1.1.6  christos     case BFD_RELOC_8:
   1921   1.1.1.9  christos       *where = value;
   1922       1.1     skrll       break;
   1923       1.1     skrll 
   1924   1.1.1.6  christos     case BFD_RELOC_16:
   1925   1.1.1.9  christos       bfd_putb16 (value, where);
   1926       1.1     skrll       break;
   1927       1.1     skrll 
   1928   1.1.1.6  christos     case BFD_RELOC_64:
   1929   1.1.1.9  christos       bfd_putb32 (value, where);
   1930   1.1.1.9  christos       bfd_putb32 (0, where + 4);
   1931       1.1     skrll       break;
   1932       1.1     skrll 
   1933       1.1     skrll     case BFD_RELOC_D30V_6:
   1934       1.1     skrll       check_size (value, 6, fixP->fx_file, fixP->fx_line);
   1935       1.1     skrll       insn |= value & 0x3F;
   1936   1.1.1.9  christos       bfd_putb32 (insn, where);
   1937       1.1     skrll       break;
   1938       1.1     skrll 
   1939       1.1     skrll     case BFD_RELOC_D30V_9_PCREL:
   1940       1.1     skrll       if (fixP->fx_where & 0x7)
   1941       1.1     skrll 	{
   1942       1.1     skrll 	  if (fixP->fx_done)
   1943       1.1     skrll 	    value += 4;
   1944       1.1     skrll 	  else
   1945       1.1     skrll 	    fixP->fx_r_type = BFD_RELOC_D30V_9_PCREL_R;
   1946       1.1     skrll 	}
   1947       1.1     skrll       check_size (value, 9, fixP->fx_file, fixP->fx_line);
   1948       1.1     skrll       insn |= ((value >> 3) & 0x3F) << 12;
   1949   1.1.1.9  christos       bfd_putb32 (insn, where);
   1950       1.1     skrll       break;
   1951       1.1     skrll 
   1952       1.1     skrll     case BFD_RELOC_D30V_15:
   1953       1.1     skrll       check_size (value, 15, fixP->fx_file, fixP->fx_line);
   1954       1.1     skrll       insn |= (value >> 3) & 0xFFF;
   1955   1.1.1.9  christos       bfd_putb32 (insn, where);
   1956       1.1     skrll       break;
   1957       1.1     skrll 
   1958       1.1     skrll     case BFD_RELOC_D30V_15_PCREL:
   1959       1.1     skrll       if (fixP->fx_where & 0x7)
   1960       1.1     skrll 	{
   1961       1.1     skrll 	  if (fixP->fx_done)
   1962       1.1     skrll 	    value += 4;
   1963       1.1     skrll 	  else
   1964       1.1     skrll 	    fixP->fx_r_type = BFD_RELOC_D30V_15_PCREL_R;
   1965       1.1     skrll 	}
   1966       1.1     skrll       check_size (value, 15, fixP->fx_file, fixP->fx_line);
   1967       1.1     skrll       insn |= (value >> 3) & 0xFFF;
   1968   1.1.1.9  christos       bfd_putb32 (insn, where);
   1969       1.1     skrll       break;
   1970       1.1     skrll 
   1971       1.1     skrll     case BFD_RELOC_D30V_21:
   1972       1.1     skrll       check_size (value, 21, fixP->fx_file, fixP->fx_line);
   1973       1.1     skrll       insn |= (value >> 3) & 0x3FFFF;
   1974   1.1.1.9  christos       bfd_putb32 (insn, where);
   1975       1.1     skrll       break;
   1976       1.1     skrll 
   1977       1.1     skrll     case BFD_RELOC_D30V_21_PCREL:
   1978       1.1     skrll       if (fixP->fx_where & 0x7)
   1979       1.1     skrll 	{
   1980       1.1     skrll 	  if (fixP->fx_done)
   1981       1.1     skrll 	    value += 4;
   1982       1.1     skrll 	  else
   1983       1.1     skrll 	    fixP->fx_r_type = BFD_RELOC_D30V_21_PCREL_R;
   1984       1.1     skrll 	}
   1985       1.1     skrll       check_size (value, 21, fixP->fx_file, fixP->fx_line);
   1986       1.1     skrll       insn |= (value >> 3) & 0x3FFFF;
   1987   1.1.1.9  christos       bfd_putb32 (insn, where);
   1988       1.1     skrll       break;
   1989       1.1     skrll 
   1990       1.1     skrll     case BFD_RELOC_D30V_32:
   1991   1.1.1.9  christos       insn2 = bfd_getb32 (where + 4);
   1992       1.1     skrll       insn |= (value >> 26) & 0x3F;		/* Top 6 bits.  */
   1993       1.1     skrll       insn2 |= ((value & 0x03FC0000) << 2);	/* Next 8 bits.  */
   1994       1.1     skrll       insn2 |= value & 0x0003FFFF;		/* Bottom 18 bits.  */
   1995   1.1.1.9  christos       bfd_putb32 (insn, where);
   1996   1.1.1.9  christos       bfd_putb32 (insn2, where + 4);
   1997       1.1     skrll       break;
   1998       1.1     skrll 
   1999       1.1     skrll     case BFD_RELOC_D30V_32_PCREL:
   2000   1.1.1.9  christos       insn2 = bfd_getb32 (where + 4);
   2001       1.1     skrll       insn |= (value >> 26) & 0x3F;		/* Top 6 bits.  */
   2002       1.1     skrll       insn2 |= ((value & 0x03FC0000) << 2);	/* Next 8 bits.  */
   2003       1.1     skrll       insn2 |= value & 0x0003FFFF;		/* Bottom 18 bits.  */
   2004   1.1.1.9  christos       bfd_putb32 (insn, where);
   2005   1.1.1.9  christos       bfd_putb32 (insn2, where + 4);
   2006       1.1     skrll       break;
   2007       1.1     skrll 
   2008       1.1     skrll     case BFD_RELOC_32:
   2009   1.1.1.9  christos       bfd_putb32 (value, where);
   2010       1.1     skrll       break;
   2011       1.1     skrll 
   2012       1.1     skrll     default:
   2013       1.1     skrll       as_bad (_("line %d: unknown relocation type: 0x%x"),
   2014       1.1     skrll 	      fixP->fx_line, fixP->fx_r_type);
   2015       1.1     skrll     }
   2016       1.1     skrll }
   2017       1.1     skrll 
   2018       1.1     skrll /* Handle the .align pseudo-op.  This aligns to a power of two.  We
   2019       1.1     skrll    hook here to latch the current alignment.  */
   2020       1.1     skrll 
   2021       1.1     skrll static void
   2022       1.1     skrll s_d30v_align (int ignore ATTRIBUTE_UNUSED)
   2023       1.1     skrll {
   2024       1.1     skrll   int align;
   2025       1.1     skrll   char fill, *pfill = NULL;
   2026       1.1     skrll   long max_alignment = 15;
   2027       1.1     skrll 
   2028       1.1     skrll   align = get_absolute_expression ();
   2029       1.1     skrll   if (align > max_alignment)
   2030       1.1     skrll     {
   2031       1.1     skrll       align = max_alignment;
   2032       1.1     skrll       as_warn (_("Alignment too large: %d assumed"), align);
   2033       1.1     skrll     }
   2034       1.1     skrll   else if (align < 0)
   2035       1.1     skrll     {
   2036       1.1     skrll       as_warn (_("Alignment negative: 0 assumed"));
   2037       1.1     skrll       align = 0;
   2038       1.1     skrll     }
   2039       1.1     skrll 
   2040       1.1     skrll   if (*input_line_pointer == ',')
   2041       1.1     skrll     {
   2042       1.1     skrll       input_line_pointer++;
   2043       1.1     skrll       fill = get_absolute_expression ();
   2044       1.1     skrll       pfill = &fill;
   2045       1.1     skrll     }
   2046       1.1     skrll 
   2047       1.1     skrll   d30v_last_label = NULL;
   2048       1.1     skrll   d30v_align (align, pfill, NULL);
   2049       1.1     skrll 
   2050       1.1     skrll   demand_empty_rest_of_line ();
   2051       1.1     skrll }
   2052       1.1     skrll 
   2053       1.1     skrll /* Handle the .text pseudo-op.  This is like the usual one, but it
   2054       1.1     skrll    clears the saved last label and resets known alignment.  */
   2055       1.1     skrll 
   2056       1.1     skrll static void
   2057       1.1     skrll s_d30v_text (int i)
   2058       1.1     skrll 
   2059       1.1     skrll {
   2060   1.1.1.8  christos   obj_elf_text (i);
   2061       1.1     skrll   d30v_last_label = NULL;
   2062       1.1     skrll   d30v_current_align = 0;
   2063       1.1     skrll   d30v_current_align_seg = now_seg;
   2064       1.1     skrll }
   2065       1.1     skrll 
   2066       1.1     skrll /* Handle the .data pseudo-op.  This is like the usual one, but it
   2067       1.1     skrll    clears the saved last label and resets known alignment.  */
   2068       1.1     skrll 
   2069       1.1     skrll static void
   2070       1.1     skrll s_d30v_data (int i)
   2071       1.1     skrll {
   2072   1.1.1.8  christos   obj_elf_data (i);
   2073       1.1     skrll   d30v_last_label = NULL;
   2074       1.1     skrll   d30v_current_align = 0;
   2075       1.1     skrll   d30v_current_align_seg = now_seg;
   2076       1.1     skrll }
   2077       1.1     skrll 
   2078       1.1     skrll /* Handle the .section pseudo-op.  This is like the usual one, but it
   2079       1.1     skrll    clears the saved last label and resets known alignment.  */
   2080       1.1     skrll 
   2081       1.1     skrll static void
   2082       1.1     skrll s_d30v_section (int ignore)
   2083       1.1     skrll {
   2084       1.1     skrll   obj_elf_section (ignore);
   2085       1.1     skrll   d30v_last_label = NULL;
   2086       1.1     skrll   d30v_current_align = 0;
   2087       1.1     skrll   d30v_current_align_seg = now_seg;
   2088       1.1     skrll }
   2089       1.1     skrll 
   2090       1.1     skrll /* The target specific pseudo-ops which we support.  */
   2091       1.1     skrll const pseudo_typeS md_pseudo_table[] =
   2092       1.1     skrll {
   2093       1.1     skrll   { "word", cons, 4 },
   2094       1.1     skrll   { "hword", cons, 2 },
   2095       1.1     skrll   { "align", s_d30v_align, 0 },
   2096       1.1     skrll   { "text", s_d30v_text, 0 },
   2097       1.1     skrll   { "data", s_d30v_data, 0 },
   2098       1.1     skrll   { "section", s_d30v_section, 0 },
   2099       1.1     skrll   { "section.s", s_d30v_section, 0 },
   2100       1.1     skrll   { "sect", s_d30v_section, 0 },
   2101       1.1     skrll   { "sect.s", s_d30v_section, 0 },
   2102       1.1     skrll   { NULL, NULL, 0 }
   2103       1.1     skrll };
   2104