2001-02-17 Philip Blundell <philb@gnu.org>
[binutils.git] / opcodes / h8500-dis.c
blob802827eb8d56cf7fa49452609a91ed69cae3fbe7
1 /* Disassemble h8500 instructions.
2 Copyright (C) 1993, 94, 95, 1998 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <stdio.h>
20 #define DISASSEMBLER_TABLE
21 #define DEFINE_TABLE
23 #include "sysdep.h"
24 #include "h8500-opc.h"
25 #include "dis-asm.h"
26 #include "opintl.h"
28 /* Maximum length of an instruction. */
29 #define MAXLEN 8
31 #include <setjmp.h>
33 struct private
35 /* Points to first byte not fetched. */
36 bfd_byte *max_fetched;
37 bfd_byte the_buffer[MAXLEN];
38 bfd_vma insn_start;
39 jmp_buf bailout;
42 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
43 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
44 on error. */
45 #define FETCH_DATA(info, addr) \
46 ((addr) <= ((struct private *)(info->private_data))->max_fetched \
47 ? 1 : fetch_data ((info), (addr)))
49 static int
50 fetch_data (info, addr)
51 struct disassemble_info *info;
52 bfd_byte *addr;
54 int status;
55 struct private *priv = (struct private *) info->private_data;
56 bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer);
58 status = (*info->read_memory_func) (start,
59 priv->max_fetched,
60 addr - priv->max_fetched,
61 info);
62 if (status != 0)
64 (*info->memory_error_func) (status, start, info);
65 longjmp (priv->bailout, 1);
67 else
68 priv->max_fetched = addr;
69 return 1;
72 static char *crname[] = { "sr", "ccr", "*", "br", "ep", "dp", "*", "tp" };
74 int
75 print_insn_h8500 (addr, info)
76 bfd_vma addr;
77 disassemble_info *info;
79 h8500_opcode_info *opcode;
80 void *stream = info->stream;
81 fprintf_ftype func = info->fprintf_func;
83 struct private priv;
84 bfd_byte *buffer = priv.the_buffer;
86 info->private_data = (PTR) & priv;
87 priv.max_fetched = priv.the_buffer;
88 priv.insn_start = addr;
89 if (setjmp (priv.bailout) != 0)
90 /* Error return. */
91 return -1;
93 if (0)
95 static int one;
97 if (!one)
99 one = 1;
100 for (opcode = h8500_table; opcode->name; opcode++)
102 if ((opcode->bytes[0].contents & 0x8) == 0)
103 printf ("%s\n", opcode->name);
108 /* Run down the table to find the one which matches. */
109 for (opcode = h8500_table; opcode->name; opcode++)
111 int byte;
112 int rn = 0;
113 int rd = 0;
114 int rs = 0;
115 int disp = 0;
116 int abs = 0;
117 int imm = 0;
118 int pcrel = 0;
119 int qim = 0;
120 int i;
121 int cr = 0;
123 for (byte = 0; byte < opcode->length; byte++)
125 FETCH_DATA (info, buffer + byte + 1);
126 if ((buffer[byte] & opcode->bytes[byte].mask)
127 != (opcode->bytes[byte].contents))
129 goto next;
131 else
133 /* Extract any info parts. */
134 switch (opcode->bytes[byte].insert)
136 case 0:
137 case FP:
138 break;
139 default:
140 /* xgettext:c-format */
141 func (stream, _("can't cope with insert %d\n"),
142 opcode->bytes[byte].insert);
143 break;
144 case RN:
145 rn = buffer[byte] & 0x7;
146 break;
147 case RS:
148 rs = buffer[byte] & 0x7;
149 break;
150 case CRB:
151 cr = buffer[byte] & 0x7;
152 if (cr == 0)
153 goto next;
154 break;
155 case CRW:
156 cr = buffer[byte] & 0x7;
157 if (cr != 0)
158 goto next;
159 break;
160 case DISP16:
161 FETCH_DATA (info, buffer + byte + 2);
162 disp = (buffer[byte] << 8) | (buffer[byte + 1]);
163 break;
164 case FPIND_D8:
165 case DISP8:
166 disp = ((char) (buffer[byte]));
167 break;
168 case RD:
169 case RDIND:
170 rd = buffer[byte] & 0x7;
171 break;
172 case ABS24:
173 FETCH_DATA (info, buffer + byte + 3);
174 abs =
175 (buffer[byte] << 16)
176 | (buffer[byte + 1] << 8)
177 | (buffer[byte + 2]);
178 break;
179 case ABS16:
180 FETCH_DATA (info, buffer + byte + 2);
181 abs = (buffer[byte] << 8) | (buffer[byte + 1]);
182 break;
183 case ABS8:
184 abs = (buffer[byte]);
185 break;
186 case IMM16:
187 FETCH_DATA (info, buffer + byte + 2);
188 imm = (buffer[byte] << 8) | (buffer[byte + 1]);
189 break;
190 case IMM4:
191 imm = (buffer[byte]) & 0xf;
192 break;
193 case IMM8:
194 case RLIST:
195 imm = (buffer[byte]);
196 break;
197 case PCREL16:
198 FETCH_DATA (info, buffer + byte + 2);
199 pcrel = (buffer[byte] << 8) | (buffer[byte + 1]);
200 break;
201 case PCREL8:
202 pcrel = (buffer[byte]);
203 break;
204 case QIM:
205 switch (buffer[byte] & 0x7)
207 case 0:
208 qim = 1;
209 break;
210 case 1:
211 qim = 2;
212 break;
213 case 4:
214 qim = -1;
215 break;
216 case 5:
217 qim = -2;
218 break;
220 break;
225 /* We get here when all the masks have passed so we can output
226 the operands. */
227 FETCH_DATA (info, buffer + opcode->length);
228 for (i = 0; i < opcode->length; i++)
230 (func) (stream, "%02x ", buffer[i]);
232 for (; i < 6; i++)
234 (func) (stream, " ");
236 (func) (stream, "%s\t", opcode->name);
237 for (i = 0; i < opcode->nargs; i++)
239 if (i)
240 (func) (stream, ",");
241 switch (opcode->arg_type[i])
243 case FP:
244 func (stream, "fp");
245 break;
246 case RNIND_D16:
247 func (stream, "@(0x%x:16,r%d)", disp, rn);
248 break;
249 case RNIND_D8:
250 func (stream, "@(0x%x:8 (%d),r%d)", disp & 0xff, disp, rn);
251 break;
252 case RDIND_D16:
253 func (stream, "@(0x%x:16,r%d)", disp, rd);
254 break;
255 case RDIND_D8:
256 func (stream, "@(0x%x:8 (%d), r%d)", disp & 0xff, disp, rd);
257 break;
258 case FPIND_D8:
259 func (stream, "@(0x%x:8 (%d), fp)", disp & 0xff, disp, rn);
260 break;
261 case CRB:
262 case CRW:
263 func (stream, "%s", crname[cr]);
264 break;
265 case RN:
266 func (stream, "r%d", rn);
267 break;
268 case RD:
269 func (stream, "r%d", rd);
270 break;
271 case RS:
272 func (stream, "r%d", rs);
273 break;
274 case RNDEC:
275 func (stream, "@-r%d", rn);
276 break;
277 case RNINC:
278 func (stream, "@r%d+", rn);
279 break;
280 case RNIND:
281 func (stream, "@r%d", rn);
282 break;
283 case RDIND:
284 func (stream, "@r%d", rd);
285 break;
286 case SPINC:
287 func (stream, "@sp+");
288 break;
289 case SPDEC:
290 func (stream, "@-sp");
291 break;
292 case ABS24:
293 func (stream, "@0x%0x:24", abs);
294 break;
295 case ABS16:
296 func (stream, "@0x%0x:16", abs & 0xffff);
297 break;
298 case ABS8:
299 func (stream, "@0x%0x:8", abs & 0xff);
300 break;
301 case IMM16:
302 func (stream, "#0x%0x:16", imm & 0xffff);
303 break;
304 case RLIST:
306 int i;
307 int nc = 0;
308 func (stream, "(");
309 for (i = 0; i < 8; i++)
311 if (imm & (1 << i))
313 func (stream, "r%d", i);
314 if (nc)
315 func (stream, ",");
316 nc = 1;
319 func (stream, ")");
321 break;
322 case IMM8:
323 func (stream, "#0x%0x:8", imm & 0xff);
324 break;
325 case PCREL16:
326 func (stream, "0x%0x:16",
327 (pcrel + addr + opcode->length) & 0xffff);
328 break;
329 case PCREL8:
330 func (stream, "#0x%0x:8",
331 ((char) pcrel + addr + opcode->length) & 0xffff);
332 break;
333 case QIM:
334 func (stream, "#%d:q", qim);
335 break;
336 case IMM4:
337 func (stream, "#%d:4", imm);
338 break;
341 return opcode->length;
342 next:
346 /* Couldn't understand anything. */
347 /* xgettext:c-format */
348 func (stream, _("%02x\t\t*unknown*"), buffer[0]);
349 return 1;