10SN/A/* pj-dis.c -- Disassemble picoJava instructions.
217231Sserb   Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
30SN/A   Contributed by Steve Chamberlain, of Transmeta (sac@pobox.com).
40SN/A
50SN/AThis program is free software; you can redistribute it and/or modify
60SN/Ait under the terms of the GNU General Public License as published by
72362SN/Athe Free Software Foundation; either version 2 of the License, or
80SN/A(at your option) any later version.
92362SN/A
100SN/AThis program is distributed in the hope that it will be useful,
110SN/Abut WITHOUT ANY WARRANTY; without even the implied warranty of
120SN/AMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
130SN/AGNU General Public License for more details.
140SN/A
150SN/AYou should have received a copy of the GNU General Public License
160SN/Aalong with this program; if not, write to the Free Software
170SN/AFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
180SN/A
190SN/A#include <stdio.h>
200SN/A#include "sysdep.h"
212362SN/A#include "opcode/pj.h"
222362SN/A#include "dis-asm.h"
232362SN/A
240SN/Aextern const pj_opc_info_t pj_opc_info[512];
2517231Sserb
260SN/Astatic int get_int PARAMS ((bfd_vma, int *, struct disassemble_info *));
270SN/A
280SN/A
2917231Sserbstatic int
3017231Sserbget_int (memaddr, iptr, info)
3117231Sserb     bfd_vma memaddr;
3217231Sserb     int *iptr;
3317231Sserb     struct disassemble_info *info;
3417231Sserb{
3517231Sserb  unsigned char ival[4];
3617231Sserb
3717231Sserb  int status = info->read_memory_func (memaddr, ival, 4, info);
3817231Sserb
3917231Sserb  *iptr = (ival[0] << 24)
4017231Sserb    | (ival[1] << 16)
410SN/A    | (ival[2] << 8)
420SN/A    | (ival[3] << 0);
4317231Sserb
4417231Sserb  return status;
4517231Sserb}
4617231Sserb
4717231Sserbint
4817231Sserbprint_insn_pj (addr, info)
4917231Sserb     bfd_vma addr;
5017231Sserb     struct disassemble_info *info;
510SN/A{
527814SN/A  fprintf_ftype fprintf_fn = info->fprintf_func;
5317231Sserb  void *stream = info->stream;
5417231Sserb  unsigned char opcode;
5517231Sserb  int status;
5617231Sserb
5717231Sserb  if ((status = info->read_memory_func (addr, &opcode, 1, info)))
5817231Sserb    goto fail;
5917231Sserb
6017231Sserb  if (opcode == 0xff)
6117231Sserb    {
6217231Sserb      unsigned char byte_2;
6317231Sserb      if ((status = info->read_memory_func (addr + 1, &byte_2, 1, info)))
6417231Sserb	goto fail;
6517231Sserb      fprintf_fn (stream, "%s\t", pj_opc_info[opcode + byte_2].u.name);
6617231Sserb      return 2;
6717231Sserb    }
6817231Sserb  else
6917231Sserb    {
700SN/A      char *sep = "\t";
71      int insn_start = addr;
72      const pj_opc_info_t *op = &pj_opc_info[opcode];
73      int a;
74      addr++;
75      fprintf_fn (stream, "%s", op->u.name);
76
77      /* The tableswitch instruction is followed by the default
78	 address, low value, high value and the destinations.  */
79
80      if (strcmp (op->u.name, "tableswitch") == 0)
81	{
82	  int lowval;
83	  int highval;
84	  int val;
85
86	  addr = (addr + 3) & ~3;
87	  if ((status = get_int (addr, &val, info)))
88	    goto fail;
89
90	  fprintf_fn (stream, " default: ");
91	  (*info->print_address_func) (val + insn_start, info);
92	  addr += 4;
93
94	  if ((status = get_int (addr, &lowval, info)))
95	    goto fail;
96	  addr += 4;
97
98	  if ((status = get_int (addr, &highval, info)))
99	    goto fail;
100	  addr += 4;
101
102	  while (lowval <= highval)
103	    {
104	      if ((status = get_int (addr, &val, info)))
105		goto fail;
106	      fprintf_fn (stream, " %d:[", lowval);
107	      (*info->print_address_func) (val + insn_start, info);
108	      fprintf_fn (stream, " ]");
109	      addr += 4;
110	      lowval++;
111	    }
112	  return addr - insn_start;
113	}
114
115      /* The lookupswitch instruction is followed by the default
116	 address, element count and pairs of values and
117	 addresses.  */
118
119      if (strcmp (op->u.name, "lookupswitch") == 0)
120	{
121	  int count;
122	  int val;
123
124	  addr = (addr + 3) & ~3;
125	  if ((status = get_int (addr, &val, info)))
126	    goto fail;
127	  addr += 4;
128
129	  fprintf_fn (stream, " default: ");
130	  (*info->print_address_func) (val + insn_start, info);
131
132	  if ((status = get_int (addr, &count, info)))
133	    goto fail;
134	  addr += 4;
135
136	  while (count--)
137	    {
138	      if ((status = get_int (addr, &val, info)))
139		goto fail;
140	      addr += 4;
141	      fprintf_fn (stream, " %d:[", val);
142
143	      if ((status = get_int (addr, &val, info)))
144		goto fail;
145	      addr += 4;
146
147	      (*info->print_address_func) (val + insn_start, info);
148	      fprintf_fn (stream, " ]");
149	    }
150	  return addr - insn_start;
151	}
152      for (a = 0; op->arg[a]; a++)
153	{
154	  unsigned char data[4];
155	  int val = 0;
156	  int i;
157	  int size = ASIZE (op->arg[a]);
158
159	  if ((status = info->read_memory_func (addr, data, size, info)))
160	    goto fail;
161
162	  val = (UNS (op->arg[0]) || ((data[0] & 0x80) == 0)) ? 0 : -1;
163
164	  for (i = 0; i < size; i++)
165	    val = (val << 8) | (data[i] & 0xff);
166
167	  if (PCREL (op->arg[a]))
168	    (*info->print_address_func) (val + insn_start, info);
169	  else
170	    fprintf_fn (stream, "%s%d", sep, val);
171
172	  sep = ",";
173	  addr += size;
174	}
175      return op->len;
176    }
177
178 fail:
179  info->memory_error_func (status, addr, info);
180  return -1;
181}
182