Home | History | Annotate | Line # | Download | only in opcodes
cris-dis.c revision 1.1.1.1
      1 /* Disassembler code for CRIS.
      2    Copyright 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2012
      3    Free Software Foundation, Inc.
      4    Contributed by Axis Communications AB, Lund, Sweden.
      5    Written by Hans-Peter Nilsson.
      6 
      7    This file is part of the GNU opcodes library.
      8 
      9    This library is free software; you can redistribute it and/or modify
     10    it under the terms of the GNU General Public License as published by
     11    the Free Software Foundation; either version 3, or (at your option)
     12    any later version.
     13 
     14    It is distributed in the hope that it will be useful, but WITHOUT
     15    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     16    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
     17    License for more details.
     18 
     19    You should have received a copy of the GNU General Public License
     20    along with this program; if not, write to the Free Software
     21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     22    MA 02110-1301, USA.  */
     23 
     24 #include "sysdep.h"
     25 #include "dis-asm.h"
     26 #include "opcode/cris.h"
     27 #include "libiberty.h"
     28 
     29 /* No instruction will be disassembled longer than this.  In theory, and
     31    in silicon, address prefixes can be cascaded.  In practice, cascading
     32    is not used by GCC, and not supported by the assembler.  */
     33 #ifndef MAX_BYTES_PER_CRIS_INSN
     34 #define MAX_BYTES_PER_CRIS_INSN 8
     35 #endif
     36 
     37 /* Whether or not to decode prefixes, folding it into the following
     38    instruction.  FIXME: Make this optional later.  */
     39 #ifndef PARSE_PREFIX
     40 #define PARSE_PREFIX 1
     41 #endif
     42 
     43 /* Sometimes we prefix all registers with this character.  */
     44 #define REGISTER_PREFIX_CHAR '$'
     45 
     46 /* Whether or not to trace the following sequence:
     47    sub* X,r%d
     48    bound* Y,r%d
     49    adds.w [pc+r%d.w],pc
     50 
     51    This is the assembly form of a switch-statement in C.
     52    The "sub is optional.  If there is none, then X will be zero.
     53    X is the value of the first case,
     54    Y is the number of cases (including default).
     55 
     56    This results in case offsets printed on the form:
     57     case N: -> case_address
     58    where N is an estimation on the corresponding 'case' operand in C,
     59    and case_address is where execution of that case continues after the
     60    sequence presented above.
     61 
     62    The old style of output was to print the offsets as instructions,
     63    which made it hard to follow "case"-constructs in the disassembly,
     64    and caused a lot of annoying warnings about undefined instructions.
     65 
     66    FIXME: Make this optional later.  */
     67 #ifndef TRACE_CASE
     68 #define TRACE_CASE (disdata->trace_case)
     69 #endif
     70 
     71 enum cris_disass_family
     72  { cris_dis_v0_v10, cris_dis_common_v10_v32, cris_dis_v32 };
     73 
     74 /* Stored in the disasm_info->private_data member.  */
     75 struct cris_disasm_data
     76 {
     77   /* Whether to print something less confusing if we find something
     78      matching a switch-construct.  */
     79   bfd_boolean trace_case;
     80 
     81   /* Whether this code is flagged as crisv32.  FIXME: Should be an enum
     82      that includes "compatible".  */
     83   enum cris_disass_family distype;
     84 };
     85 
     86 /* Value of first element in switch.  */
     87 static long case_offset = 0;
     88 
     89 /* How many more case-offsets to print.  */
     90 static long case_offset_counter = 0;
     91 
     92 /* Number of case offsets.  */
     93 static long no_of_case_offsets = 0;
     94 
     95 /* Candidate for next case_offset.  */
     96 static long last_immediate = 0;
     97 
     98 static int cris_constraint
     99   (const char *, unsigned, unsigned, struct cris_disasm_data *);
    100 
    101 /* Parse disassembler options and store state in info.  FIXME: For the
    102    time being, we abuse static variables.  */
    103 
    104 static bfd_boolean
    105 cris_parse_disassembler_options (disassemble_info *info,
    106 				 enum cris_disass_family distype)
    107 {
    108   struct cris_disasm_data *disdata;
    109 
    110   info->private_data = calloc (1, sizeof (struct cris_disasm_data));
    111   disdata = (struct cris_disasm_data *) info->private_data;
    112   if (disdata == NULL)
    113     return FALSE;
    114 
    115   /* Default true.  */
    116   disdata->trace_case
    117     = (info->disassembler_options == NULL
    118        || (strcmp (info->disassembler_options, "nocase") != 0));
    119 
    120   disdata->distype = distype;
    121   return TRUE;
    122 }
    123 
    124 static const struct cris_spec_reg *
    125 spec_reg_info (unsigned int sreg, enum cris_disass_family distype)
    126 {
    127   int i;
    128 
    129   for (i = 0; cris_spec_regs[i].name != NULL; i++)
    130     {
    131       if (cris_spec_regs[i].number == sreg)
    132 	{
    133 	  if (distype == cris_dis_v32)
    134 	    switch (cris_spec_regs[i].applicable_version)
    135 	      {
    136 	      case cris_ver_warning:
    137 	      case cris_ver_version_all:
    138 	      case cris_ver_v3p:
    139 	      case cris_ver_v8p:
    140 	      case cris_ver_v10p:
    141 	      case cris_ver_v32p:
    142 		/* No ambiguous sizes or register names with CRISv32.  */
    143 		if (cris_spec_regs[i].warning == NULL)
    144 		  return &cris_spec_regs[i];
    145 	      default:
    146 		;
    147 	      }
    148 	  else if (cris_spec_regs[i].applicable_version != cris_ver_v32p)
    149 	    return &cris_spec_regs[i];
    150 	}
    151     }
    152 
    153   return NULL;
    154 }
    155 
    156 /* Return the number of bits in the argument.  */
    157 
    158 static int
    159 number_of_bits (unsigned int val)
    160 {
    161   int bits;
    162 
    163   for (bits = 0; val != 0; val &= val - 1)
    164     bits++;
    165 
    166   return bits;
    167 }
    168 
    169 /* Get an entry in the opcode-table.  */
    170 
    171 static const struct cris_opcode *
    172 get_opcode_entry (unsigned int insn,
    173 		  unsigned int prefix_insn,
    174 		  struct cris_disasm_data *disdata)
    175 {
    176   /* For non-prefixed insns, we keep a table of pointers, indexed by the
    177      insn code.  Each entry is initialized when found to be NULL.  */
    178   static const struct cris_opcode **opc_table = NULL;
    179 
    180   const struct cris_opcode *max_matchedp = NULL;
    181   const struct cris_opcode **prefix_opc_table = NULL;
    182 
    183   /* We hold a table for each prefix that need to be handled differently.  */
    184   static const struct cris_opcode **dip_prefixes = NULL;
    185   static const struct cris_opcode **bdapq_m1_prefixes = NULL;
    186   static const struct cris_opcode **bdapq_m2_prefixes = NULL;
    187   static const struct cris_opcode **bdapq_m4_prefixes = NULL;
    188   static const struct cris_opcode **rest_prefixes = NULL;
    189 
    190   /* Allocate and clear the opcode-table.  */
    191   if (opc_table == NULL)
    192     {
    193       opc_table = malloc (65536 * sizeof (opc_table[0]));
    194       if (opc_table == NULL)
    195 	return NULL;
    196 
    197       memset (opc_table, 0, 65536 * sizeof (const struct cris_opcode *));
    198 
    199       dip_prefixes
    200 	= malloc (65536 * sizeof (const struct cris_opcode **));
    201       if (dip_prefixes == NULL)
    202 	return NULL;
    203 
    204       memset (dip_prefixes, 0, 65536 * sizeof (dip_prefixes[0]));
    205 
    206       bdapq_m1_prefixes
    207 	= malloc (65536 * sizeof (const struct cris_opcode **));
    208       if (bdapq_m1_prefixes == NULL)
    209 	return NULL;
    210 
    211       memset (bdapq_m1_prefixes, 0, 65536 * sizeof (bdapq_m1_prefixes[0]));
    212 
    213       bdapq_m2_prefixes
    214 	= malloc (65536 * sizeof (const struct cris_opcode **));
    215       if (bdapq_m2_prefixes == NULL)
    216 	return NULL;
    217 
    218       memset (bdapq_m2_prefixes, 0, 65536 * sizeof (bdapq_m2_prefixes[0]));
    219 
    220       bdapq_m4_prefixes
    221 	= malloc (65536 * sizeof (const struct cris_opcode **));
    222       if (bdapq_m4_prefixes == NULL)
    223 	return NULL;
    224 
    225       memset (bdapq_m4_prefixes, 0, 65536 * sizeof (bdapq_m4_prefixes[0]));
    226 
    227       rest_prefixes
    228 	= malloc (65536 * sizeof (const struct cris_opcode **));
    229       if (rest_prefixes == NULL)
    230 	return NULL;
    231 
    232       memset (rest_prefixes, 0, 65536 * sizeof (rest_prefixes[0]));
    233     }
    234 
    235   /* Get the right table if this is a prefix.
    236      This code is connected to cris_constraints in that it knows what
    237      prefixes play a role in recognition of patterns; the necessary
    238      state is reflected by which table is used.  If constraints
    239      involving match or non-match of prefix insns are changed, then this
    240      probably needs changing too.  */
    241   if (prefix_insn != NO_CRIS_PREFIX)
    242     {
    243       const struct cris_opcode *popcodep
    244 	= (opc_table[prefix_insn] != NULL
    245 	   ? opc_table[prefix_insn]
    246 	   : get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata));
    247 
    248       if (popcodep == NULL)
    249 	return NULL;
    250 
    251       if (popcodep->match == BDAP_QUICK_OPCODE)
    252 	{
    253 	  /* Since some offsets are recognized with "push" macros, we
    254 	     have to have different tables for them.  */
    255 	  int offset = (prefix_insn & 255);
    256 
    257 	  if (offset > 127)
    258 	    offset -= 256;
    259 
    260 	  switch (offset)
    261 	    {
    262 	    case -4:
    263 	      prefix_opc_table = bdapq_m4_prefixes;
    264 	      break;
    265 
    266 	    case -2:
    267 	      prefix_opc_table = bdapq_m2_prefixes;
    268 	      break;
    269 
    270 	    case -1:
    271 	      prefix_opc_table = bdapq_m1_prefixes;
    272 	      break;
    273 
    274 	    default:
    275 	      prefix_opc_table = rest_prefixes;
    276 	      break;
    277 	    }
    278 	}
    279       else if (popcodep->match == DIP_OPCODE)
    280 	/* We don't allow postincrement when the prefix is DIP, so use a
    281 	   different table for DIP.  */
    282 	prefix_opc_table = dip_prefixes;
    283       else
    284 	prefix_opc_table = rest_prefixes;
    285     }
    286 
    287   if (prefix_insn != NO_CRIS_PREFIX
    288       && prefix_opc_table[insn] != NULL)
    289     max_matchedp = prefix_opc_table[insn];
    290   else if (prefix_insn == NO_CRIS_PREFIX && opc_table[insn] != NULL)
    291     max_matchedp = opc_table[insn];
    292   else
    293     {
    294       const struct cris_opcode *opcodep;
    295       int max_level_of_match = -1;
    296 
    297       for (opcodep = cris_opcodes;
    298 	   opcodep->name != NULL;
    299 	   opcodep++)
    300 	{
    301 	  int level_of_match;
    302 
    303 	  if (disdata->distype == cris_dis_v32)
    304 	    {
    305 	      switch (opcodep->applicable_version)
    306 		{
    307 		case cris_ver_version_all:
    308 		  break;
    309 
    310 		case cris_ver_v0_3:
    311 		case cris_ver_v0_10:
    312 		case cris_ver_v3_10:
    313 		case cris_ver_sim_v0_10:
    314 		case cris_ver_v8_10:
    315 		case cris_ver_v10:
    316 		case cris_ver_warning:
    317 		  continue;
    318 
    319 		case cris_ver_v3p:
    320 		case cris_ver_v8p:
    321 		case cris_ver_v10p:
    322 		case cris_ver_v32p:
    323 		  break;
    324 
    325 		case cris_ver_v8:
    326 		  abort ();
    327 		default:
    328 		  abort ();
    329 		}
    330 	    }
    331 	  else
    332 	    {
    333 	      switch (opcodep->applicable_version)
    334 		{
    335 		case cris_ver_version_all:
    336 		case cris_ver_v0_3:
    337 		case cris_ver_v3p:
    338 		case cris_ver_v0_10:
    339 		case cris_ver_v8p:
    340 		case cris_ver_v8_10:
    341 		case cris_ver_v10:
    342 		case cris_ver_sim_v0_10:
    343 		case cris_ver_v10p:
    344 		case cris_ver_warning:
    345 		  break;
    346 
    347 		case cris_ver_v32p:
    348 		  continue;
    349 
    350 		case cris_ver_v8:
    351 		  abort ();
    352 		default:
    353 		  abort ();
    354 		}
    355 	    }
    356 
    357 	  /* We give a double lead for bits matching the template in
    358 	     cris_opcodes.  Not even, because then "move p8,r10" would
    359 	     be given 2 bits lead over "clear.d r10".  When there's a
    360 	     tie, the first entry in the table wins.  This is
    361 	     deliberate, to avoid a more complicated recognition
    362 	     formula.  */
    363 	  if ((opcodep->match & insn) == opcodep->match
    364 	      && (opcodep->lose & insn) == 0
    365 	      && ((level_of_match
    366 		   = cris_constraint (opcodep->args,
    367 				      insn,
    368 				      prefix_insn,
    369 				      disdata))
    370 		  >= 0)
    371 	      && ((level_of_match
    372 		   += 2 * number_of_bits (opcodep->match
    373 					  | opcodep->lose))
    374 			  > max_level_of_match))
    375 		    {
    376 		      max_matchedp = opcodep;
    377 		      max_level_of_match = level_of_match;
    378 
    379 		      /* If there was a full match, never mind looking
    380 			 further.  */
    381 		      if (level_of_match >= 2 * 16)
    382 			break;
    383 		    }
    384 		}
    385       /* Fill in the new entry.
    386 
    387 	 If there are changes to the opcode-table involving prefixes, and
    388 	 disassembly then does not work correctly, try removing the
    389 	 else-clause below that fills in the prefix-table.  If that
    390 	 helps, you need to change the prefix_opc_table setting above, or
    391 	 something related.  */
    392       if (prefix_insn == NO_CRIS_PREFIX)
    393 	opc_table[insn] = max_matchedp;
    394       else
    395 	prefix_opc_table[insn] = max_matchedp;
    396     }
    397 
    398   return max_matchedp;
    399 }
    400 
    401 /* Return -1 if the constraints of a bitwise-matched instruction say
    402    that there is no match.  Otherwise return a nonnegative number
    403    indicating the confidence in the match (higher is better).  */
    404 
    405 static int
    406 cris_constraint (const char *cs,
    407 		 unsigned int insn,
    408 		 unsigned int prefix_insn,
    409 		 struct cris_disasm_data *disdata)
    410 {
    411   int retval = 0;
    412   int tmp;
    413   int prefix_ok = 0;
    414   const char *s;
    415 
    416   for (s = cs; *s; s++)
    417     switch (*s)
    418       {
    419       case '!':
    420 	/* Do not recognize "pop" if there's a prefix and then only for
    421            v0..v10.  */
    422 	if (prefix_insn != NO_CRIS_PREFIX
    423 	    || disdata->distype != cris_dis_v0_v10)
    424 	  return -1;
    425 	break;
    426 
    427       case 'U':
    428 	/* Not recognized at disassembly.  */
    429 	return -1;
    430 
    431       case 'M':
    432 	/* Size modifier for "clear", i.e. special register 0, 4 or 8.
    433 	   Check that it is one of them.  Only special register 12 could
    434 	   be mismatched, but checking for matches is more logical than
    435 	   checking for mismatches when there are only a few cases.  */
    436 	tmp = ((insn >> 12) & 0xf);
    437 	if (tmp != 0 && tmp != 4 && tmp != 8)
    438 	  return -1;
    439 	break;
    440 
    441       case 'm':
    442 	if ((insn & 0x30) == 0x30)
    443 	  return -1;
    444 	break;
    445 
    446       case 'S':
    447 	/* A prefix operand without side-effect.  */
    448 	if (prefix_insn != NO_CRIS_PREFIX && (insn & 0x400) == 0)
    449 	  {
    450 	    prefix_ok = 1;
    451 	    break;
    452 	  }
    453 	else
    454 	  return -1;
    455 
    456       case 's':
    457       case 'y':
    458       case 'Y':
    459 	/* If this is a prefixed insn with postincrement (side-effect),
    460 	   the prefix must not be DIP.  */
    461 	if (prefix_insn != NO_CRIS_PREFIX)
    462 	  {
    463 	    if (insn & 0x400)
    464 	      {
    465 		const struct cris_opcode *prefix_opcodep
    466 		  = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata);
    467 
    468 		if (prefix_opcodep->match == DIP_OPCODE)
    469 		  return -1;
    470 	      }
    471 
    472 	    prefix_ok = 1;
    473 	  }
    474 	break;
    475 
    476       case 'B':
    477 	/* If we don't fall through, then the prefix is ok.  */
    478 	prefix_ok = 1;
    479 
    480 	/* A "push" prefix.  Check for valid "push" size.
    481 	   In case of special register, it may be != 4.  */
    482 	if (prefix_insn != NO_CRIS_PREFIX)
    483 	  {
    484 	    /* Match the prefix insn to BDAPQ.  */
    485 	    const struct cris_opcode *prefix_opcodep
    486 	      = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata);
    487 
    488 	    if (prefix_opcodep->match == BDAP_QUICK_OPCODE)
    489 	      {
    490 		int pushsize = (prefix_insn & 255);
    491 
    492 		if (pushsize > 127)
    493 		  pushsize -= 256;
    494 
    495 		if (s[1] == 'P')
    496 		  {
    497 		    unsigned int spec_reg = (insn >> 12) & 15;
    498 		    const struct cris_spec_reg *sregp
    499 		      = spec_reg_info (spec_reg, disdata->distype);
    500 
    501 		    /* For a special-register, the "prefix size" must
    502 		       match the size of the register.  */
    503 		    if (sregp && sregp->reg_size == (unsigned int) -pushsize)
    504 		      break;
    505 		  }
    506 		else if (s[1] == 'R')
    507 		  {
    508 		    if ((insn & 0x30) == 0x20 && pushsize == -4)
    509 		      break;
    510 		  }
    511 		/* FIXME:  Should abort here; next constraint letter
    512 		   *must* be 'P' or 'R'.  */
    513 	      }
    514 	  }
    515 	return -1;
    516 
    517       case 'D':
    518 	retval = (((insn >> 12) & 15) == (insn & 15));
    519 	if (!retval)
    520 	  return -1;
    521 	else
    522 	  retval += 4;
    523 	break;
    524 
    525       case 'P':
    526 	{
    527 	  const struct cris_spec_reg *sregp
    528 	    = spec_reg_info ((insn >> 12) & 15, disdata->distype);
    529 
    530 	  /* Since we match four bits, we will give a value of 4-1 = 3
    531 	     in a match.  If there is a corresponding exact match of a
    532 	     special register in another pattern, it will get a value of
    533 	     4, which will be higher.  This should be correct in that an
    534 	     exact pattern would match better than a general pattern.
    535 
    536 	     Note that there is a reason for not returning zero; the
    537 	     pattern for "clear" is partly  matched in the bit-pattern
    538 	     (the two lower bits must be zero), while the bit-pattern
    539 	     for a move from a special register is matched in the
    540 	     register constraint.  */
    541 
    542 	  if (sregp != NULL)
    543 	    {
    544 	      retval += 3;
    545 	      break;
    546 	    }
    547 	  else
    548 	    return -1;
    549 	}
    550       }
    551 
    552   if (prefix_insn != NO_CRIS_PREFIX && ! prefix_ok)
    553     return -1;
    554 
    555   return retval;
    556 }
    557 
    558 /* Format number as hex with a leading "0x" into outbuffer.  */
    559 
    560 static char *
    561 format_hex (unsigned long number,
    562 	    char *outbuffer,
    563 	    struct cris_disasm_data *disdata)
    564 {
    565   /* Truncate negative numbers on >32-bit hosts.  */
    566   number &= 0xffffffff;
    567 
    568   sprintf (outbuffer, "0x%lx", number);
    569 
    570   /* Save this value for the "case" support.  */
    571   if (TRACE_CASE)
    572     last_immediate = number;
    573 
    574   return outbuffer + strlen (outbuffer);
    575 }
    576 
    577 /* Format number as decimal into outbuffer.  Parameter signedp says
    578    whether the number should be formatted as signed (!= 0) or
    579    unsigned (== 0).  */
    580 
    581 static char *
    582 format_dec (long number, char *outbuffer, int signedp)
    583 {
    584   last_immediate = number;
    585   if (signedp)
    586     sprintf (outbuffer, "%ld", number);
    587   else
    588     sprintf (outbuffer, "%lu", (unsigned long) number);
    589 
    590   return outbuffer + strlen (outbuffer);
    591 }
    592 
    593 /* Format the name of the general register regno into outbuffer.  */
    594 
    595 static char *
    596 format_reg (struct cris_disasm_data *disdata,
    597 	    int regno,
    598 	    char *outbuffer_start,
    599 	    bfd_boolean with_reg_prefix)
    600 {
    601   char *outbuffer = outbuffer_start;
    602 
    603   if (with_reg_prefix)
    604     *outbuffer++ = REGISTER_PREFIX_CHAR;
    605 
    606   switch (regno)
    607     {
    608     case 15:
    609       /* For v32, there is no context in which we output PC.  */
    610       if (disdata->distype == cris_dis_v32)
    611 	strcpy (outbuffer, "acr");
    612       else
    613 	strcpy (outbuffer, "pc");
    614       break;
    615 
    616     case 14:
    617       strcpy (outbuffer, "sp");
    618       break;
    619 
    620     default:
    621       sprintf (outbuffer, "r%d", regno);
    622       break;
    623     }
    624 
    625   return outbuffer_start + strlen (outbuffer_start);
    626 }
    627 
    628 /* Format the name of a support register into outbuffer.  */
    629 
    630 static char *
    631 format_sup_reg (unsigned int regno,
    632 		char *outbuffer_start,
    633 		bfd_boolean with_reg_prefix)
    634 {
    635   char *outbuffer = outbuffer_start;
    636   int i;
    637 
    638   if (with_reg_prefix)
    639     *outbuffer++ = REGISTER_PREFIX_CHAR;
    640 
    641   for (i = 0; cris_support_regs[i].name != NULL; i++)
    642     if (cris_support_regs[i].number == regno)
    643       {
    644 	sprintf (outbuffer, "%s", cris_support_regs[i].name);
    645 	return outbuffer_start + strlen (outbuffer_start);
    646       }
    647 
    648   /* There's supposed to be register names covering all numbers, though
    649      some may be generic names.  */
    650   sprintf (outbuffer, "format_sup_reg-BUG");
    651   return outbuffer_start + strlen (outbuffer_start);
    652 }
    653 
    654 /* Return the length of an instruction.  */
    655 
    656 static unsigned
    657 bytes_to_skip (unsigned int insn,
    658 	       const struct cris_opcode *matchedp,
    659 	       enum cris_disass_family distype,
    660 	       const struct cris_opcode *prefix_matchedp)
    661 {
    662   /* Each insn is a word plus "immediate" operands.  */
    663   unsigned to_skip = 2;
    664   const char *template_name = (const char *) matchedp->args;
    665   const char *s;
    666 
    667   for (s = template_name; *s; s++)
    668     if ((*s == 's' || *s == 'N' || *s == 'Y')
    669 	&& (insn & 0x400) && (insn & 15) == 15
    670 	&& prefix_matchedp == NULL)
    671       {
    672 	/* Immediate via [pc+], so we have to check the size of the
    673 	   operand.  */
    674 	int mode_size = 1 << ((insn >> 4) & (*template_name == 'z' ? 1 : 3));
    675 
    676 	if (matchedp->imm_oprnd_size == SIZE_FIX_32)
    677 	  to_skip += 4;
    678 	else if (matchedp->imm_oprnd_size == SIZE_SPEC_REG)
    679 	  {
    680 	    const struct cris_spec_reg *sregp
    681 	      = spec_reg_info ((insn >> 12) & 15, distype);
    682 
    683 	    /* FIXME: Improve error handling; should have been caught
    684 	       earlier.  */
    685 	    if (sregp == NULL)
    686 	      return 2;
    687 
    688 	    /* PC is incremented by two, not one, for a byte.  Except on
    689 	       CRISv32, where constants are always DWORD-size for
    690 	       special registers.  */
    691 	    to_skip +=
    692 	      distype == cris_dis_v32 ? 4 : (sregp->reg_size + 1) & ~1;
    693 	  }
    694 	else
    695 	  to_skip += (mode_size + 1) & ~1;
    696       }
    697     else if (*s == 'n')
    698       to_skip += 4;
    699     else if (*s == 'b')
    700       to_skip += 2;
    701 
    702   return to_skip;
    703 }
    704 
    705 /* Print condition code flags.  */
    706 
    707 static char *
    708 print_flags (struct cris_disasm_data *disdata, unsigned int insn, char *cp)
    709 {
    710   /* Use the v8 (Etrax 100) flag definitions for disassembly.
    711      The differences with v0 (Etrax 1..4) vs. Svinto are:
    712       v0 'd' <=> v8 'm'
    713       v0 'e' <=> v8 'b'.
    714      FIXME: Emit v0..v3 flag names somehow.  */
    715   static const char v8_fnames[] = "cvznxibm";
    716   static const char v32_fnames[] = "cvznxiup";
    717   const char *fnames
    718     = disdata->distype == cris_dis_v32 ? v32_fnames : v8_fnames;
    719 
    720   unsigned char flagbits = (((insn >> 8) & 0xf0) | (insn & 15));
    721   int i;
    722 
    723   for (i = 0; i < 8; i++)
    724     if (flagbits & (1 << i))
    725       *cp++ = fnames[i];
    726 
    727   return cp;
    728 }
    729 
    730 /* Print out an insn with its operands, and update the info->insn_type
    731    fields.  The prefix_opcodep and the rest hold a prefix insn that is
    732    supposed to be output as an address mode.  */
    733 
    734 static void
    735 print_with_operands (const struct cris_opcode *opcodep,
    736 		     unsigned int insn,
    737 		     unsigned char *buffer,
    738 		     bfd_vma addr,
    739 		     disassemble_info *info,
    740 		     /* If a prefix insn was before this insn (and is supposed
    741 			to be output as an address), here is a description of
    742 			it.  */
    743 		     const struct cris_opcode *prefix_opcodep,
    744 		     unsigned int prefix_insn,
    745 		     unsigned char *prefix_buffer,
    746 		     bfd_boolean with_reg_prefix)
    747 {
    748   /* Get a buffer of somewhat reasonable size where we store
    749      intermediate parts of the insn.  */
    750   char temp[sizeof (".d [$r13=$r12-2147483648],$r10") * 2];
    751   char *tp = temp;
    752   static const char mode_char[] = "bwd?";
    753   const char *s;
    754   const char *cs;
    755   struct cris_disasm_data *disdata
    756     = (struct cris_disasm_data *) info->private_data;
    757 
    758   /* Print out the name first thing we do.  */
    759   (*info->fprintf_func) (info->stream, "%s", opcodep->name);
    760 
    761   cs = opcodep->args;
    762   s = cs;
    763 
    764   /* Ignore any prefix indicator.  */
    765   if (*s == 'p')
    766     s++;
    767 
    768   if (*s == 'm' || *s == 'M' || *s == 'z')
    769     {
    770       *tp++ = '.';
    771 
    772       /* Get the size-letter.  */
    773       *tp++ = *s == 'M'
    774 	? (insn & 0x8000 ? 'd'
    775 	   : insn & 0x4000 ? 'w' : 'b')
    776 	: mode_char[(insn >> 4) & (*s == 'z' ? 1 : 3)];
    777 
    778       /* Ignore the size and the space character that follows.  */
    779       s += 2;
    780     }
    781 
    782   /* Add a space if this isn't a long-branch, because for those will add
    783      the condition part of the name later.  */
    784   if (opcodep->match != (BRANCH_PC_LOW + BRANCH_INCR_HIGH * 256))
    785     *tp++ = ' ';
    786 
    787   /* Fill in the insn-type if deducible from the name (and there's no
    788      better way).  */
    789   if (opcodep->name[0] == 'j')
    790     {
    791       if (CONST_STRNEQ (opcodep->name, "jsr"))
    792 	/* It's "jsr" or "jsrc".  */
    793 	info->insn_type = dis_jsr;
    794       else
    795 	/* Any other jump-type insn is considered a branch.  */
    796 	info->insn_type = dis_branch;
    797     }
    798 
    799   /* We might know some more fields right now.  */
    800   info->branch_delay_insns = opcodep->delayed;
    801 
    802   /* Handle operands.  */
    803   for (; *s; s++)
    804     {
    805     switch (*s)
    806       {
    807       case 'T':
    808 	tp = format_sup_reg ((insn >> 12) & 15, tp, with_reg_prefix);
    809 	break;
    810 
    811       case 'A':
    812 	if (with_reg_prefix)
    813 	  *tp++ = REGISTER_PREFIX_CHAR;
    814 	*tp++ = 'a';
    815 	*tp++ = 'c';
    816 	*tp++ = 'r';
    817 	break;
    818 
    819       case '[':
    820       case ']':
    821       case ',':
    822 	*tp++ = *s;
    823 	break;
    824 
    825       case '!':
    826 	/* Ignore at this point; used at earlier stages to avoid
    827 	   recognition if there's a prefix at something that in other
    828 	   ways looks like a "pop".  */
    829 	break;
    830 
    831       case 'd':
    832 	/* Ignore.  This is an optional ".d " on the large one of
    833 	   relaxable insns.  */
    834 	break;
    835 
    836       case 'B':
    837 	/* This was the prefix that made this a "push".  We've already
    838 	   handled it by recognizing it, so signal that the prefix is
    839 	   handled by setting it to NULL.  */
    840 	prefix_opcodep = NULL;
    841 	break;
    842 
    843       case 'D':
    844       case 'r':
    845 	tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
    846 	break;
    847 
    848       case 'R':
    849 	tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
    850 	break;
    851 
    852       case 'n':
    853 	{
    854 	  /* Like N but pc-relative to the start of the insn.  */
    855 	  unsigned long number
    856 	    = (buffer[2] + buffer[3] * 256 + buffer[4] * 65536
    857 	       + buffer[5] * 0x1000000 + addr);
    858 
    859 	  /* Finish off and output previous formatted bytes.  */
    860 	  *tp = 0;
    861 	  if (temp[0])
    862 	    (*info->fprintf_func) (info->stream, "%s", temp);
    863 	  tp = temp;
    864 
    865 	  (*info->print_address_func) ((bfd_vma) number, info);
    866 	}
    867 	break;
    868 
    869       case 'u':
    870 	{
    871 	  /* Like n but the offset is bits <3:0> in the instruction.  */
    872 	  unsigned long number = (buffer[0] & 0xf) * 2 + addr;
    873 
    874 	  /* Finish off and output previous formatted bytes.  */
    875 	  *tp = 0;
    876 	  if (temp[0])
    877 	    (*info->fprintf_func) (info->stream, "%s", temp);
    878 	  tp = temp;
    879 
    880 	  (*info->print_address_func) ((bfd_vma) number, info);
    881 	}
    882 	break;
    883 
    884       case 'N':
    885       case 'y':
    886       case 'Y':
    887       case 'S':
    888       case 's':
    889 	/* Any "normal" memory operand.  */
    890 	if ((insn & 0x400) && (insn & 15) == 15 && prefix_opcodep == NULL)
    891 	  {
    892 	    /* We're looking at [pc+], i.e. we need to output an immediate
    893 	       number, where the size can depend on different things.  */
    894 	    long number;
    895 	    int signedp
    896 	      = ((*cs == 'z' && (insn & 0x20))
    897 		 || opcodep->match == BDAP_QUICK_OPCODE);
    898 	    int nbytes;
    899 
    900 	    if (opcodep->imm_oprnd_size == SIZE_FIX_32)
    901 	      nbytes = 4;
    902 	    else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
    903 	      {
    904 		const struct cris_spec_reg *sregp
    905 		  = spec_reg_info ((insn >> 12) & 15, disdata->distype);
    906 
    907 		/* A NULL return should have been as a non-match earlier,
    908 		   so catch it as an internal error in the error-case
    909 		   below.  */
    910 		if (sregp == NULL)
    911 		  /* Whatever non-valid size.  */
    912 		  nbytes = 42;
    913 		else
    914 		  /* PC is always incremented by a multiple of two.
    915 		     For CRISv32, immediates are always 4 bytes for
    916 		     special registers.  */
    917 		  nbytes = disdata->distype == cris_dis_v32
    918 		    ? 4 : (sregp->reg_size + 1) & ~1;
    919 	      }
    920 	    else
    921 	      {
    922 		int mode_size = 1 << ((insn >> 4) & (*cs == 'z' ? 1 : 3));
    923 
    924 		if (mode_size == 1)
    925 		  nbytes = 2;
    926 		else
    927 		  nbytes = mode_size;
    928 	      }
    929 
    930 	    switch (nbytes)
    931 	      {
    932 	      case 1:
    933 		number = buffer[2];
    934 		if (signedp && number > 127)
    935 		  number -= 256;
    936 		break;
    937 
    938 	      case 2:
    939 		number = buffer[2] + buffer[3] * 256;
    940 		if (signedp && number > 32767)
    941 		  number -= 65536;
    942 		break;
    943 
    944 	      case 4:
    945 		number
    946 		  = buffer[2] + buffer[3] * 256 + buffer[4] * 65536
    947 		  + buffer[5] * 0x1000000;
    948 		break;
    949 
    950 	      default:
    951 		strcpy (tp, "bug");
    952 		tp += 3;
    953 		number = 42;
    954 	      }
    955 
    956 	    if ((*cs == 'z' && (insn & 0x20))
    957 		|| (opcodep->match == BDAP_QUICK_OPCODE
    958 		    && (nbytes <= 2 || buffer[1 + nbytes] == 0)))
    959 	      tp = format_dec (number, tp, signedp);
    960 	    else
    961 	      {
    962 		unsigned int highbyte = (number >> 24) & 0xff;
    963 
    964 		/* Either output this as an address or as a number.  If it's
    965 		   a dword with the same high-byte as the address of the
    966 		   insn, assume it's an address, and also if it's a non-zero
    967 		   non-0xff high-byte.  If this is a jsr or a jump, then
    968 		   it's definitely an address.  */
    969 		if (nbytes == 4
    970 		    && (highbyte == ((addr >> 24) & 0xff)
    971 			|| (highbyte != 0 && highbyte != 0xff)
    972 			|| info->insn_type == dis_branch
    973 			|| info->insn_type == dis_jsr))
    974 		  {
    975 		    /* Finish off and output previous formatted bytes.  */
    976 		    *tp = 0;
    977 		    tp = temp;
    978 		    if (temp[0])
    979 		      (*info->fprintf_func) (info->stream, "%s", temp);
    980 
    981 		    (*info->print_address_func) ((bfd_vma) number, info);
    982 
    983 		    info->target = number;
    984 		  }
    985 		else
    986 		  tp = format_hex (number, tp, disdata);
    987 	      }
    988 	  }
    989 	else
    990 	  {
    991 	    /* Not an immediate number.  Then this is a (possibly
    992 	       prefixed) memory operand.  */
    993 	    if (info->insn_type != dis_nonbranch)
    994 	      {
    995 		int mode_size
    996 		  = 1 << ((insn >> 4)
    997 			  & (opcodep->args[0] == 'z' ? 1 : 3));
    998 		int size;
    999 		info->insn_type = dis_dref;
   1000 		info->flags |= CRIS_DIS_FLAG_MEMREF;
   1001 
   1002 		if (opcodep->imm_oprnd_size == SIZE_FIX_32)
   1003 		  size = 4;
   1004 		else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
   1005 		  {
   1006 		    const struct cris_spec_reg *sregp
   1007 		      = spec_reg_info ((insn >> 12) & 15, disdata->distype);
   1008 
   1009 		    /* FIXME: Improve error handling; should have been caught
   1010 		       earlier.  */
   1011 		    if (sregp == NULL)
   1012 		      size = 4;
   1013 		    else
   1014 		      size = sregp->reg_size;
   1015 		  }
   1016 		else
   1017 		  size = mode_size;
   1018 
   1019 		info->data_size = size;
   1020 	      }
   1021 
   1022 	    *tp++ = '[';
   1023 
   1024 	    if (prefix_opcodep
   1025 		/* We don't match dip with a postincremented field
   1026 		   as a side-effect address mode.  */
   1027 		&& ((insn & 0x400) == 0
   1028 		    || prefix_opcodep->match != DIP_OPCODE))
   1029 	      {
   1030 		if (insn & 0x400)
   1031 		  {
   1032 		    tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
   1033 		    *tp++ = '=';
   1034 		  }
   1035 
   1036 
   1037 		/* We mainly ignore the prefix format string when the
   1038 		   address-mode syntax is output.  */
   1039 		switch (prefix_opcodep->match)
   1040 		  {
   1041 		  case DIP_OPCODE:
   1042 		    /* It's [r], [r+] or [pc+].  */
   1043 		    if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
   1044 		      {
   1045 			/* It's [pc+].  This cannot possibly be anything
   1046 			   but an address.  */
   1047 			unsigned long number
   1048 			  = prefix_buffer[2] + prefix_buffer[3] * 256
   1049 			  + prefix_buffer[4] * 65536
   1050 			  + prefix_buffer[5] * 0x1000000;
   1051 
   1052 			info->target = (bfd_vma) number;
   1053 
   1054 			/* Finish off and output previous formatted
   1055 			   data.  */
   1056 			*tp = 0;
   1057 			tp = temp;
   1058 			if (temp[0])
   1059 			  (*info->fprintf_func) (info->stream, "%s", temp);
   1060 
   1061 			(*info->print_address_func) ((bfd_vma) number, info);
   1062 		      }
   1063 		    else
   1064 		      {
   1065 			/* For a memref in an address, we use target2.
   1066 			   In this case, target is zero.  */
   1067 			info->flags
   1068 			  |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
   1069 			      | CRIS_DIS_FLAG_MEM_TARGET2_MEM);
   1070 
   1071 			info->target2 = prefix_insn & 15;
   1072 
   1073 			*tp++ = '[';
   1074 			tp = format_reg (disdata, prefix_insn & 15, tp,
   1075 					 with_reg_prefix);
   1076 			if (prefix_insn & 0x400)
   1077 			  *tp++ = '+';
   1078 			*tp++ = ']';
   1079 		      }
   1080 		    break;
   1081 
   1082 		  case BDAP_QUICK_OPCODE:
   1083 		    {
   1084 		      int number;
   1085 
   1086 		      number = prefix_buffer[0];
   1087 		      if (number > 127)
   1088 			number -= 256;
   1089 
   1090 		      /* Output "reg+num" or, if num < 0, "reg-num".  */
   1091 		      tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
   1092 				       with_reg_prefix);
   1093 		      if (number >= 0)
   1094 			*tp++ = '+';
   1095 		      tp = format_dec (number, tp, 1);
   1096 
   1097 		      info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
   1098 		      info->target = (prefix_insn >> 12) & 15;
   1099 		      info->target2 = (bfd_vma) number;
   1100 		      break;
   1101 		    }
   1102 
   1103 		  case BIAP_OPCODE:
   1104 		    /* Output "r+R.m".  */
   1105 		    tp = format_reg (disdata, prefix_insn & 15, tp,
   1106 				     with_reg_prefix);
   1107 		    *tp++ = '+';
   1108 		    tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
   1109 				     with_reg_prefix);
   1110 		    *tp++ = '.';
   1111 		    *tp++ = mode_char[(prefix_insn >> 4) & 3];
   1112 
   1113 		    info->flags
   1114 		      |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
   1115 			  | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
   1116 
   1117 			  | ((prefix_insn & 0x8000)
   1118 			     ? CRIS_DIS_FLAG_MEM_TARGET2_MULT4
   1119 			     : ((prefix_insn & 0x8000)
   1120 				? CRIS_DIS_FLAG_MEM_TARGET2_MULT2 : 0)));
   1121 
   1122 		    /* Is it the casejump?  It's a "adds.w [pc+r%d.w],pc".  */
   1123 		    if (insn == 0xf83f && (prefix_insn & ~0xf000) == 0x55f)
   1124 		      /* Then start interpreting data as offsets.  */
   1125 		      case_offset_counter = no_of_case_offsets;
   1126 		    break;
   1127 
   1128 		  case BDAP_INDIR_OPCODE:
   1129 		    /* Output "r+s.m", or, if "s" is [pc+], "r+s" or
   1130 		       "r-s".  */
   1131 		    tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
   1132 				     with_reg_prefix);
   1133 
   1134 		    if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
   1135 		      {
   1136 			long number;
   1137 			unsigned int nbytes;
   1138 
   1139 			/* It's a value.  Get its size.  */
   1140 			int mode_size = 1 << ((prefix_insn >> 4) & 3);
   1141 
   1142 			if (mode_size == 1)
   1143 			  nbytes = 2;
   1144 			else
   1145 			  nbytes = mode_size;
   1146 
   1147 			switch (nbytes)
   1148 			  {
   1149 			  case 1:
   1150 			    number = prefix_buffer[2];
   1151 			    if (number > 127)
   1152 			      number -= 256;
   1153 			    break;
   1154 
   1155 			  case 2:
   1156 			    number = prefix_buffer[2] + prefix_buffer[3] * 256;
   1157 			    if (number > 32767)
   1158 			      number -= 65536;
   1159 			    break;
   1160 
   1161 			  case 4:
   1162 			    number
   1163 			      = prefix_buffer[2] + prefix_buffer[3] * 256
   1164 			      + prefix_buffer[4] * 65536
   1165 			      + prefix_buffer[5] * 0x1000000;
   1166 			    break;
   1167 
   1168 			  default:
   1169 			    strcpy (tp, "bug");
   1170 			    tp += 3;
   1171 			    number = 42;
   1172 			  }
   1173 
   1174 			info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
   1175 			info->target2 = (bfd_vma) number;
   1176 
   1177 			/* If the size is dword, then assume it's an
   1178 			   address.  */
   1179 			if (nbytes == 4)
   1180 			  {
   1181 			    /* Finish off and output previous formatted
   1182 			       bytes.  */
   1183 			    *tp++ = '+';
   1184 			    *tp = 0;
   1185 			    tp = temp;
   1186 			    (*info->fprintf_func) (info->stream, "%s", temp);
   1187 
   1188 			    (*info->print_address_func) ((bfd_vma) number, info);
   1189 			  }
   1190 			else
   1191 			  {
   1192 			    if (number >= 0)
   1193 			      *tp++ = '+';
   1194 			    tp = format_dec (number, tp, 1);
   1195 			  }
   1196 		      }
   1197 		    else
   1198 		      {
   1199 			/* Output "r+[R].m" or "r+[R+].m".  */
   1200 			*tp++ = '+';
   1201 			*tp++ = '[';
   1202 			tp = format_reg (disdata, prefix_insn & 15, tp,
   1203 					 with_reg_prefix);
   1204 			if (prefix_insn & 0x400)
   1205 			  *tp++ = '+';
   1206 			*tp++ = ']';
   1207 			*tp++ = '.';
   1208 			*tp++ = mode_char[(prefix_insn >> 4) & 3];
   1209 
   1210 			info->flags
   1211 			  |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
   1212 			      | CRIS_DIS_FLAG_MEM_TARGET2_MEM
   1213 			      | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
   1214 
   1215 			      | (((prefix_insn >> 4) == 2)
   1216 				 ? 0
   1217 				 : (((prefix_insn >> 4) & 3) == 1
   1218 				    ? CRIS_DIS_FLAG_MEM_TARGET2_MEM_WORD
   1219 				    : CRIS_DIS_FLAG_MEM_TARGET2_MEM_BYTE)));
   1220 		      }
   1221 		    break;
   1222 
   1223 		  default:
   1224 		    (*info->fprintf_func) (info->stream, "?prefix-bug");
   1225 		  }
   1226 
   1227 		/* To mark that the prefix is used, reset it.  */
   1228 		prefix_opcodep = NULL;
   1229 	      }
   1230 	    else
   1231 	      {
   1232 		tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
   1233 
   1234 		info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
   1235 		info->target = insn & 15;
   1236 
   1237 		if (insn & 0x400)
   1238 		  *tp++ = '+';
   1239 	      }
   1240 	    *tp++ = ']';
   1241 	  }
   1242 	break;
   1243 
   1244       case 'x':
   1245 	tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
   1246 	*tp++ = '.';
   1247 	*tp++ = mode_char[(insn >> 4) & 3];
   1248 	break;
   1249 
   1250       case 'I':
   1251 	tp = format_dec (insn & 63, tp, 0);
   1252 	break;
   1253 
   1254       case 'b':
   1255 	{
   1256 	  int where = buffer[2] + buffer[3] * 256;
   1257 
   1258 	  if (where > 32767)
   1259 	    where -= 65536;
   1260 
   1261 	  where += addr + ((disdata->distype == cris_dis_v32) ? 0 : 4);
   1262 
   1263 	  if (insn == BA_PC_INCR_OPCODE)
   1264 	    info->insn_type = dis_branch;
   1265 	  else
   1266 	    info->insn_type = dis_condbranch;
   1267 
   1268 	  info->target = (bfd_vma) where;
   1269 
   1270 	  *tp = 0;
   1271 	  tp = temp;
   1272 	  (*info->fprintf_func) (info->stream, "%s%s ",
   1273 				 temp, cris_cc_strings[insn >> 12]);
   1274 
   1275 	  (*info->print_address_func) ((bfd_vma) where, info);
   1276 	}
   1277       break;
   1278 
   1279     case 'c':
   1280       tp = format_dec (insn & 31, tp, 0);
   1281       break;
   1282 
   1283     case 'C':
   1284       tp = format_dec (insn & 15, tp, 0);
   1285       break;
   1286 
   1287     case 'o':
   1288       {
   1289 	long offset = insn & 0xfe;
   1290 	bfd_vma target;
   1291 
   1292 	if (insn & 1)
   1293 	  offset |= ~0xff;
   1294 
   1295 	if (opcodep->match == BA_QUICK_OPCODE)
   1296 	  info->insn_type = dis_branch;
   1297 	else
   1298 	  info->insn_type = dis_condbranch;
   1299 
   1300 	target = addr + ((disdata->distype == cris_dis_v32) ? 0 : 2) + offset;
   1301 	info->target = target;
   1302 	*tp = 0;
   1303 	tp = temp;
   1304 	(*info->fprintf_func) (info->stream, "%s", temp);
   1305 	(*info->print_address_func) (target, info);
   1306       }
   1307       break;
   1308 
   1309     case 'Q':
   1310     case 'O':
   1311       {
   1312 	long number = buffer[0];
   1313 
   1314 	if (number > 127)
   1315 	  number = number - 256;
   1316 
   1317 	tp = format_dec (number, tp, 1);
   1318 	*tp++ = ',';
   1319 	tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
   1320       }
   1321       break;
   1322 
   1323     case 'f':
   1324       tp = print_flags (disdata, insn, tp);
   1325       break;
   1326 
   1327     case 'i':
   1328       tp = format_dec ((insn & 32) ? (insn & 31) | ~31L : insn & 31, tp, 1);
   1329       break;
   1330 
   1331     case 'P':
   1332       {
   1333 	const struct cris_spec_reg *sregp
   1334 	  = spec_reg_info ((insn >> 12) & 15, disdata->distype);
   1335 
   1336 	if (sregp->name == NULL)
   1337 	  /* Should have been caught as a non-match eariler.  */
   1338 	  *tp++ = '?';
   1339 	else
   1340 	  {
   1341 	    if (with_reg_prefix)
   1342 	      *tp++ = REGISTER_PREFIX_CHAR;
   1343 	    strcpy (tp, sregp->name);
   1344 	    tp += strlen (tp);
   1345 	  }
   1346       }
   1347       break;
   1348 
   1349     default:
   1350       strcpy (tp, "???");
   1351       tp += 3;
   1352     }
   1353   }
   1354 
   1355   *tp = 0;
   1356 
   1357   if (prefix_opcodep)
   1358     (*info->fprintf_func) (info->stream, " (OOPS unused prefix \"%s: %s\")",
   1359 			   prefix_opcodep->name, prefix_opcodep->args);
   1360 
   1361   (*info->fprintf_func) (info->stream, "%s", temp);
   1362 
   1363   /* Get info for matching case-tables, if we don't have any active.
   1364      We assume that the last constant seen is used; either in the insn
   1365      itself or in a "move.d const,rN, sub.d rN,rM"-like sequence.  */
   1366   if (TRACE_CASE && case_offset_counter == 0)
   1367     {
   1368       if (CONST_STRNEQ (opcodep->name, "sub"))
   1369 	case_offset = last_immediate;
   1370 
   1371       /* It could also be an "add", if there are negative case-values.  */
   1372       else if (CONST_STRNEQ (opcodep->name, "add"))
   1373 	/* The first case is the negated operand to the add.  */
   1374 	case_offset = -last_immediate;
   1375 
   1376       /* A bound insn will tell us the number of cases.  */
   1377       else if (CONST_STRNEQ (opcodep->name, "bound"))
   1378 	no_of_case_offsets = last_immediate + 1;
   1379 
   1380       /* A jump or jsr or branch breaks the chain of insns for a
   1381 	 case-table, so assume default first-case again.  */
   1382       else if (info->insn_type == dis_jsr
   1383 	       || info->insn_type == dis_branch
   1384 	       || info->insn_type == dis_condbranch)
   1385 	case_offset = 0;
   1386     }
   1387 }
   1388 
   1389 
   1390 /* Print the CRIS instruction at address memaddr on stream.  Returns
   1391    length of the instruction, in bytes.  Prefix register names with `$' if
   1392    WITH_REG_PREFIX.  */
   1393 
   1394 static int
   1395 print_insn_cris_generic (bfd_vma memaddr,
   1396 			 disassemble_info *info,
   1397 			 bfd_boolean with_reg_prefix)
   1398 {
   1399   int nbytes;
   1400   unsigned int insn;
   1401   const struct cris_opcode *matchedp;
   1402   int advance = 0;
   1403   struct cris_disasm_data *disdata
   1404     = (struct cris_disasm_data *) info->private_data;
   1405 
   1406   /* No instruction will be disassembled as longer than this number of
   1407      bytes; stacked prefixes will not be expanded.  */
   1408   unsigned char buffer[MAX_BYTES_PER_CRIS_INSN];
   1409   unsigned char *bufp;
   1410   int status = 0;
   1411   bfd_vma addr;
   1412 
   1413   /* There will be an "out of range" error after the last instruction.
   1414      Reading pairs of bytes in decreasing number, we hope that we will get
   1415      at least the amount that we will consume.
   1416 
   1417      If we can't get any data, or we do not get enough data, we print
   1418      the error message.  */
   1419 
   1420   for (nbytes = MAX_BYTES_PER_CRIS_INSN; nbytes > 0; nbytes -= 2)
   1421     {
   1422       status = (*info->read_memory_func) (memaddr, buffer, nbytes, info);
   1423       if (status == 0)
   1424 	break;
   1425     }
   1426 
   1427   /* If we did not get all we asked for, then clear the rest.
   1428      Hopefully this makes a reproducible result in case of errors.  */
   1429   if (nbytes != MAX_BYTES_PER_CRIS_INSN)
   1430     memset (buffer + nbytes, 0, MAX_BYTES_PER_CRIS_INSN - nbytes);
   1431 
   1432   addr = memaddr;
   1433   bufp = buffer;
   1434 
   1435   /* Set some defaults for the insn info.  */
   1436   info->insn_info_valid = 1;
   1437   info->branch_delay_insns = 0;
   1438   info->data_size = 0;
   1439   info->insn_type = dis_nonbranch;
   1440   info->flags = 0;
   1441   info->target = 0;
   1442   info->target2 = 0;
   1443 
   1444   /* If we got any data, disassemble it.  */
   1445   if (nbytes != 0)
   1446     {
   1447       matchedp = NULL;
   1448 
   1449       insn = bufp[0] + bufp[1] * 256;
   1450 
   1451       /* If we're in a case-table, don't disassemble the offsets.  */
   1452       if (TRACE_CASE && case_offset_counter != 0)
   1453 	{
   1454 	  info->insn_type = dis_noninsn;
   1455 	  advance += 2;
   1456 
   1457 	  /* If to print data as offsets, then shortcut here.  */
   1458 	  (*info->fprintf_func) (info->stream, "case %ld%s: -> ",
   1459 				 case_offset + no_of_case_offsets
   1460 				 - case_offset_counter,
   1461 				 case_offset_counter == 1 ? "/default" :
   1462 				 "");
   1463 
   1464 	  (*info->print_address_func) ((bfd_vma)
   1465 				       ((short) (insn)
   1466 					+ (long) (addr
   1467 						  - (no_of_case_offsets
   1468 						     - case_offset_counter)
   1469 						  * 2)), info);
   1470 	  case_offset_counter--;
   1471 
   1472 	  /* The default case start (without a "sub" or "add") must be
   1473 	     zero.  */
   1474 	  if (case_offset_counter == 0)
   1475 	    case_offset = 0;
   1476 	}
   1477       else if (insn == 0)
   1478 	{
   1479 	  /* We're often called to disassemble zeroes.  While this is a
   1480 	     valid "bcc .+2" insn, it is also useless enough and enough
   1481 	     of a nuiscance that we will just output "bcc .+2" for it
   1482 	     and signal it as a noninsn.  */
   1483 	  (*info->fprintf_func) (info->stream,
   1484 				 disdata->distype == cris_dis_v32
   1485 				 ? "bcc ." : "bcc .+2");
   1486 	  info->insn_type = dis_noninsn;
   1487 	  advance += 2;
   1488 	}
   1489       else
   1490 	{
   1491 	  const struct cris_opcode *prefix_opcodep = NULL;
   1492 	  unsigned char *prefix_buffer = bufp;
   1493 	  unsigned int prefix_insn = insn;
   1494 	  int prefix_size = 0;
   1495 
   1496 	  matchedp = get_opcode_entry (insn, NO_CRIS_PREFIX, disdata);
   1497 
   1498 	  /* Check if we're supposed to write out prefixes as address
   1499 	     modes and if this was a prefix.  */
   1500 	  if (matchedp != NULL && PARSE_PREFIX && matchedp->args[0] == 'p')
   1501 	    {
   1502 	      /* If it's a prefix, put it into the prefix vars and get the
   1503 		 main insn.  */
   1504 	      prefix_size = bytes_to_skip (prefix_insn, matchedp,
   1505 					   disdata->distype, NULL);
   1506 	      prefix_opcodep = matchedp;
   1507 
   1508 	      insn = bufp[prefix_size] + bufp[prefix_size + 1] * 256;
   1509 	      matchedp = get_opcode_entry (insn, prefix_insn, disdata);
   1510 
   1511 	      if (matchedp != NULL)
   1512 		{
   1513 		  addr += prefix_size;
   1514 		  bufp += prefix_size;
   1515 		  advance += prefix_size;
   1516 		}
   1517 	      else
   1518 		{
   1519 		  /* The "main" insn wasn't valid, at least not when
   1520 		     prefixed.  Put back things enough to output the
   1521 		     prefix insn only, as a normal insn.  */
   1522 		  matchedp = prefix_opcodep;
   1523 		  insn = prefix_insn;
   1524 		  prefix_opcodep = NULL;
   1525 		}
   1526 	    }
   1527 
   1528 	  if (matchedp == NULL)
   1529 	    {
   1530 	      (*info->fprintf_func) (info->stream, "??0x%x", insn);
   1531 	      advance += 2;
   1532 
   1533 	      info->insn_type = dis_noninsn;
   1534 	    }
   1535 	  else
   1536 	    {
   1537 	      advance
   1538 		+= bytes_to_skip (insn, matchedp, disdata->distype,
   1539 				  prefix_opcodep);
   1540 
   1541 	      /* The info_type and assorted fields will be set according
   1542 		 to the operands.   */
   1543 	      print_with_operands (matchedp, insn, bufp, addr, info,
   1544 				   prefix_opcodep, prefix_insn,
   1545 				   prefix_buffer, with_reg_prefix);
   1546 	    }
   1547 	}
   1548     }
   1549   else
   1550     info->insn_type = dis_noninsn;
   1551 
   1552   /* If we read less than MAX_BYTES_PER_CRIS_INSN, i.e. we got an error
   1553      status when reading that much, and the insn decoding indicated a
   1554      length exceeding what we read, there is an error.  */
   1555   if (status != 0 && (nbytes == 0 || advance > nbytes))
   1556     {
   1557       (*info->memory_error_func) (status, memaddr, info);
   1558       return -1;
   1559     }
   1560 
   1561   /* Max supported insn size with one folded prefix insn.  */
   1562   info->bytes_per_line = MAX_BYTES_PER_CRIS_INSN;
   1563 
   1564   /* I would like to set this to a fixed value larger than the actual
   1565      number of bytes to print in order to avoid spaces between bytes,
   1566      but objdump.c (2.9.1) does not like that, so we print 16-bit
   1567      chunks, which is the next choice.  */
   1568   info->bytes_per_chunk = 2;
   1569 
   1570   /* Printing bytes in order of increasing addresses makes sense,
   1571      especially on a little-endian target.
   1572      This is completely the opposite of what you think; setting this to
   1573      BFD_ENDIAN_LITTLE will print bytes in order N..0 rather than the 0..N
   1574      we want.  */
   1575   info->display_endian = BFD_ENDIAN_BIG;
   1576 
   1577   return advance;
   1578 }
   1579 
   1580 /* Disassemble, prefixing register names with `$'.  CRIS v0..v10.  */
   1581 
   1582 static int
   1583 print_insn_cris_with_register_prefix (bfd_vma vma,
   1584 				      disassemble_info *info)
   1585 {
   1586   if (info->private_data == NULL
   1587       && !cris_parse_disassembler_options (info, cris_dis_v0_v10))
   1588     return -1;
   1589   return print_insn_cris_generic (vma, info, TRUE);
   1590 }
   1591 
   1592 /* Disassemble, prefixing register names with `$'.  CRIS v32.  */
   1593 
   1594 static int
   1595 print_insn_crisv32_with_register_prefix (bfd_vma vma,
   1596 					 disassemble_info *info)
   1597 {
   1598   if (info->private_data == NULL
   1599       && !cris_parse_disassembler_options (info, cris_dis_v32))
   1600     return -1;
   1601   return print_insn_cris_generic (vma, info, TRUE);
   1602 }
   1603 
   1604 /* Disassemble, prefixing register names with `$'.
   1605    Common v10 and v32 subset.  */
   1606 
   1607 static int
   1608 print_insn_crisv10_v32_with_register_prefix (bfd_vma vma,
   1609 					     disassemble_info *info)
   1610 {
   1611   if (info->private_data == NULL
   1612       && !cris_parse_disassembler_options (info, cris_dis_common_v10_v32))
   1613     return -1;
   1614   return print_insn_cris_generic (vma, info, TRUE);
   1615 }
   1616 
   1617 /* Disassemble, no prefixes on register names.  CRIS v0..v10.  */
   1618 
   1619 static int
   1620 print_insn_cris_without_register_prefix (bfd_vma vma,
   1621 					 disassemble_info *info)
   1622 {
   1623   if (info->private_data == NULL
   1624       && !cris_parse_disassembler_options (info, cris_dis_v0_v10))
   1625     return -1;
   1626   return print_insn_cris_generic (vma, info, FALSE);
   1627 }
   1628 
   1629 /* Disassemble, no prefixes on register names.  CRIS v32.  */
   1630 
   1631 static int
   1632 print_insn_crisv32_without_register_prefix (bfd_vma vma,
   1633 					    disassemble_info *info)
   1634 {
   1635   if (info->private_data == NULL
   1636       && !cris_parse_disassembler_options (info, cris_dis_v32))
   1637     return -1;
   1638   return print_insn_cris_generic (vma, info, FALSE);
   1639 }
   1640 
   1641 /* Disassemble, no prefixes on register names.
   1642    Common v10 and v32 subset.  */
   1643 
   1644 static int
   1645 print_insn_crisv10_v32_without_register_prefix (bfd_vma vma,
   1646 						disassemble_info *info)
   1647 {
   1648   if (info->private_data == NULL
   1649       && !cris_parse_disassembler_options (info, cris_dis_common_v10_v32))
   1650     return -1;
   1651   return print_insn_cris_generic (vma, info, FALSE);
   1652 }
   1653 
   1654 /* Return a disassembler-function that prints registers with a `$' prefix,
   1655    or one that prints registers without a prefix.
   1656    FIXME: We should improve the solution to avoid the multitude of
   1657    functions seen above.  */
   1658 
   1659 disassembler_ftype
   1660 cris_get_disassembler (bfd *abfd)
   1661 {
   1662   /* If there's no bfd in sight, we return what is valid as input in all
   1663      contexts if fed back to the assembler: disassembly *with* register
   1664      prefix.  Unfortunately this will be totally wrong for v32.  */
   1665   if (abfd == NULL)
   1666     return print_insn_cris_with_register_prefix;
   1667 
   1668   if (bfd_get_symbol_leading_char (abfd) == 0)
   1669     {
   1670       if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
   1671 	return print_insn_crisv32_with_register_prefix;
   1672       if (bfd_get_mach (abfd) == bfd_mach_cris_v10_v32)
   1673 	return print_insn_crisv10_v32_with_register_prefix;
   1674 
   1675       /* We default to v10.  This may be specifically specified in the
   1676 	 bfd mach, but is also the default setting.  */
   1677       return print_insn_cris_with_register_prefix;
   1678     }
   1679 
   1680   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
   1681     return print_insn_crisv32_without_register_prefix;
   1682   if (bfd_get_mach (abfd) == bfd_mach_cris_v10_v32)
   1683     return print_insn_crisv10_v32_without_register_prefix;
   1684   return print_insn_cris_without_register_prefix;
   1685 }
   1686 
   1687 /* Local variables:
   1688    eval: (c-set-style "gnu")
   1689    indent-tabs-mode: t
   1690    End:  */
   1691