Back to index

cell-binutils  2.17cvs20070401
cris-dis.c
Go to the documentation of this file.
00001 /* Disassembler code for CRIS.
00002    Copyright 2000, 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
00003    Contributed by Axis Communications AB, Lund, Sweden.
00004    Written by Hans-Peter Nilsson.
00005 
00006    This file is part of the GNU binutils and GDB, the GNU debugger.
00007 
00008    This program is free software; you can redistribute it and/or modify it
00009    under the terms of the GNU General Public License as published by the
00010    Free Software Foundation; either version 2, or (at your option) any later
00011    version.
00012 
00013    This program is distributed in the hope that it will be useful, but WITHOUT
00014    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00016    more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
00021    MA 02110-1301, USA.  */
00022 
00023 #include "dis-asm.h"
00024 #include "sysdep.h"
00025 #include "opcode/cris.h"
00026 #include "libiberty.h"
00027 
00028 /* No instruction will be disassembled longer than this.  In theory, and
00029    in silicon, address prefixes can be cascaded.  In practice, cascading
00030    is not used by GCC, and not supported by the assembler.  */
00031 #ifndef MAX_BYTES_PER_CRIS_INSN
00032 #define MAX_BYTES_PER_CRIS_INSN 8
00033 #endif
00034 
00035 /* Whether or not to decode prefixes, folding it into the following
00036    instruction.  FIXME: Make this optional later.  */
00037 #ifndef PARSE_PREFIX
00038 #define PARSE_PREFIX 1
00039 #endif
00040 
00041 /* Sometimes we prefix all registers with this character.  */
00042 #define REGISTER_PREFIX_CHAR '$'
00043 
00044 /* Whether or not to trace the following sequence:
00045    sub* X,r%d
00046    bound* Y,r%d
00047    adds.w [pc+r%d.w],pc
00048 
00049    This is the assembly form of a switch-statement in C.
00050    The "sub is optional.  If there is none, then X will be zero.
00051    X is the value of the first case,
00052    Y is the number of cases (including default).
00053 
00054    This results in case offsets printed on the form:
00055     case N: -> case_address
00056    where N is an estimation on the corresponding 'case' operand in C,
00057    and case_address is where execution of that case continues after the
00058    sequence presented above.
00059 
00060    The old style of output was to print the offsets as instructions,
00061    which made it hard to follow "case"-constructs in the disassembly,
00062    and caused a lot of annoying warnings about undefined instructions.
00063 
00064    FIXME: Make this optional later.  */
00065 #ifndef TRACE_CASE
00066 #define TRACE_CASE (disdata->trace_case)
00067 #endif
00068 
00069 enum cris_disass_family
00070  { cris_dis_v0_v10, cris_dis_common_v10_v32, cris_dis_v32 };
00071 
00072 /* Stored in the disasm_info->private_data member.  */
00073 struct cris_disasm_data
00074 {
00075   /* Whether to print something less confusing if we find something
00076      matching a switch-construct.  */
00077   bfd_boolean trace_case;
00078 
00079   /* Whether this code is flagged as crisv32.  FIXME: Should be an enum
00080      that includes "compatible".  */
00081   enum cris_disass_family distype;
00082 };
00083 
00084 /* Value of first element in switch.  */
00085 static long case_offset = 0;
00086 
00087 /* How many more case-offsets to print.  */
00088 static long case_offset_counter = 0;
00089 
00090 /* Number of case offsets.  */
00091 static long no_of_case_offsets = 0;
00092 
00093 /* Candidate for next case_offset.  */
00094 static long last_immediate = 0;
00095 
00096 static int cris_constraint
00097   (const char *, unsigned, unsigned, struct cris_disasm_data *);
00098 
00099 /* Parse disassembler options and store state in info.  FIXME: For the
00100    time being, we abuse static variables.  */
00101 
00102 static bfd_boolean
00103 cris_parse_disassembler_options (disassemble_info *info,
00104                              enum cris_disass_family distype)
00105 {
00106   struct cris_disasm_data *disdata;
00107 
00108   info->private_data = calloc (1, sizeof (struct cris_disasm_data));
00109   disdata = (struct cris_disasm_data *) info->private_data;
00110   if (disdata == NULL)
00111     return FALSE;
00112 
00113   /* Default true.  */
00114   disdata->trace_case
00115     = (info->disassembler_options == NULL
00116        || (strcmp (info->disassembler_options, "nocase") != 0));
00117 
00118   disdata->distype = distype;
00119   return TRUE;
00120 }
00121 
00122 static const struct cris_spec_reg *
00123 spec_reg_info (unsigned int sreg, enum cris_disass_family distype)
00124 {
00125   int i;
00126 
00127   for (i = 0; cris_spec_regs[i].name != NULL; i++)
00128     {
00129       if (cris_spec_regs[i].number == sreg)
00130        {
00131          if (distype == cris_dis_v32)
00132            switch (cris_spec_regs[i].applicable_version)
00133              {
00134              case cris_ver_warning:
00135              case cris_ver_version_all:
00136              case cris_ver_v3p:
00137              case cris_ver_v8p:
00138              case cris_ver_v10p:
00139              case cris_ver_v32p:
00140               /* No ambiguous sizes or register names with CRISv32.  */
00141               if (cris_spec_regs[i].warning == NULL)
00142                 return &cris_spec_regs[i];
00143              default:
00144               ;
00145              }
00146          else if (cris_spec_regs[i].applicable_version != cris_ver_v32p)
00147            return &cris_spec_regs[i];
00148        }
00149     }
00150 
00151   return NULL;
00152 }
00153 
00154 /* Return the number of bits in the argument.  */
00155 
00156 static int
00157 number_of_bits (unsigned int val)
00158 {
00159   int bits;
00160 
00161   for (bits = 0; val != 0; val &= val - 1)
00162     bits++;
00163 
00164   return bits;
00165 }
00166 
00167 /* Get an entry in the opcode-table.  */
00168 
00169 static const struct cris_opcode *
00170 get_opcode_entry (unsigned int insn,
00171                 unsigned int prefix_insn,
00172                 struct cris_disasm_data *disdata)
00173 {
00174   /* For non-prefixed insns, we keep a table of pointers, indexed by the
00175      insn code.  Each entry is initialized when found to be NULL.  */
00176   static const struct cris_opcode **opc_table = NULL;
00177 
00178   const struct cris_opcode *max_matchedp = NULL;
00179   const struct cris_opcode **prefix_opc_table = NULL;
00180 
00181   /* We hold a table for each prefix that need to be handled differently.  */
00182   static const struct cris_opcode **dip_prefixes = NULL;
00183   static const struct cris_opcode **bdapq_m1_prefixes = NULL;
00184   static const struct cris_opcode **bdapq_m2_prefixes = NULL;
00185   static const struct cris_opcode **bdapq_m4_prefixes = NULL;
00186   static const struct cris_opcode **rest_prefixes = NULL;
00187 
00188   /* Allocate and clear the opcode-table.  */
00189   if (opc_table == NULL)
00190     {
00191       opc_table = malloc (65536 * sizeof (opc_table[0]));
00192       if (opc_table == NULL)
00193        return NULL;
00194 
00195       memset (opc_table, 0, 65536 * sizeof (const struct cris_opcode *));
00196 
00197       dip_prefixes
00198        = malloc (65536 * sizeof (const struct cris_opcode **));
00199       if (dip_prefixes == NULL)
00200        return NULL;
00201 
00202       memset (dip_prefixes, 0, 65536 * sizeof (dip_prefixes[0]));
00203 
00204       bdapq_m1_prefixes
00205        = malloc (65536 * sizeof (const struct cris_opcode **));
00206       if (bdapq_m1_prefixes == NULL)
00207        return NULL;
00208 
00209       memset (bdapq_m1_prefixes, 0, 65536 * sizeof (bdapq_m1_prefixes[0]));
00210 
00211       bdapq_m2_prefixes
00212        = malloc (65536 * sizeof (const struct cris_opcode **));
00213       if (bdapq_m2_prefixes == NULL)
00214        return NULL;
00215 
00216       memset (bdapq_m2_prefixes, 0, 65536 * sizeof (bdapq_m2_prefixes[0]));
00217 
00218       bdapq_m4_prefixes
00219        = malloc (65536 * sizeof (const struct cris_opcode **));
00220       if (bdapq_m4_prefixes == NULL)
00221        return NULL;
00222 
00223       memset (bdapq_m4_prefixes, 0, 65536 * sizeof (bdapq_m4_prefixes[0]));
00224 
00225       rest_prefixes
00226        = malloc (65536 * sizeof (const struct cris_opcode **));
00227       if (rest_prefixes == NULL)
00228        return NULL;
00229 
00230       memset (rest_prefixes, 0, 65536 * sizeof (rest_prefixes[0]));
00231     }
00232 
00233   /* Get the right table if this is a prefix.
00234      This code is connected to cris_constraints in that it knows what
00235      prefixes play a role in recognition of patterns; the necessary
00236      state is reflected by which table is used.  If constraints
00237      involving match or non-match of prefix insns are changed, then this
00238      probably needs changing too.  */
00239   if (prefix_insn != NO_CRIS_PREFIX)
00240     {
00241       const struct cris_opcode *popcodep
00242        = (opc_table[prefix_insn] != NULL
00243           ? opc_table[prefix_insn]
00244           : get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata));
00245 
00246       if (popcodep == NULL)
00247        return NULL;
00248 
00249       if (popcodep->match == BDAP_QUICK_OPCODE)
00250        {
00251          /* Since some offsets are recognized with "push" macros, we
00252             have to have different tables for them.  */
00253          int offset = (prefix_insn & 255);
00254 
00255          if (offset > 127)
00256            offset -= 256;
00257 
00258          switch (offset)
00259            {
00260            case -4:
00261              prefix_opc_table = bdapq_m4_prefixes;
00262              break;
00263 
00264            case -2:
00265              prefix_opc_table = bdapq_m2_prefixes;
00266              break;
00267 
00268            case -1:
00269              prefix_opc_table = bdapq_m1_prefixes;
00270              break;
00271 
00272            default:
00273              prefix_opc_table = rest_prefixes;
00274              break;
00275            }
00276        }
00277       else if (popcodep->match == DIP_OPCODE)
00278        /* We don't allow postincrement when the prefix is DIP, so use a
00279           different table for DIP.  */
00280        prefix_opc_table = dip_prefixes;
00281       else
00282        prefix_opc_table = rest_prefixes;
00283     }
00284 
00285   if (prefix_insn != NO_CRIS_PREFIX
00286       && prefix_opc_table[insn] != NULL)
00287     max_matchedp = prefix_opc_table[insn];
00288   else if (prefix_insn == NO_CRIS_PREFIX && opc_table[insn] != NULL)
00289     max_matchedp = opc_table[insn];
00290   else
00291     {
00292       const struct cris_opcode *opcodep;
00293       int max_level_of_match = -1;
00294 
00295       for (opcodep = cris_opcodes;
00296           opcodep->name != NULL;
00297           opcodep++)
00298        {
00299          int level_of_match;
00300 
00301          if (disdata->distype == cris_dis_v32)
00302            {
00303              switch (opcodep->applicable_version)
00304               {
00305               case cris_ver_version_all:
00306                 break;
00307 
00308               case cris_ver_v0_3:
00309               case cris_ver_v0_10:
00310               case cris_ver_v3_10:
00311               case cris_ver_sim_v0_10:
00312               case cris_ver_v8_10:
00313               case cris_ver_v10:
00314               case cris_ver_warning:
00315                 continue;
00316 
00317               case cris_ver_v3p:
00318               case cris_ver_v8p:
00319               case cris_ver_v10p:
00320               case cris_ver_v32p:
00321                 break;
00322 
00323               case cris_ver_v8:
00324                 abort ();
00325               default:
00326                 abort ();
00327               }
00328            }
00329          else
00330            {
00331              switch (opcodep->applicable_version)
00332               {
00333               case cris_ver_version_all:
00334               case cris_ver_v0_3:
00335               case cris_ver_v3p:
00336               case cris_ver_v0_10:
00337               case cris_ver_v8p:
00338               case cris_ver_v8_10:
00339               case cris_ver_v10:
00340               case cris_ver_sim_v0_10:
00341               case cris_ver_v10p:
00342               case cris_ver_warning:
00343                 break;
00344 
00345               case cris_ver_v32p:
00346                 continue;
00347 
00348               case cris_ver_v8:
00349                 abort ();
00350               default:
00351                 abort ();
00352               }
00353            }
00354 
00355          /* We give a double lead for bits matching the template in
00356             cris_opcodes.  Not even, because then "move p8,r10" would
00357             be given 2 bits lead over "clear.d r10".  When there's a
00358             tie, the first entry in the table wins.  This is
00359             deliberate, to avoid a more complicated recognition
00360             formula.  */
00361          if ((opcodep->match & insn) == opcodep->match
00362              && (opcodep->lose & insn) == 0
00363              && ((level_of_match
00364                  = cris_constraint (opcodep->args,
00365                                   insn,
00366                                   prefix_insn,
00367                                   disdata))
00368                 >= 0)
00369              && ((level_of_match
00370                  += 2 * number_of_bits (opcodep->match
00371                                      | opcodep->lose))
00372                        > max_level_of_match))
00373                   {
00374                     max_matchedp = opcodep;
00375                     max_level_of_match = level_of_match;
00376 
00377                     /* If there was a full match, never mind looking
00378                       further.  */
00379                     if (level_of_match >= 2 * 16)
00380                      break;
00381                   }
00382               }
00383       /* Fill in the new entry.
00384 
00385         If there are changes to the opcode-table involving prefixes, and
00386         disassembly then does not work correctly, try removing the
00387         else-clause below that fills in the prefix-table.  If that
00388         helps, you need to change the prefix_opc_table setting above, or
00389         something related.  */
00390       if (prefix_insn == NO_CRIS_PREFIX)
00391        opc_table[insn] = max_matchedp;
00392       else
00393        prefix_opc_table[insn] = max_matchedp;
00394     }
00395 
00396   return max_matchedp;
00397 }
00398 
00399 /* Return -1 if the constraints of a bitwise-matched instruction say
00400    that there is no match.  Otherwise return a nonnegative number
00401    indicating the confidence in the match (higher is better).  */
00402 
00403 static int
00404 cris_constraint (const char *cs,
00405                unsigned int insn,
00406                unsigned int prefix_insn,
00407                struct cris_disasm_data *disdata)
00408 {
00409   int retval = 0;
00410   int tmp;
00411   int prefix_ok = 0;
00412   const char *s;
00413 
00414   for (s = cs; *s; s++)
00415     switch (*s)
00416       {
00417       case '!':
00418        /* Do not recognize "pop" if there's a prefix and then only for
00419            v0..v10.  */
00420        if (prefix_insn != NO_CRIS_PREFIX
00421            || disdata->distype != cris_dis_v0_v10)
00422          return -1;
00423        break;
00424 
00425       case 'U':
00426        /* Not recognized at disassembly.  */
00427        return -1;
00428 
00429       case 'M':
00430        /* Size modifier for "clear", i.e. special register 0, 4 or 8.
00431           Check that it is one of them.  Only special register 12 could
00432           be mismatched, but checking for matches is more logical than
00433           checking for mismatches when there are only a few cases.  */
00434        tmp = ((insn >> 12) & 0xf);
00435        if (tmp != 0 && tmp != 4 && tmp != 8)
00436          return -1;
00437        break;
00438 
00439       case 'm':
00440        if ((insn & 0x30) == 0x30)
00441          return -1;
00442        break;
00443 
00444       case 'S':
00445        /* A prefix operand without side-effect.  */
00446        if (prefix_insn != NO_CRIS_PREFIX && (insn & 0x400) == 0)
00447          {
00448            prefix_ok = 1;
00449            break;
00450          }
00451        else
00452          return -1;
00453 
00454       case 's':
00455       case 'y':
00456       case 'Y':
00457        /* If this is a prefixed insn with postincrement (side-effect),
00458           the prefix must not be DIP.  */
00459        if (prefix_insn != NO_CRIS_PREFIX)
00460          {
00461            if (insn & 0x400)
00462              {
00463               const struct cris_opcode *prefix_opcodep
00464                 = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata);
00465 
00466               if (prefix_opcodep->match == DIP_OPCODE)
00467                 return -1;
00468              }
00469 
00470            prefix_ok = 1;
00471          }
00472        break;
00473 
00474       case 'B':
00475        /* If we don't fall through, then the prefix is ok.  */
00476        prefix_ok = 1;
00477 
00478        /* A "push" prefix.  Check for valid "push" size.
00479           In case of special register, it may be != 4.  */
00480        if (prefix_insn != NO_CRIS_PREFIX)
00481          {
00482            /* Match the prefix insn to BDAPQ.  */
00483            const struct cris_opcode *prefix_opcodep
00484              = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata);
00485 
00486            if (prefix_opcodep->match == BDAP_QUICK_OPCODE)
00487              {
00488               int pushsize = (prefix_insn & 255);
00489 
00490               if (pushsize > 127)
00491                 pushsize -= 256;
00492 
00493               if (s[1] == 'P')
00494                 {
00495                   unsigned int spec_reg = (insn >> 12) & 15;
00496                   const struct cris_spec_reg *sregp
00497                     = spec_reg_info (spec_reg, disdata->distype);
00498 
00499                   /* For a special-register, the "prefix size" must
00500                      match the size of the register.  */
00501                   if (sregp && sregp->reg_size == (unsigned int) -pushsize)
00502                     break;
00503                 }
00504               else if (s[1] == 'R')
00505                 {
00506                   if ((insn & 0x30) == 0x20 && pushsize == -4)
00507                     break;
00508                 }
00509               /* FIXME:  Should abort here; next constraint letter
00510                  *must* be 'P' or 'R'.  */
00511              }
00512          }
00513        return -1;
00514 
00515       case 'D':
00516        retval = (((insn >> 12) & 15) == (insn & 15));
00517        if (!retval)
00518          return -1;
00519        else
00520          retval += 4;
00521        break;
00522 
00523       case 'P':
00524        {
00525          const struct cris_spec_reg *sregp
00526            = spec_reg_info ((insn >> 12) & 15, disdata->distype);
00527 
00528          /* Since we match four bits, we will give a value of 4-1 = 3
00529             in a match.  If there is a corresponding exact match of a
00530             special register in another pattern, it will get a value of
00531             4, which will be higher.  This should be correct in that an
00532             exact pattern would match better than a general pattern.
00533 
00534             Note that there is a reason for not returning zero; the
00535             pattern for "clear" is partly  matched in the bit-pattern
00536             (the two lower bits must be zero), while the bit-pattern
00537             for a move from a special register is matched in the
00538             register constraint.  */
00539 
00540          if (sregp != NULL)
00541            {
00542              retval += 3;
00543              break;
00544            }
00545          else
00546            return -1;
00547        }
00548       }
00549 
00550   if (prefix_insn != NO_CRIS_PREFIX && ! prefix_ok)
00551     return -1;
00552 
00553   return retval;
00554 }
00555 
00556 /* Format number as hex with a leading "0x" into outbuffer.  */
00557 
00558 static char *
00559 format_hex (unsigned long number,
00560            char *outbuffer,
00561            struct cris_disasm_data *disdata)
00562 {
00563   /* Truncate negative numbers on >32-bit hosts.  */
00564   number &= 0xffffffff;
00565 
00566   sprintf (outbuffer, "0x%lx", number);
00567 
00568   /* Save this value for the "case" support.  */
00569   if (TRACE_CASE)
00570     last_immediate = number;
00571 
00572   return outbuffer + strlen (outbuffer);
00573 }
00574 
00575 /* Format number as decimal into outbuffer.  Parameter signedp says
00576    whether the number should be formatted as signed (!= 0) or
00577    unsigned (== 0).  */
00578 
00579 static char *
00580 format_dec (long number, char *outbuffer, int signedp)
00581 {
00582   last_immediate = number;
00583   sprintf (outbuffer, signedp ? "%ld" : "%lu", number);
00584 
00585   return outbuffer + strlen (outbuffer);
00586 }
00587 
00588 /* Format the name of the general register regno into outbuffer.  */
00589 
00590 static char *
00591 format_reg (struct cris_disasm_data *disdata,
00592            int regno,
00593            char *outbuffer_start,
00594            bfd_boolean with_reg_prefix)
00595 {
00596   char *outbuffer = outbuffer_start;
00597 
00598   if (with_reg_prefix)
00599     *outbuffer++ = REGISTER_PREFIX_CHAR;
00600 
00601   switch (regno)
00602     {
00603     case 15:
00604       /* For v32, there is no context in which we output PC.  */
00605       if (disdata->distype == cris_dis_v32)
00606        strcpy (outbuffer, "acr");
00607       else
00608        strcpy (outbuffer, "pc");
00609       break;
00610 
00611     case 14:
00612       strcpy (outbuffer, "sp");
00613       break;
00614 
00615     default:
00616       sprintf (outbuffer, "r%d", regno);
00617       break;
00618     }
00619 
00620   return outbuffer_start + strlen (outbuffer_start);
00621 }
00622 
00623 /* Format the name of a support register into outbuffer.  */
00624 
00625 static char *
00626 format_sup_reg (unsigned int regno,
00627               char *outbuffer_start,
00628               bfd_boolean with_reg_prefix)
00629 {
00630   char *outbuffer = outbuffer_start;
00631   int i;
00632 
00633   if (with_reg_prefix)
00634     *outbuffer++ = REGISTER_PREFIX_CHAR;
00635 
00636   for (i = 0; cris_support_regs[i].name != NULL; i++)
00637     if (cris_support_regs[i].number == regno)
00638       {
00639        sprintf (outbuffer, "%s", cris_support_regs[i].name);
00640        return outbuffer_start + strlen (outbuffer_start);
00641       }
00642 
00643   /* There's supposed to be register names covering all numbers, though
00644      some may be generic names.  */
00645   sprintf (outbuffer, "format_sup_reg-BUG");
00646   return outbuffer_start + strlen (outbuffer_start);
00647 }
00648 
00649 /* Return the length of an instruction.  */
00650 
00651 static unsigned
00652 bytes_to_skip (unsigned int insn,
00653               const struct cris_opcode *matchedp,
00654               enum cris_disass_family distype,
00655               const struct cris_opcode *prefix_matchedp)
00656 {
00657   /* Each insn is a word plus "immediate" operands.  */
00658   unsigned to_skip = 2;
00659   const char *template = matchedp->args;
00660   const char *s;
00661 
00662   for (s = template; *s; s++)
00663     if ((*s == 's' || *s == 'N' || *s == 'Y')
00664        && (insn & 0x400) && (insn & 15) == 15
00665        && prefix_matchedp == NULL)
00666       {
00667        /* Immediate via [pc+], so we have to check the size of the
00668           operand.  */
00669        int mode_size = 1 << ((insn >> 4) & (*template == 'z' ? 1 : 3));
00670 
00671        if (matchedp->imm_oprnd_size == SIZE_FIX_32)
00672          to_skip += 4;
00673        else if (matchedp->imm_oprnd_size == SIZE_SPEC_REG)
00674          {
00675            const struct cris_spec_reg *sregp
00676              = spec_reg_info ((insn >> 12) & 15, distype);
00677 
00678            /* FIXME: Improve error handling; should have been caught
00679               earlier.  */
00680            if (sregp == NULL)
00681              return 2;
00682 
00683            /* PC is incremented by two, not one, for a byte.  Except on
00684               CRISv32, where constants are always DWORD-size for
00685               special registers.  */
00686            to_skip +=
00687              distype == cris_dis_v32 ? 4 : (sregp->reg_size + 1) & ~1;
00688          }
00689        else
00690          to_skip += (mode_size + 1) & ~1;
00691       }
00692     else if (*s == 'n')
00693       to_skip += 4;
00694     else if (*s == 'b')
00695       to_skip += 2;
00696 
00697   return to_skip;
00698 }
00699 
00700 /* Print condition code flags.  */
00701 
00702 static char *
00703 print_flags (struct cris_disasm_data *disdata, unsigned int insn, char *cp)
00704 {
00705   /* Use the v8 (Etrax 100) flag definitions for disassembly.
00706      The differences with v0 (Etrax 1..4) vs. Svinto are:
00707       v0 'd' <=> v8 'm'
00708       v0 'e' <=> v8 'b'.
00709      FIXME: Emit v0..v3 flag names somehow.  */
00710   static const char v8_fnames[] = "cvznxibm";
00711   static const char v32_fnames[] = "cvznxiup";
00712   const char *fnames
00713     = disdata->distype == cris_dis_v32 ? v32_fnames : v8_fnames;
00714 
00715   unsigned char flagbits = (((insn >> 8) & 0xf0) | (insn & 15));
00716   int i;
00717 
00718   for (i = 0; i < 8; i++)
00719     if (flagbits & (1 << i))
00720       *cp++ = fnames[i];
00721 
00722   return cp;
00723 }
00724 
00725 /* Print out an insn with its operands, and update the info->insn_type
00726    fields.  The prefix_opcodep and the rest hold a prefix insn that is
00727    supposed to be output as an address mode.  */
00728 
00729 static void
00730 print_with_operands (const struct cris_opcode *opcodep,
00731                    unsigned int insn,
00732                    unsigned char *buffer,
00733                    bfd_vma addr,
00734                    disassemble_info *info,
00735                    /* If a prefix insn was before this insn (and is supposed
00736                      to be output as an address), here is a description of
00737                      it.  */
00738                    const struct cris_opcode *prefix_opcodep,
00739                    unsigned int prefix_insn,
00740                    unsigned char *prefix_buffer,
00741                    bfd_boolean with_reg_prefix)
00742 {
00743   /* Get a buffer of somewhat reasonable size where we store
00744      intermediate parts of the insn.  */
00745   char temp[sizeof (".d [$r13=$r12-2147483648],$r10") * 2];
00746   char *tp = temp;
00747   static const char mode_char[] = "bwd?";
00748   const char *s;
00749   const char *cs;
00750   struct cris_disasm_data *disdata
00751     = (struct cris_disasm_data *) info->private_data;
00752 
00753   /* Print out the name first thing we do.  */
00754   (*info->fprintf_func) (info->stream, "%s", opcodep->name);
00755 
00756   cs = opcodep->args;
00757   s = cs;
00758 
00759   /* Ignore any prefix indicator.  */
00760   if (*s == 'p')
00761     s++;
00762 
00763   if (*s == 'm' || *s == 'M' || *s == 'z')
00764     {
00765       *tp++ = '.';
00766 
00767       /* Get the size-letter.  */
00768       *tp++ = *s == 'M'
00769        ? (insn & 0x8000 ? 'd'
00770           : insn & 0x4000 ? 'w' : 'b')
00771        : mode_char[(insn >> 4) & (*s == 'z' ? 1 : 3)];
00772 
00773       /* Ignore the size and the space character that follows.  */
00774       s += 2;
00775     }
00776 
00777   /* Add a space if this isn't a long-branch, because for those will add
00778      the condition part of the name later.  */
00779   if (opcodep->match != (BRANCH_PC_LOW + BRANCH_INCR_HIGH * 256))
00780     *tp++ = ' ';
00781 
00782   /* Fill in the insn-type if deducible from the name (and there's no
00783      better way).  */
00784   if (opcodep->name[0] == 'j')
00785     {
00786       if (CONST_STRNEQ (opcodep->name, "jsr"))
00787        /* It's "jsr" or "jsrc".  */
00788        info->insn_type = dis_jsr;
00789       else
00790        /* Any other jump-type insn is considered a branch.  */
00791        info->insn_type = dis_branch;
00792     }
00793 
00794   /* We might know some more fields right now.  */
00795   info->branch_delay_insns = opcodep->delayed;
00796 
00797   /* Handle operands.  */
00798   for (; *s; s++)
00799     {
00800     switch (*s)
00801       {
00802       case 'T':
00803        tp = format_sup_reg ((insn >> 12) & 15, tp, with_reg_prefix);
00804        break;
00805 
00806       case 'A':
00807        if (with_reg_prefix)
00808          *tp++ = REGISTER_PREFIX_CHAR;
00809        *tp++ = 'a';
00810        *tp++ = 'c';
00811        *tp++ = 'r';
00812        break;
00813        
00814       case '[':
00815       case ']':
00816       case ',':
00817        *tp++ = *s;
00818        break;
00819 
00820       case '!':
00821        /* Ignore at this point; used at earlier stages to avoid
00822           recognition if there's a prefix at something that in other
00823           ways looks like a "pop".  */
00824        break;
00825 
00826       case 'd':
00827        /* Ignore.  This is an optional ".d " on the large one of
00828           relaxable insns.  */
00829        break;
00830 
00831       case 'B':
00832        /* This was the prefix that made this a "push".  We've already
00833           handled it by recognizing it, so signal that the prefix is
00834           handled by setting it to NULL.  */
00835        prefix_opcodep = NULL;
00836        break;
00837 
00838       case 'D':
00839       case 'r':
00840        tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
00841        break;
00842 
00843       case 'R':
00844        tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
00845        break;
00846 
00847       case 'n':
00848        {
00849          /* Like N but pc-relative to the start of the insn.  */
00850          unsigned long number
00851            = (buffer[2] + buffer[3] * 256 + buffer[4] * 65536
00852               + buffer[5] * 0x1000000 + addr);
00853 
00854          /* Finish off and output previous formatted bytes.  */
00855          *tp = 0;
00856          if (temp[0])
00857            (*info->fprintf_func) (info->stream, "%s", temp);
00858          tp = temp;
00859 
00860          (*info->print_address_func) ((bfd_vma) number, info);
00861        }
00862        break;
00863 
00864       case 'u':
00865        {
00866          /* Like n but the offset is bits <3:0> in the instruction.  */
00867          unsigned long number = (buffer[0] & 0xf) * 2 + addr;
00868 
00869          /* Finish off and output previous formatted bytes.  */
00870          *tp = 0;
00871          if (temp[0])
00872            (*info->fprintf_func) (info->stream, "%s", temp);
00873          tp = temp;
00874 
00875          (*info->print_address_func) ((bfd_vma) number, info);
00876        }
00877        break;
00878 
00879       case 'N':
00880       case 'y':
00881       case 'Y':
00882       case 'S':
00883       case 's':
00884        /* Any "normal" memory operand.  */
00885        if ((insn & 0x400) && (insn & 15) == 15 && prefix_opcodep == NULL)
00886          {
00887            /* We're looking at [pc+], i.e. we need to output an immediate
00888               number, where the size can depend on different things.  */
00889            long number;
00890            int signedp
00891              = ((*cs == 'z' && (insn & 0x20))
00892                || opcodep->match == BDAP_QUICK_OPCODE);
00893            int nbytes;
00894 
00895            if (opcodep->imm_oprnd_size == SIZE_FIX_32)
00896              nbytes = 4;
00897            else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
00898              {
00899               const struct cris_spec_reg *sregp
00900                 = spec_reg_info ((insn >> 12) & 15, disdata->distype);
00901 
00902               /* A NULL return should have been as a non-match earlier,
00903                  so catch it as an internal error in the error-case
00904                  below.  */
00905               if (sregp == NULL)
00906                 /* Whatever non-valid size.  */
00907                 nbytes = 42;
00908               else
00909                 /* PC is always incremented by a multiple of two.
00910                    For CRISv32, immediates are always 4 bytes for
00911                    special registers.  */
00912                 nbytes = disdata->distype == cris_dis_v32
00913                   ? 4 : (sregp->reg_size + 1) & ~1;
00914              }
00915            else
00916              {
00917               int mode_size = 1 << ((insn >> 4) & (*cs == 'z' ? 1 : 3));
00918 
00919               if (mode_size == 1)
00920                 nbytes = 2;
00921               else
00922                 nbytes = mode_size;
00923              }
00924 
00925            switch (nbytes)
00926              {
00927              case 1:
00928               number = buffer[2];
00929               if (signedp && number > 127)
00930                 number -= 256;
00931               break;
00932 
00933              case 2:
00934               number = buffer[2] + buffer[3] * 256;
00935               if (signedp && number > 32767)
00936                 number -= 65536;
00937               break;
00938 
00939              case 4:
00940               number
00941                 = buffer[2] + buffer[3] * 256 + buffer[4] * 65536
00942                 + buffer[5] * 0x1000000;
00943               break;
00944 
00945              default:
00946               strcpy (tp, "bug");
00947               tp += 3;
00948               number = 42;
00949              }
00950 
00951            if ((*cs == 'z' && (insn & 0x20))
00952               || (opcodep->match == BDAP_QUICK_OPCODE
00953                   && (nbytes <= 2 || buffer[1 + nbytes] == 0)))
00954              tp = format_dec (number, tp, signedp);
00955            else
00956              {
00957               unsigned int highbyte = (number >> 24) & 0xff;
00958 
00959               /* Either output this as an address or as a number.  If it's
00960                  a dword with the same high-byte as the address of the
00961                  insn, assume it's an address, and also if it's a non-zero
00962                  non-0xff high-byte.  If this is a jsr or a jump, then
00963                  it's definitely an address.  */
00964               if (nbytes == 4
00965                   && (highbyte == ((addr >> 24) & 0xff)
00966                      || (highbyte != 0 && highbyte != 0xff)
00967                      || info->insn_type == dis_branch
00968                      || info->insn_type == dis_jsr))
00969                 {
00970                   /* Finish off and output previous formatted bytes.  */
00971                   *tp = 0;
00972                   tp = temp;
00973                   if (temp[0])
00974                     (*info->fprintf_func) (info->stream, "%s", temp);
00975 
00976                   (*info->print_address_func) ((bfd_vma) number, info);
00977 
00978                   info->target = number;
00979                 }
00980               else
00981                 tp = format_hex (number, tp, disdata);
00982              }
00983          }
00984        else
00985          {
00986            /* Not an immediate number.  Then this is a (possibly
00987               prefixed) memory operand.  */
00988            if (info->insn_type != dis_nonbranch)
00989              {
00990               int mode_size
00991                 = 1 << ((insn >> 4)
00992                        & (opcodep->args[0] == 'z' ? 1 : 3));
00993               int size;
00994               info->insn_type = dis_dref;
00995               info->flags |= CRIS_DIS_FLAG_MEMREF;
00996 
00997               if (opcodep->imm_oprnd_size == SIZE_FIX_32)
00998                 size = 4;
00999               else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
01000                 {
01001                   const struct cris_spec_reg *sregp
01002                     = spec_reg_info ((insn >> 12) & 15, disdata->distype);
01003 
01004                   /* FIXME: Improve error handling; should have been caught
01005                      earlier.  */
01006                   if (sregp == NULL)
01007                     size = 4;
01008                   else
01009                     size = sregp->reg_size;
01010                 }
01011               else
01012                 size = mode_size;
01013 
01014               info->data_size = size;
01015              }
01016 
01017            *tp++ = '[';
01018 
01019            if (prefix_opcodep
01020               /* We don't match dip with a postincremented field
01021                  as a side-effect address mode.  */
01022               && ((insn & 0x400) == 0
01023                   || prefix_opcodep->match != DIP_OPCODE))
01024              {
01025               if (insn & 0x400)
01026                 {
01027                   tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
01028                   *tp++ = '=';
01029                 }
01030 
01031 
01032               /* We mainly ignore the prefix format string when the
01033                  address-mode syntax is output.  */
01034               switch (prefix_opcodep->match)
01035                 {
01036                 case DIP_OPCODE:
01037                   /* It's [r], [r+] or [pc+].  */
01038                   if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
01039                     {
01040                      /* It's [pc+].  This cannot possibly be anything
01041                         but an address.  */
01042                      unsigned long number
01043                        = prefix_buffer[2] + prefix_buffer[3] * 256
01044                        + prefix_buffer[4] * 65536
01045                        + prefix_buffer[5] * 0x1000000;
01046 
01047                      info->target = (bfd_vma) number;
01048 
01049                      /* Finish off and output previous formatted
01050                         data.  */
01051                      *tp = 0;
01052                      tp = temp;
01053                      if (temp[0])
01054                        (*info->fprintf_func) (info->stream, "%s", temp);
01055 
01056                      (*info->print_address_func) ((bfd_vma) number, info);
01057                     }
01058                   else
01059                     {
01060                      /* For a memref in an address, we use target2.
01061                         In this case, target is zero.  */
01062                      info->flags
01063                        |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
01064                            | CRIS_DIS_FLAG_MEM_TARGET2_MEM);
01065 
01066                      info->target2 = prefix_insn & 15;
01067 
01068                      *tp++ = '[';
01069                      tp = format_reg (disdata, prefix_insn & 15, tp,
01070                                     with_reg_prefix);
01071                      if (prefix_insn & 0x400)
01072                        *tp++ = '+';
01073                      *tp++ = ']';
01074                     }
01075                   break;
01076 
01077                 case BDAP_QUICK_OPCODE:
01078                   {
01079                     int number;
01080 
01081                     number = prefix_buffer[0];
01082                     if (number > 127)
01083                      number -= 256;
01084 
01085                     /* Output "reg+num" or, if num < 0, "reg-num".  */
01086                     tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
01087                                    with_reg_prefix);
01088                     if (number >= 0)
01089                      *tp++ = '+';
01090                     tp = format_dec (number, tp, 1);
01091 
01092                     info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
01093                     info->target = (prefix_insn >> 12) & 15;
01094                     info->target2 = (bfd_vma) number;
01095                     break;
01096                   }
01097 
01098                 case BIAP_OPCODE:
01099                   /* Output "r+R.m".  */
01100                   tp = format_reg (disdata, prefix_insn & 15, tp,
01101                                  with_reg_prefix);
01102                   *tp++ = '+';
01103                   tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
01104                                  with_reg_prefix);
01105                   *tp++ = '.';
01106                   *tp++ = mode_char[(prefix_insn >> 4) & 3];
01107 
01108                   info->flags
01109                     |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
01110                        | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
01111 
01112                        | ((prefix_insn & 0x8000)
01113                           ? CRIS_DIS_FLAG_MEM_TARGET2_MULT4
01114                           : ((prefix_insn & 0x8000)
01115                             ? CRIS_DIS_FLAG_MEM_TARGET2_MULT2 : 0)));
01116 
01117                   /* Is it the casejump?  It's a "adds.w [pc+r%d.w],pc".  */
01118                   if (insn == 0xf83f && (prefix_insn & ~0xf000) == 0x55f)
01119                     /* Then start interpreting data as offsets.  */
01120                     case_offset_counter = no_of_case_offsets;
01121                   break;
01122 
01123                 case BDAP_INDIR_OPCODE:
01124                   /* Output "r+s.m", or, if "s" is [pc+], "r+s" or
01125                      "r-s".  */
01126                   tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
01127                                  with_reg_prefix);
01128 
01129                   if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
01130                     {
01131                      long number;
01132                      unsigned int nbytes;
01133 
01134                      /* It's a value.  Get its size.  */
01135                      int mode_size = 1 << ((prefix_insn >> 4) & 3);
01136 
01137                      if (mode_size == 1)
01138                        nbytes = 2;
01139                      else
01140                        nbytes = mode_size;
01141 
01142                      switch (nbytes)
01143                        {
01144                        case 1:
01145                          number = prefix_buffer[2];
01146                          if (number > 127)
01147                            number -= 256;
01148                          break;
01149 
01150                        case 2:
01151                          number = prefix_buffer[2] + prefix_buffer[3] * 256;
01152                          if (number > 32767)
01153                            number -= 65536;
01154                          break;
01155 
01156                        case 4:
01157                          number
01158                            = prefix_buffer[2] + prefix_buffer[3] * 256
01159                            + prefix_buffer[4] * 65536
01160                            + prefix_buffer[5] * 0x1000000;
01161                          break;
01162 
01163                        default:
01164                          strcpy (tp, "bug");
01165                          tp += 3;
01166                          number = 42;
01167                        }
01168 
01169                      info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
01170                      info->target2 = (bfd_vma) number;
01171 
01172                      /* If the size is dword, then assume it's an
01173                         address.  */
01174                      if (nbytes == 4)
01175                        {
01176                          /* Finish off and output previous formatted
01177                             bytes.  */
01178                          *tp++ = '+';
01179                          *tp = 0;
01180                          tp = temp;
01181                          (*info->fprintf_func) (info->stream, "%s", temp);
01182 
01183                          (*info->print_address_func) ((bfd_vma) number, info);
01184                        }
01185                      else
01186                        {
01187                          if (number >= 0)
01188                            *tp++ = '+';
01189                          tp = format_dec (number, tp, 1);
01190                        }
01191                     }
01192                   else
01193                     {
01194                      /* Output "r+[R].m" or "r+[R+].m".  */
01195                      *tp++ = '+';
01196                      *tp++ = '[';
01197                      tp = format_reg (disdata, prefix_insn & 15, tp,
01198                                     with_reg_prefix);
01199                      if (prefix_insn & 0x400)
01200                        *tp++ = '+';
01201                      *tp++ = ']';
01202                      *tp++ = '.';
01203                      *tp++ = mode_char[(prefix_insn >> 4) & 3];
01204 
01205                      info->flags
01206                        |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
01207                            | CRIS_DIS_FLAG_MEM_TARGET2_MEM
01208                            | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
01209 
01210                            | (((prefix_insn >> 4) == 2)
01211                              ? 0
01212                              : (((prefix_insn >> 4) & 3) == 1
01213                                 ? CRIS_DIS_FLAG_MEM_TARGET2_MEM_WORD
01214                                 : CRIS_DIS_FLAG_MEM_TARGET2_MEM_BYTE)));
01215                     }
01216                   break;
01217 
01218                 default:
01219                   (*info->fprintf_func) (info->stream, "?prefix-bug");
01220                 }
01221 
01222               /* To mark that the prefix is used, reset it.  */
01223               prefix_opcodep = NULL;
01224              }
01225            else
01226              {
01227               tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
01228 
01229               info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
01230               info->target = insn & 15;
01231 
01232               if (insn & 0x400)
01233                 *tp++ = '+';
01234              }
01235            *tp++ = ']';
01236          }
01237        break;
01238 
01239       case 'x':
01240        tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
01241        *tp++ = '.';
01242        *tp++ = mode_char[(insn >> 4) & 3];
01243        break;
01244 
01245       case 'I':
01246        tp = format_dec (insn & 63, tp, 0);
01247        break;
01248 
01249       case 'b':
01250        {
01251          int where = buffer[2] + buffer[3] * 256;
01252 
01253          if (where > 32767)
01254            where -= 65536;
01255 
01256          where += addr + ((disdata->distype == cris_dis_v32) ? 0 : 4);
01257 
01258          if (insn == BA_PC_INCR_OPCODE)
01259            info->insn_type = dis_branch;
01260          else
01261            info->insn_type = dis_condbranch;
01262 
01263          info->target = (bfd_vma) where;
01264 
01265          *tp = 0;
01266          tp = temp;
01267          (*info->fprintf_func) (info->stream, "%s%s ",
01268                              temp, cris_cc_strings[insn >> 12]);
01269 
01270          (*info->print_address_func) ((bfd_vma) where, info);
01271        }
01272       break;
01273 
01274     case 'c':
01275       tp = format_dec (insn & 31, tp, 0);
01276       break;
01277 
01278     case 'C':
01279       tp = format_dec (insn & 15, tp, 0);
01280       break;
01281 
01282     case 'o':
01283       {
01284        long offset = insn & 0xfe;
01285        bfd_vma target;
01286 
01287        if (insn & 1)
01288          offset |= ~0xff;
01289 
01290        if (opcodep->match == BA_QUICK_OPCODE)
01291          info->insn_type = dis_branch;
01292        else
01293          info->insn_type = dis_condbranch;
01294 
01295        target = addr + ((disdata->distype == cris_dis_v32) ? 0 : 2) + offset;
01296        info->target = target;
01297        *tp = 0;
01298        tp = temp;
01299        (*info->fprintf_func) (info->stream, "%s", temp);
01300        (*info->print_address_func) (target, info);
01301       }
01302       break;
01303 
01304     case 'Q':
01305     case 'O':
01306       {
01307        long number = buffer[0];
01308 
01309        if (number > 127)
01310          number = number - 256;
01311 
01312        tp = format_dec (number, tp, 1);
01313        *tp++ = ',';
01314        tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
01315       }
01316       break;
01317 
01318     case 'f':
01319       tp = print_flags (disdata, insn, tp);
01320       break;
01321 
01322     case 'i':
01323       tp = format_dec ((insn & 32) ? (insn & 31) | ~31L : insn & 31, tp, 1);
01324       break;
01325 
01326     case 'P':
01327       {
01328        const struct cris_spec_reg *sregp
01329          = spec_reg_info ((insn >> 12) & 15, disdata->distype);
01330 
01331        if (sregp->name == NULL)
01332          /* Should have been caught as a non-match eariler.  */
01333          *tp++ = '?';
01334        else
01335          {
01336            if (with_reg_prefix)
01337              *tp++ = REGISTER_PREFIX_CHAR;
01338            strcpy (tp, sregp->name);
01339            tp += strlen (tp);
01340          }
01341       }
01342       break;
01343 
01344     default:
01345       strcpy (tp, "???");
01346       tp += 3;
01347     }
01348   }
01349 
01350   *tp = 0;
01351 
01352   if (prefix_opcodep)
01353     (*info->fprintf_func) (info->stream, " (OOPS unused prefix \"%s: %s\")",
01354                         prefix_opcodep->name, prefix_opcodep->args);
01355 
01356   (*info->fprintf_func) (info->stream, "%s", temp);
01357 
01358   /* Get info for matching case-tables, if we don't have any active.
01359      We assume that the last constant seen is used; either in the insn
01360      itself or in a "move.d const,rN, sub.d rN,rM"-like sequence.  */
01361   if (TRACE_CASE && case_offset_counter == 0)
01362     {
01363       if (CONST_STRNEQ (opcodep->name, "sub"))
01364        case_offset = last_immediate;
01365 
01366       /* It could also be an "add", if there are negative case-values.  */
01367       else if (CONST_STRNEQ (opcodep->name, "add"))
01368        /* The first case is the negated operand to the add.  */
01369        case_offset = -last_immediate;
01370 
01371       /* A bound insn will tell us the number of cases.  */
01372       else if (CONST_STRNEQ (opcodep->name, "bound"))
01373        no_of_case_offsets = last_immediate + 1;
01374 
01375       /* A jump or jsr or branch breaks the chain of insns for a
01376         case-table, so assume default first-case again.  */
01377       else if (info->insn_type == dis_jsr
01378               || info->insn_type == dis_branch
01379               || info->insn_type == dis_condbranch)
01380        case_offset = 0;
01381     }
01382 }
01383 
01384 
01385 /* Print the CRIS instruction at address memaddr on stream.  Returns
01386    length of the instruction, in bytes.  Prefix register names with `$' if
01387    WITH_REG_PREFIX.  */
01388 
01389 static int
01390 print_insn_cris_generic (bfd_vma memaddr,
01391                       disassemble_info *info,
01392                       bfd_boolean with_reg_prefix)
01393 {
01394   int nbytes;
01395   unsigned int insn;
01396   const struct cris_opcode *matchedp;
01397   int advance = 0;
01398   struct cris_disasm_data *disdata
01399     = (struct cris_disasm_data *) info->private_data;
01400 
01401   /* No instruction will be disassembled as longer than this number of
01402      bytes; stacked prefixes will not be expanded.  */
01403   unsigned char buffer[MAX_BYTES_PER_CRIS_INSN];
01404   unsigned char *bufp;
01405   int status = 0;
01406   bfd_vma addr;
01407 
01408   /* There will be an "out of range" error after the last instruction.
01409      Reading pairs of bytes in decreasing number, we hope that we will get
01410      at least the amount that we will consume.
01411 
01412      If we can't get any data, or we do not get enough data, we print
01413      the error message.  */
01414 
01415   for (nbytes = MAX_BYTES_PER_CRIS_INSN; nbytes > 0; nbytes -= 2)
01416     {
01417       status = (*info->read_memory_func) (memaddr, buffer, nbytes, info);
01418       if (status == 0)
01419        break;
01420     }
01421 
01422   /* If we did not get all we asked for, then clear the rest.
01423      Hopefully this makes a reproducible result in case of errors.  */
01424   if (nbytes != MAX_BYTES_PER_CRIS_INSN)
01425     memset (buffer + nbytes, 0, MAX_BYTES_PER_CRIS_INSN - nbytes);
01426 
01427   addr = memaddr;
01428   bufp = buffer;
01429 
01430   /* Set some defaults for the insn info.  */
01431   info->insn_info_valid = 1;
01432   info->branch_delay_insns = 0;
01433   info->data_size = 0;
01434   info->insn_type = dis_nonbranch;
01435   info->flags = 0;
01436   info->target = 0;
01437   info->target2 = 0;
01438 
01439   /* If we got any data, disassemble it.  */
01440   if (nbytes != 0)
01441     {
01442       matchedp = NULL;
01443 
01444       insn = bufp[0] + bufp[1] * 256;
01445 
01446       /* If we're in a case-table, don't disassemble the offsets.  */
01447       if (TRACE_CASE && case_offset_counter != 0)
01448        {
01449          info->insn_type = dis_noninsn;
01450          advance += 2;
01451 
01452          /* If to print data as offsets, then shortcut here.  */
01453          (*info->fprintf_func) (info->stream, "case %ld%s: -> ",
01454                              case_offset + no_of_case_offsets
01455                              - case_offset_counter,
01456                              case_offset_counter == 1 ? "/default" :
01457                              "");
01458 
01459          (*info->print_address_func) ((bfd_vma)
01460                                    ((short) (insn)
01461                                    + (long) (addr
01462                                             - (no_of_case_offsets
01463                                                - case_offset_counter)
01464                                             * 2)), info);
01465          case_offset_counter--;
01466 
01467          /* The default case start (without a "sub" or "add") must be
01468             zero.  */
01469          if (case_offset_counter == 0)
01470            case_offset = 0;
01471        }
01472       else if (insn == 0)
01473        {
01474          /* We're often called to disassemble zeroes.  While this is a
01475             valid "bcc .+2" insn, it is also useless enough and enough
01476             of a nuiscance that we will just output "bcc .+2" for it
01477             and signal it as a noninsn.  */
01478          (*info->fprintf_func) (info->stream,
01479                              disdata->distype == cris_dis_v32
01480                              ? "bcc ." : "bcc .+2");
01481          info->insn_type = dis_noninsn;
01482          advance += 2;
01483        }
01484       else
01485        {
01486          const struct cris_opcode *prefix_opcodep = NULL;
01487          unsigned char *prefix_buffer = bufp;
01488          unsigned int prefix_insn = insn;
01489          int prefix_size = 0;
01490 
01491          matchedp = get_opcode_entry (insn, NO_CRIS_PREFIX, disdata);
01492 
01493          /* Check if we're supposed to write out prefixes as address
01494             modes and if this was a prefix.  */
01495          if (matchedp != NULL && PARSE_PREFIX && matchedp->args[0] == 'p')
01496            {
01497              /* If it's a prefix, put it into the prefix vars and get the
01498                main insn.  */
01499              prefix_size = bytes_to_skip (prefix_insn, matchedp,
01500                                       disdata->distype, NULL);
01501              prefix_opcodep = matchedp;
01502 
01503              insn = bufp[prefix_size] + bufp[prefix_size + 1] * 256;
01504              matchedp = get_opcode_entry (insn, prefix_insn, disdata);
01505 
01506              if (matchedp != NULL)
01507               {
01508                 addr += prefix_size;
01509                 bufp += prefix_size;
01510                 advance += prefix_size;
01511               }
01512              else
01513               {
01514                 /* The "main" insn wasn't valid, at least not when
01515                    prefixed.  Put back things enough to output the
01516                    prefix insn only, as a normal insn.  */
01517                 matchedp = prefix_opcodep;
01518                 insn = prefix_insn;
01519                 prefix_opcodep = NULL;
01520               }
01521            }
01522 
01523          if (matchedp == NULL)
01524            {
01525              (*info->fprintf_func) (info->stream, "??0x%x", insn);
01526              advance += 2;
01527 
01528              info->insn_type = dis_noninsn;
01529            }
01530          else
01531            {
01532              advance
01533               += bytes_to_skip (insn, matchedp, disdata->distype,
01534                               prefix_opcodep);
01535 
01536              /* The info_type and assorted fields will be set according
01537                to the operands.   */
01538              print_with_operands (matchedp, insn, bufp, addr, info,
01539                                prefix_opcodep, prefix_insn,
01540                                prefix_buffer, with_reg_prefix);
01541            }
01542        }
01543     }
01544   else
01545     info->insn_type = dis_noninsn;
01546 
01547   /* If we read less than MAX_BYTES_PER_CRIS_INSN, i.e. we got an error
01548      status when reading that much, and the insn decoding indicated a
01549      length exceeding what we read, there is an error.  */
01550   if (status != 0 && (nbytes == 0 || advance > nbytes))
01551     {
01552       (*info->memory_error_func) (status, memaddr, info);
01553       return -1;
01554     }
01555 
01556   /* Max supported insn size with one folded prefix insn.  */
01557   info->bytes_per_line = MAX_BYTES_PER_CRIS_INSN;
01558 
01559   /* I would like to set this to a fixed value larger than the actual
01560      number of bytes to print in order to avoid spaces between bytes,
01561      but objdump.c (2.9.1) does not like that, so we print 16-bit
01562      chunks, which is the next choice.  */
01563   info->bytes_per_chunk = 2;
01564 
01565   /* Printing bytes in order of increasing addresses makes sense,
01566      especially on a little-endian target.
01567      This is completely the opposite of what you think; setting this to
01568      BFD_ENDIAN_LITTLE will print bytes in order N..0 rather than the 0..N
01569      we want.  */
01570   info->display_endian = BFD_ENDIAN_BIG;
01571 
01572   return advance;
01573 }
01574 
01575 /* Disassemble, prefixing register names with `$'.  CRIS v0..v10.  */
01576 
01577 static int
01578 print_insn_cris_with_register_prefix (bfd_vma vma,
01579                                   disassemble_info *info)
01580 {
01581   if (info->private_data == NULL
01582       && !cris_parse_disassembler_options (info, cris_dis_v0_v10))
01583     return -1;
01584   return print_insn_cris_generic (vma, info, TRUE);
01585 }
01586 
01587 /* Disassemble, prefixing register names with `$'.  CRIS v32.  */
01588 
01589 static int
01590 print_insn_crisv32_with_register_prefix (bfd_vma vma,
01591                                     disassemble_info *info)
01592 {
01593   if (info->private_data == NULL
01594       && !cris_parse_disassembler_options (info, cris_dis_v32))
01595     return -1;
01596   return print_insn_cris_generic (vma, info, TRUE);
01597 }
01598 
01599 /* Disassemble, prefixing register names with `$'.
01600    Common v10 and v32 subset.  */
01601 
01602 static int
01603 print_insn_crisv10_v32_with_register_prefix (bfd_vma vma,
01604                                         disassemble_info *info)
01605 {
01606   if (info->private_data == NULL
01607       && !cris_parse_disassembler_options (info, cris_dis_common_v10_v32))
01608     return -1;
01609   return print_insn_cris_generic (vma, info, TRUE);
01610 }
01611 
01612 /* Disassemble, no prefixes on register names.  CRIS v0..v10.  */
01613 
01614 static int
01615 print_insn_cris_without_register_prefix (bfd_vma vma,
01616                                     disassemble_info *info)
01617 {
01618   if (info->private_data == NULL
01619       && !cris_parse_disassembler_options (info, cris_dis_v0_v10))
01620     return -1;
01621   return print_insn_cris_generic (vma, info, FALSE);
01622 }
01623 
01624 /* Disassemble, no prefixes on register names.  CRIS v32.  */
01625 
01626 static int
01627 print_insn_crisv32_without_register_prefix (bfd_vma vma,
01628                                        disassemble_info *info)
01629 {
01630   if (info->private_data == NULL
01631       && !cris_parse_disassembler_options (info, cris_dis_v32))
01632     return -1;
01633   return print_insn_cris_generic (vma, info, FALSE);
01634 }
01635 
01636 /* Disassemble, no prefixes on register names.
01637    Common v10 and v32 subset.  */
01638 
01639 static int
01640 print_insn_crisv10_v32_without_register_prefix (bfd_vma vma,
01641                                           disassemble_info *info)
01642 {
01643   if (info->private_data == NULL
01644       && !cris_parse_disassembler_options (info, cris_dis_common_v10_v32))
01645     return -1;
01646   return print_insn_cris_generic (vma, info, FALSE);
01647 }
01648 
01649 /* Return a disassembler-function that prints registers with a `$' prefix,
01650    or one that prints registers without a prefix.
01651    FIXME: We should improve the solution to avoid the multitude of
01652    functions seen above.  */
01653 
01654 disassembler_ftype
01655 cris_get_disassembler (bfd *abfd)
01656 {
01657   /* If there's no bfd in sight, we return what is valid as input in all
01658      contexts if fed back to the assembler: disassembly *with* register
01659      prefix.  Unfortunately this will be totally wrong for v32.  */
01660   if (abfd == NULL)
01661     return print_insn_cris_with_register_prefix;
01662 
01663   if (bfd_get_symbol_leading_char (abfd) == 0)
01664     {
01665       if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
01666        return print_insn_crisv32_with_register_prefix;
01667       if (bfd_get_mach (abfd) == bfd_mach_cris_v10_v32)
01668        return print_insn_crisv10_v32_with_register_prefix;
01669 
01670       /* We default to v10.  This may be specifically specified in the
01671         bfd mach, but is also the default setting.  */
01672       return print_insn_cris_with_register_prefix;
01673     }
01674 
01675   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
01676     return print_insn_crisv32_without_register_prefix;
01677   if (bfd_get_mach (abfd) == bfd_mach_cris_v10_v32)
01678     return print_insn_crisv10_v32_without_register_prefix;
01679   return print_insn_cris_without_register_prefix;
01680 }
01681 
01682 /* Local variables:
01683    eval: (c-set-style "gnu")
01684    indent-tabs-mode: t
01685    End:  */