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