* m68k-dis.c: Fix formatting.
[external/binutils.git] / opcodes / pj-dis.c
1 /* pj-dis.c -- Disassemble picoJava instructions.
2    Copyright 1999, 2000 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 #include <stdio.h>
20 #include "sysdep.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
27 get_int (memaddr, iptr, info)
28      bfd_vma memaddr;
29      int *iptr;
30      struct disassemble_info *info;
31 {
32   unsigned char ival[4];
33
34   int status = info->read_memory_func (memaddr, ival, 4, info);
35
36   *iptr = (ival[0] << 24) 
37     | (ival[1] << 16) 
38     | (ival[2] << 8) 
39     | (ival[3] << 0) ;
40
41   return status;
42 }
43
44 int
45 print_insn_pj (addr, info)
46      bfd_vma addr;
47      struct disassemble_info *info;
48 {
49   fprintf_ftype fprintf_fn = info->fprintf_func;
50   void *stream = info->stream;
51   unsigned char opcode;
52   int status;
53
54   if ((status = info->read_memory_func (addr, &opcode, 1, info)))
55     goto fail;
56
57   if (opcode == 0xff)
58     {
59       unsigned char byte_2;
60       if ((status = info->read_memory_func (addr + 1, &byte_2, 1, info)))
61         goto fail;
62       fprintf_fn (stream, "%s\t", pj_opc_info[opcode + byte_2].name);
63       return 2;
64     }
65   else
66     {
67       char *sep = "\t";
68       int insn_start = addr;
69       const pj_opc_info_t *op = &pj_opc_info[opcode];
70       int a;
71       addr++;
72       fprintf_fn (stream, "%s", op->name);
73
74       /* The tableswitch instruction is followed by the default
75          address, low value, high value and the destinations. */
76
77       if (strcmp (op->name, "tableswitch") == 0)
78         {
79           int lowval;
80           int highval;
81           int val;
82
83           addr = (addr + 3) & ~3;
84           if ((status = get_int (addr, &val, info)))
85             goto fail;
86
87           fprintf_fn (stream, " default: ");
88           (*info->print_address_func) (val + insn_start, info);
89           addr += 4;
90
91           if ((status = get_int (addr, &lowval, info)))
92             goto fail;
93           addr += 4;
94
95           if ((status = get_int (addr, &highval, info)))
96             goto fail;
97           addr += 4;
98
99           while (lowval <= highval)
100             {
101               if ((status = get_int (addr, &val, info)))
102                 goto fail;
103               fprintf_fn (stream, " %d:[", lowval);
104               (*info->print_address_func) (val + insn_start, info);
105               fprintf_fn (stream, " ]");
106               addr += 4;
107               lowval++;
108             }
109           return addr - insn_start;
110         }
111
112       /* The lookupswitch instruction is followed by the default
113          address, element count and pairs of values and
114          addresses. */
115             
116       if (strcmp (op->name, "lookupswitch") == 0)
117         {
118           int count;
119           int val;
120
121           addr = (addr + 3) & ~3;
122           if ((status = get_int (addr, &val, info)))
123             goto fail;
124           addr += 4;
125
126           fprintf_fn (stream, " default: ");
127           (*info->print_address_func) (val + insn_start, info);
128
129           if ((status = get_int (addr, &count, info)))
130             goto fail;
131           addr += 4;
132
133           while (count--)
134             {
135               if ((status = get_int (addr, &val, info)))
136                 goto fail;
137               addr += 4;
138               fprintf_fn (stream, " %d:[", val);
139
140               if ((status = get_int (addr, &val, info)))
141                 goto fail;
142               addr += 4;
143
144               (*info->print_address_func) (val + insn_start, info);
145               fprintf_fn (stream, " ]");
146             }
147           return addr - insn_start;
148         }
149       for (a = 0; op->arg[a]; a++)
150         {
151           unsigned char data[4];
152           int val = 0;
153           int i;
154           int size = ASIZE (op->arg[a]);
155
156           if ((status = info->read_memory_func (addr, data, size, info)))
157             goto fail;
158
159           val = (UNS (op->arg[0]) || ((data[0] & 0x80) == 0)) ? 0 : -1;
160
161           for (i = 0; i < size; i++)
162             val = (val << 8) | (data[i] & 0xff);
163
164           if (PCREL (op->arg[a]))
165             (*info->print_address_func) (val + insn_start, info);
166           else
167             fprintf_fn (stream, "%s%d", sep, val);
168
169           sep = ",";
170           addr += size;
171         }
172       return op->len;
173     }
174
175  fail:
176   info->memory_error_func (status, addr, info);
177   return -1;
178 }