home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / opcodes / sh-dis.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  5KB  |  235 lines

  1. /* Disassemble h8500 instructions.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #define STATIC_TABLE
  20. #define DEFINE_TABLE
  21.  
  22. #include "sh-opc.h"
  23. #include "dis-asm.h"
  24.  
  25. int 
  26. print_insn_sh(memaddr, info)
  27.      bfd_vma memaddr;
  28.      struct disassemble_info *info;
  29. {
  30.   fprintf_ftype fprintf = info->fprintf_func;
  31.   void *stream = info->stream;
  32.   unsigned  char insn[2];
  33.   unsigned  char nibs[4];
  34.   int status;
  35.   int relmask = ~0;
  36.   sh_opcode_info *op;
  37.  
  38.   int dslot = 0;
  39.   
  40.   status = info->read_memory_func(memaddr, insn, 2, info);
  41.  
  42.   if (status != 0) 
  43.     {
  44.       info->memory_error_func(status, memaddr, info);
  45.       return -1;
  46.     }
  47.  
  48.   nibs[0] = (insn[0] >> 4) & 0xf;
  49.   nibs[1] = insn[0] & 0xf;
  50.  
  51.   nibs[2] = (insn[1] >> 4) & 0xf;
  52.   nibs[3] = insn[1] & 0xf;
  53.  
  54.   for (op = sh_table; op->name; op++) 
  55.     {
  56.       int n;
  57.       int imm;
  58.       int rn;
  59.       int rm;
  60.       for (n = 0; n < 4; n++) {
  61.     int i = op->nibbles[n];
  62.     if (i < 16) 
  63.       {
  64.         if (nibs[n] == i) continue;
  65.         goto fail;
  66.       }
  67.     switch (i)
  68.       {
  69.       case BRANCH_8:
  70.         imm = (nibs[2] << 4) | (nibs[3]);      
  71.         if (imm & 0x80)
  72.           imm |= ~0xff;
  73.         imm = ((char)imm) * 2 + 4 ;
  74.         goto ok;
  75.  
  76.       case BRANCH_12:
  77.         imm = ((nibs[1]) << 8) | (nibs[2] << 4) | (nibs[3]);
  78.         if (imm & 0x800)
  79.           imm |= ~0xfff;
  80.         imm = imm * 2 + 4;
  81.         goto ok;
  82.       case IMM_4:
  83.         imm = nibs[3];
  84.         goto ok;
  85.       case IMM_4BY2:
  86.         imm = nibs[3] <<1;
  87.         goto ok;
  88.       case IMM_4BY4:
  89.         imm = nibs[3] <<2;
  90.         goto ok;
  91.  
  92.         
  93.       case IMM_8:
  94.         imm = (nibs[2] << 4) | nibs[3];
  95.         goto ok;
  96.       case PCRELIMM_8BY2:
  97.         imm = ((nibs[2] << 4) | nibs[3]) <<1;
  98.         relmask  = ~1;
  99.         
  100.         goto ok;
  101.  
  102.       case PCRELIMM_8BY4:
  103.         imm = ((nibs[2] << 4) | nibs[3]) <<2;
  104.         relmask  = ~3;        
  105.         goto ok;
  106.         
  107.       case IMM_8BY2:
  108.         imm = ((nibs[2] << 4) | nibs[3]) <<1;
  109.         goto ok;
  110.       case IMM_8BY4:
  111.         imm = ((nibs[2] << 4) | nibs[3]) <<2;
  112.         goto ok;
  113.       case DISP_8:
  114.         imm = (nibs[2] << 4) | (nibs[3]);      
  115.         goto ok;
  116.       case DISP_4:
  117.         imm = nibs[3];
  118.         goto ok;
  119.       case REG_N:
  120.         rn = nibs[n];
  121.         break;
  122.       case REG_M:
  123.         rm = nibs[n];
  124.         break;
  125.       default:
  126.         abort();
  127.       }
  128.  
  129.       }
  130.     ok:
  131.       fprintf(stream,"%s\t", op->name);
  132.       for (n = 0; n < 2 && op->arg[n] != A_END; n++) 
  133.     {
  134.       if (n && op->arg[1] != A_END)
  135.         fprintf(stream,",");
  136.       switch (op->arg[n]) 
  137.         {
  138.         case A_IMM:
  139.           fprintf(stream,"#%d", (char)(imm));
  140.           break;
  141.         case A_R0:
  142.           fprintf(stream,"r0");
  143.           break;
  144.         case A_REG_N:
  145.           fprintf(stream,"r%d", rn);
  146.           break;
  147.         case A_INC_N:
  148.           fprintf(stream,"@r%d+", rn);    
  149.           break;
  150.         case A_DEC_N:
  151.           fprintf(stream,"@-r%d", rn);    
  152.           break;
  153.         case A_IND_N:
  154.           fprintf(stream,"@r%d", rn);    
  155.           break;
  156.         case A_DISP_REG_N:
  157.           fprintf(stream,"@(%d,r%d)",imm, rn);    
  158.           break;
  159.         case A_REG_M:
  160.           fprintf(stream,"r%d", rm);
  161.           break;
  162.         case A_INC_M:
  163.           fprintf(stream,"@r%d+", rm);    
  164.           break;
  165.         case A_DEC_M:
  166.           fprintf(stream,"@-r%d", rm);    
  167.           break;
  168.         case A_IND_M:
  169.           fprintf(stream,"@r%d", rm);    
  170.           break;
  171.         case A_DISP_REG_M:
  172.           fprintf(stream,"@(%d,r%d)",imm, rm);    
  173.           break;
  174.         case A_DISP_PC:
  175.           fprintf(stream,"0x%0x", imm+ 4+(memaddr&relmask));
  176.           break;
  177.         case A_IND_R0_REG_N:
  178.           fprintf(stream,"@(r0,r%d)", rn);
  179.           break; 
  180.         case A_IND_R0_REG_M:
  181.           fprintf(stream,"@(r0,r%d)", rm);
  182.           break; 
  183.         case A_DISP_GBR:
  184.           fprintf(stream,"@(%d,gbr)",imm);
  185.           break;
  186.         case A_R0_GBR:
  187.           fprintf(stream,"@(r0,gbr)");
  188.           break;
  189.         case A_BDISP12:
  190.         case A_BDISP8:
  191.           fprintf(stream,"0x%x", imm + memaddr);    
  192.           break;
  193.         case A_SR:
  194.           fprintf(stream,"sr");
  195.           break;
  196.         case A_GBR:
  197.           fprintf(stream,"gbr");
  198.           break;
  199.         case A_VBR:
  200.           fprintf(stream,"vbr");
  201.           break;
  202.         case A_MACH:
  203.           fprintf(stream,"mach");
  204.           break;
  205.         case A_MACL:
  206.           fprintf(stream,"macl");
  207.           break;
  208.         case A_PR:
  209.           fprintf(stream,"pr");
  210.           break;
  211.         default:
  212.           abort();
  213.         }
  214.     
  215.     }
  216.       if (op->name[0] == 'j'
  217.       || (op->name[0] == 'b' && (op->name[1] == 'r' 
  218.                      || op->name[1] == 's'))
  219.       || (op->name[0] == 'r' && op->name[1] == 't')
  220.       || (op->name[0] == 'b' && op->name[2] == '.'))
  221.     {
  222.       fprintf(stream,"\t(slot ");  print_insn_sh(memaddr +2, info);
  223.       fprintf(stream,")");
  224.       return 4;
  225.     }
  226.       
  227.       return 2;
  228.     fail:
  229.       ;
  230.  
  231.     }
  232.   fprintf(stream,".word 0x%02x%02x", insn[0], insn[1]);
  233.   return 2;
  234. }
  235.