Back to index

cell-binutils  2.17cvs20070401
or32-dis.c
Go to the documentation of this file.
00001 /* Instruction printing code for the OpenRISC 1000
00002    Copyright (C) 2002, 2005 Free Software Foundation, Inc.
00003    Contributed by Damjan Lampret <lampret@opencores.org>.
00004    Modified from a29k port.
00005 
00006    This file is part of Binutils.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for 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 #define DEBUG 0
00024 
00025 #include "dis-asm.h"
00026 #include "opcode/or32.h"
00027 #include "safe-ctype.h"
00028 #include <string.h>
00029 #include <stdlib.h>
00030 
00031 #define EXTEND29(x) ((x) & (unsigned long) 0x10000000 ? ((x) | (unsigned long) 0xf0000000) : ((x)))
00032 
00033 /* Now find the four bytes of INSN_CH and put them in *INSN.  */
00034 
00035 static void
00036 find_bytes_big (unsigned char *insn_ch, unsigned long *insn)
00037 {
00038   *insn =
00039     ((unsigned long) insn_ch[0] << 24) +
00040     ((unsigned long) insn_ch[1] << 16) +
00041     ((unsigned long) insn_ch[2] << 8) +
00042     ((unsigned long) insn_ch[3]);
00043 #if DEBUG
00044   printf ("find_bytes_big3: %x\n", *insn);
00045 #endif
00046 }
00047 
00048 static void
00049 find_bytes_little (unsigned char *insn_ch, unsigned long *insn)
00050 {
00051   *insn =
00052     ((unsigned long) insn_ch[3] << 24) +
00053     ((unsigned long) insn_ch[2] << 16) +
00054     ((unsigned long) insn_ch[1] << 8) +
00055     ((unsigned long) insn_ch[0]);
00056 }
00057 
00058 typedef void (*find_byte_func_type) (unsigned char *, unsigned long *);
00059 
00060 static unsigned long
00061 or32_extract (char param_ch, char *enc_initial, unsigned long insn)
00062 {
00063   char *enc;
00064   unsigned long ret = 0;
00065   int opc_pos = 0;
00066   int param_pos = 0;
00067 
00068   for (enc = enc_initial; *enc != '\0'; enc++)
00069     if (*enc == param_ch)
00070       {
00071        if (enc - 2 >= enc_initial && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
00072          continue;
00073        else
00074          param_pos++;
00075       }
00076 
00077 #if DEBUG
00078   printf ("or32_extract: %c %x ", param_ch, param_pos);
00079 #endif
00080   opc_pos = 32;
00081 
00082   for (enc = enc_initial; *enc != '\0'; )
00083     if ((*enc == '0') && (*(enc + 1) == 'x'))
00084       {
00085        opc_pos -= 4;
00086 
00087        if ((param_ch == '0') || (param_ch == '1'))
00088          {
00089            unsigned long tmp = strtoul (enc, NULL, 16);
00090 #if DEBUG
00091            printf (" enc=%s, tmp=%x ", enc, tmp);
00092 #endif
00093            if (param_ch == '0')
00094              tmp = 15 - tmp;
00095            ret |= tmp << opc_pos;
00096          }
00097        enc += 3;
00098       }
00099     else if ((*enc == '0') || (*enc == '1'))
00100       {
00101        opc_pos--;
00102        if (param_ch == *enc)
00103          ret |= 1 << opc_pos;
00104        enc++;
00105       }
00106     else if (*enc == param_ch)
00107       {
00108        opc_pos--;
00109        param_pos--;
00110 #if DEBUG
00111        printf ("\n  ret=%x opc_pos=%x, param_pos=%x\n", ret, opc_pos, param_pos);
00112 #endif
00113        ret += ((insn >> opc_pos) & 0x1) << param_pos;
00114 
00115        if (!param_pos
00116            && letter_signed (param_ch)
00117            && ret >> (letter_range (param_ch) - 1))
00118          {
00119 #if DEBUG
00120            printf ("\n  ret=%x opc_pos=%x, param_pos=%x\n",
00121                   ret, opc_pos, param_pos);
00122 #endif
00123            ret |= 0xffffffff << letter_range(param_ch);
00124 #if DEBUG
00125            printf ("\n  after conversion to signed: ret=%x\n", ret);
00126 #endif
00127          }
00128        enc++;
00129       }
00130     else if (ISALPHA (*enc))
00131       {
00132        opc_pos--;
00133        enc++;
00134       }
00135     else if (*enc == '-')
00136       {
00137        opc_pos--;
00138        enc++;
00139       }
00140     else
00141       enc++;
00142 
00143 #if DEBUG
00144   printf ("ret=%x\n", ret);
00145 #endif
00146   return ret;
00147 }
00148 
00149 static int
00150 or32_opcode_match (unsigned long insn, char *encoding)
00151 {
00152   unsigned long ones, zeros;
00153 
00154 #if DEBUG
00155   printf ("or32_opcode_match: %.8lx\n", insn);
00156 #endif    
00157   ones  = or32_extract ('1', encoding, insn);
00158   zeros = or32_extract ('0', encoding, insn);
00159   
00160 #if DEBUG
00161   printf ("ones: %x \n", ones);
00162   printf ("zeros: %x \n", zeros);
00163 #endif
00164   if ((insn & ones) != ones)
00165     {
00166 #if DEBUG
00167       printf ("ret1\n");
00168 #endif
00169       return 0;
00170     }
00171     
00172   if ((~insn & zeros) != zeros)
00173     {
00174 #if DEBUG
00175       printf ("ret2\n");
00176 #endif
00177       return 0;
00178     }
00179   
00180 #if DEBUG
00181   printf ("ret3\n");
00182 #endif
00183   return 1;
00184 }
00185 
00186 /* Print register to INFO->STREAM. Used only by print_insn.  */
00187 
00188 static void
00189 or32_print_register (char param_ch,
00190                    char *encoding,
00191                    unsigned long insn,
00192                    struct disassemble_info *info)
00193 {
00194   int regnum = or32_extract (param_ch, encoding, insn);
00195   
00196 #if DEBUG
00197   printf ("or32_print_register: %c, %s, %x\n", param_ch, encoding, insn);
00198 #endif  
00199   if (param_ch == 'A')
00200     (*info->fprintf_func) (info->stream, "r%d", regnum);
00201   else if (param_ch == 'B')
00202     (*info->fprintf_func) (info->stream, "r%d", regnum);
00203   else if (param_ch == 'D')
00204     (*info->fprintf_func) (info->stream, "r%d", regnum);
00205   else if (regnum < 16)
00206     (*info->fprintf_func) (info->stream, "r%d", regnum);
00207   else if (regnum < 32)
00208     (*info->fprintf_func) (info->stream, "r%d", regnum-16);
00209   else
00210     (*info->fprintf_func) (info->stream, "X%d", regnum);
00211 }
00212 
00213 /* Print immediate to INFO->STREAM. Used only by print_insn.  */
00214 
00215 static void
00216 or32_print_immediate (char param_ch,
00217                     char *encoding,
00218                     unsigned long insn,
00219                     struct disassemble_info *info)
00220 {
00221   int imm = or32_extract(param_ch, encoding, insn);
00222   
00223   if (letter_signed(param_ch))
00224     (*info->fprintf_func) (info->stream, "0x%x", imm);
00225 /*    (*info->fprintf_func) (info->stream, "%d", imm); */
00226   else
00227     (*info->fprintf_func) (info->stream, "0x%x", imm);
00228 }
00229 
00230 /* Print one instruction from MEMADDR on INFO->STREAM.
00231    Return the size of the instruction (always 4 on or32).  */
00232 
00233 static int
00234 print_insn (bfd_vma memaddr, struct disassemble_info *info)
00235 {
00236   /* The raw instruction.  */
00237   unsigned char insn_ch[4];
00238   /* Address. Will be sign extened 27-bit.  */
00239   unsigned long addr;
00240   /* The four bytes of the instruction.  */
00241   unsigned long insn;
00242   find_byte_func_type find_byte_func = (find_byte_func_type) info->private_data;
00243   struct or32_opcode const * opcode;
00244 
00245   {
00246     int status =
00247       (*info->read_memory_func) (memaddr, (bfd_byte *) &insn_ch[0], 4, info);
00248 
00249     if (status != 0)
00250       {
00251         (*info->memory_error_func) (status, memaddr, info);
00252         return -1;
00253       }
00254   }
00255 
00256   (*find_byte_func) (&insn_ch[0], &insn);
00257 
00258   for (opcode = &or32_opcodes[0];
00259        opcode < &or32_opcodes[or32_num_opcodes];
00260        ++opcode)
00261     {
00262       if (or32_opcode_match (insn, opcode->encoding))
00263         {
00264           char *s;
00265 
00266           (*info->fprintf_func) (info->stream, "%s ", opcode->name);
00267 
00268           for (s = opcode->args; *s != '\0'; ++s)
00269             {
00270               switch (*s)
00271                 {
00272                 case '\0':
00273                   return 4;
00274       
00275                 case 'r':
00276                   or32_print_register (*++s, opcode->encoding, insn, info);
00277                   break;
00278 
00279                 case 'X':
00280                   addr = or32_extract ('X', opcode->encoding, insn) << 2;
00281 
00282                   /* Calulate the correct address.  XXX is this really correct ??  */
00283                   addr = memaddr + EXTEND29 (addr);
00284 
00285                   (*info->print_address_func)
00286                     (addr, info);
00287                   break;
00288 
00289                 default:
00290                   if (strchr (opcode->encoding, *s))
00291                     or32_print_immediate (*s, opcode->encoding, insn, info);
00292                   else
00293                     (*info->fprintf_func) (info->stream, "%c", *s);
00294                 }
00295             }
00296 
00297           return 4;
00298         }
00299     }
00300 
00301   /* This used to be %8x for binutils.  */
00302   (*info->fprintf_func)
00303     (info->stream, ".word 0x%08lx", insn);
00304   return 4;
00305 }
00306 
00307 /* Disassemble a big-endian or32 instruction.  */
00308 
00309 int
00310 print_insn_big_or32 (bfd_vma memaddr, struct disassemble_info *info)
00311 {
00312   info->private_data = find_bytes_big;
00313 
00314   return print_insn (memaddr, info);
00315 }
00316 
00317 /* Disassemble a little-endian or32 instruction.  */
00318 
00319 int
00320 print_insn_little_or32 (bfd_vma memaddr, struct disassemble_info *info)
00321 {
00322   info->private_data = find_bytes_little;
00323   return print_insn (memaddr, info);
00324 }