xref: /openbsd-src/gnu/usr.bin/binutils/opcodes/a29k-dis.c (revision c074d1c999f3e07019cd5e9a2f190b057ef3b935)
12159047fSniklas /* Instruction printing code for the AMD 29000
2*c074d1c9Sdrahn    Copyright 1990, 1993, 1994, 1995, 1998, 2000, 2001, 2002
3b55d4692Sfgsch    Free Software Foundation, Inc.
42159047fSniklas    Contributed by Cygnus Support.  Written by Jim Kingdon.
52159047fSniklas 
62159047fSniklas This file is part of GDB.
72159047fSniklas 
82159047fSniklas This program is free software; you can redistribute it and/or modify
92159047fSniklas it under the terms of the GNU General Public License as published by
102159047fSniklas the Free Software Foundation; either version 2 of the License, or
112159047fSniklas (at your option) any later version.
122159047fSniklas 
132159047fSniklas This program is distributed in the hope that it will be useful,
142159047fSniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
152159047fSniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
162159047fSniklas GNU General Public License for more details.
172159047fSniklas 
182159047fSniklas You should have received a copy of the GNU General Public License
192159047fSniklas along with this program; if not, write to the Free Software
202159047fSniklas Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
212159047fSniklas 
22f7cc78ecSespie #include "sysdep.h"
232159047fSniklas #include "dis-asm.h"
242159047fSniklas #include "opcode/a29k.h"
252159047fSniklas 
26*c074d1c9Sdrahn static void print_general PARAMS ((int, struct disassemble_info *));
27*c074d1c9Sdrahn static void print_special PARAMS ((unsigned int, struct disassemble_info *));
28*c074d1c9Sdrahn static int is_delayed_branch PARAMS ((int));
29*c074d1c9Sdrahn static void find_bytes_little
30*c074d1c9Sdrahn   PARAMS ((char *, unsigned char *, unsigned char *, unsigned char *,
31*c074d1c9Sdrahn 	   unsigned char *));
32*c074d1c9Sdrahn static void find_bytes_big
33*c074d1c9Sdrahn   PARAMS ((char *, unsigned char *, unsigned char *, unsigned char *,
34*c074d1c9Sdrahn 	   unsigned char *));
35*c074d1c9Sdrahn static int print_insn PARAMS ((bfd_vma, struct disassemble_info *));
36*c074d1c9Sdrahn 
37*c074d1c9Sdrahn 
382159047fSniklas /* Print a symbolic representation of a general-purpose
392159047fSniklas    register number NUM on STREAM.
402159047fSniklas    NUM is a number as found in the instruction, not as found in
412159047fSniklas    debugging symbols; it must be in the range 0-255.  */
422159047fSniklas static void
print_general(num,info)432159047fSniklas print_general (num, info)
442159047fSniklas      int num;
452159047fSniklas      struct disassemble_info *info;
462159047fSniklas {
472159047fSniklas   if (num < 128)
482159047fSniklas     (*info->fprintf_func) (info->stream, "gr%d", num);
492159047fSniklas   else
502159047fSniklas     (*info->fprintf_func) (info->stream, "lr%d", num - 128);
512159047fSniklas }
522159047fSniklas 
532159047fSniklas /* Like print_general but a special-purpose register.
542159047fSniklas 
552159047fSniklas    The mnemonics used by the AMD assembler are not quite the same
562159047fSniklas    as the ones in the User's Manual.  We use the ones that the
572159047fSniklas    assembler uses.  */
582159047fSniklas static void
print_special(num,info)592159047fSniklas print_special (num, info)
602159047fSniklas      unsigned int num;
612159047fSniklas      struct disassemble_info *info;
622159047fSniklas {
632159047fSniklas   /* Register names of registers 0-SPEC0_NUM-1.  */
642159047fSniklas   static char *spec0_names[] = {
652159047fSniklas     "vab", "ops", "cps", "cfg", "cha", "chd", "chc", "rbp", "tmc", "tmr",
662159047fSniklas     "pc0", "pc1", "pc2", "mmu", "lru", "rsn", "rma0", "rmc0", "rma1", "rmc1",
672159047fSniklas     "spc0", "spc1", "spc2", "iba0", "ibc0", "iba1", "ibc1", "dba", "dbc",
682159047fSniklas     "cir", "cdr"
692159047fSniklas     };
702159047fSniklas #define SPEC0_NUM ((sizeof spec0_names) / (sizeof spec0_names[0]))
712159047fSniklas 
722159047fSniklas   /* Register names of registers 128-128+SPEC128_NUM-1.  */
732159047fSniklas   static char *spec128_names[] = {
742159047fSniklas     "ipc", "ipa", "ipb", "q", "alu", "bp", "fc", "cr"
752159047fSniklas     };
762159047fSniklas #define SPEC128_NUM ((sizeof spec128_names) / (sizeof spec128_names[0]))
772159047fSniklas 
782159047fSniklas   /* Register names of registers 160-160+SPEC160_NUM-1.  */
792159047fSniklas   static char *spec160_names[] = {
802159047fSniklas     "fpe", "inte", "fps", "sr163", "exop"
812159047fSniklas     };
822159047fSniklas #define SPEC160_NUM ((sizeof spec160_names) / (sizeof spec160_names[0]))
832159047fSniklas 
842159047fSniklas   if (num < SPEC0_NUM)
852159047fSniklas     (*info->fprintf_func) (info->stream, spec0_names[num]);
862159047fSniklas   else if (num >= 128 && num < 128 + SPEC128_NUM)
872159047fSniklas     (*info->fprintf_func) (info->stream, spec128_names[num-128]);
882159047fSniklas   else if (num >= 160 && num < 160 + SPEC160_NUM)
892159047fSniklas     (*info->fprintf_func) (info->stream, spec160_names[num-160]);
902159047fSniklas   else
912159047fSniklas     (*info->fprintf_func) (info->stream, "sr%d", num);
922159047fSniklas }
932159047fSniklas 
942159047fSniklas /* Is an instruction with OPCODE a delayed branch?  */
952159047fSniklas static int
is_delayed_branch(opcode)962159047fSniklas is_delayed_branch (opcode)
972159047fSniklas      int opcode;
982159047fSniklas {
992159047fSniklas   return (opcode == 0xa8 || opcode == 0xa9 || opcode == 0xa0 || opcode == 0xa1
1002159047fSniklas 	  || opcode == 0xa4 || opcode == 0xa5
1012159047fSniklas 	  || opcode == 0xb4 || opcode == 0xb5
1022159047fSniklas 	  || opcode == 0xc4 || opcode == 0xc0
1032159047fSniklas 	  || opcode == 0xac || opcode == 0xad
1042159047fSniklas 	  || opcode == 0xcc);
1052159047fSniklas }
1062159047fSniklas 
1072159047fSniklas /* Now find the four bytes of INSN and put them in *INSN{0,8,16,24}.  */
1082159047fSniklas static void
find_bytes_big(insn,insn0,insn8,insn16,insn24)1092159047fSniklas find_bytes_big (insn, insn0, insn8, insn16, insn24)
1102159047fSniklas      char *insn;
1112159047fSniklas      unsigned char *insn0;
1122159047fSniklas      unsigned char *insn8;
1132159047fSniklas      unsigned char *insn16;
1142159047fSniklas      unsigned char *insn24;
1152159047fSniklas {
1162159047fSniklas   *insn24 = insn[0];
1172159047fSniklas   *insn16 = insn[1];
1182159047fSniklas   *insn8  = insn[2];
1192159047fSniklas   *insn0  = insn[3];
1202159047fSniklas }
1212159047fSniklas 
1222159047fSniklas static void
find_bytes_little(insn,insn0,insn8,insn16,insn24)1232159047fSniklas find_bytes_little (insn, insn0, insn8, insn16, insn24)
1242159047fSniklas      char *insn;
1252159047fSniklas      unsigned char *insn0;
1262159047fSniklas      unsigned char *insn8;
1272159047fSniklas      unsigned char *insn16;
1282159047fSniklas      unsigned char *insn24;
1292159047fSniklas {
1302159047fSniklas   *insn24 = insn[3];
1312159047fSniklas   *insn16 = insn[2];
1322159047fSniklas   *insn8 = insn[1];
1332159047fSniklas   *insn0 = insn[0];
1342159047fSniklas }
1352159047fSniklas 
136f7cc78ecSespie typedef void (*find_byte_func_type)
1372159047fSniklas      PARAMS ((char *, unsigned char *, unsigned char *,
1382159047fSniklas 	      unsigned char *, unsigned char *));
1392159047fSniklas 
1402159047fSniklas /* Print one instruction from MEMADDR on INFO->STREAM.
1412159047fSniklas    Return the size of the instruction (always 4 on a29k).  */
1422159047fSniklas 
1432159047fSniklas static int
print_insn(memaddr,info)1442159047fSniklas print_insn (memaddr, info)
1452159047fSniklas      bfd_vma memaddr;
1462159047fSniklas      struct disassemble_info *info;
1472159047fSniklas {
1482159047fSniklas   /* The raw instruction.  */
1492159047fSniklas   char insn[4];
1502159047fSniklas 
1512159047fSniklas   /* The four bytes of the instruction.  */
1522159047fSniklas   unsigned char insn24, insn16, insn8, insn0;
1532159047fSniklas 
1542159047fSniklas   find_byte_func_type find_byte_func = (find_byte_func_type)info->private_data;
1552159047fSniklas 
156*c074d1c9Sdrahn   struct a29k_opcode const * opcode;
1572159047fSniklas 
1582159047fSniklas   {
1592159047fSniklas     int status =
1602159047fSniklas       (*info->read_memory_func) (memaddr, (bfd_byte *) &insn[0], 4, info);
1612159047fSniklas     if (status != 0)
1622159047fSniklas       {
1632159047fSniklas 	(*info->memory_error_func) (status, memaddr, info);
1642159047fSniklas 	return -1;
1652159047fSniklas       }
1662159047fSniklas   }
1672159047fSniklas 
1682159047fSniklas   (*find_byte_func) (insn, &insn0, &insn8, &insn16, &insn24);
1692159047fSniklas 
1702159047fSniklas   printf ("%02x%02x%02x%02x ", insn24, insn16, insn8, insn0);
1712159047fSniklas 
1722159047fSniklas   /* Handle the nop (aseq 0x40,gr1,gr1) specially */
1732159047fSniklas   if ((insn24==0x70) && (insn16==0x40) && (insn8==0x01) && (insn0==0x01)) {
1742159047fSniklas     (*info->fprintf_func) (info->stream,"nop");
1752159047fSniklas     return 4;
1762159047fSniklas   }
1772159047fSniklas 
1782159047fSniklas   /* The opcode is always in insn24.  */
1792159047fSniklas   for (opcode = &a29k_opcodes[0];
1802159047fSniklas        opcode < &a29k_opcodes[num_opcodes];
1812159047fSniklas        ++opcode)
1822159047fSniklas     {
1832159047fSniklas       if (((unsigned long) insn24 << 24) == opcode->opcode)
1842159047fSniklas 	{
1852159047fSniklas 	  char *s;
1862159047fSniklas 
1872159047fSniklas 	  (*info->fprintf_func) (info->stream, "%s ", opcode->name);
1882159047fSniklas 	  for (s = opcode->args; *s != '\0'; ++s)
1892159047fSniklas 	    {
1902159047fSniklas 	      switch (*s)
1912159047fSniklas 		{
1922159047fSniklas 		case 'a':
1932159047fSniklas 		  print_general (insn8, info);
1942159047fSniklas 		  break;
1952159047fSniklas 
1962159047fSniklas 		case 'b':
1972159047fSniklas 		  print_general (insn0, info);
1982159047fSniklas 		  break;
1992159047fSniklas 
2002159047fSniklas 		case 'c':
2012159047fSniklas 		  print_general (insn16, info);
2022159047fSniklas 		  break;
2032159047fSniklas 
2042159047fSniklas 		case 'i':
2052159047fSniklas 		  (*info->fprintf_func) (info->stream, "%d", insn0);
2062159047fSniklas 		  break;
2072159047fSniklas 
2082159047fSniklas 		case 'x':
2092159047fSniklas 		  (*info->fprintf_func) (info->stream, "0x%x", (insn16 << 8) + insn0);
2102159047fSniklas 		  break;
2112159047fSniklas 
2122159047fSniklas 		case 'h':
2132159047fSniklas 		  /* This used to be %x for binutils.  */
2142159047fSniklas 		  (*info->fprintf_func) (info->stream, "0x%x",
2152159047fSniklas 				    (insn16 << 24) + (insn0 << 16));
2162159047fSniklas 		  break;
2172159047fSniklas 
2182159047fSniklas 		case 'X':
2192159047fSniklas 		  (*info->fprintf_func) (info->stream, "%d",
2202159047fSniklas 				    ((insn16 << 8) + insn0) | 0xffff0000);
2212159047fSniklas 		  break;
2222159047fSniklas 
2232159047fSniklas 		case 'P':
2242159047fSniklas 		  /* This output looks just like absolute addressing, but
2252159047fSniklas 		     maybe that's OK (it's what the GDB m68k and EBMON
2262159047fSniklas 		     a29k disassemblers do).  */
2272159047fSniklas 		  /* All the shifting is to sign-extend it.  p*/
2282159047fSniklas 		  (*info->print_address_func)
2292159047fSniklas 		    (memaddr +
2302159047fSniklas 		     (((int)((insn16 << 10) + (insn0 << 2)) << 14) >> 14),
2312159047fSniklas 		     info);
2322159047fSniklas 		  break;
2332159047fSniklas 
2342159047fSniklas 		case 'A':
2352159047fSniklas 		  (*info->print_address_func)
2362159047fSniklas 		    ((insn16 << 10) + (insn0 << 2), info);
2372159047fSniklas 		  break;
2382159047fSniklas 
2392159047fSniklas 		case 'e':
2402159047fSniklas 		  (*info->fprintf_func) (info->stream, "%d", insn16 >> 7);
2412159047fSniklas 		  break;
2422159047fSniklas 
2432159047fSniklas 		case 'n':
2442159047fSniklas 		  (*info->fprintf_func) (info->stream, "0x%x", insn16 & 0x7f);
2452159047fSniklas 		  break;
2462159047fSniklas 
2472159047fSniklas 		case 'v':
2482159047fSniklas 		  (*info->fprintf_func) (info->stream, "0x%x", insn16);
2492159047fSniklas 		  break;
2502159047fSniklas 
2512159047fSniklas 		case 's':
2522159047fSniklas 		  print_special (insn8, info);
2532159047fSniklas 		  break;
2542159047fSniklas 
2552159047fSniklas 		case 'u':
2562159047fSniklas 		  (*info->fprintf_func) (info->stream, "%d", insn0 >> 7);
2572159047fSniklas 		  break;
2582159047fSniklas 
2592159047fSniklas 		case 'r':
2602159047fSniklas 		  (*info->fprintf_func) (info->stream, "%d", (insn0 >> 4) & 7);
2612159047fSniklas 		  break;
2622159047fSniklas 
2632159047fSniklas 		case 'I':
2642159047fSniklas 		  if ((insn16 & 3) != 0)
2652159047fSniklas 		    (*info->fprintf_func) (info->stream, "%d", insn16 & 3);
2662159047fSniklas 		  break;
2672159047fSniklas 
2682159047fSniklas 		case 'd':
2692159047fSniklas 		  (*info->fprintf_func) (info->stream, "%d", (insn0 >> 2) & 3);
2702159047fSniklas 		  break;
2712159047fSniklas 
2722159047fSniklas 		case 'f':
2732159047fSniklas 		  (*info->fprintf_func) (info->stream, "%d", insn0 & 3);
2742159047fSniklas 		  break;
2752159047fSniklas 
2762159047fSniklas 		case 'F':
2772159047fSniklas 		  (*info->fprintf_func) (info->stream, "%d", (insn16 >> 2) & 15);
2782159047fSniklas 		  break;
2792159047fSniklas 
2802159047fSniklas 		case 'C':
2812159047fSniklas 		  (*info->fprintf_func) (info->stream, "%d", insn16 & 3);
2822159047fSniklas 		  break;
2832159047fSniklas 
2842159047fSniklas 		default:
2852159047fSniklas 		  (*info->fprintf_func) (info->stream, "%c", *s);
2862159047fSniklas 		}
2872159047fSniklas 	    }
2882159047fSniklas 
2892159047fSniklas 	  /* Now we look for a const,consth pair of instructions,
2902159047fSniklas 	     in which case we try to print the symbolic address.  */
2912159047fSniklas 	  if (insn24 == 2)  /* consth */
2922159047fSniklas 	    {
2932159047fSniklas 	      int errcode;
2942159047fSniklas 	      char prev_insn[4];
2952159047fSniklas 	      unsigned char prev_insn0, prev_insn8, prev_insn16, prev_insn24;
2962159047fSniklas 
2972159047fSniklas 	      errcode = (*info->read_memory_func) (memaddr - 4,
2982159047fSniklas 						   (bfd_byte *) &prev_insn[0],
2992159047fSniklas 						   4,
3002159047fSniklas 						   info);
3012159047fSniklas 	      if (errcode == 0)
3022159047fSniklas 		{
3032159047fSniklas 		  /* If it is a delayed branch, we need to look at the
3042159047fSniklas 		     instruction before the delayed brach to handle
3052159047fSniklas 		     things like
3062159047fSniklas 
3072159047fSniklas 		     const _foo
3082159047fSniklas 		     call _printf
3092159047fSniklas 		     consth _foo
3102159047fSniklas 		     */
3112159047fSniklas 		  (*find_byte_func) (prev_insn, &prev_insn0, &prev_insn8,
3122159047fSniklas 				     &prev_insn16, &prev_insn24);
3132159047fSniklas 		  if (is_delayed_branch (prev_insn24))
3142159047fSniklas 		    {
3152159047fSniklas 		      errcode = (*info->read_memory_func)
3162159047fSniklas 			(memaddr - 8, (bfd_byte *) &prev_insn[0], 4, info);
3172159047fSniklas 		      (*find_byte_func) (prev_insn, &prev_insn0, &prev_insn8,
3182159047fSniklas 					 &prev_insn16, &prev_insn24);
3192159047fSniklas 		    }
3202159047fSniklas 		}
3212159047fSniklas 
3222159047fSniklas 	      /* If there was a problem reading memory, then assume
3232159047fSniklas 		 the previous instruction was not const.  */
3242159047fSniklas 	      if (errcode == 0)
3252159047fSniklas 		{
3262159047fSniklas 		  /* Is it const to the same register?  */
3272159047fSniklas 		  if (prev_insn24 == 3
3282159047fSniklas 		      && prev_insn8 == insn8)
3292159047fSniklas 		    {
3302159047fSniklas 		      (*info->fprintf_func) (info->stream, "\t; ");
3312159047fSniklas 		      (*info->print_address_func)
3322159047fSniklas 			(((insn16 << 24) + (insn0 << 16)
3332159047fSniklas 			  + (prev_insn16 << 8) + (prev_insn0)),
3342159047fSniklas 			 info);
3352159047fSniklas 		    }
3362159047fSniklas 		}
3372159047fSniklas 	    }
3382159047fSniklas 
3392159047fSniklas 	  return 4;
3402159047fSniklas 	}
3412159047fSniklas     }
3422159047fSniklas   /* This used to be %8x for binutils.  */
3432159047fSniklas   (*info->fprintf_func)
3442159047fSniklas     (info->stream, ".word 0x%08x",
3452159047fSniklas      (insn24 << 24) + (insn16 << 16) + (insn8 << 8) + insn0);
3462159047fSniklas   return 4;
3472159047fSniklas }
3482159047fSniklas 
3492159047fSniklas /* Disassemble an big-endian a29k instruction.  */
3502159047fSniklas int
print_insn_big_a29k(memaddr,info)3512159047fSniklas print_insn_big_a29k (memaddr, info)
3522159047fSniklas      bfd_vma memaddr;
3532159047fSniklas      struct disassemble_info *info;
3542159047fSniklas {
3552159047fSniklas   info->private_data = (PTR) find_bytes_big;
3562159047fSniklas   return print_insn (memaddr, info);
3572159047fSniklas }
3582159047fSniklas 
3592159047fSniklas /* Disassemble a little-endian a29k instruction.  */
3602159047fSniklas int
print_insn_little_a29k(memaddr,info)3612159047fSniklas print_insn_little_a29k (memaddr, info)
3622159047fSniklas      bfd_vma memaddr;
3632159047fSniklas      struct disassemble_info *info;
3642159047fSniklas {
3652159047fSniklas   info->private_data = (PTR) find_bytes_little;
3662159047fSniklas   return print_insn (memaddr, info);
3672159047fSniklas }
368