Fix an overflow checking bug uncovered when a 32 bit target is compiled
[binutils.git] / opcodes / h8300-dis.c
blob57753a98d548d6386ea67eeb5983603500be977f
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 #define h8_opcodes h8ops
21 #include "opcode/h8300.h"
22 #include "dis-asm.h"
23 #include "opintl.h"
26 /* Run through the opcodes and sort them into order to make them easy
27 to disassemble
29 static void
30 bfd_h8_disassemble_init ()
32 unsigned int i;
35 struct h8_opcode *p;
37 for (p = h8_opcodes; p->name; p++)
39 int n1 = 0;
40 int n2 = 0;
42 if ((int) p->data.nib[0] < 16)
44 n1 = (int) p->data.nib[0];
46 else
47 n1 = 0;
48 if ((int) p->data.nib[1] < 16)
50 n2 = (int) p->data.nib[1];
52 else
53 n2 = 0;
55 /* Just make sure there are an even number of nibbles in it, and
56 that the count is the same s the length */
57 for (i = 0; p->data.nib[i] != E; i++)
58 /*EMPTY*/ ;
59 if (i & 1)
60 abort ();
61 p->length = i / 2;
67 unsigned int
68 bfd_h8_disassemble (addr, info, mode)
69 bfd_vma addr;
70 disassemble_info *info;
71 int mode;
73 /* Find the first entry in the table for this opcode */
74 static CONST char *regnames[] =
76 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
77 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"};
79 static CONST char *wregnames[] =
81 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
82 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
85 static CONST char *lregnames[] =
87 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
88 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
92 int rs = 0;
93 int rd = 0;
94 int rdisp = 0;
95 int abs = 0;
96 int bit = 0;
97 int plen = 0;
98 static boolean init = 0;
99 struct h8_opcode *q = h8_opcodes;
100 char CONST **pregnames = mode != 0 ? lregnames : wregnames;
101 int status;
102 int l;
104 unsigned char data[20];
105 void *stream = info->stream;
106 fprintf_ftype fprintf = info->fprintf_func;
108 if (!init)
110 bfd_h8_disassemble_init ();
111 init = 1;
114 status = info->read_memory_func(addr, data, 2, info);
115 if (status != 0)
117 info->memory_error_func(status, addr, info);
118 return -1;
120 for (l = 2; status == 0 && l < 10; l+=2)
122 status = info->read_memory_func(addr+l, data+l, 2, info);
127 /* Find the exact opcode/arg combo */
128 while (q->name)
130 op_type *nib;
131 unsigned int len = 0;
133 nib = q->data.nib;
135 while (1)
137 op_type looking_for = *nib;
138 int thisnib = data[len >> 1];
140 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
142 if (looking_for < 16 && looking_for >=0)
145 if (looking_for != thisnib)
146 goto fail;
149 else
152 if ((int) looking_for & (int) B31)
154 if (! (((int) thisnib & 0x8) != 0))
155 goto fail;
156 looking_for = (op_type) ((int) looking_for & ~(int) B31);
158 if ((int) looking_for & (int) B30)
160 if (!(((int) thisnib & 0x8) == 0))
161 goto fail;
162 looking_for = (op_type) ((int) looking_for & ~(int) B30);
165 if (looking_for & DBIT)
167 if ((looking_for & 5) != (thisnib &5)) goto fail;
168 abs = (thisnib & 0x8) ? 2 : 1;
171 else if (looking_for & (REG | IND|INC|DEC))
173 if (looking_for & SRC)
175 rs = thisnib;
177 else
179 rd = thisnib;
182 else if (looking_for & L_16)
184 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
185 plen = 16;
188 else if(looking_for & ABSJMP)
190 abs =
191 (data[1] << 16)
192 | (data[2] << 8)
193 | (data[3]);
195 else if(looking_for & MEMIND)
197 abs = data[1];
199 else if (looking_for & L_32)
201 int i = len >> 1;
202 abs = (data[i] << 24)
203 | (data[i + 1] << 16)
204 | (data[i + 2] << 8)
205 | (data[i+ 3]);
207 plen =32;
210 else if (looking_for & L_24)
212 int i = len >> 1;
213 abs = (data[i] << 16) | (data[i + 1] << 8)| (data[i+2]);
214 plen =24;
216 else if (looking_for & IGNORE)
220 else if (looking_for & DISPREG)
222 rdisp = thisnib;
224 else if (looking_for & KBIT)
226 switch (thisnib)
228 case 9:
229 abs = 4;
230 break;
231 case 8:
232 abs = 2;
233 break;
234 case 0:
235 abs = 1;
236 break;
237 default:
238 goto fail;
241 else if (looking_for & L_8)
243 plen = 8;
244 abs = data[len >> 1];
246 else if (looking_for & L_3)
248 bit = thisnib & 0x7;
250 else if (looking_for & L_2)
252 plen = 2;
253 abs = thisnib & 0x3;
255 else if (looking_for & MACREG)
257 abs = (thisnib == 3);
259 else if (looking_for == E)
263 int i;
265 for (i = 0; i < q->length; i++)
267 fprintf (stream, "%02x ", data[i]);
269 for (; i < 6; i++)
271 fprintf (stream, " ");
274 fprintf (stream, "%s\t", q->name);
276 /* Gross. Disgusting. */
277 if (strcmp (q->name, "ldm.l") == 0)
279 int count, high;
281 count = (data[1] >> 4) & 0x3;
282 high = data[3] & 0x7;
284 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
285 return q->length;
288 if (strcmp (q->name, "stm.l") == 0)
290 int count, low;
292 count = (data[1] >> 4) & 0x3;
293 low = data[3] & 0x7;
295 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
296 return q->length;
299 /* Fill in the args */
301 op_type *args = q->args.nib;
302 int hadone = 0;
305 while (*args != E)
307 int x = *args;
308 if (hadone)
309 fprintf (stream, ",");
312 if (x & L_3)
314 fprintf (stream, "#0x%x", (unsigned) bit);
316 else if (x & (IMM|KBIT|DBIT))
318 /* Bletch. For shal #2,er0 and friends. */
319 if (*(args+1) & SRC_IN_DST)
320 abs = 2;
322 fprintf (stream, "#0x%x", (unsigned) abs);
324 else if (x & REG)
326 int rn = (x & DST) ? rd : rs;
327 switch (x & SIZE)
329 case L_8:
330 fprintf (stream, "%s", regnames[rn]);
331 break;
332 case L_16:
333 fprintf (stream, "%s", wregnames[rn]);
334 break;
335 case L_P:
336 case L_32:
337 fprintf (stream, "%s", lregnames[rn]);
338 break;
342 else if (x & MACREG)
344 fprintf (stream, "mac%c", abs ? 'l' : 'h');
346 else if (x & INC)
348 fprintf (stream, "@%s+", pregnames[rs]);
350 else if (x & DEC)
352 fprintf (stream, "@-%s", pregnames[rd]);
355 else if (x & IND)
357 int rn = (x & DST) ? rd : rs;
358 fprintf (stream, "@%s", pregnames[rn]);
361 else if (x & ABS8MEM)
363 fprintf (stream, "@0x%x:8", (unsigned) abs);
366 else if (x & (ABS|ABSJMP))
368 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
371 else if (x & MEMIND)
373 fprintf (stream, "@@%d (%x)", abs, abs);
376 else if (x & PCREL)
378 if (x & L_16)
380 abs +=2;
381 fprintf (stream, ".%s%d (%x)", (short) abs > 0 ? "+" : "", (short) abs,
382 addr + (short) abs + 2);
384 else {
385 fprintf (stream, ".%s%d (%x)", (char) abs > 0 ? "+" : "", (char) abs,
386 addr + (char) abs + 2);
389 else if (x & DISP)
391 fprintf (stream, "@(0x%x:%d,%s)", abs,plen, pregnames[rdisp]);
394 else if (x & CCR)
396 fprintf (stream, "ccr");
398 else if (x & EXR)
400 fprintf (stream, "exr");
402 else
403 /* xgettext:c-format */
404 fprintf (stream, _("Hmmmm %x"), x);
405 hadone = 1;
406 args++;
409 return q->length;
413 else
415 /* xgettext:c-format */
416 fprintf (stream, _("Don't understand %x \n"), looking_for);
420 len++;
421 nib++;
424 fail:
425 q++;
428 /* Fell of the end */
429 fprintf (stream, "%02x %02x .word\tH'%x,H'%x",
430 data[0], data[1],
431 data[0], data[1]);
432 return 2;
435 int
436 print_insn_h8300 (addr, info)
437 bfd_vma addr;
438 disassemble_info *info;
440 return bfd_h8_disassemble (addr, info , 0);
443 int
444 print_insn_h8300h (addr, info)
445 bfd_vma addr;
446 disassemble_info *info;
448 return bfd_h8_disassemble (addr, info , 1);
451 int
452 print_insn_h8300s (addr, info)
453 bfd_vma addr;
454 disassemble_info *info;
456 return bfd_h8_disassemble (addr, info , 2);