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