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