daily update
[binutils.git] / opcodes / h8300-dis.c
blobc521c773fce2fbbefaa83a62dbad859570a62df1
1 /* Disassemble h8300 instructions.
2 Copyright 1993, 1994, 1996, 1998, 2000, 2001 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 #define DEFINE_TABLE
20 #include "sysdep.h"
21 #define h8_opcodes h8ops
22 #include "opcode/h8300.h"
23 #include "dis-asm.h"
24 #include "opintl.h"
26 static void bfd_h8_disassemble_init PARAMS ((void));
27 static unsigned int bfd_h8_disassemble
28 PARAMS ((bfd_vma, disassemble_info *, int));
30 /* Run through the opcodes and sort them into order to make them easy
31 to disassemble. */
32 static void
33 bfd_h8_disassemble_init ()
35 unsigned int i;
36 struct h8_opcode *p;
38 for (p = h8_opcodes; p->name; p++)
40 int n1 = 0;
41 int n2 = 0;
43 if ((int) p->data.nib[0] < 16)
44 n1 = (int) p->data.nib[0];
45 else
46 n1 = 0;
48 if ((int) p->data.nib[1] < 16)
49 n2 = (int) p->data.nib[1];
50 else
51 n2 = 0;
53 /* Just make sure there are an even number of nibbles in it, and
54 that the count is the same as the length. */
55 for (i = 0; p->data.nib[i] != E; i++)
58 if (i & 1)
59 abort ();
61 p->length = i / 2;
65 static unsigned int
66 bfd_h8_disassemble (addr, info, mode)
67 bfd_vma addr;
68 disassemble_info *info;
69 int mode;
71 /* Find the first entry in the table for this opcode. */
72 static CONST char *regnames[] =
74 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
75 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
77 static CONST char *wregnames[] =
79 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
80 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
82 static CONST char *lregnames[] =
84 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
85 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
87 int rs = 0;
88 int rd = 0;
89 int rdisp = 0;
90 int abs = 0;
91 int bit = 0;
92 int plen = 0;
93 static boolean init = 0;
94 struct h8_opcode *q;
95 char CONST **pregnames = mode != 0 ? lregnames : wregnames;
96 int status;
97 int l;
98 unsigned char data[20];
99 void *stream = info->stream;
100 fprintf_ftype fprintf = info->fprintf_func;
102 if (!init)
104 bfd_h8_disassemble_init ();
105 init = 1;
108 status = info->read_memory_func (addr, data, 2, info);
109 if (status != 0)
111 info->memory_error_func (status, addr, info);
112 return -1;
115 for (l = 2; status == 0 && l < 10; l += 2)
116 status = info->read_memory_func (addr + l, data + l, 2, info);
118 /* Find the exact opcode/arg combo. */
119 for (q = h8_opcodes; q->name; q++)
121 op_type *nib = q->data.nib;
122 unsigned int len = 0;
124 while (1)
126 op_type looking_for = *nib;
127 int thisnib = data[len >> 1];
129 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
131 if (looking_for < 16 && looking_for >= 0)
133 if (looking_for != thisnib)
134 goto fail;
136 else
138 if ((int) looking_for & (int) B31)
140 if (!(((int) thisnib & 0x8) != 0))
141 goto fail;
143 looking_for = (op_type) ((int) looking_for & ~(int) B31);
146 if ((int) looking_for & (int) B30)
148 if (!(((int) thisnib & 0x8) == 0))
149 goto fail;
151 looking_for = (op_type) ((int) looking_for & ~(int) B30);
154 if (looking_for & DBIT)
156 /* Exclude adds/subs by looking at bit 0 and 2, and
157 make sure the operand size, either w or l,
158 matches by looking at bit 1. */
159 if ((looking_for & 7) != (thisnib & 7))
160 goto fail;
162 abs = (thisnib & 0x8) ? 2 : 1;
164 else if (looking_for & (REG | IND | INC | DEC))
166 if (looking_for & SRC)
167 rs = thisnib;
168 else
169 rd = thisnib;
171 else if (looking_for & L_16)
173 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
174 plen = 16;
176 else if (looking_for & ABSJMP)
178 abs = (data[1] << 16) | (data[2] << 8) | (data[3]);
180 else if (looking_for & MEMIND)
182 abs = data[1];
184 else if (looking_for & L_32)
186 int i = len >> 1;
188 abs = (data[i] << 24)
189 | (data[i + 1] << 16)
190 | (data[i + 2] << 8)
191 | (data[i + 3]);
193 plen = 32;
195 else if (looking_for & L_24)
197 int i = len >> 1;
199 abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
200 plen = 24;
202 else if (looking_for & IGNORE)
206 else if (looking_for & DISPREG)
208 rdisp = thisnib;
210 else if (looking_for & KBIT)
212 switch (thisnib)
214 case 9:
215 abs = 4;
216 break;
217 case 8:
218 abs = 2;
219 break;
220 case 0:
221 abs = 1;
222 break;
223 default:
224 goto fail;
227 else if (looking_for & L_8)
229 plen = 8;
230 abs = data[len >> 1];
232 else if (looking_for & L_3)
234 bit = thisnib & 0x7;
236 else if (looking_for & L_2)
238 plen = 2;
239 abs = thisnib & 0x3;
241 else if (looking_for & MACREG)
243 abs = (thisnib == 3);
245 else if (looking_for == E)
247 int i;
249 for (i = 0; i < q->length; i++)
250 fprintf (stream, "%02x ", data[i]);
252 for (; i < 6; i++)
253 fprintf (stream, " ");
255 fprintf (stream, "%s\t", q->name);
257 /* Gross. Disgusting. */
258 if (strcmp (q->name, "ldm.l") == 0)
260 int count, high;
262 count = (data[1] >> 4) & 0x3;
263 high = data[3] & 0x7;
265 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
266 return q->length;
269 if (strcmp (q->name, "stm.l") == 0)
271 int count, low;
273 count = (data[1] >> 4) & 0x3;
274 low = data[3] & 0x7;
276 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
277 return q->length;
280 /* Fill in the args. */
282 op_type *args = q->args.nib;
283 int hadone = 0;
285 while (*args != E)
287 int x = *args;
289 if (hadone)
290 fprintf (stream, ",");
292 if (x & L_3)
294 fprintf (stream, "#0x%x", (unsigned) bit);
296 else if (x & (IMM | KBIT | DBIT))
298 /* Bletch. For shal #2,er0 and friends. */
299 if (*(args + 1) & SRC_IN_DST)
300 abs = 2;
302 fprintf (stream, "#0x%x", (unsigned) abs);
304 else if (x & REG)
306 int rn = (x & DST) ? rd : rs;
308 switch (x & SIZE)
310 case L_8:
311 fprintf (stream, "%s", regnames[rn]);
312 break;
313 case L_16:
314 fprintf (stream, "%s", wregnames[rn]);
315 break;
316 case L_P:
317 case L_32:
318 fprintf (stream, "%s", lregnames[rn]);
319 break;
322 else if (x & MACREG)
324 fprintf (stream, "mac%c", abs ? 'l' : 'h');
326 else if (x & INC)
328 fprintf (stream, "@%s+", pregnames[rs]);
330 else if (x & DEC)
332 fprintf (stream, "@-%s", pregnames[rd]);
334 else if (x & IND)
336 int rn = (x & DST) ? rd : rs;
337 fprintf (stream, "@%s", pregnames[rn]);
339 else if (x & ABS8MEM)
341 fprintf (stream, "@0x%x:8", (unsigned) abs);
343 else if (x & (ABS | ABSJMP))
345 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
347 else if (x & MEMIND)
349 fprintf (stream, "@@%d (%x)", abs, abs);
351 else if (x & PCREL)
353 if (x & L_16)
355 abs += 2;
356 fprintf (stream,
357 ".%s%d (%x)",
358 (short) abs > 0 ? "+" : "",
359 (short) abs, addr + (short) abs + 2);
361 else
363 fprintf (stream,
364 ".%s%d (%x)",
365 (char) abs > 0 ? "+" : "",
366 (char) abs, addr + (char) abs + 2);
369 else if (x & DISP)
371 fprintf (stream, "@(0x%x:%d,%s)",
372 abs, plen, pregnames[rdisp]);
374 else if (x & CCR)
376 fprintf (stream, "ccr");
378 else if (x & EXR)
380 fprintf (stream, "exr");
382 else
383 /* xgettext:c-format */
384 fprintf (stream, _("Hmmmm %x"), x);
386 hadone = 1;
387 args++;
391 return q->length;
393 else
394 /* xgettext:c-format */
395 fprintf (stream, _("Don't understand %x \n"), looking_for);
398 len++;
399 nib++;
402 fail:
406 /* Fell off the end. */
407 fprintf (stream, "%02x %02x .word\tH'%x,H'%x",
408 data[0], data[1],
409 data[0], data[1]);
410 return 2;
414 print_insn_h8300 (addr, info)
415 bfd_vma addr;
416 disassemble_info *info;
418 return bfd_h8_disassemble (addr, info, 0);
422 print_insn_h8300h (addr, info)
423 bfd_vma addr;
424 disassemble_info *info;
426 return bfd_h8_disassemble (addr, info, 1);
430 print_insn_h8300s (addr, info)
431 bfd_vma addr;
432 disassemble_info *info;
434 return bfd_h8_disassemble (addr, info, 2);