Home | History | Annotate | Download | only in config
      1 /* tc-mep.c -- Assembler for the Toshiba Media Processor.
      2    Copyright (C) 2001-2014 Free Software Foundation, Inc.
      3 
      4    This file is part of GAS, the GNU Assembler.
      5 
      6    GAS is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3, or (at your option)
      9    any later version.
     10 
     11    GAS is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with GAS; see the file COPYING.  If not, write to
     18    the Free Software Foundation, 51 Franklin Street, Fifth Floor,
     19    Boston, MA 02110-1301, USA.  */
     20 
     21 #include "as.h"
     22 #include <stdio.h>
     23 #include "dwarf2dbg.h"
     24 #include "subsegs.h"
     25 #include "symcat.h"
     26 #include "opcodes/mep-desc.h"
     27 #include "opcodes/mep-opc.h"
     28 #include "cgen.h"
     29 #include "elf/common.h"
     30 #include "elf/mep.h"
     31 #include "libbfd.h"
     32 #include "xregex.h"
     33 
     34 /* Structure to hold all of the different components describing
     35    an individual instruction.  */
     36 typedef struct
     37 {
     38   const CGEN_INSN *	insn;
     39   const CGEN_INSN *	orig_insn;
     40   CGEN_FIELDS		fields;
     41 #if CGEN_INT_INSN_P
     42   CGEN_INSN_INT         buffer [1];
     43 #define INSN_VALUE(buf) (*(buf))
     44 #else
     45   unsigned char         buffer [CGEN_MAX_INSN_SIZE];
     46 #define INSN_VALUE(buf) (buf)
     47 #endif
     48   char *		addr;
     49   fragS *		frag;
     50   int                   num_fixups;
     51   fixS *                fixups [GAS_CGEN_MAX_FIXUPS];
     52   int                   indices [MAX_OPERAND_INSTANCES];
     53 } mep_insn;
     54 
     55 static int mode = CORE; /* Start in core mode. */
     56 static int pluspresent = 0;
     57 static int allow_disabled_registers = 0;
     58 static int library_flag = 0;
     59 static int mep_cop = EF_MEP_COP_NONE;
     60 
     61 /* We're going to need to store all of the instructions along with
     62    their fixups so that we can parallelization grouping rules. */
     63 
     64 static mep_insn saved_insns[MAX_SAVED_FIXUP_CHAINS];
     65 static int num_insns_saved = 0;
     66 
     67 const char comment_chars[]        = "#";
     68 const char line_comment_chars[]   = ";#";
     69 const char line_separator_chars[] = ";";
     70 const char EXP_CHARS[]            = "eE";
     71 const char FLT_CHARS[]            = "dD";
     72 
     73 static void mep_switch_to_vliw_mode (int);
     74 static void mep_switch_to_core_mode (int);
     75 static void mep_s_vtext (int);
     76 static void mep_noregerr (int);
     77 
     78 /* The target specific pseudo-ops which we support.  */
     79 const pseudo_typeS md_pseudo_table[] =
     80 {
     81   { "word",	cons,	                        4 },
     82   { "vliw", 	mep_switch_to_vliw_mode,	0 },
     83   { "core", 	mep_switch_to_core_mode,	0 },
     84   { "vtext", 	mep_s_vtext,             	0 },
     85   { "noregerr",	mep_noregerr,	             	0 },
     86   { NULL, 	NULL,		        	0 }
     87 };
     88 
     89 /* Relocations against symbols are done in two
     90    parts, with a HI relocation and a LO relocation.  Each relocation
     91    has only 16 bits of space to store an addend.  This means that in
     92    order for the linker to handle carries correctly, it must be able
     93    to locate both the HI and the LO relocation.  This means that the
     94    relocations must appear in order in the relocation table.
     95 
     96    In order to implement this, we keep track of each unmatched HI
     97    relocation.  We then sort them so that they immediately precede the
     98    corresponding LO relocation. */
     99 
    100 struct mep_hi_fixup
    101 {
    102   struct mep_hi_fixup * next;	/* Next HI fixup.  */
    103   fixS * fixp;			/* This fixup.  */
    104   segT seg;			/* The section this fixup is in.  */
    105 };
    106 
    107 /* The list of unmatched HI relocs.  */
    108 static struct mep_hi_fixup * mep_hi_fixup_list;
    109 
    110 
    111 #define OPTION_EB		(OPTION_MD_BASE + 0)
    113 #define OPTION_EL		(OPTION_MD_BASE + 1)
    114 #define OPTION_CONFIG		(OPTION_MD_BASE + 2)
    115 #define OPTION_AVERAGE		(OPTION_MD_BASE + 3)
    116 #define OPTION_NOAVERAGE	(OPTION_MD_BASE + 4)
    117 #define OPTION_MULT		(OPTION_MD_BASE + 5)
    118 #define OPTION_NOMULT		(OPTION_MD_BASE + 6)
    119 #define OPTION_DIV		(OPTION_MD_BASE + 7)
    120 #define OPTION_NODIV		(OPTION_MD_BASE + 8)
    121 #define OPTION_BITOPS		(OPTION_MD_BASE + 9)
    122 #define OPTION_NOBITOPS		(OPTION_MD_BASE + 10)
    123 #define OPTION_LEADZ		(OPTION_MD_BASE + 11)
    124 #define OPTION_NOLEADZ		(OPTION_MD_BASE + 12)
    125 #define OPTION_ABSDIFF		(OPTION_MD_BASE + 13)
    126 #define OPTION_NOABSDIFF	(OPTION_MD_BASE + 14)
    127 #define OPTION_MINMAX		(OPTION_MD_BASE + 15)
    128 #define OPTION_NOMINMAX		(OPTION_MD_BASE + 16)
    129 #define OPTION_CLIP		(OPTION_MD_BASE + 17)
    130 #define OPTION_NOCLIP		(OPTION_MD_BASE + 18)
    131 #define OPTION_SATUR		(OPTION_MD_BASE + 19)
    132 #define OPTION_NOSATUR		(OPTION_MD_BASE + 20)
    133 #define OPTION_COP32		(OPTION_MD_BASE + 21)
    134 #define OPTION_REPEAT		(OPTION_MD_BASE + 25)
    135 #define OPTION_NOREPEAT		(OPTION_MD_BASE + 26)
    136 #define OPTION_DEBUG		(OPTION_MD_BASE + 27)
    137 #define OPTION_NODEBUG		(OPTION_MD_BASE + 28)
    138 #define OPTION_UCI		(OPTION_MD_BASE + 29)
    139 #define OPTION_NOUCI		(OPTION_MD_BASE + 30)
    140 #define OPTION_DSP		(OPTION_MD_BASE + 31)
    141 #define OPTION_NODSP		(OPTION_MD_BASE + 32)
    142 #define OPTION_LIBRARY		(OPTION_MD_BASE + 33)
    143 
    144 struct option md_longopts[] = {
    145   { "EB",          no_argument, NULL, OPTION_EB},
    146   { "EL",          no_argument, NULL, OPTION_EL},
    147   { "mconfig",     required_argument, NULL, OPTION_CONFIG},
    148   { "maverage",    no_argument, NULL, OPTION_AVERAGE},
    149   { "mno-average", no_argument, NULL, OPTION_NOAVERAGE},
    150   { "mmult",       no_argument, NULL, OPTION_MULT},
    151   { "mno-mult",    no_argument, NULL, OPTION_NOMULT},
    152   { "mdiv",        no_argument, NULL, OPTION_DIV},
    153   { "mno-div",     no_argument, NULL, OPTION_NODIV},
    154   { "mbitops",     no_argument, NULL, OPTION_BITOPS},
    155   { "mno-bitops",  no_argument, NULL, OPTION_NOBITOPS},
    156   { "mleadz",      no_argument, NULL, OPTION_LEADZ},
    157   { "mno-leadz",   no_argument, NULL, OPTION_NOLEADZ},
    158   { "mabsdiff",    no_argument, NULL, OPTION_ABSDIFF},
    159   { "mno-absdiff", no_argument, NULL, OPTION_NOABSDIFF},
    160   { "mminmax",     no_argument, NULL, OPTION_MINMAX},
    161   { "mno-minmax",  no_argument, NULL, OPTION_NOMINMAX},
    162   { "mclip",       no_argument, NULL, OPTION_CLIP},
    163   { "mno-clip",    no_argument, NULL, OPTION_NOCLIP},
    164   { "msatur",      no_argument, NULL, OPTION_SATUR},
    165   { "mno-satur",   no_argument, NULL, OPTION_NOSATUR},
    166   { "mcop32",	   no_argument, NULL, OPTION_COP32},
    167   { "mdebug",      no_argument, NULL, OPTION_DEBUG},
    168   { "mno-debug",   no_argument, NULL, OPTION_NODEBUG},
    169   { "muci",        no_argument, NULL, OPTION_UCI},
    170   { "mno-uci",     no_argument, NULL, OPTION_NOUCI},
    171   { "mdsp",        no_argument, NULL, OPTION_DSP},
    172   { "mno-dsp",     no_argument, NULL, OPTION_NODSP},
    173   { "mlibrary",    no_argument, NULL, OPTION_LIBRARY},
    174   { NULL, 0, NULL, 0 } };
    175 size_t md_longopts_size = sizeof (md_longopts);
    176 
    177 /* Options which default to on/off together.  See the comment where
    178    this is used for details.  Note that CP and CP64 are not in this
    179    list because disabling those overrides the -mivc2 option.  */
    180 #define OPTION_MASK \
    181 	( (1 << CGEN_INSN_OPTIONAL_BIT_INSN) \
    182 	| (1 << CGEN_INSN_OPTIONAL_MUL_INSN) \
    183 	| (1 << CGEN_INSN_OPTIONAL_DIV_INSN) \
    184 	| (1 << CGEN_INSN_OPTIONAL_DEBUG_INSN) \
    185 	| (1 << CGEN_INSN_OPTIONAL_LDZ_INSN) \
    186 	| (1 << CGEN_INSN_OPTIONAL_ABS_INSN) \
    187 	| (1 << CGEN_INSN_OPTIONAL_AVE_INSN) \
    188 	| (1 << CGEN_INSN_OPTIONAL_MINMAX_INSN) \
    189 	| (1 << CGEN_INSN_OPTIONAL_CLIP_INSN) \
    190 	| (1 << CGEN_INSN_OPTIONAL_SAT_INSN) \
    191 	| (1 << CGEN_INSN_OPTIONAL_UCI_INSN) \
    192 	| (1 << CGEN_INSN_OPTIONAL_DSP_INSN) )
    193 
    194 const char * md_shortopts = "";
    195 static int optbits = 0;
    196 static int optbitset = 0;
    197 
    198 int
    199 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
    200 {
    201   int i, idx;
    202   switch (c)
    203     {
    204     case OPTION_EB:
    205       target_big_endian = 1;
    206       break;
    207     case OPTION_EL:
    208       target_big_endian = 0;
    209       break;
    210     case OPTION_CONFIG:
    211       idx = 0;
    212       for (i=1; mep_config_map[i].name; i++)
    213 	if (strcmp (mep_config_map[i].name, arg) == 0)
    214 	  {
    215 	    idx = i;
    216 	    break;
    217 	  }
    218       if (!idx)
    219 	{
    220 	  fprintf (stderr, "Error: unknown configuration %s\n", arg);
    221 	  return 0;
    222 	}
    223       mep_config_index = idx;
    224       target_big_endian = mep_config_map[idx].big_endian;
    225       break;
    226     case OPTION_AVERAGE:
    227       optbits |= 1 << CGEN_INSN_OPTIONAL_AVE_INSN;
    228       optbitset |= 1 << CGEN_INSN_OPTIONAL_AVE_INSN;
    229       break;
    230     case OPTION_NOAVERAGE:
    231       optbits &= ~(1 << CGEN_INSN_OPTIONAL_AVE_INSN);
    232       optbitset |= 1 << CGEN_INSN_OPTIONAL_AVE_INSN;
    233       break;
    234     case OPTION_MULT:
    235       optbits |= 1 << CGEN_INSN_OPTIONAL_MUL_INSN;
    236       optbitset |= 1 << CGEN_INSN_OPTIONAL_MUL_INSN;
    237       break;
    238     case OPTION_NOMULT:
    239       optbits &= ~(1 << CGEN_INSN_OPTIONAL_MUL_INSN);
    240       optbitset |= 1 << CGEN_INSN_OPTIONAL_MUL_INSN;
    241       break;
    242     case OPTION_DIV:
    243       optbits |= 1 << CGEN_INSN_OPTIONAL_DIV_INSN;
    244       optbitset |= 1 << CGEN_INSN_OPTIONAL_DIV_INSN;
    245       break;
    246     case OPTION_NODIV:
    247       optbits &= ~(1 << CGEN_INSN_OPTIONAL_DIV_INSN);
    248       optbitset |= 1 << CGEN_INSN_OPTIONAL_DIV_INSN;
    249       break;
    250     case OPTION_BITOPS:
    251       optbits |= 1 << CGEN_INSN_OPTIONAL_BIT_INSN;
    252       optbitset |= 1 << CGEN_INSN_OPTIONAL_BIT_INSN;
    253       break;
    254     case OPTION_NOBITOPS:
    255       optbits &= ~(1 << CGEN_INSN_OPTIONAL_BIT_INSN);
    256       optbitset |= 1 << CGEN_INSN_OPTIONAL_BIT_INSN;
    257       break;
    258     case OPTION_LEADZ:
    259       optbits |= 1 << CGEN_INSN_OPTIONAL_LDZ_INSN;
    260       optbitset |= 1 << CGEN_INSN_OPTIONAL_LDZ_INSN;
    261       break;
    262     case OPTION_NOLEADZ:
    263       optbits &= ~(1 << CGEN_INSN_OPTIONAL_LDZ_INSN);
    264       optbitset |= 1 << CGEN_INSN_OPTIONAL_LDZ_INSN;
    265       break;
    266     case OPTION_ABSDIFF:
    267       optbits |= 1 << CGEN_INSN_OPTIONAL_ABS_INSN;
    268       optbitset |= 1 << CGEN_INSN_OPTIONAL_ABS_INSN;
    269       break;
    270     case OPTION_NOABSDIFF:
    271       optbits &= ~(1 << CGEN_INSN_OPTIONAL_ABS_INSN);
    272       optbitset |= 1 << CGEN_INSN_OPTIONAL_ABS_INSN;
    273       break;
    274     case OPTION_MINMAX:
    275       optbits |= 1 << CGEN_INSN_OPTIONAL_MINMAX_INSN;
    276       optbitset |= 1 << CGEN_INSN_OPTIONAL_MINMAX_INSN;
    277       break;
    278     case OPTION_NOMINMAX:
    279       optbits &= ~(1 << CGEN_INSN_OPTIONAL_MINMAX_INSN);
    280       optbitset |= 1 << CGEN_INSN_OPTIONAL_MINMAX_INSN;
    281       break;
    282     case OPTION_CLIP:
    283       optbits |= 1 << CGEN_INSN_OPTIONAL_CLIP_INSN;
    284       optbitset |= 1 << CGEN_INSN_OPTIONAL_CLIP_INSN;
    285       break;
    286     case OPTION_NOCLIP:
    287       optbits &= ~(1 << CGEN_INSN_OPTIONAL_CLIP_INSN);
    288       optbitset |= 1 << CGEN_INSN_OPTIONAL_CLIP_INSN;
    289       break;
    290     case OPTION_SATUR:
    291       optbits |= 1 << CGEN_INSN_OPTIONAL_SAT_INSN;
    292       optbitset |= 1 << CGEN_INSN_OPTIONAL_SAT_INSN;
    293       break;
    294     case OPTION_NOSATUR:
    295       optbits &= ~(1 << CGEN_INSN_OPTIONAL_SAT_INSN);
    296       optbitset |= 1 << CGEN_INSN_OPTIONAL_SAT_INSN;
    297       break;
    298     case OPTION_COP32:
    299       optbits |= 1 << CGEN_INSN_OPTIONAL_CP_INSN;
    300       optbitset |= 1 << CGEN_INSN_OPTIONAL_CP_INSN;
    301       break;
    302     case OPTION_DEBUG:
    303       optbits |= 1 << CGEN_INSN_OPTIONAL_DEBUG_INSN;
    304       optbitset |= 1 << CGEN_INSN_OPTIONAL_DEBUG_INSN;
    305       break;
    306     case OPTION_NODEBUG:
    307       optbits &= ~(1 << CGEN_INSN_OPTIONAL_DEBUG_INSN);
    308       optbitset |= 1 << CGEN_INSN_OPTIONAL_DEBUG_INSN;
    309       break;
    310     case OPTION_UCI:
    311       optbits |= 1 << CGEN_INSN_OPTIONAL_UCI_INSN;
    312       optbitset |= 1 << CGEN_INSN_OPTIONAL_UCI_INSN;
    313       break;
    314     case OPTION_NOUCI:
    315       optbits &= ~(1 << CGEN_INSN_OPTIONAL_UCI_INSN);
    316       optbitset |= 1 << CGEN_INSN_OPTIONAL_UCI_INSN;
    317       break;
    318     case OPTION_DSP:
    319       optbits |= 1 << CGEN_INSN_OPTIONAL_DSP_INSN;
    320       optbitset |= 1 << CGEN_INSN_OPTIONAL_DSP_INSN;
    321       break;
    322     case OPTION_NODSP:
    323       optbits &= ~(1 << CGEN_INSN_OPTIONAL_DSP_INSN);
    324       optbitset |= 1 << CGEN_INSN_OPTIONAL_DSP_INSN;
    325       break;
    326     case OPTION_LIBRARY:
    327       library_flag = EF_MEP_LIBRARY;
    328       break;
    329     case OPTION_REPEAT:
    330     case OPTION_NOREPEAT:
    331       break;
    332     default:
    333       return 0;
    334     }
    335   return 1;
    336 }
    337 
    338 void
    339 md_show_usage (FILE *stream)
    340 {
    341   fprintf (stream, _("MeP specific command line options:\n\
    342   -EB                     assemble for a big endian system\n\
    343   -EL                     assemble for a little endian system (default)\n\
    344   -mconfig=<name>         specify a chip configuration to use\n\
    345   -maverage -mno-average -mmult -mno-mult -mdiv -mno-div\n\
    346   -mbitops -mno-bitops -mleadz -mno-leadz -mabsdiff -mno-absdiff\n\
    347   -mminmax -mno-minmax -mclip -mno-clip -msatur -mno-satur -mcop32\n\
    348                           enable/disable the given opcodes\n\
    349 \n\
    350   If -mconfig is given, the other -m options modify it.  Otherwise,\n\
    351   if no -m options are given, all core opcodes are enabled;\n\
    352   if any enabling -m options are given, only those are enabled;\n\
    353   if only disabling -m options are given, only those are disabled.\n\
    354 "));
    355   if (mep_config_map[1].name)
    356     {
    357       int i;
    358       fprintf (stream, "  -mconfig=STR            specify the configuration to use\n");
    359       fprintf (stream, "  Configurations:");
    360       for (i=0; mep_config_map[i].name; i++)
    361 	fprintf (stream, " %s", mep_config_map[i].name);
    362       fprintf (stream, "\n");
    363     }
    364 }
    365 
    366 
    367 
    369 static void
    370 mep_check_for_disabled_registers (mep_insn *insn)
    371 {
    372   static int initted = 0;
    373   static int has_mul_div = 0;
    374   static int has_cop = 0;
    375   static int has_debug = 0;
    376   unsigned int b, r;
    377 
    378   if (allow_disabled_registers)
    379     return;
    380 
    381 #if !CGEN_INT_INSN_P
    382   if (target_big_endian)
    383     b = insn->buffer[0] * 256 + insn->buffer[1];
    384   else
    385     b = insn->buffer[1] * 256 + insn->buffer[0];
    386 #else
    387   b = insn->buffer[0];
    388 #endif
    389 
    390   if ((b & 0xfffff00e) == 0x7008 /* stc */
    391       || (b & 0xfffff00e) == 0x700a /* ldc */)
    392     {
    393       if (!initted)
    394 	{
    395 	  initted = 1;
    396 	  if ((MEP_OMASK & (1 << CGEN_INSN_OPTIONAL_MUL_INSN))
    397 	      || (MEP_OMASK & (1 << CGEN_INSN_OPTIONAL_DIV_INSN)))
    398 	    has_mul_div = 1;
    399 	  if (MEP_OMASK & (1 << CGEN_INSN_OPTIONAL_DEBUG_INSN))
    400 	    has_debug = 1;
    401 	  if (MEP_OMASK & (1 << CGEN_INSN_OPTIONAL_CP_INSN))
    402 	    has_cop = 1;
    403 	}
    404 
    405       r = ((b & 0x00f0) >> 4) | ((b & 0x0001) << 4);
    406       switch (r)
    407 	{
    408 	case 7: /* $hi */
    409 	case 8: /* $lo */
    410 	  if (!has_mul_div)
    411 	    as_bad (_("$hi and $lo are disabled when MUL and DIV are off"));
    412 	  break;
    413 	case 12: /* $mb0 */
    414 	case 13: /* $me0 */
    415 	case 14: /* $mb1 */
    416 	case 15: /* $me1 */
    417 	  if (!has_cop)
    418 	    as_bad (_("$mb0, $me0, $mb1, and $me1 are disabled when COP is off"));
    419 	  break;
    420 	case 24: /* $dbg */
    421 	case 25: /* $depc */
    422 	  if (!has_debug)
    423 	    as_bad (_("$dbg and $depc are disabled when DEBUG is off"));
    424 	  break;
    425 	}
    426     }
    427 }
    428 
    429 static int
    430 mep_machine (void)
    431 {
    432   switch (MEP_CPU & EF_MEP_CPU_MASK)
    433     {
    434     default: break;
    435     case EF_MEP_CPU_C2: return bfd_mach_mep;
    436     case EF_MEP_CPU_C3: return bfd_mach_mep;
    437     case EF_MEP_CPU_C4: return bfd_mach_mep;
    438     case EF_MEP_CPU_C5: return bfd_mach_mep_c5;
    439     case EF_MEP_CPU_H1: return bfd_mach_mep_h1;
    440     }
    441 
    442   return bfd_mach_mep;
    443 }
    444 
    445 /* The MeP version of the cgen parse_operand function.  The only difference
    446    from the standard version is that we want to avoid treating '$foo' and
    447    '($foo...)' as references to a symbol called '$foo'.  The chances are
    448    that '$foo' is really a misspelt register.  */
    449 
    450 static const char *
    451 mep_parse_operand (CGEN_CPU_DESC cd, enum cgen_parse_operand_type want,
    452 		   const char **strP, int opindex, int opinfo,
    453 		   enum cgen_parse_operand_result *resultP, bfd_vma *valueP)
    454 {
    455   if (want == CGEN_PARSE_OPERAND_INTEGER || want == CGEN_PARSE_OPERAND_ADDRESS)
    456     {
    457       const char *next;
    458 
    459       next = *strP;
    460       while (*next == '(')
    461 	next++;
    462       if (*next == '$')
    463 	return "Not a valid literal";
    464     }
    465   return gas_cgen_parse_operand (cd, want, strP, opindex, opinfo,
    466 				 resultP, valueP);
    467 }
    468 
    469 void
    470 md_begin ()
    471 {
    472   /* Initialize the `cgen' interface.  */
    473 
    474   /* If the user specifies no options, we default to allowing
    475      everything.  If the user specifies any enabling options, we
    476      default to allowing only what is specified.  If the user
    477      specifies only disabling options, we only disable what is
    478      specified.  If the user specifies options and a config, the
    479      options modify the config.  */
    480   if (optbits && mep_config_index == 0)
    481     {
    482       MEP_OMASK &= ~OPTION_MASK;
    483       MEP_OMASK |= optbits;
    484     }
    485   else
    486     MEP_OMASK = (MEP_OMASK & ~optbitset) | optbits;
    487 
    488   mep_cop = mep_config_map[mep_config_index].cpu_flag & EF_MEP_COP_MASK;
    489 
    490   /* Set the machine number and endian.  */
    491   gas_cgen_cpu_desc = mep_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
    492 					 CGEN_CPU_OPEN_ENDIAN,
    493 					 target_big_endian
    494 					 ? CGEN_ENDIAN_BIG
    495 					 : CGEN_ENDIAN_LITTLE,
    496 					 CGEN_CPU_OPEN_ISAS, 0,
    497 					 CGEN_CPU_OPEN_END);
    498   mep_cgen_init_asm (gas_cgen_cpu_desc);
    499 
    500   /* This is a callback from cgen to gas to parse operands.  */
    501   cgen_set_parse_operand_fn (gas_cgen_cpu_desc, mep_parse_operand);
    502 
    503   /* Identify the architecture.  */
    504   bfd_default_set_arch_mach (stdoutput, bfd_arch_mep, mep_machine ());
    505 
    506   /* Store the configuration number and core.  */
    507   bfd_set_private_flags (stdoutput, MEP_CPU | MEP_CONFIG | library_flag);
    508 
    509   /* Initialize the array we'll be using to store fixups.  */
    510   gas_cgen_initialize_saved_fixups_array();
    511 }
    512 
    513 /* Variant of mep_cgen_assemble_insn.  Assemble insn STR of cpu CD as a
    514    coprocessor instruction, if possible, into FIELDS, BUF, and INSN.  */
    515 
    516 static const CGEN_INSN *
    517 mep_cgen_assemble_cop_insn (CGEN_CPU_DESC cd,
    518 			    const char *str,
    519 			    CGEN_FIELDS *fields,
    520 			    CGEN_INSN_BYTES_PTR buf,
    521 			    const struct cgen_insn *pinsn)
    522 {
    523   const char *start;
    524   CGEN_INSN_LIST *ilist;
    525   const char *errmsg = NULL;
    526 
    527   /* The instructions are stored in hashed lists. */
    528   ilist = CGEN_ASM_LOOKUP_INSN (gas_cgen_cpu_desc,
    529 				CGEN_INSN_MNEMONIC (pinsn));
    530 
    531   start = str;
    532   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
    533     {
    534       const CGEN_INSN *insn = ilist->insn;
    535       if (strcmp (CGEN_INSN_MNEMONIC (ilist->insn),
    536 		  CGEN_INSN_MNEMONIC (pinsn)) == 0
    537 	  && MEP_INSN_COP_P (ilist->insn)
    538 	  && mep_cgen_insn_supported (cd, insn))
    539 	{
    540 	  str = start;
    541 
    542 	  /* skip this insn if str doesn't look right lexically */
    543 	  if (CGEN_INSN_RX (insn) != NULL &&
    544 	      regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
    545 	    continue;
    546 
    547 	  /* Allow parse/insert handlers to obtain length of insn.  */
    548 	  CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
    549 
    550 	  errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
    551 	  if (errmsg != NULL)
    552 	    continue;
    553 
    554 	  errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
    555 					      (bfd_vma) 0);
    556 	  if (errmsg != NULL)
    557 	    continue;
    558 
    559 	  return insn;
    560 	}
    561     }
    562   return pinsn;
    563 }
    564 
    565 static void
    566 mep_save_insn (mep_insn insn)
    567 {
    568   /* Consider change MAX_SAVED_FIXUP_CHAINS to MAX_PARALLEL_INSNS. */
    569   if (num_insns_saved < 0 || num_insns_saved >= MAX_SAVED_FIXUP_CHAINS)
    570     {
    571       as_fatal("index into saved_insns[] out of bounds.");
    572       return;
    573     }
    574   saved_insns[num_insns_saved] = insn;
    575   gas_cgen_save_fixups(num_insns_saved);
    576   num_insns_saved++;
    577 }
    578 
    579 static void
    580 mep_check_parallel32_scheduling (void)
    581 {
    582   int insn0iscopro, insn1iscopro, insn0length, insn1length;
    583 
    584   /* More than two instructions means that either someone is referring to
    585      an internally parallel core or an internally parallel coprocessor,
    586      neither of which are supported at this time.  */
    587   if ( num_insns_saved > 2 )
    588     as_fatal("Internally paralled cores and coprocessors not supported.");
    589 
    590   /* If there are no insns saved, that's ok.  Just return.  This will
    591      happen when mep_process_saved_insns is called when the end of the
    592      source file is reached and there are no insns left to be processed.  */
    593   if (num_insns_saved == 0)
    594     return;
    595 
    596   /* Check some of the attributes of the first insn.  */
    597   insn0iscopro = MEP_INSN_COP_P (saved_insns[0].insn);
    598   insn0length = CGEN_FIELDS_BITSIZE (& saved_insns[0].fields);
    599 
    600   if (num_insns_saved == 2)
    601     {
    602       /* Check some of the attributes of the first insn.  */
    603       insn1iscopro = MEP_INSN_COP_P (saved_insns[1].insn);
    604       insn1length = CGEN_FIELDS_BITSIZE (& saved_insns[1].fields);
    605 
    606       if ((insn0iscopro && !insn1iscopro)
    607           || (insn1iscopro && !insn0iscopro))
    608 	{
    609           /* We have one core and one copro insn.  If their sizes
    610              add up to 32, then the combination is valid.  */
    611 	  if (insn0length + insn1length == 32)
    612 	    return;
    613           else
    614 	    as_bad (_("core and copro insn lengths must total 32 bits."));
    615 	}
    616       else
    617         as_bad (_("vliw group must consist of 1 core and 1 copro insn."));
    618     }
    619   else
    620     {
    621       /* If we arrive here, we have one saved instruction.  There are a
    622 	 number of possible cases:
    623 
    624 	 1.  The instruction is a 32 bit core or coprocessor insn and
    625              can be executed by itself.  Valid.
    626 
    627          2.  The instrucion is a core instruction for which a cop nop
    628              exists.  In this case, insert the cop nop into the saved
    629              insn array after the core insn and return.  Valid.
    630 
    631          3.  The instruction is a coprocessor insn for which a core nop
    632              exists.  In this case, move the coprocessor insn to the
    633              second element of the array and put the nop in the first
    634 	     element then return.  Valid.
    635 
    636          4. The instruction is a core or coprocessor instruction for
    637             which there is no matching coprocessor or core nop to use
    638 	    to form a valid vliw insn combination.  In this case, we
    639 	    we have to abort.  */
    640 
    641       if (insn0length > 32)
    642 	as_fatal ("Cannot use 48- or 64-bit insns with a 32 bit datapath.");
    643 
    644       if (insn0length == 32)
    645 	return;
    646 
    647       /* Insn is smaller than datapath.  If there are no matching
    648          nops for this insn, then terminate assembly.  */
    649       if (CGEN_INSN_ATTR_VALUE (saved_insns[0].insn,
    650                                 CGEN_INSN_VLIW32_NO_MATCHING_NOP))
    651 	as_fatal ("No valid nop.");
    652 
    653       /* At this point we know that we have a single 16-bit insn that has
    654 	 a matching nop.  We have to assemble it and put it into the saved
    655          insn and fixup chain arrays. */
    656 
    657       if (insn0iscopro)
    658 	{
    659           char *errmsg;
    660           mep_insn insn;
    661 
    662           /* Move the insn and it's fixups to the second element of the
    663              saved insns arrary and insert a 16 bit core nope into the
    664              first element. */
    665              insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "nop",
    666                                                  &insn.fields, insn.buffer,
    667                                                  &errmsg);
    668              if (!insn.insn)
    669                {
    670                  as_bad ("%s", errmsg);
    671                  return;
    672                }
    673 
    674              /* Move the insn in element 0 to element 1 and insert the
    675                  nop into element 0.  Move the fixups in element 0 to
    676                  element 1 and save the current fixups to element 0.
    677                  Really there aren't any fixups at this point because we're
    678                  inserting a nop but we might as well be general so that
    679                  if there's ever a need to insert a general insn, we'll
    680                  have an example. */
    681               saved_insns[1] = saved_insns[0];
    682               saved_insns[0] = insn;
    683               num_insns_saved++;
    684               gas_cgen_swap_fixups (0);
    685               gas_cgen_save_fixups (1);
    686 	}
    687       else
    688 	{
    689           char * errmsg;
    690           mep_insn insn;
    691 	  int insn_num = saved_insns[0].insn->base->num;
    692 
    693 	  /* Use 32 bit branches and skip the nop.  */
    694 	  if (insn_num == MEP_INSN_BSR12
    695 	      || insn_num == MEP_INSN_BEQZ
    696 	      || insn_num == MEP_INSN_BNEZ)
    697 	    return;
    698 
    699           /* Insert a 16-bit coprocessor nop.  Note that at the time */
    700           /* this was done, no 16-bit coprocessor nop was defined.   */
    701 	  insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "cpnop16",
    702 					      &insn.fields, insn.buffer,
    703 					      &errmsg);
    704           if (!insn.insn)
    705             {
    706               as_bad ("%s", errmsg);
    707               return;
    708             }
    709 
    710           /* Now put the insn and fixups into the arrays.  */
    711           mep_save_insn (insn);
    712 	}
    713     }
    714 }
    715 
    716 static void
    717 mep_check_parallel64_scheduling (void)
    718 {
    719   int insn0iscopro, insn1iscopro, insn0length, insn1length;
    720 
    721   /* More than two instructions means that someone is referring to an
    722      internally parallel core or an internally parallel coprocessor.  */
    723   /* These are not currently supported.  */
    724   if (num_insns_saved > 2)
    725     as_fatal ("Internally parallel cores of coprocessors not supported.");
    726 
    727   /* If there are no insns saved, that's ok.  Just return.  This will
    728      happen when mep_process_saved_insns is called when the end of the
    729      source file is reached and there are no insns left to be processed.  */
    730   if (num_insns_saved == 0)
    731     return;
    732 
    733   /* Check some of the attributes of the first insn.  */
    734   insn0iscopro = MEP_INSN_COP_P (saved_insns[0].insn);
    735   insn0length = CGEN_FIELDS_BITSIZE (& saved_insns[0].fields);
    736 
    737   if (num_insns_saved == 2)
    738     {
    739       /* Check some of the attributes of the first insn. */
    740       insn1iscopro = MEP_INSN_COP_P (saved_insns[1].insn);
    741       insn1length = CGEN_FIELDS_BITSIZE (& saved_insns[1].fields);
    742 
    743       if ((insn0iscopro && !insn1iscopro)
    744 	  || (insn1iscopro && !insn0iscopro))
    745 	{
    746 	  /* We have one core and one copro insn.  If their sizes
    747 	     add up to 64, then the combination is valid.  */
    748 	  if (insn0length + insn1length == 64)
    749             return;
    750 	  else
    751             as_bad (_("core and copro insn lengths must total 64 bits."));
    752 	}
    753       else
    754         as_bad (_("vliw group must consist of 1 core and 1 copro insn."));
    755     }
    756   else
    757     {
    758       /* If we arrive here, we have one saved instruction.  There are a
    759 	 number of possible cases:
    760 
    761          1.  The instruction is a 64 bit coprocessor insn and can be
    762              executed by itself.  Valid.
    763 
    764          2.  The instrucion is a core instruction for which a cop nop
    765              exists.  In this case, insert the cop nop into the saved
    766              insn array after the core insn and return.  Valid.
    767 
    768          3.  The instruction is a coprocessor insn for which a core nop
    769              exists.  In this case, move the coprocessor insn to the
    770              second element of the array and put the nop in the first
    771              element then return.  Valid.
    772 
    773          4.  The instruction is a core or coprocessor instruction for
    774              which there is no matching coprocessor or core nop to use
    775              to form a valid vliw insn combination.  In this case, we
    776 	     we have to abort.  */
    777 
    778       /* If the insn is 64 bits long, it can run alone.  The size check
    779 	 is done indepependantly of whether the insn is core or copro
    780 	 in case 64 bit coprocessor insns are added later.  */
    781       if (insn0length == 64)
    782         return;
    783 
    784       /* Insn is smaller than datapath.  If there are no matching
    785 	 nops for this insn, then terminate assembly.  */
    786       if (CGEN_INSN_ATTR_VALUE (saved_insns[0].insn,
    787 				CGEN_INSN_VLIW64_NO_MATCHING_NOP))
    788 	as_fatal ("No valid nop.");
    789 
    790       if (insn0iscopro)
    791 	{
    792 	  char *errmsg;
    793 	  mep_insn insn;
    794 
    795           /* Initialize the insn buffer.  */
    796 	  memset (insn.buffer, 0, sizeof(insn.buffer));
    797 
    798 	  /* We have a coprocessor insn.  At this point in time there
    799 	     are is 32-bit core nop.  There is only a 16-bit core
    800 	     nop.  The idea is to allow for a relatively arbitrary
    801 	     coprocessor to be specified.  We aren't looking at
    802 	     trying to cover future changes in the core at this time
    803 	     since it is assumed that the core will remain fairly
    804 	     static.  If there ever are 32 or 48 bit core nops added,
    805 	     they will require entries below.  */
    806 
    807 	  if (insn0length == 48)
    808 	    {
    809 	      /* Move the insn and fixups to the second element of the
    810 		 arrays then assemble and insert a 16 bit core nop.  */
    811 	      insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "nop",
    812 						  & insn.fields, insn.buffer,
    813 						  & errmsg);
    814 	    }
    815           else
    816             {
    817               /* If this is reached, then we have a single coprocessor
    818                  insn that is not 48 bits long, but for which the assembler
    819                  thinks there is a matching core nop.  If a 32-bit core
    820                  nop has been added, then make the necessary changes and
    821                  handle its assembly and insertion here.  Otherwise,
    822                  go figure out why either:
    823 
    824                  1. The assembler thinks that there is a 32-bit core nop
    825                     to match a 32-bit coprocessor insn, or
    826                  2. The assembler thinks that there is a 48-bit core nop
    827                     to match a 16-bit coprocessor insn.  */
    828 
    829               as_fatal ("Assembler expects a non-existent core nop.");
    830             }
    831 
    832 	 if (!insn.insn)
    833 	   {
    834 	     as_bad ("%s", errmsg);
    835 	     return;
    836 	   }
    837 
    838          /* Move the insn in element 0 to element 1 and insert the
    839             nop into element 0.  Move the fixups in element 0 to
    840             element 1 and save the current fixups to element 0.
    841 	    Really there aren't any fixups at this point because we're
    842 	    inserting a nop but we might as well be general so that
    843 	    if there's ever a need to insert a general insn, we'll
    844 	    have an example. */
    845 
    846          saved_insns[1] = saved_insns[0];
    847          saved_insns[0] = insn;
    848          num_insns_saved++;
    849          gas_cgen_swap_fixups(0);
    850          gas_cgen_save_fixups(1);
    851 
    852 	}
    853       else
    854 	{
    855 	  char * errmsg;
    856 	  mep_insn insn;
    857 
    858           /* Initialize the insn buffer */
    859 	  memset (insn.buffer, 0, sizeof(insn.buffer));
    860 
    861 	  /* We have a core insn.  We have to handle all possible nop
    862 	     lengths.  If a coprocessor doesn't have a nop of a certain
    863 	     length but there exists core insns that when combined with
    864 	      a nop of that length would fill the datapath, those core
    865 	      insns will be flagged with the VLIW_NO_CORRESPONDING_NOP
    866 	      attribute.  That will ensure that when used in a way that
    867 	      requires a nop to be inserted, assembly will terminate
    868 	      before reaching this section of code.  This guarantees
    869 	      that cases below which would result in the attempted
    870 	      insertion of nop that doesn't exist will never be entered.  */
    871 	  if (insn0length == 16)
    872 	    {
    873 	      /* Insert 48 bit coprocessor nop.          */
    874 	      /* Assemble it and put it into the arrays. */
    875 	      insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "cpnop48",
    876 						  &insn.fields, insn.buffer,
    877 						  &errmsg);
    878 	    }
    879 	  else if (insn0length == 32)
    880 	    {
    881 	      /* Insert 32 bit coprocessor nop. */
    882 	      insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "cpnop32",
    883 						  &insn.fields, insn.buffer,
    884 						  &errmsg);
    885 	    }
    886 	  else if (insn0length == 48)
    887 	    {
    888 	      /* Insert 16 bit coprocessor nop. */
    889 	      insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "cpnop16",
    890 						  &insn.fields, insn.buffer,
    891 						  &errmsg);
    892 	    }
    893 	  else
    894 	    /* Core insn has an invalid length.  Something has gone wrong. */
    895 	    as_fatal ("Core insn has invalid length!  Something is wrong!");
    896 
    897 	  if (!insn.insn)
    898 	    {
    899 	      as_bad ("%s", errmsg);
    900 	      return;
    901 	    }
    902 
    903 	  /* Now put the insn and fixups into the arrays.  */
    904 	  mep_save_insn (insn);
    905 	}
    906     }
    907 }
    908 
    909 #ifdef MEP_IVC2_SUPPORTED
    910 
    911 /* IVC2 packing is different than other VLIW coprocessors.  Many of
    912    the COP insns can be placed in any of three different types of
    913    slots, and each bundle can hold up to three insns - zero or one
    914    core insns and one or two IVC2 insns.  The insns in CGEN are tagged
    915    with which slots they're allowed in, and we have to decide based on
    916    that whether or not the user had given us a possible bundling.  */
    917 
    918 static int
    919 slot_ok (int idx, int slot)
    920 {
    921   const CGEN_INSN *insn = saved_insns[idx].insn;
    922   return CGEN_ATTR_CGEN_INSN_SLOTS_VALUE (CGEN_INSN_ATTRS (insn)) & (1 << slot);
    923 }
    924 
    925 static void
    926 mep_check_ivc2_scheduling (void)
    927 {
    928   /* VLIW modes:
    929 
    930      V1 [-----core-----][--------p0s-------][------------p1------------]
    931      V2 [-------------core-------------]xxxx[------------p1------------]
    932      V3 1111[--p0--]0111[--------p0--------][------------p1------------]
    933   */
    934 
    935   int slots[5]; /* Indexed off the SLOTS_ATTR enum.  */
    936   int corelength, realcorelength;
    937   int i;
    938   bfd_byte temp[4];
    939   bfd_byte *f;
    940   int e = target_big_endian ? 0 : 1;
    941 
    942   /* If there are no insns saved, that's ok.  Just return.  This will
    943      happen when mep_process_saved_insns is called when the end of the
    944      source file is reached and there are no insns left to be processed.  */
    945   if (num_insns_saved == 0)
    946     return;
    947 
    948   for (i=0; i<5; i++)
    949     slots[i] = -1;
    950 
    951   if (slot_ok (0, SLOTS_CORE))
    952     {
    953       slots[SLOTS_CORE] = 0;
    954       realcorelength = corelength = CGEN_FIELDS_BITSIZE (& saved_insns[0].fields);
    955 
    956       /* If we encounter one of these, it may get relaxed later into a
    957 	 longer instruction.  We can't just push the other opcodes
    958 	 away, the bigger insn has to fit into the existing slot.  So,
    959 	 we make room for the relaxed instruction here.  */
    960 
    961       if (saved_insns[0].insn->base->num == MEP_INSN_BSR12
    962 	  || saved_insns[0].insn->base->num == MEP_INSN_BRA)
    963 	corelength = 32;
    964     }
    965   else
    966     realcorelength = corelength = 0;
    967 
    968   if (corelength == 16)
    969     {
    970       /* V1 mode: we need a P0S slot and a P1 slot.  */
    971       switch (num_insns_saved)
    972 	{
    973 	case 1:
    974 	  /* No other insns, fill with NOPs. */
    975 	  break;
    976 
    977 	case 2:
    978 	  if (slot_ok (1, SLOTS_P1))
    979 	    slots[SLOTS_P1] = 1;
    980 	  else if (slot_ok (1, SLOTS_P0S))
    981 	    slots[SLOTS_P0S] = 1;
    982 	  else
    983 	    as_bad (_("cannot pack %s with a 16-bit insn"),
    984 		    CGEN_INSN_NAME (saved_insns[1].insn));
    985 	  break;
    986 
    987 	case 3:
    988 	  if (slot_ok (1, SLOTS_P0S)
    989 	      && slot_ok (2, SLOTS_P1))
    990 	    {
    991 	      slots[SLOTS_P0S] = 1;
    992 	      slots[SLOTS_P1] = 2;
    993 	    }
    994 	  else if (slot_ok (1, SLOTS_P1)
    995 	      && slot_ok (2, SLOTS_P0S))
    996 	    {
    997 	      slots[SLOTS_P1] = 1;
    998 	      slots[SLOTS_P0S] = 2;
    999 	    }
   1000 	  else
   1001 	    as_bad (_("cannot pack %s and %s together with a 16-bit insn"),
   1002 		    CGEN_INSN_NAME (saved_insns[1].insn),
   1003 		    CGEN_INSN_NAME (saved_insns[2].insn));
   1004 	  break;
   1005 
   1006 	default:
   1007 	  as_bad (_("too many IVC2 insns to pack with a 16-bit core insn"));
   1008 	  break;
   1009 	}
   1010     }
   1011   else if (corelength == 32)
   1012     {
   1013       /* V2 mode: we need a P1 slot.  */
   1014       switch (num_insns_saved)
   1015 	{
   1016 	case 1:
   1017 	  /* No other insns, fill with NOPs. */
   1018 	  break;
   1019 	case 2:
   1020 	  /* The other insn must allow P1.  */
   1021 	  if (!slot_ok (1, SLOTS_P1))
   1022 	    as_bad (_("cannot pack %s into slot P1"),
   1023 		    CGEN_INSN_NAME (saved_insns[1].insn));
   1024 	  else
   1025 	    slots[SLOTS_P1] = 1;
   1026 	  break;
   1027 	default:
   1028 	  as_bad (_("too many IVC2 insns to pack with a 32-bit core insn"));
   1029 	  break;
   1030 	}
   1031     }
   1032   else if (corelength == 0)
   1033     {
   1034       /* V3 mode: we need a P0 slot and a P1 slot, or a P0S+P1 with a
   1035 	 core NOP.  */
   1036       switch (num_insns_saved)
   1037 	{
   1038 	case 1:
   1039 	  if (slot_ok (0, SLOTS_P0))
   1040 	    slots[SLOTS_P0] = 0;
   1041 	  else if (slot_ok (0, SLOTS_P1))
   1042 	    slots[SLOTS_P1] = 0;
   1043 	  else if (slot_ok (0, SLOTS_P0S))
   1044 	    slots[SLOTS_P0S] = 0;
   1045 	  else
   1046 	    as_bad (_("unable to pack %s by itself?"),
   1047 		    CGEN_INSN_NAME (saved_insns[0].insn));
   1048 	  break;
   1049 
   1050 	case 2:
   1051 	  if (slot_ok (0, SLOTS_P0)
   1052 	      && slot_ok (1, SLOTS_P1))
   1053 	    {
   1054 	      slots[SLOTS_P0] = 0;
   1055 	      slots[SLOTS_P1] = 1;
   1056 	    }
   1057 	  else if (slot_ok (0, SLOTS_P1)
   1058 	      && slot_ok (1, SLOTS_P0))
   1059 	    {
   1060 	      slots[SLOTS_P1] = 0;
   1061 	      slots[SLOTS_P0] = 1;
   1062 	    }
   1063 	  else if (slot_ok (0, SLOTS_P0S)
   1064 	      && slot_ok (1, SLOTS_P1))
   1065 	    {
   1066 	      slots[SLOTS_P0S] = 0;
   1067 	      slots[SLOTS_P1] = 1;
   1068 	    }
   1069 	  else if (slot_ok (0, SLOTS_P1)
   1070 	      && slot_ok (1, SLOTS_P0S))
   1071 	    {
   1072 	      slots[SLOTS_P1] = 0;
   1073 	      slots[SLOTS_P0S] = 1;
   1074 	    }
   1075 	  else
   1076 	    as_bad (_("cannot pack %s and %s together"),
   1077 		    CGEN_INSN_NAME (saved_insns[0].insn),
   1078 		    CGEN_INSN_NAME (saved_insns[1].insn));
   1079 	  break;
   1080 
   1081 	default:
   1082 	  as_bad (_("too many IVC2 insns to pack together"));
   1083 	  break;
   1084 	}
   1085     }
   1086 
   1087   /* The core insn needs to be done normally so that fixups,
   1088      relaxation, etc are done.  Other IVC2 insns need only be resolved
   1089      to bit patterns; there are no relocations for them.  */
   1090   if (slots[SLOTS_CORE] != -1)
   1091     {
   1092       gas_cgen_restore_fixups (0);
   1093       gas_cgen_finish_insn (saved_insns[0].insn, saved_insns[0].buffer,
   1094 			    CGEN_FIELDS_BITSIZE (& saved_insns[0].fields),
   1095 			    1, NULL);
   1096     }
   1097 
   1098   /* Allocate whatever bytes remain in our insn word.  Adjust the
   1099      pointer to point (as if it were) to the beginning of the whole
   1100      word, so that we don't have to adjust for it elsewhere.  */
   1101   f = (bfd_byte *) frag_more (8 - realcorelength / 8);
   1102   /* Unused slots are filled with NOPs, which happen to be all zeros.  */
   1103   memset (f, 0, 8 - realcorelength / 8);
   1104   f -= realcorelength / 8;
   1105 
   1106   for (i=1; i<5; i++)
   1107     {
   1108       mep_insn *m;
   1109 
   1110       if (slots[i] == -1)
   1111 	continue;
   1112 
   1113       m = & saved_insns[slots[i]];
   1114 
   1115 #if CGEN_INT_INSN_P
   1116       cgen_put_insn_value (gas_cgen_cpu_desc, (unsigned char *) temp, 32,
   1117 			   m->buffer[0]);
   1118 #else
   1119       memcpy (temp, m->buffer, byte_len);
   1120 #endif
   1121 
   1122       switch (i)
   1123 	{
   1124 	case SLOTS_P0S:
   1125 	  f[2^e] = temp[1^e];
   1126 	  f[3^e] = temp[2^e];
   1127 	  f[4^e] |= temp[3^e] & 0xf0;
   1128 	  break;
   1129 	case SLOTS_P0:
   1130 	  f[0^e] = 0xf0 | temp[0^e] >> 4;
   1131 	  f[1^e] = temp[0^e] << 4 | 0x07;
   1132 	  f[2^e] = temp[1^e];
   1133 	  f[3^e] = temp[2^e];
   1134 	  f[4^e] |= temp[3^e] & 0xf0;
   1135 	  break;
   1136 	case SLOTS_P1:
   1137 	  f[4^e] |= temp[0^e] >> 4;
   1138 	  f[5^e] = temp[0^e] << 4 | temp[1^e] >> 4;
   1139 	  f[6^e] = temp[1^e] << 4 | temp[2^e] >> 4;
   1140 	  f[7^e] = temp[2^e] << 4 | temp[3^e] >> 4;
   1141 	  break;
   1142 	default:
   1143 	  break;
   1144 	}
   1145     }
   1146 }
   1147 
   1148 #endif /* MEP_IVC2_SUPPORTED */
   1149 
   1150 /* The scheduling functions are just filters for invalid combinations.
   1151    If there is a violation, they terminate assembly.  Otherise they
   1152    just fall through.  Succesful combinations cause no side effects
   1153    other than valid nop insertion.  */
   1154 
   1155 static void
   1156 mep_check_parallel_scheduling (void)
   1157 {
   1158   /* This is where we will eventually read the config information
   1159      and choose which scheduling checking function to call.  */
   1160 #ifdef MEP_IVC2_SUPPORTED
   1161   if (mep_cop == EF_MEP_COP_IVC2)
   1162     mep_check_ivc2_scheduling ();
   1163   else
   1164 #endif /* MEP_IVC2_SUPPORTED */
   1165     if (MEP_VLIW64)
   1166     mep_check_parallel64_scheduling ();
   1167   else
   1168     mep_check_parallel32_scheduling ();
   1169 }
   1170 
   1171 static void
   1172 mep_process_saved_insns (void)
   1173 {
   1174   int i;
   1175 
   1176   gas_cgen_save_fixups (MAX_SAVED_FIXUP_CHAINS - 1);
   1177 
   1178   /* We have to check for valid scheduling here. */
   1179   mep_check_parallel_scheduling ();
   1180 
   1181   /* IVC2 has to pack instructions in a funny way, so it does it
   1182      itself.  */
   1183   if (mep_cop != EF_MEP_COP_IVC2)
   1184     {
   1185       /* If the last call didn't cause assembly to terminate, we have
   1186 	 a valid vliw insn/insn pair saved. Restore this instructions'
   1187 	 fixups and process the insns. */
   1188       for (i = 0;i<num_insns_saved;i++)
   1189 	{
   1190 	  gas_cgen_restore_fixups (i);
   1191 	  gas_cgen_finish_insn (saved_insns[i].insn, saved_insns[i].buffer,
   1192 				CGEN_FIELDS_BITSIZE (& saved_insns[i].fields),
   1193 				1, NULL);
   1194 	}
   1195     }
   1196   gas_cgen_restore_fixups (MAX_SAVED_FIXUP_CHAINS - 1);
   1197 
   1198   /* Clear the fixups and reset the number insn saved to 0. */
   1199   gas_cgen_initialize_saved_fixups_array ();
   1200   num_insns_saved = 0;
   1201   listing_prev_line ();
   1202 }
   1203 
   1204 void
   1205 md_assemble (char * str)
   1206 {
   1207   static CGEN_BITSET* isas = NULL;
   1208   char * errmsg;
   1209 
   1210   /* Initialize GAS's cgen interface for a new instruction.  */
   1211   gas_cgen_init_parse ();
   1212 
   1213   /* There are two possible modes: core and vliw.  We have to assemble
   1214      differently for each.
   1215 
   1216      Core Mode:  We assemble normally.  All instructions are on a
   1217                  single line and are made up of one mnemonic and one
   1218                  set of operands.
   1219      VLIW Mode:  Vliw combinations are indicated as follows:
   1220 
   1221 		       core insn
   1222 		     + copro insn
   1223 
   1224                  We want to handle the general case where more than
   1225                  one instruction can be preceeded by a +.  This will
   1226                  happen later if we add support for internally parallel
   1227                  coprocessors.  We'll make the parsing nice and general
   1228                  so that it can handle an arbitrary number of insns
   1229                  with leading +'s.  The actual checking for valid
   1230                  combinations is done elsewhere.  */
   1231 
   1232   /* Initialize the isa to refer to the core.  */
   1233   if (isas == NULL)
   1234     isas = cgen_bitset_copy (& MEP_CORE_ISA);
   1235   else
   1236     {
   1237       cgen_bitset_clear (isas);
   1238       cgen_bitset_union (isas, & MEP_CORE_ISA, isas);
   1239     }
   1240   gas_cgen_cpu_desc->isas = isas;
   1241 
   1242   if (mode == VLIW)
   1243     {
   1244       /* VLIW mode.  */
   1245 
   1246       int thisInsnIsCopro = 0;
   1247       mep_insn insn;
   1248       int i;
   1249 
   1250       /* Initialize the insn buffer */
   1251 
   1252       if (! CGEN_INT_INSN_P)
   1253          for (i=0; i < CGEN_MAX_INSN_SIZE; i++)
   1254             insn.buffer[i]='\0';
   1255 
   1256 
   1257       /* IVC2 has two sets of coprocessor opcodes, one for CORE mode
   1258 	 and one for VLIW mode.  They have the same names.  To specify
   1259 	 which one we want, we use the COP isas - the 32 bit ISA is
   1260 	 for the core instructions (which are always 32 bits), and the
   1261 	 other ISAs are for the VLIW ones (which always pack into 64
   1262 	 bit insns).  We use other attributes to determine slotting
   1263 	 later.  */
   1264       if (mep_cop == EF_MEP_COP_IVC2)
   1265 	{
   1266 	  cgen_bitset_union (isas, & MEP_COP16_ISA, isas);
   1267 	  cgen_bitset_union (isas, & MEP_COP48_ISA, isas);
   1268 	  cgen_bitset_union (isas, & MEP_COP64_ISA, isas);
   1269 	}
   1270       else
   1271 	{
   1272 	  /* Can't tell core / copro insns apart at parse time! */
   1273 	  cgen_bitset_union (isas, & MEP_COP_ISA, isas);
   1274 	}
   1275 
   1276       /* Assemble the insn so we can examine its attributes. */
   1277       insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, str,
   1278 					  &insn.fields, insn.buffer,
   1279 					  &errmsg);
   1280       if (!insn.insn)
   1281 	{
   1282 	  as_bad ("%s", errmsg);
   1283 	  return;
   1284 	}
   1285       mep_check_for_disabled_registers (&insn);
   1286 
   1287       /* Check to see if it's a coprocessor instruction. */
   1288       thisInsnIsCopro = MEP_INSN_COP_P (insn.insn);
   1289 
   1290       if (!thisInsnIsCopro)
   1291 	{
   1292 	  insn.insn = mep_cgen_assemble_cop_insn (gas_cgen_cpu_desc, str,
   1293 						  &insn.fields, insn.buffer,
   1294 						  insn.insn);
   1295 	  thisInsnIsCopro = MEP_INSN_COP_P (insn.insn);
   1296 	  mep_check_for_disabled_registers (&insn);
   1297 	}
   1298 
   1299       if (pluspresent)
   1300 	{
   1301 	  /* A plus was present. */
   1302 	  /* Check for a + with a core insn and abort if found. */
   1303 	  if (!thisInsnIsCopro)
   1304 	    {
   1305 	      as_fatal("A core insn cannot be preceeded by a +.\n");
   1306 	      return;
   1307 	    }
   1308 
   1309 	  if (num_insns_saved > 0)
   1310 	    {
   1311 	      /* There are insns in the queue. Add this one. */
   1312 	      mep_save_insn (insn);
   1313 	    }
   1314 	  else
   1315 	    {
   1316 	      /* There are no insns in the queue and a plus is present.
   1317 		 This is a syntax error.  Let's not tolerate this.
   1318 		 We can relax this later if necessary.  */
   1319 	      as_bad (_("Invalid use of parallelization operator."));
   1320 	      return;
   1321 	    }
   1322 	}
   1323       else
   1324 	{
   1325 	  /* No plus was present. */
   1326 	  if (num_insns_saved > 0)
   1327 	    {
   1328 	      /* There are insns saved and we came across an insn without a
   1329 		 leading +.  That's the signal to process the saved insns
   1330 		 before proceeding then treat the current insn as the first
   1331 		 in a new vliw group.  */
   1332 	      mep_process_saved_insns ();
   1333 	      num_insns_saved = 0;
   1334 	      /* mep_save_insn (insn); */
   1335 	    }
   1336 	  mep_save_insn (insn);
   1337 #if 0
   1338 	  else
   1339 	    {
   1340 
   1341               /* Core Insn. Add it to the beginning of the queue. */
   1342               mep_save_insn (insn);
   1343 	      /* gas_cgen_save_fixups(num_insns_saved); */
   1344 	    }
   1345 #endif
   1346 	}
   1347 
   1348       pluspresent = 0;
   1349     }
   1350   else
   1351     {
   1352       /* Core mode.  */
   1353 
   1354       /* Only single instructions are assembled in core mode. */
   1355       mep_insn insn;
   1356 
   1357       /* See comment in the VLIW clause above about this.  */
   1358       if (mep_cop & EF_MEP_COP_IVC2)
   1359 	cgen_bitset_union (isas, & MEP_COP32_ISA, isas);
   1360 
   1361       /* If a leading '+' was present, issue an error.
   1362 	 That's not allowed in core mode. */
   1363       if (pluspresent)
   1364 	{
   1365 	  as_bad (_("Leading plus sign not allowed in core mode"));
   1366 	  return;
   1367 	}
   1368 
   1369       insn.insn = mep_cgen_assemble_insn
   1370 	(gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
   1371 
   1372       if (!insn.insn)
   1373 	{
   1374 	  as_bad ("%s", errmsg);
   1375 	  return;
   1376 	}
   1377       gas_cgen_finish_insn (insn.insn, insn.buffer,
   1378 			    CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
   1379       mep_check_for_disabled_registers (&insn);
   1380     }
   1381 }
   1382 
   1383 valueT
   1384 md_section_align (segT segment, valueT size)
   1385 {
   1386   int align = bfd_get_section_alignment (stdoutput, segment);
   1387   return ((size + (1 << align) - 1) & (-1 << align));
   1388 }
   1389 
   1390 
   1391 symbolS *
   1392 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
   1393 {
   1394   return 0;
   1395 }
   1396 
   1397 /* Interface to relax_segment.  */
   1399 
   1400 
   1401 const relax_typeS md_relax_table[] =
   1402 {
   1403   /* The fields are:
   1404      1) most positive reach of this state,
   1405      2) most negative reach of this state,
   1406      3) how many bytes this mode will have in the variable part of the frag
   1407      4) which index into the table to try if we can't fit into this one.  */
   1408   /* Note that we use "beq" because "jmp" has a peculiarity - it cannot
   1409      jump to addresses with any bits 27..24 set.  So, we use beq as a
   1410      17-bit pc-relative branch to avoid using jmp, just in case.  */
   1411 
   1412   /* 0 */ {     0,      0, 0, 0 }, /* unused */
   1413   /* 1 */ {     0,      0, 0, 0 }, /* marker for "don't know yet" */
   1414 
   1415   /* 2 */ {  2047,  -2048, 0, 3 }, /* bsr12 */
   1416   /* 3 */ {     0,      0, 2, 0 }, /* bsr16 */
   1417 
   1418   /* 4 */ {  2047,  -2048, 0, 5 }, /* bra */
   1419   /* 5 */ { 65535, -65536, 2, 6 }, /* beq $0,$0 */
   1420   /* 6 */ {     0,      0, 2, 0 }, /* jmp24 */
   1421 
   1422   /* 7 */ { 65535, -65536, 0, 8 }, /* beqi */
   1423   /* 8 */ {     0,      0, 4, 0 }, /* bnei/jmp */
   1424 
   1425   /* 9 */  {   127,   -128, 0, 10 }, /* beqz */
   1426   /* 10 */ { 65535, -65536, 2, 11 }, /* beqi */
   1427   /* 11 */ {     0,      0, 4,  0 }, /* bnei/jmp */
   1428 
   1429   /* 12 */ { 65535, -65536, 0, 13 }, /* bnei */
   1430   /* 13 */ {     0,      0, 4,  0 }, /* beqi/jmp */
   1431 
   1432   /* 14 */ {   127,   -128, 0, 15 }, /* bnez */
   1433   /* 15 */ { 65535, -65536, 2, 16 }, /* bnei */
   1434   /* 16 */ {     0,      0, 4,  0 },  /* beqi/jmp */
   1435 
   1436   /* 17 */ { 65535, -65536, 0, 13 }, /* bgei */
   1437   /* 18 */ {     0,      0, 4,  0 },
   1438   /* 19 */ { 65535, -65536, 0, 13 }, /* blti */
   1439   /* 20 */ {     0,      0, 4,  0 },
   1440   /* 19 */ { 65535, -65536, 0, 13 }, /* bcpeq */
   1441   /* 20 */ {     0,      0, 4,  0 },
   1442   /* 19 */ { 65535, -65536, 0, 13 }, /* bcpne */
   1443   /* 20 */ {     0,      0, 4,  0 },
   1444   /* 19 */ { 65535, -65536, 0, 13 }, /* bcpat */
   1445   /* 20 */ {     0,      0, 4,  0 },
   1446   /* 19 */ { 65535, -65536, 0, 13 }, /* bcpaf */
   1447   /* 20 */ {     0,      0, 4,  0 }
   1448 };
   1449 
   1450 /* Pseudo-values for 64 bit "insns" which are combinations of two 32
   1451    bit insns.  */
   1452 typedef enum {
   1453   MEP_PSEUDO64_NONE,
   1454   MEP_PSEUDO64_16BITCC,
   1455   MEP_PSEUDO64_32BITCC,
   1456 } MepPseudo64Values;
   1457 
   1458 static struct {
   1459   int insn;
   1460   int growth;
   1461   int insn_for_extern;
   1462 } subtype_mappings[] = {
   1463   { 0, 0, 0 },
   1464   { 0, 0, 0 },
   1465   { MEP_INSN_BSR12, 0, MEP_INSN_BSR24 },
   1466   { MEP_INSN_BSR24, 2, MEP_INSN_BSR24 },
   1467   { MEP_INSN_BRA,   0, MEP_INSN_BRA   },
   1468   { MEP_INSN_BEQ,   2, MEP_INSN_BEQ   },
   1469   { MEP_INSN_JMP,   2, MEP_INSN_JMP   },
   1470   { MEP_INSN_BEQI,  0, MEP_INSN_BEQI  },
   1471   { -1,             4, MEP_PSEUDO64_32BITCC },
   1472   { MEP_INSN_BEQZ,  0, MEP_INSN_BEQZ  },
   1473   { MEP_INSN_BEQI,  2, MEP_INSN_BEQI  },
   1474   { -1,             4, MEP_PSEUDO64_16BITCC },
   1475   { MEP_INSN_BNEI,  0, MEP_INSN_BNEI  },
   1476   { -1,             4, MEP_PSEUDO64_32BITCC },
   1477   { MEP_INSN_BNEZ,  0, MEP_INSN_BNEZ  },
   1478   { MEP_INSN_BNEI,  2, MEP_INSN_BNEI  },
   1479   { -1,             4, MEP_PSEUDO64_16BITCC },
   1480   { MEP_INSN_BGEI,  0, MEP_INSN_BGEI  },
   1481   { -1,             4, MEP_PSEUDO64_32BITCC },
   1482   { MEP_INSN_BLTI,  0, MEP_INSN_BLTI  },
   1483   { -1,             4, MEP_PSEUDO64_32BITCC },
   1484   { MEP_INSN_BCPEQ, 0, MEP_INSN_BCPEQ  },
   1485   { -1,             4, MEP_PSEUDO64_32BITCC },
   1486   { MEP_INSN_BCPNE, 0, MEP_INSN_BCPNE  },
   1487   { -1,             4, MEP_PSEUDO64_32BITCC },
   1488   { MEP_INSN_BCPAT, 0, MEP_INSN_BCPAT  },
   1489   { -1,             4, MEP_PSEUDO64_32BITCC },
   1490   { MEP_INSN_BCPAF, 0, MEP_INSN_BCPAF  },
   1491   { -1,             4, MEP_PSEUDO64_32BITCC }
   1492 };
   1493 #define NUM_MAPPINGS (sizeof (subtype_mappings) / sizeof (subtype_mappings[0]))
   1494 
   1495 void
   1496 mep_prepare_relax_scan (fragS *fragP, offsetT *aim, relax_substateT this_state)
   1497 {
   1498   symbolS *symbolP = fragP->fr_symbol;
   1499   if (symbolP && !S_IS_DEFINED (symbolP))
   1500     *aim = 0;
   1501   /* Adjust for MeP pcrel not being relative to the next opcode.  */
   1502   *aim += 2 + md_relax_table[this_state].rlx_length;
   1503 }
   1504 
   1505 static int
   1506 insn_to_subtype (int insn)
   1507 {
   1508   unsigned int i;
   1509   for (i=0; i<NUM_MAPPINGS; i++)
   1510     if (insn == subtype_mappings[i].insn)
   1511       return i;
   1512   abort ();
   1513 }
   1514 
   1515 /* Return an initial guess of the length by which a fragment must grow
   1516    to hold a branch to reach its destination.  Also updates fr_type
   1517    and fr_subtype as necessary.
   1518 
   1519    Called just before doing relaxation.  Any symbol that is now
   1520    undefined will not become defined.  The guess for fr_var is
   1521    ACTUALLY the growth beyond fr_fix.  Whatever we do to grow fr_fix
   1522    or fr_var contributes to our returned value.  Although it may not
   1523    be explicit in the frag, pretend fr_var starts with a 0 value.  */
   1524 
   1525 int
   1526 md_estimate_size_before_relax (fragS * fragP, segT segment)
   1527 {
   1528   if (fragP->fr_subtype == 1)
   1529     fragP->fr_subtype = insn_to_subtype (fragP->fr_cgen.insn->base->num);
   1530 
   1531   if (S_GET_SEGMENT (fragP->fr_symbol) != segment
   1532       || S_IS_WEAK (fragP->fr_symbol)
   1533 #ifdef MEP_IVC2_SUPPORTED
   1534       || (mep_cop == EF_MEP_COP_IVC2
   1535 	  && bfd_get_section_flags (stdoutput, segment) & SEC_MEP_VLIW)
   1536 #endif /* MEP_IVC2_SUPPORTED */
   1537       )
   1538     {
   1539       int new_insn;
   1540 
   1541       new_insn = subtype_mappings[fragP->fr_subtype].insn_for_extern;
   1542       fragP->fr_subtype = insn_to_subtype (new_insn);
   1543     }
   1544 
   1545   if (MEP_VLIW && ! MEP_VLIW64
   1546       && (bfd_get_section_flags (stdoutput, segment) & SEC_MEP_VLIW))
   1547     {
   1548       /* Use 32 bit branches for vliw32 so the vliw word is not split.  */
   1549       switch (fragP->fr_cgen.insn->base->num)
   1550 	{
   1551 	case MEP_INSN_BSR12:
   1552 	  fragP->fr_subtype = insn_to_subtype
   1553 	    (subtype_mappings[fragP->fr_subtype].insn_for_extern);
   1554 	  break;
   1555 	case MEP_INSN_BEQZ:
   1556 	  fragP->fr_subtype ++;
   1557 	  break;
   1558 	case MEP_INSN_BNEZ:
   1559 	  fragP->fr_subtype ++;
   1560 	  break;
   1561 	}
   1562     }
   1563 
   1564   if (fragP->fr_cgen.insn->base
   1565       && fragP->fr_cgen.insn->base->num
   1566          != subtype_mappings[fragP->fr_subtype].insn)
   1567     {
   1568       int new_insn= subtype_mappings[fragP->fr_subtype].insn;
   1569       if (new_insn != -1)
   1570 	{
   1571 	  fragP->fr_cgen.insn = (fragP->fr_cgen.insn
   1572 				 - fragP->fr_cgen.insn->base->num
   1573 				 + new_insn);
   1574 	}
   1575     }
   1576 
   1577 #ifdef MEP_IVC2_SUPPORTED
   1578   if (mep_cop == EF_MEP_COP_IVC2
   1579       && bfd_get_section_flags (stdoutput, segment) & SEC_MEP_VLIW)
   1580     return 0;
   1581 #endif /* MEP_IVC2_SUPPORTED */
   1582 
   1583   return subtype_mappings[fragP->fr_subtype].growth;
   1584 }
   1585 
   1586 /* VLIW does relaxing, but not growth.  */
   1587 
   1588 long
   1589 mep_relax_frag (segT segment, fragS *fragP, long stretch)
   1590 {
   1591   long rv = relax_frag (segment, fragP, stretch);
   1592 #ifdef MEP_IVC2_SUPPORTED
   1593   if (mep_cop == EF_MEP_COP_IVC2
   1594       && bfd_get_section_flags (stdoutput, segment) & SEC_MEP_VLIW)
   1595     return 0;
   1596 #endif
   1597   return rv;
   1598 }
   1599 
   1600 /* *fragP has been relaxed to its final size, and now needs to have
   1601    the bytes inside it modified to conform to the new size.
   1602 
   1603    Called after relaxation is finished.
   1604    fragP->fr_type == rs_machine_dependent.
   1605    fragP->fr_subtype is the subtype of what the address relaxed to.  */
   1606 
   1607 static int
   1608 target_address_for (fragS *frag)
   1609 {
   1610   int rv = frag->fr_offset;
   1611   symbolS *sym = frag->fr_symbol;
   1612 
   1613   if (sym)
   1614     rv += S_GET_VALUE (sym);
   1615 
   1616   return rv;
   1617 }
   1618 
   1619 void
   1620 md_convert_frag (bfd *abfd  ATTRIBUTE_UNUSED,
   1621 		 segT seg ATTRIBUTE_UNUSED,
   1622 		 fragS *fragP)
   1623 {
   1624   int addend, rn, bit = 0;
   1625   int operand;
   1626   int where = fragP->fr_opcode - fragP->fr_literal;
   1627   int e = target_big_endian ? 0 : 1;
   1628   int core_mode;
   1629 
   1630 #ifdef MEP_IVC2_SUPPORTED
   1631   if (bfd_get_section_flags (stdoutput, seg) & SEC_MEP_VLIW
   1632       && mep_cop == EF_MEP_COP_IVC2)
   1633     core_mode = 0;
   1634   else
   1635 #endif /* MEP_IVC2_SUPPORTED */
   1636     core_mode = 1;
   1637 
   1638   addend = target_address_for (fragP) - (fragP->fr_address + where);
   1639 
   1640   if (subtype_mappings[fragP->fr_subtype].insn == -1)
   1641     {
   1642       if (core_mode)
   1643 	fragP->fr_fix += subtype_mappings[fragP->fr_subtype].growth;
   1644       switch (subtype_mappings[fragP->fr_subtype].insn_for_extern)
   1645 	{
   1646 	case MEP_PSEUDO64_16BITCC:
   1647 	  fragP->fr_opcode[1^e] = ((fragP->fr_opcode[1^e] & 1) ^ 1) | 0x06;
   1648 	  fragP->fr_opcode[2^e] = 0xd8;
   1649 	  fragP->fr_opcode[3^e] = 0x08;
   1650 	  fragP->fr_opcode[4^e] = 0;
   1651 	  fragP->fr_opcode[5^e] = 0;
   1652 	  where += 2;
   1653 	  break;
   1654 	case MEP_PSEUDO64_32BITCC:
   1655 	  if (fragP->fr_opcode[0^e] & 0x10)
   1656 	    fragP->fr_opcode[1^e] ^= 0x01;
   1657 	  else
   1658 	    fragP->fr_opcode[1^e] ^= 0x04;
   1659 	  fragP->fr_opcode[2^e] = 0;
   1660 	  fragP->fr_opcode[3^e] = 4;
   1661 	  fragP->fr_opcode[4^e] = 0xd8;
   1662 	  fragP->fr_opcode[5^e] = 0x08;
   1663 	  fragP->fr_opcode[6^e] = 0;
   1664 	  fragP->fr_opcode[7^e] = 0;
   1665 	  where += 4;
   1666 	  break;
   1667 	default:
   1668 	  abort ();
   1669 	}
   1670       fragP->fr_cgen.insn = (fragP->fr_cgen.insn
   1671 			     - fragP->fr_cgen.insn->base->num
   1672 			     + MEP_INSN_JMP);
   1673       operand = MEP_OPERAND_PCABS24A2;
   1674     }
   1675   else
   1676     switch (fragP->fr_cgen.insn->base->num)
   1677       {
   1678       case MEP_INSN_BSR12:
   1679 	fragP->fr_opcode[0^e] = 0xb0 | ((addend >> 8) & 0x0f);
   1680 	fragP->fr_opcode[1^e] = 0x01 | (addend & 0xfe);
   1681 	operand = MEP_OPERAND_PCREL12A2;
   1682 	break;
   1683 
   1684       case MEP_INSN_BSR24:
   1685 	if (core_mode)
   1686 	  fragP->fr_fix += 2;
   1687 	fragP->fr_opcode[0^e] = 0xd8 | ((addend >> 5) & 0x07);
   1688 	fragP->fr_opcode[1^e] = 0x09 | ((addend << 3) & 0xf0);
   1689 	fragP->fr_opcode[2^e] = 0x00 | ((addend >>16) & 0xff);
   1690 	fragP->fr_opcode[3^e] = 0x00 | ((addend >> 8) & 0xff);
   1691 	operand = MEP_OPERAND_PCREL24A2;
   1692 	break;
   1693 
   1694       case MEP_INSN_BRA:
   1695 	fragP->fr_opcode[0^e] = 0xb0 | ((addend >> 8) & 0x0f);
   1696 	fragP->fr_opcode[1^e] = 0x00 | (addend & 0xfe);
   1697 	operand = MEP_OPERAND_PCREL12A2;
   1698 	break;
   1699 
   1700       case MEP_INSN_BEQ:
   1701 	/* The default relax_frag doesn't change the state if there is no
   1702 	   growth, so we must manually handle converting out-of-range BEQ
   1703 	   instructions to JMP.  */
   1704 	if (addend <= 65535 && addend >= -65536)
   1705 	  {
   1706 	    if (core_mode)
   1707 	      fragP->fr_fix += 2;
   1708 	    fragP->fr_opcode[0^e] = 0xe0;
   1709 	    fragP->fr_opcode[1^e] = 0x01;
   1710 	    fragP->fr_opcode[2^e] = 0x00 | ((addend >> 9) & 0xff);
   1711 	    fragP->fr_opcode[3^e] = 0x00 | ((addend >> 1) & 0xff);
   1712 	    operand = MEP_OPERAND_PCREL17A2;
   1713 	    break;
   1714 	  }
   1715 	/* ...FALLTHROUGH... */
   1716 
   1717       case MEP_INSN_JMP:
   1718 	addend = target_address_for (fragP);
   1719 	if (core_mode)
   1720 	  fragP->fr_fix += 2;
   1721 	fragP->fr_opcode[0^e] = 0xd8 | ((addend >> 5) & 0x07);
   1722 	fragP->fr_opcode[1^e] = 0x08 | ((addend << 3) & 0xf0);
   1723 	fragP->fr_opcode[2^e] = 0x00 | ((addend >>16) & 0xff);
   1724 	fragP->fr_opcode[3^e] = 0x00 | ((addend >> 8) & 0xff);
   1725 	operand = MEP_OPERAND_PCABS24A2;
   1726 	break;
   1727 
   1728       case MEP_INSN_BNEZ:
   1729 	bit = 1;
   1730       case MEP_INSN_BEQZ:
   1731 	fragP->fr_opcode[1^e] = bit | (addend & 0xfe);
   1732 	operand = MEP_OPERAND_PCREL8A2;
   1733 	break;
   1734 
   1735       case MEP_INSN_BNEI:
   1736 	bit = 4;
   1737       case MEP_INSN_BEQI:
   1738 	if (subtype_mappings[fragP->fr_subtype].growth)
   1739 	  {
   1740 	    if (core_mode)
   1741 	      fragP->fr_fix += subtype_mappings[fragP->fr_subtype].growth;
   1742 	    rn = fragP->fr_opcode[0^e] & 0x0f;
   1743 	    fragP->fr_opcode[0^e] = 0xe0 | rn;
   1744 	    fragP->fr_opcode[1^e] = bit;
   1745 	  }
   1746 	fragP->fr_opcode[2^e] = 0x00 | ((addend >> 9) & 0xff);
   1747 	fragP->fr_opcode[3^e] = 0x00 | ((addend >> 1) & 0xff);
   1748 	operand = MEP_OPERAND_PCREL17A2;
   1749 	break;
   1750 
   1751       case MEP_INSN_BLTI:
   1752       case MEP_INSN_BGEI:
   1753       case MEP_INSN_BCPEQ:
   1754       case MEP_INSN_BCPNE:
   1755       case MEP_INSN_BCPAT:
   1756       case MEP_INSN_BCPAF:
   1757 	/* No opcode change needed, just operand.  */
   1758 	fragP->fr_opcode[2^e] = (addend >> 9) & 0xff;
   1759 	fragP->fr_opcode[3^e] = (addend >> 1) & 0xff;
   1760 	operand = MEP_OPERAND_PCREL17A2;
   1761 	break;
   1762 
   1763       default:
   1764 	abort ();
   1765       }
   1766 
   1767   if (S_GET_SEGMENT (fragP->fr_symbol) != seg
   1768       || S_IS_WEAK (fragP->fr_symbol)
   1769       || operand == MEP_OPERAND_PCABS24A2)
   1770     {
   1771       gas_assert (fragP->fr_cgen.insn != 0);
   1772       gas_cgen_record_fixup (fragP,
   1773 			     where,
   1774 			     fragP->fr_cgen.insn,
   1775 			     (fragP->fr_fix - where) * 8,
   1776 			     cgen_operand_lookup_by_num (gas_cgen_cpu_desc,
   1777 							 operand),
   1778 			     fragP->fr_cgen.opinfo,
   1779 			     fragP->fr_symbol, fragP->fr_offset);
   1780     }
   1781 }
   1782 
   1783 
   1784 /* Functions concerning relocs.  */
   1786 
   1787 void
   1788 mep_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
   1789 {
   1790   /* If we already know the fixup value, adjust it in the same
   1791      way that the linker would have done.  */
   1792   if (fixP->fx_addsy == 0)
   1793     switch (fixP->fx_cgen.opinfo)
   1794       {
   1795       case BFD_RELOC_MEP_LOW16:
   1796 	*valP = ((long)(*valP & 0xffff)) << 16 >> 16;
   1797 	break;
   1798       case BFD_RELOC_MEP_HI16U:
   1799 	*valP >>= 16;
   1800 	break;
   1801       case BFD_RELOC_MEP_HI16S:
   1802 	*valP = (*valP + 0x8000) >> 16;
   1803 	break;
   1804       }
   1805 
   1806   /* Now call cgen's md_aply_fix.  */
   1807   gas_cgen_md_apply_fix (fixP, valP, seg);
   1808 }
   1809 
   1810 long
   1811 md_pcrel_from_section (fixS *fixP, segT sec)
   1812 {
   1813   if (fixP->fx_addsy != (symbolS *) NULL
   1814       && (! S_IS_DEFINED (fixP->fx_addsy)
   1815 	  || S_IS_WEAK (fixP->fx_addsy)
   1816 	  || S_GET_SEGMENT (fixP->fx_addsy) != sec))
   1817     /* The symbol is undefined (or is defined but not in this section).
   1818        Let the linker figure it out.  */
   1819     return 0;
   1820 
   1821   /* If we've got other reasons for emitting this relocation, let the
   1822      linker handle pc-rel also.  */
   1823   if (mep_force_relocation (fixP))
   1824     return 0;
   1825 
   1826   /* Return the address of the opcode - cgen adjusts for opcode size
   1827      itself, to be consistent with the disassembler, which must do
   1828      so.  */
   1829   return fixP->fx_where + fixP->fx_frag->fr_address;
   1830 }
   1831 
   1832 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
   1833    Returns BFD_RELOC_NONE if no reloc type can be found.
   1834    *FIXP may be modified if desired.  */
   1835 
   1836 #if defined (__STDC__) || defined (ALMOST_STDC) || defined (HAVE_STRINGIZE)
   1837 #define MAP(n) case MEP_OPERAND_##n: return BFD_RELOC_MEP_##n;
   1838 #else
   1839 #define MAP(n) case MEP_OPERAND_/**/n: return BFD_RELOC_MEP_/**/n;
   1840 #endif
   1841 
   1842 bfd_reloc_code_real_type
   1843 md_cgen_lookup_reloc (const CGEN_INSN *insn ATTRIBUTE_UNUSED,
   1844 		      const CGEN_OPERAND *operand,
   1845 		      fixS *fixP)
   1846 {
   1847   enum bfd_reloc_code_real reloc = fixP->fx_cgen.opinfo;
   1848   static char printed[MEP_OPERAND_MAX] = { 0 };
   1849 
   1850   /* If there's a reloc here, it's because the parser saw a %foo() and
   1851      is giving us the correct reloc to use, or because we converted to
   1852      a different size reloc below and want to avoid "converting" more
   1853      than once.  */
   1854   if (reloc && reloc != BFD_RELOC_NONE)
   1855     return reloc;
   1856 
   1857   switch (operand->type)
   1858     {
   1859       MAP (PCREL8A2);	/* beqz */
   1860       MAP (PCREL12A2);	/* bsr16 */
   1861       MAP (PCREL17A2);	/* beqi */
   1862       MAP (PCREL24A2);	/* bsr24 */
   1863       MAP (PCABS24A2);	/* jmp */
   1864       MAP (UIMM24);	/* mov */
   1865       MAP (ADDR24A4);	/* sw/lw */
   1866 
   1867     /* The rest of the relocs should be generated by the parser,
   1868        for things such as %tprel(), etc. */
   1869     case MEP_OPERAND_SIMM16:
   1870 #ifdef OBJ_COMPLEX_RELC
   1871       /* coalescing this into RELOC_MEP_16 is actually a bug,
   1872 	 since it's a signed operand. let the relc code handle it. */
   1873       return BFD_RELOC_RELC;
   1874 #endif
   1875 
   1876     case MEP_OPERAND_UIMM16:
   1877     case MEP_OPERAND_SDISP16:
   1878     case MEP_OPERAND_CODE16:
   1879       fixP->fx_where += 2;
   1880       /* to avoid doing the above add twice */
   1881       fixP->fx_cgen.opinfo = BFD_RELOC_MEP_16;
   1882       return BFD_RELOC_MEP_16;
   1883 
   1884     default:
   1885 #ifdef OBJ_COMPLEX_RELC
   1886       /* this is not an error, yet.
   1887 	 pass it to the linker. */
   1888       return BFD_RELOC_RELC;
   1889 #endif
   1890       if (printed[operand->type])
   1891 	return BFD_RELOC_NONE;
   1892       printed[operand->type] = 1;
   1893 
   1894       as_bad_where (fixP->fx_file, fixP->fx_line,
   1895 		    _("Don't know how to relocate plain operands of type %s"),
   1896 		    operand->name);
   1897 
   1898       /* Print some helpful hints for the user.  */
   1899       switch (operand->type)
   1900 	{
   1901 	case MEP_OPERAND_UDISP7:
   1902 	case MEP_OPERAND_UDISP7A2:
   1903 	case MEP_OPERAND_UDISP7A4:
   1904 	  as_bad_where (fixP->fx_file, fixP->fx_line,
   1905 			_("Perhaps you are missing %%tpoff()?"));
   1906 	  break;
   1907 	default:
   1908 	  break;
   1909 	}
   1910       return BFD_RELOC_NONE;
   1911     }
   1912 }
   1913 
   1914 /* Called while parsing an instruction to create a fixup.
   1915    We need to check for HI16 relocs and queue them up for later sorting.  */
   1916 
   1917 fixS *
   1918 mep_cgen_record_fixup_exp (fragS *frag,
   1919 			   int where,
   1920 			   const CGEN_INSN *insn,
   1921 			   int length,
   1922 			   const CGEN_OPERAND *operand,
   1923 			   int opinfo,
   1924 			   expressionS *exp)
   1925 {
   1926   fixS * fixP = gas_cgen_record_fixup_exp (frag, where, insn, length,
   1927 					   operand, opinfo, exp);
   1928   return fixP;
   1929 }
   1930 
   1931 /* Return BFD reloc type from opinfo field in a fixS.
   1932    It's tricky using fx_r_type in mep_frob_file because the values
   1933    are BFD_RELOC_UNUSED + operand number.  */
   1934 #define FX_OPINFO_R_TYPE(f) ((f)->fx_cgen.opinfo)
   1935 
   1936 /* Sort any unmatched HI16 relocs so that they immediately precede
   1937    the corresponding LO16 reloc.  This is called before md_apply_fix and
   1938    tc_gen_reloc.  */
   1939 
   1940 void
   1941 mep_frob_file ()
   1942 {
   1943   struct mep_hi_fixup * l;
   1944 
   1945   for (l = mep_hi_fixup_list; l != NULL; l = l->next)
   1946     {
   1947       segment_info_type * seginfo;
   1948       int pass;
   1949 
   1950       gas_assert (FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_HI16
   1951 	      || FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_LO16);
   1952 
   1953       /* Check quickly whether the next fixup happens to be a matching low.  */
   1954       if (l->fixp->fx_next != NULL
   1955 	  && FX_OPINFO_R_TYPE (l->fixp->fx_next) == BFD_RELOC_LO16
   1956 	  && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
   1957 	  && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
   1958 	continue;
   1959 
   1960       /* Look through the fixups for this segment for a matching
   1961          `low'.  When we find one, move the high just in front of it.
   1962          We do this in two passes.  In the first pass, we try to find
   1963          a unique `low'.  In the second pass, we permit multiple
   1964          high's relocs for a single `low'.  */
   1965       seginfo = seg_info (l->seg);
   1966       for (pass = 0; pass < 2; pass++)
   1967 	{
   1968 	  fixS * f;
   1969 	  fixS * prev;
   1970 
   1971 	  prev = NULL;
   1972 	  for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
   1973 	    {
   1974 	      /* Check whether this is a `low' fixup which matches l->fixp.  */
   1975 	      if (FX_OPINFO_R_TYPE (f) == BFD_RELOC_LO16
   1976 		  && f->fx_addsy == l->fixp->fx_addsy
   1977 		  && f->fx_offset == l->fixp->fx_offset
   1978 		  && (pass == 1
   1979 		      || prev == NULL
   1980 		      || (FX_OPINFO_R_TYPE (prev) != BFD_RELOC_HI16)
   1981 		      || prev->fx_addsy != f->fx_addsy
   1982 		      || prev->fx_offset !=  f->fx_offset))
   1983 		{
   1984 		  fixS ** pf;
   1985 
   1986 		  /* Move l->fixp before f.  */
   1987 		  for (pf = &seginfo->fix_root;
   1988 		       * pf != l->fixp;
   1989 		       pf = & (* pf)->fx_next)
   1990 		    gas_assert (* pf != NULL);
   1991 
   1992 		  * pf = l->fixp->fx_next;
   1993 
   1994 		  l->fixp->fx_next = f;
   1995 		  if (prev == NULL)
   1996 		    seginfo->fix_root = l->fixp;
   1997 		  else
   1998 		    prev->fx_next = l->fixp;
   1999 
   2000 		  break;
   2001 		}
   2002 
   2003 	      prev = f;
   2004 	    }
   2005 
   2006 	  if (f != NULL)
   2007 	    break;
   2008 
   2009 	  if (pass == 1)
   2010 	    as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
   2011 			   _("Unmatched high relocation"));
   2012 	}
   2013     }
   2014 }
   2015 
   2016 /* See whether we need to force a relocation into the output file. */
   2017 
   2018 int
   2019 mep_force_relocation (fixS *fixp)
   2020 {
   2021   if (   fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
   2022 	 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
   2023     return 1;
   2024 
   2025   if (generic_force_reloc (fixp))
   2026     return 1;
   2027 
   2028   /* Allow branches to global symbols to be resolved at assembly time.
   2029      This is consistent with way relaxable branches are handled, since
   2030      branches to both global and local symbols are relaxed.  It also
   2031      corresponds to the assumptions made in md_pcrel_from_section.  */
   2032   return S_FORCE_RELOC (fixp->fx_addsy, !fixp->fx_pcrel);
   2033 }
   2034 
   2035 /* Write a value out to the object file, using the appropriate endianness.  */
   2037 
   2038 void
   2039 md_number_to_chars (char *buf, valueT val, int n)
   2040 {
   2041   if (target_big_endian)
   2042     number_to_chars_bigendian (buf, val, n);
   2043   else
   2044     number_to_chars_littleendian (buf, val, n);
   2045 }
   2046 
   2047 char *
   2048 md_atof (int type, char *litP, int *sizeP)
   2049 {
   2050   return ieee_md_atof (type, litP, sizeP, TRUE);
   2051 }
   2052 
   2053 bfd_boolean
   2054 mep_fix_adjustable (fixS *fixP)
   2055 {
   2056   bfd_reloc_code_real_type reloc_type;
   2057 
   2058   if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
   2059     {
   2060       const CGEN_INSN *insn = NULL;
   2061       int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
   2062       const CGEN_OPERAND *operand
   2063 	= cgen_operand_lookup_by_num(gas_cgen_cpu_desc, opindex);
   2064       reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
   2065     }
   2066   else
   2067     reloc_type = fixP->fx_r_type;
   2068 
   2069   if (fixP->fx_addsy == NULL)
   2070     return 1;
   2071 
   2072   /* Prevent all adjustments to global symbols. */
   2073   if (S_IS_EXTERNAL (fixP->fx_addsy))
   2074     return 0;
   2075 
   2076   if (S_IS_WEAK (fixP->fx_addsy))
   2077     return 0;
   2078 
   2079   /* We need the symbol name for the VTABLE entries */
   2080   if (reloc_type == BFD_RELOC_VTABLE_INHERIT
   2081       || reloc_type == BFD_RELOC_VTABLE_ENTRY)
   2082     return 0;
   2083 
   2084   return 1;
   2085 }
   2086 
   2087 bfd_vma
   2088 mep_elf_section_letter (int letter, char **ptrmsg)
   2089 {
   2090   if (letter == 'v')
   2091     return SHF_MEP_VLIW;
   2092 
   2093   *ptrmsg = _("bad .section directive: want a,v,w,x,M,S in string");
   2094   return -1;
   2095 }
   2096 
   2097 flagword
   2098 mep_elf_section_flags (flagword flags, bfd_vma attr, int type ATTRIBUTE_UNUSED)
   2099 {
   2100   if (attr & SHF_MEP_VLIW)
   2101     flags |= SEC_MEP_VLIW;
   2102   return flags;
   2103 }
   2104 
   2105 /* In vliw mode, the default section is .vtext.  We have to be able
   2106    to switch into .vtext using only the .vtext directive.  */
   2107 
   2108 static segT
   2109 mep_vtext_section (void)
   2110 {
   2111   static segT vtext_section;
   2112 
   2113   if (! vtext_section)
   2114     {
   2115       flagword applicable = bfd_applicable_section_flags (stdoutput);
   2116       vtext_section = subseg_new (VTEXT_SECTION_NAME, 0);
   2117       bfd_set_section_flags (stdoutput, vtext_section,
   2118 			     applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
   2119 					   | SEC_CODE | SEC_READONLY
   2120 					   | SEC_MEP_VLIW));
   2121     }
   2122 
   2123   return vtext_section;
   2124 }
   2125 
   2126 static void
   2127 mep_s_vtext (int ignore ATTRIBUTE_UNUSED)
   2128 {
   2129   int temp;
   2130 
   2131   /* Record previous_section and previous_subsection.  */
   2132   obj_elf_section_change_hook ();
   2133 
   2134   temp = get_absolute_expression ();
   2135   subseg_set (mep_vtext_section (), (subsegT) temp);
   2136   demand_empty_rest_of_line ();
   2137 }
   2138 
   2139 static void
   2140 mep_switch_to_core_mode (int dummy ATTRIBUTE_UNUSED)
   2141 {
   2142   mep_process_saved_insns ();
   2143   pluspresent = 0;
   2144   mode = CORE;
   2145 }
   2146 
   2147 static void
   2148 mep_switch_to_vliw_mode (int dummy ATTRIBUTE_UNUSED)
   2149 {
   2150   if (! MEP_VLIW)
   2151     as_bad (_(".vliw unavailable when VLIW is disabled."));
   2152   mode = VLIW;
   2153   /* Switch into .vtext here too. */
   2154   /* mep_s_vtext(); */
   2155 }
   2156 
   2157 /* This is an undocumented pseudo-op used to disable gas's
   2158    "disabled_registers" check.  Used for code which checks for those
   2159    registers at runtime.  */
   2160 static void
   2161 mep_noregerr (int i ATTRIBUTE_UNUSED)
   2162 {
   2163   allow_disabled_registers = 1;
   2164 }
   2165 
   2166 /* mep_unrecognized_line: This is called when a line that can't be parsed
   2167    is encountered.  We use it to check for a leading '+' sign which indicates
   2168    that the current instruction is a coprocessor instruction that is to be
   2169    parallelized with a previous core insn.  This function accepts the '+' and
   2170    rejects all other characters that might indicate garbage at the beginning
   2171    of the line.  The '+' character gets lost as the calling loop continues,
   2172    so we need to indicate that we saw it.  */
   2173 
   2174 int
   2175 mep_unrecognized_line (int ch)
   2176 {
   2177   switch (ch)
   2178     {
   2179     case '+':
   2180       pluspresent = 1;
   2181       return 1; /* '+' indicates an instruction to be parallelized. */
   2182     default:
   2183       return 0; /* If it's not a '+', the line can't be parsed. */
   2184     }
   2185 }
   2186 
   2187 void
   2188 mep_cleanup (void)
   2189 {
   2190   /* Take care of any insns left to be parallelized when the file ends.
   2191      This is mainly here to handle the case where the file ends with an
   2192      insn preceeded by a + or the file ends unexpectedly.  */
   2193   if (mode == VLIW)
   2194     mep_process_saved_insns ();
   2195 }
   2196 
   2197 int
   2198 mep_flush_pending_output (void)
   2199 {
   2200   if (mode == VLIW)
   2201     {
   2202       mep_process_saved_insns ();
   2203       pluspresent = 0;
   2204     }
   2205 
   2206   return 1;
   2207 }
   2208