* dwarf2.c (add_line_info): Ensure that the line_info_table is
[binutils.git] / opcodes / h8300-dis.c
blobd14fda9858b102b2905be98325ced061bd3339d7
1 /* Disassemble h8300 instructions.
2 Copyright 1993, 1994, 1996, 1998, 2000, 2001, 2002
3 Free Software Foundation, Inc.
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.
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.
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. */
19 #define DEFINE_TABLE
21 #include "sysdep.h"
22 #define h8_opcodes h8ops
23 #include "opcode/h8300.h"
24 #include "dis-asm.h"
25 #include "opintl.h"
27 static void bfd_h8_disassemble_init PARAMS ((void));
28 static unsigned int bfd_h8_disassemble
29 PARAMS ((bfd_vma, disassemble_info *, int));
31 /* Run through the opcodes and sort them into order to make them easy
32 to disassemble. */
33 static void
34 bfd_h8_disassemble_init ()
36 unsigned int i;
37 struct h8_opcode *p;
39 for (p = h8_opcodes; p->name; p++)
41 int n1 = 0;
42 int n2 = 0;
44 if ((int) p->data.nib[0] < 16)
45 n1 = (int) p->data.nib[0];
46 else
47 n1 = 0;
49 if ((int) p->data.nib[1] < 16)
50 n2 = (int) p->data.nib[1];
51 else
52 n2 = 0;
54 /* Just make sure there are an even number of nibbles in it, and
55 that the count is the same as the length. */
56 for (i = 0; p->data.nib[i] != E; i++)
59 if (i & 1)
60 abort ();
62 p->length = i / 2;
66 static unsigned int
67 bfd_h8_disassemble (addr, info, mode)
68 bfd_vma addr;
69 disassemble_info *info;
70 int mode;
72 /* Find the first entry in the table for this opcode. */
73 static const char *regnames[] =
75 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
76 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
78 static const char *wregnames[] =
80 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
81 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
83 static const char *lregnames[] =
85 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
86 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
88 int rs = 0;
89 int rd = 0;
90 int rdisp = 0;
91 int abs = 0;
92 int bit = 0;
93 int plen = 0;
94 static boolean init = 0;
95 struct h8_opcode *q;
96 char const **pregnames = mode != 0 ? lregnames : wregnames;
97 int status;
98 int l;
99 unsigned char data[20];
100 void *stream = info->stream;
101 fprintf_ftype fprintf = info->fprintf_func;
103 if (!init)
105 bfd_h8_disassemble_init ();
106 init = 1;
109 status = info->read_memory_func (addr, data, 2, info);
110 if (status != 0)
112 info->memory_error_func (status, addr, info);
113 return -1;
116 for (l = 2; status == 0 && l < 10; l += 2)
117 status = info->read_memory_func (addr + l, data + l, 2, info);
119 /* Find the exact opcode/arg combo. */
120 for (q = h8_opcodes; q->name; q++)
122 op_type *nib = q->data.nib;
123 unsigned int len = 0;
125 while (1)
127 op_type looking_for = *nib;
128 int thisnib = data[len >> 1];
130 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
132 if (looking_for < 16 && looking_for >= 0)
134 if (looking_for != thisnib)
135 goto fail;
137 else
139 if ((int) looking_for & (int) B31)
141 if (!(((int) thisnib & 0x8) != 0))
142 goto fail;
144 looking_for = (op_type) ((int) looking_for & ~(int) B31);
147 if ((int) looking_for & (int) B30)
149 if (!(((int) thisnib & 0x8) == 0))
150 goto fail;
152 looking_for = (op_type) ((int) looking_for & ~(int) B30);
155 if (looking_for & DBIT)
157 /* Exclude adds/subs by looking at bit 0 and 2, and
158 make sure the operand size, either w or l,
159 matches by looking at bit 1. */
160 if ((looking_for & 7) != (thisnib & 7))
161 goto fail;
163 abs = (thisnib & 0x8) ? 2 : 1;
165 else if (looking_for & (REG | IND | INC | DEC))
167 if (looking_for & SRC)
168 rs = thisnib;
169 else
170 rd = thisnib;
172 else if (looking_for & L_16)
174 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
175 plen = 16;
177 else if (looking_for & ABSJMP)
179 abs = (data[1] << 16) | (data[2] << 8) | (data[3]);
181 else if (looking_for & MEMIND)
183 abs = data[1];
185 else if (looking_for & L_32)
187 int i = len >> 1;
189 abs = (data[i] << 24)
190 | (data[i + 1] << 16)
191 | (data[i + 2] << 8)
192 | (data[i + 3]);
194 plen = 32;
196 else if (looking_for & L_24)
198 int i = len >> 1;
200 abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
201 plen = 24;
203 else if (looking_for & IGNORE)
207 else if (looking_for & DISPREG)
209 rdisp = thisnib;
211 else if (looking_for & KBIT)
213 switch (thisnib)
215 case 9:
216 abs = 4;
217 break;
218 case 8:
219 abs = 2;
220 break;
221 case 0:
222 abs = 1;
223 break;
224 default:
225 goto fail;
228 else if (looking_for & L_8)
230 plen = 8;
231 abs = data[len >> 1];
233 else if (looking_for & L_3)
235 bit = thisnib & 0x7;
237 else if (looking_for & L_2)
239 plen = 2;
240 abs = thisnib & 0x3;
242 else if (looking_for & MACREG)
244 abs = (thisnib == 3);
246 else if (looking_for == E)
248 int i;
250 for (i = 0; i < q->length; i++)
251 fprintf (stream, "%02x ", data[i]);
253 for (; i < 6; i++)
254 fprintf (stream, " ");
256 fprintf (stream, "%s\t", q->name);
258 /* Gross. Disgusting. */
259 if (strcmp (q->name, "ldm.l") == 0)
261 int count, high;
263 count = (data[1] >> 4) & 0x3;
264 high = data[3] & 0x7;
266 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
267 return q->length;
270 if (strcmp (q->name, "stm.l") == 0)
272 int count, low;
274 count = (data[1] >> 4) & 0x3;
275 low = data[3] & 0x7;
277 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
278 return q->length;
281 /* Fill in the args. */
283 op_type *args = q->args.nib;
284 int hadone = 0;
286 while (*args != E)
288 int x = *args;
290 if (hadone)
291 fprintf (stream, ",");
293 if (x & L_3)
295 fprintf (stream, "#0x%x", (unsigned) bit);
297 else if (x & (IMM | KBIT | DBIT))
299 /* Bletch. For shal #2,er0 and friends. */
300 if (*(args + 1) & SRC_IN_DST)
301 abs = 2;
303 fprintf (stream, "#0x%x", (unsigned) abs);
305 else if (x & REG)
307 int rn = (x & DST) ? rd : rs;
309 switch (x & SIZE)
311 case L_8:
312 fprintf (stream, "%s", regnames[rn]);
313 break;
314 case L_16:
315 fprintf (stream, "%s", wregnames[rn]);
316 break;
317 case L_P:
318 case L_32:
319 fprintf (stream, "%s", lregnames[rn]);
320 break;
323 else if (x & MACREG)
325 fprintf (stream, "mac%c", abs ? 'l' : 'h');
327 else if (x & INC)
329 fprintf (stream, "@%s+", pregnames[rs]);
331 else if (x & DEC)
333 fprintf (stream, "@-%s", pregnames[rd]);
335 else if (x & IND)
337 int rn = (x & DST) ? rd : rs;
338 fprintf (stream, "@%s", pregnames[rn]);
340 else if (x & ABS8MEM)
342 fprintf (stream, "@0x%x:8", (unsigned) abs);
344 else if (x & (ABS | ABSJMP))
346 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
348 else if (x & MEMIND)
350 fprintf (stream, "@@%d (%x)", abs, abs);
352 else if (x & PCREL)
354 if (x & L_16)
356 abs += 2;
357 fprintf (stream,
358 ".%s%d (%x)",
359 (short) abs > 0 ? "+" : "",
360 (short) abs, addr + (short) abs + 2);
362 else
364 fprintf (stream,
365 ".%s%d (%x)",
366 (char) abs > 0 ? "+" : "",
367 (char) abs, addr + (char) abs + 2);
370 else if (x & DISP)
372 fprintf (stream, "@(0x%x:%d,%s)",
373 abs, plen, pregnames[rdisp]);
375 else if (x & CCR)
377 fprintf (stream, "ccr");
379 else if (x & EXR)
381 fprintf (stream, "exr");
383 else
384 /* xgettext:c-format */
385 fprintf (stream, _("Hmmmm %x"), x);
387 hadone = 1;
388 args++;
392 return q->length;
394 else
395 /* xgettext:c-format */
396 fprintf (stream, _("Don't understand %x \n"), looking_for);
399 len++;
400 nib++;
403 fail:
407 /* Fell off the end. */
408 fprintf (stream, "%02x %02x .word\tH'%x,H'%x",
409 data[0], data[1],
410 data[0], data[1]);
411 return 2;
415 print_insn_h8300 (addr, info)
416 bfd_vma addr;
417 disassemble_info *info;
419 return bfd_h8_disassemble (addr, info, 0);
423 print_insn_h8300h (addr, info)
424 bfd_vma addr;
425 disassemble_info *info;
427 return bfd_h8_disassemble (addr, info, 1);
431 print_insn_h8300s (addr, info)
432 bfd_vma addr;
433 disassemble_info *info;
435 return bfd_h8_disassemble (addr, info, 2);