xref: /openbsd-src/gnu/usr.bin/binutils/opcodes/pj-dis.c (revision d2201f2f89f0be1a0be6f7568000ed297414a06d)
1f7cc78ecSespie /* pj-dis.c -- Disassemble picoJava instructions.
2*d2201f2fSdrahn    Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
3f7cc78ecSespie    Contributed by Steve Chamberlain, of Transmeta (sac@pobox.com).
4f7cc78ecSespie 
5f7cc78ecSespie This program is free software; you can redistribute it and/or modify
6f7cc78ecSespie it under the terms of the GNU General Public License as published by
7f7cc78ecSespie the Free Software Foundation; either version 2 of the License, or
8f7cc78ecSespie (at your option) any later version.
9f7cc78ecSespie 
10f7cc78ecSespie This program is distributed in the hope that it will be useful,
11f7cc78ecSespie but WITHOUT ANY WARRANTY; without even the implied warranty of
12f7cc78ecSespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13f7cc78ecSespie GNU General Public License for more details.
14f7cc78ecSespie 
15f7cc78ecSespie You should have received a copy of the GNU General Public License
16f7cc78ecSespie along with this program; if not, write to the Free Software
17f7cc78ecSespie Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18f7cc78ecSespie 
19f7cc78ecSespie #include <stdio.h>
205f210c2aSfgsch #include "sysdep.h"
21f7cc78ecSespie #include "opcode/pj.h"
22f7cc78ecSespie #include "dis-asm.h"
23f7cc78ecSespie 
24f7cc78ecSespie extern const pj_opc_info_t pj_opc_info[512];
25f7cc78ecSespie 
26*d2201f2fSdrahn static int get_int PARAMS ((bfd_vma, int *, struct disassemble_info *));
27*d2201f2fSdrahn 
28*d2201f2fSdrahn 
29*d2201f2fSdrahn static int
get_int(memaddr,iptr,info)30*d2201f2fSdrahn get_int (memaddr, iptr, info)
31f7cc78ecSespie      bfd_vma memaddr;
32f7cc78ecSespie      int *iptr;
33f7cc78ecSespie      struct disassemble_info *info;
34f7cc78ecSespie {
35f7cc78ecSespie   unsigned char ival[4];
36f7cc78ecSespie 
37f7cc78ecSespie   int status = info->read_memory_func (memaddr, ival, 4, info);
38f7cc78ecSespie 
39f7cc78ecSespie   *iptr = (ival[0] << 24)
40f7cc78ecSespie     | (ival[1] << 16)
41f7cc78ecSespie     | (ival[2] << 8)
42f7cc78ecSespie     | (ival[3] << 0);
43f7cc78ecSespie 
44f7cc78ecSespie   return status;
45f7cc78ecSespie }
46f7cc78ecSespie 
47f7cc78ecSespie int
print_insn_pj(addr,info)48f7cc78ecSespie print_insn_pj (addr, info)
49f7cc78ecSespie      bfd_vma addr;
50f7cc78ecSespie      struct disassemble_info *info;
51f7cc78ecSespie {
52f7cc78ecSespie   fprintf_ftype fprintf_fn = info->fprintf_func;
53f7cc78ecSespie   void *stream = info->stream;
54f7cc78ecSespie   unsigned char opcode;
55f7cc78ecSespie   int status;
56f7cc78ecSespie 
57f7cc78ecSespie   if ((status = info->read_memory_func (addr, &opcode, 1, info)))
58f7cc78ecSespie     goto fail;
59f7cc78ecSespie 
60f7cc78ecSespie   if (opcode == 0xff)
61f7cc78ecSespie     {
62f7cc78ecSespie       unsigned char byte_2;
63f7cc78ecSespie       if ((status = info->read_memory_func (addr + 1, &byte_2, 1, info)))
64f7cc78ecSespie 	goto fail;
65*d2201f2fSdrahn       fprintf_fn (stream, "%s\t", pj_opc_info[opcode + byte_2].u.name);
66f7cc78ecSespie       return 2;
67f7cc78ecSespie     }
68f7cc78ecSespie   else
69f7cc78ecSespie     {
70f7cc78ecSespie       char *sep = "\t";
71f7cc78ecSespie       int insn_start = addr;
72f7cc78ecSespie       const pj_opc_info_t *op = &pj_opc_info[opcode];
73f7cc78ecSespie       int a;
74f7cc78ecSespie       addr++;
75*d2201f2fSdrahn       fprintf_fn (stream, "%s", op->u.name);
76f7cc78ecSespie 
77f7cc78ecSespie       /* The tableswitch instruction is followed by the default
78f7cc78ecSespie 	 address, low value, high value and the destinations.  */
79f7cc78ecSespie 
80*d2201f2fSdrahn       if (strcmp (op->u.name, "tableswitch") == 0)
81f7cc78ecSespie 	{
82f7cc78ecSespie 	  int lowval;
83f7cc78ecSespie 	  int highval;
84f7cc78ecSespie 	  int val;
85f7cc78ecSespie 
86f7cc78ecSespie 	  addr = (addr + 3) & ~3;
87f7cc78ecSespie 	  if ((status = get_int (addr, &val, info)))
88f7cc78ecSespie 	    goto fail;
89f7cc78ecSespie 
90f7cc78ecSespie 	  fprintf_fn (stream, " default: ");
91f7cc78ecSespie 	  (*info->print_address_func) (val + insn_start, info);
92f7cc78ecSespie 	  addr += 4;
93f7cc78ecSespie 
94f7cc78ecSespie 	  if ((status = get_int (addr, &lowval, info)))
95f7cc78ecSespie 	    goto fail;
96f7cc78ecSespie 	  addr += 4;
97f7cc78ecSespie 
98f7cc78ecSespie 	  if ((status = get_int (addr, &highval, info)))
99f7cc78ecSespie 	    goto fail;
100f7cc78ecSespie 	  addr += 4;
101f7cc78ecSespie 
102*d2201f2fSdrahn 	  while (lowval <= highval)
103*d2201f2fSdrahn 	    {
104f7cc78ecSespie 	      if ((status = get_int (addr, &val, info)))
105f7cc78ecSespie 		goto fail;
106f7cc78ecSespie 	      fprintf_fn (stream, " %d:[", lowval);
107f7cc78ecSespie 	      (*info->print_address_func) (val + insn_start, info);
108f7cc78ecSespie 	      fprintf_fn (stream, " ]");
109f7cc78ecSespie 	      addr += 4;
110f7cc78ecSespie 	      lowval++;
111f7cc78ecSespie 	    }
112f7cc78ecSespie 	  return addr - insn_start;
113f7cc78ecSespie 	}
114f7cc78ecSespie 
115f7cc78ecSespie       /* The lookupswitch instruction is followed by the default
116f7cc78ecSespie 	 address, element count and pairs of values and
117f7cc78ecSespie 	 addresses.  */
118f7cc78ecSespie 
119*d2201f2fSdrahn       if (strcmp (op->u.name, "lookupswitch") == 0)
120f7cc78ecSespie 	{
121f7cc78ecSespie 	  int count;
122f7cc78ecSespie 	  int val;
123f7cc78ecSespie 
124f7cc78ecSespie 	  addr = (addr + 3) & ~3;
125f7cc78ecSespie 	  if ((status = get_int (addr, &val, info)))
126f7cc78ecSespie 	    goto fail;
127f7cc78ecSespie 	  addr += 4;
128f7cc78ecSespie 
129f7cc78ecSespie 	  fprintf_fn (stream, " default: ");
130f7cc78ecSespie 	  (*info->print_address_func) (val + insn_start, info);
131f7cc78ecSespie 
132f7cc78ecSespie 	  if ((status = get_int (addr, &count, info)))
133f7cc78ecSespie 	    goto fail;
134f7cc78ecSespie 	  addr += 4;
135f7cc78ecSespie 
136*d2201f2fSdrahn 	  while (count--)
137*d2201f2fSdrahn 	    {
138f7cc78ecSespie 	      if ((status = get_int (addr, &val, info)))
139f7cc78ecSespie 		goto fail;
140f7cc78ecSespie 	      addr += 4;
141f7cc78ecSespie 	      fprintf_fn (stream, " %d:[", val);
142f7cc78ecSespie 
143f7cc78ecSespie 	      if ((status = get_int (addr, &val, info)))
144f7cc78ecSespie 		goto fail;
145f7cc78ecSespie 	      addr += 4;
146f7cc78ecSespie 
147f7cc78ecSespie 	      (*info->print_address_func) (val + insn_start, info);
148f7cc78ecSespie 	      fprintf_fn (stream, " ]");
149f7cc78ecSespie 	    }
150f7cc78ecSespie 	  return addr - insn_start;
151f7cc78ecSespie 	}
152f7cc78ecSespie       for (a = 0; op->arg[a]; a++)
153f7cc78ecSespie 	{
154f7cc78ecSespie 	  unsigned char data[4];
155f7cc78ecSespie 	  int val = 0;
156f7cc78ecSespie 	  int i;
157f7cc78ecSespie 	  int size = ASIZE (op->arg[a]);
158f7cc78ecSespie 
159f7cc78ecSespie 	  if ((status = info->read_memory_func (addr, data, size, info)))
160f7cc78ecSespie 	    goto fail;
161f7cc78ecSespie 
162f7cc78ecSespie 	  val = (UNS (op->arg[0]) || ((data[0] & 0x80) == 0)) ? 0 : -1;
163f7cc78ecSespie 
164f7cc78ecSespie 	  for (i = 0; i < size; i++)
165f7cc78ecSespie 	    val = (val << 8) | (data[i] & 0xff);
166f7cc78ecSespie 
167f7cc78ecSespie 	  if (PCREL (op->arg[a]))
168f7cc78ecSespie 	    (*info->print_address_func) (val + insn_start, info);
169f7cc78ecSespie 	  else
170f7cc78ecSespie 	    fprintf_fn (stream, "%s%d", sep, val);
171f7cc78ecSespie 
172f7cc78ecSespie 	  sep = ",";
173f7cc78ecSespie 	  addr += size;
174f7cc78ecSespie 	}
175f7cc78ecSespie       return op->len;
176f7cc78ecSespie     }
177f7cc78ecSespie 
178f7cc78ecSespie  fail:
179f7cc78ecSespie   info->memory_error_func (status, addr, info);
180f7cc78ecSespie   return -1;
181f7cc78ecSespie }
182