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