file sysinfo.h was initially added on branch binutils-2_10-branch.
[binutils.git] / opcodes / h8300-dis.c
blob84f4aadc72d0b14c7987f4c4dd07d40510ba2119
1 /* Disassemble h8300 instructions.
2 Copyright (C) 1993, 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 #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"
27 /* Run through the opcodes and sort them into order to make them easy
28 to disassemble
30 static void
31 bfd_h8_disassemble_init ()
33 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)
45 n1 = (int) p->data.nib[0];
47 else
48 n1 = 0;
49 if ((int) p->data.nib[1] < 16)
51 n2 = (int) p->data.nib[1];
53 else
54 n2 = 0;
56 /* Just make sure there are an even number of nibbles in it, and
57 that the count is the same s the length */
58 for (i = 0; p->data.nib[i] != E; i++)
59 /*EMPTY*/ ;
60 if (i & 1)
61 abort ();
62 p->length = i / 2;
68 unsigned int
69 bfd_h8_disassemble (addr, info, mode)
70 bfd_vma addr;
71 disassemble_info *info;
72 int mode;
74 /* Find the first entry in the table for this opcode */
75 static CONST char *regnames[] =
77 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
78 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"};
80 static CONST char *wregnames[] =
82 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
83 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
86 static CONST char *lregnames[] =
88 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
89 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
93 int rs = 0;
94 int rd = 0;
95 int rdisp = 0;
96 int abs = 0;
97 int bit = 0;
98 int plen = 0;
99 static boolean init = 0;
100 struct h8_opcode *q = h8_opcodes;
101 char CONST **pregnames = mode != 0 ? lregnames : wregnames;
102 int status;
103 int l;
105 unsigned char data[20];
106 void *stream = info->stream;
107 fprintf_ftype fprintf = info->fprintf_func;
109 if (!init)
111 bfd_h8_disassemble_init ();
112 init = 1;
115 status = info->read_memory_func(addr, data, 2, info);
116 if (status != 0)
118 info->memory_error_func(status, addr, info);
119 return -1;
121 for (l = 2; status == 0 && l < 10; l+=2)
123 status = info->read_memory_func(addr+l, data+l, 2, info);
128 /* Find the exact opcode/arg combo */
129 while (q->name)
131 op_type *nib;
132 unsigned int len = 0;
134 nib = q->data.nib;
136 while (1)
138 op_type looking_for = *nib;
139 int thisnib = data[len >> 1];
141 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
143 if (looking_for < 16 && looking_for >=0)
146 if (looking_for != thisnib)
147 goto fail;
150 else
153 if ((int) looking_for & (int) B31)
155 if (! (((int) thisnib & 0x8) != 0))
156 goto fail;
157 looking_for = (op_type) ((int) looking_for & ~(int) B31);
159 if ((int) looking_for & (int) B30)
161 if (!(((int) thisnib & 0x8) == 0))
162 goto fail;
163 looking_for = (op_type) ((int) looking_for & ~(int) B30);
166 if (looking_for & DBIT)
168 if ((looking_for & 5) != (thisnib &5)) goto fail;
169 abs = (thisnib & 0x8) ? 2 : 1;
172 else if (looking_for & (REG | IND|INC|DEC))
174 if (looking_for & SRC)
176 rs = thisnib;
178 else
180 rd = thisnib;
183 else if (looking_for & L_16)
185 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
186 plen = 16;
189 else if(looking_for & ABSJMP)
191 abs =
192 (data[1] << 16)
193 | (data[2] << 8)
194 | (data[3]);
196 else if(looking_for & MEMIND)
198 abs = data[1];
200 else if (looking_for & L_32)
202 int i = len >> 1;
203 abs = (data[i] << 24)
204 | (data[i + 1] << 16)
205 | (data[i + 2] << 8)
206 | (data[i+ 3]);
208 plen =32;
211 else if (looking_for & L_24)
213 int i = len >> 1;
214 abs = (data[i] << 16) | (data[i + 1] << 8)| (data[i+2]);
215 plen =24;
217 else if (looking_for & IGNORE)
221 else if (looking_for & DISPREG)
223 rdisp = thisnib;
225 else if (looking_for & KBIT)
227 switch (thisnib)
229 case 9:
230 abs = 4;
231 break;
232 case 8:
233 abs = 2;
234 break;
235 case 0:
236 abs = 1;
237 break;
238 default:
239 goto fail;
242 else if (looking_for & L_8)
244 plen = 8;
245 abs = data[len >> 1];
247 else if (looking_for & L_3)
249 bit = thisnib & 0x7;
251 else if (looking_for & L_2)
253 plen = 2;
254 abs = thisnib & 0x3;
256 else if (looking_for & MACREG)
258 abs = (thisnib == 3);
260 else if (looking_for == E)
264 int i;
266 for (i = 0; i < q->length; i++)
268 fprintf (stream, "%02x ", data[i]);
270 for (; i < 6; i++)
272 fprintf (stream, " ");
275 fprintf (stream, "%s\t", q->name);
277 /* Gross. Disgusting. */
278 if (strcmp (q->name, "ldm.l") == 0)
280 int count, high;
282 count = (data[1] >> 4) & 0x3;
283 high = data[3] & 0x7;
285 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
286 return q->length;
289 if (strcmp (q->name, "stm.l") == 0)
291 int count, low;
293 count = (data[1] >> 4) & 0x3;
294 low = data[3] & 0x7;
296 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
297 return q->length;
300 /* Fill in the args */
302 op_type *args = q->args.nib;
303 int hadone = 0;
306 while (*args != E)
308 int x = *args;
309 if (hadone)
310 fprintf (stream, ",");
313 if (x & L_3)
315 fprintf (stream, "#0x%x", (unsigned) bit);
317 else if (x & (IMM|KBIT|DBIT))
319 /* Bletch. For shal #2,er0 and friends. */
320 if (*(args+1) & SRC_IN_DST)
321 abs = 2;
323 fprintf (stream, "#0x%x", (unsigned) abs);
325 else if (x & REG)
327 int rn = (x & DST) ? rd : rs;
328 switch (x & SIZE)
330 case L_8:
331 fprintf (stream, "%s", regnames[rn]);
332 break;
333 case L_16:
334 fprintf (stream, "%s", wregnames[rn]);
335 break;
336 case L_P:
337 case L_32:
338 fprintf (stream, "%s", lregnames[rn]);
339 break;
343 else if (x & MACREG)
345 fprintf (stream, "mac%c", abs ? 'l' : 'h');
347 else if (x & INC)
349 fprintf (stream, "@%s+", pregnames[rs]);
351 else if (x & DEC)
353 fprintf (stream, "@-%s", pregnames[rd]);
356 else if (x & IND)
358 int rn = (x & DST) ? rd : rs;
359 fprintf (stream, "@%s", pregnames[rn]);
362 else if (x & ABS8MEM)
364 fprintf (stream, "@0x%x:8", (unsigned) abs);
367 else if (x & (ABS|ABSJMP))
369 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
372 else if (x & MEMIND)
374 fprintf (stream, "@@%d (%x)", abs, abs);
377 else if (x & PCREL)
379 if (x & L_16)
381 abs +=2;
382 fprintf (stream, ".%s%d (%x)", (short) abs > 0 ? "+" : "", (short) abs,
383 addr + (short) abs + 2);
385 else {
386 fprintf (stream, ".%s%d (%x)", (char) abs > 0 ? "+" : "", (char) abs,
387 addr + (char) abs + 2);
390 else if (x & DISP)
392 fprintf (stream, "@(0x%x:%d,%s)", abs,plen, pregnames[rdisp]);
395 else if (x & CCR)
397 fprintf (stream, "ccr");
399 else if (x & EXR)
401 fprintf (stream, "exr");
403 else
404 /* xgettext:c-format */
405 fprintf (stream, _("Hmmmm %x"), x);
406 hadone = 1;
407 args++;
410 return q->length;
414 else
416 /* xgettext:c-format */
417 fprintf (stream, _("Don't understand %x \n"), looking_for);
421 len++;
422 nib++;
425 fail:
426 q++;
429 /* Fell of the end */
430 fprintf (stream, "%02x %02x .word\tH'%x,H'%x",
431 data[0], data[1],
432 data[0], data[1]);
433 return 2;
436 int
437 print_insn_h8300 (addr, info)
438 bfd_vma addr;
439 disassemble_info *info;
441 return bfd_h8_disassemble (addr, info , 0);
444 int
445 print_insn_h8300h (addr, info)
446 bfd_vma addr;
447 disassemble_info *info;
449 return bfd_h8_disassemble (addr, info , 1);
452 int
453 print_insn_h8300s (addr, info)
454 bfd_vma addr;
455 disassemble_info *info;
457 return bfd_h8_disassemble (addr, info , 2);