For include/opcode:
[deliverable/binutils-gdb.git] / opcodes / h8300-dis.c
CommitLineData
252b5132 1/* Disassemble h8300 instructions.
d83c6548 2 Copyright 1993, 1994, 1996, 1998, 2000, 2001 Free Software Foundation, Inc.
252b5132
RH
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18#define DEFINE_TABLE
19
0d8dfecf 20#include "sysdep.h"
252b5132
RH
21#define h8_opcodes h8ops
22#include "opcode/h8300.h"
23#include "dis-asm.h"
24#include "opintl.h"
25
d83c6548
AJ
26static void bfd_h8_disassemble_init PARAMS ((void));
27static unsigned int bfd_h8_disassemble
28 PARAMS ((bfd_vma, disassemble_info *, int));
29
252b5132 30/* Run through the opcodes and sort them into order to make them easy
3903e627 31 to disassemble. */
252b5132
RH
32static void
33bfd_h8_disassemble_init ()
34{
35 unsigned int i;
252b5132
RH
36 struct h8_opcode *p;
37
38 for (p = h8_opcodes; p->name; p++)
39 {
40 int n1 = 0;
41 int n2 = 0;
42
43 if ((int) p->data.nib[0] < 16)
5fec0fc5 44 n1 = (int) p->data.nib[0];
252b5132
RH
45 else
46 n1 = 0;
53d388d1 47
252b5132 48 if ((int) p->data.nib[1] < 16)
5fec0fc5 49 n2 = (int) p->data.nib[1];
252b5132
RH
50 else
51 n2 = 0;
52
53 /* Just make sure there are an even number of nibbles in it, and
3903e627 54 that the count is the same as the length. */
252b5132 55 for (i = 0; p->data.nib[i] != E; i++)
53d388d1
JL
56 ;
57
252b5132
RH
58 if (i & 1)
59 abort ();
53d388d1 60
252b5132
RH
61 p->length = i / 2;
62 }
252b5132
RH
63}
64
d83c6548 65static unsigned int
252b5132
RH
66bfd_h8_disassemble (addr, info, mode)
67 bfd_vma addr;
68 disassemble_info *info;
69 int mode;
70{
3903e627 71 /* Find the first entry in the table for this opcode. */
252b5132
RH
72 static CONST char *regnames[] =
73 {
74 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
3903e627
NC
75 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
76 };
252b5132
RH
77 static CONST char *wregnames[] =
78 {
79 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
80 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
3903e627 81 };
252b5132
RH
82 static CONST char *lregnames[] =
83 {
84 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
85 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
3903e627 86 };
252b5132
RH
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;
c07ab2ec 94 struct h8_opcode *q;
252b5132
RH
95 char CONST **pregnames = mode != 0 ? lregnames : wregnames;
96 int status;
97 int l;
5fec0fc5 98 unsigned char data[20];
252b5132
RH
99 void *stream = info->stream;
100 fprintf_ftype fprintf = info->fprintf_func;
101
102 if (!init)
103 {
104 bfd_h8_disassemble_init ();
105 init = 1;
106 }
107
3903e627 108 status = info->read_memory_func (addr, data, 2, info);
5fec0fc5 109 if (status != 0)
252b5132 110 {
3903e627 111 info->memory_error_func (status, addr, info);
252b5132
RH
112 return -1;
113 }
53d388d1 114
3903e627 115 for (l = 2; status == 0 && l < 10; l += 2)
53d388d1 116 status = info->read_memory_func (addr + l, data + l, 2, info);
252b5132 117
3903e627 118 /* Find the exact opcode/arg combo. */
c07ab2ec 119 for (q = h8_opcodes; q->name; q++)
252b5132 120 {
c07ab2ec 121 op_type *nib = q->data.nib;
252b5132
RH
122 unsigned int len = 0;
123
252b5132
RH
124 while (1)
125 {
126 op_type looking_for = *nib;
127 int thisnib = data[len >> 1];
53d388d1 128
252b5132 129 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
53d388d1 130
5fec0fc5 131 if (looking_for < 16 && looking_for >= 0)
252b5132 132 {
5fec0fc5 133 if (looking_for != thisnib)
252b5132
RH
134 goto fail;
135 }
5fec0fc5 136 else
252b5132 137 {
252b5132
RH
138 if ((int) looking_for & (int) B31)
139 {
53d388d1 140 if (!(((int) thisnib & 0x8) != 0))
252b5132 141 goto fail;
53d388d1 142
252b5132
RH
143 looking_for = (op_type) ((int) looking_for & ~(int) B31);
144 }
53d388d1 145
252b5132
RH
146 if ((int) looking_for & (int) B30)
147 {
5fec0fc5 148 if (!(((int) thisnib & 0x8) == 0))
252b5132 149 goto fail;
53d388d1 150
252b5132
RH
151 looking_for = (op_type) ((int) looking_for & ~(int) B30);
152 }
153
154 if (looking_for & DBIT)
155 {
53d388d1
JL
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))
3903e627 160 goto fail;
53d388d1 161
252b5132 162 abs = (thisnib & 0x8) ? 2 : 1;
5fec0fc5
NC
163 }
164 else if (looking_for & (REG | IND | INC | DEC))
252b5132
RH
165 {
166 if (looking_for & SRC)
3903e627 167 rs = thisnib;
252b5132 168 else
3903e627 169 rd = thisnib;
252b5132
RH
170 }
171 else if (looking_for & L_16)
172 {
173 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
174 plen = 16;
252b5132 175 }
3903e627 176 else if (looking_for & ABSJMP)
252b5132 177 {
5fec0fc5 178 abs = (data[1] << 16) | (data[2] << 8) | (data[3]);
252b5132 179 }
3903e627 180 else if (looking_for & MEMIND)
252b5132
RH
181 {
182 abs = data[1];
183 }
184 else if (looking_for & L_32)
185 {
186 int i = len >> 1;
53d388d1 187
252b5132
RH
188 abs = (data[i] << 24)
189 | (data[i + 1] << 16)
5fec0fc5 190 | (data[i + 2] << 8)
53d388d1 191 | (data[i + 3]);
252b5132 192
3903e627 193 plen = 32;
252b5132
RH
194 }
195 else if (looking_for & L_24)
196 {
197 int i = len >> 1;
53d388d1 198
5fec0fc5
NC
199 abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
200 plen = 24;
252b5132
RH
201 }
202 else if (looking_for & IGNORE)
203 {
3903e627 204 ;
252b5132
RH
205 }
206 else if (looking_for & DISPREG)
207 {
208 rdisp = thisnib;
209 }
210 else if (looking_for & KBIT)
211 {
5fec0fc5 212 switch (thisnib)
252b5132
RH
213 {
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;
225 }
226 }
227 else if (looking_for & L_8)
228 {
5fec0fc5 229 plen = 8;
252b5132
RH
230 abs = data[len >> 1];
231 }
232 else if (looking_for & L_3)
233 {
234 bit = thisnib & 0x7;
235 }
236 else if (looking_for & L_2)
237 {
238 plen = 2;
239 abs = thisnib & 0x3;
240 }
241 else if (looking_for & MACREG)
242 {
243 abs = (thisnib == 3);
244 }
245 else if (looking_for == E)
246 {
3903e627 247 int i;
252b5132 248
3903e627
NC
249 for (i = 0; i < q->length; i++)
250 fprintf (stream, "%02x ", data[i]);
53d388d1 251
3903e627
NC
252 for (; i < 6; i++)
253 fprintf (stream, " ");
53d388d1 254
252b5132
RH
255 fprintf (stream, "%s\t", q->name);
256
257 /* Gross. Disgusting. */
258 if (strcmp (q->name, "ldm.l") == 0)
259 {
260 int count, high;
261
262 count = (data[1] >> 4) & 0x3;
263 high = data[3] & 0x7;
264
265 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
266 return q->length;
267 }
268
269 if (strcmp (q->name, "stm.l") == 0)
270 {
271 int count, low;
272
273 count = (data[1] >> 4) & 0x3;
274 low = data[3] & 0x7;
275
276 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
277 return q->length;
278 }
279
3903e627 280 /* Fill in the args. */
252b5132
RH
281 {
282 op_type *args = q->args.nib;
283 int hadone = 0;
284
252b5132
RH
285 while (*args != E)
286 {
287 int x = *args;
53d388d1 288
252b5132
RH
289 if (hadone)
290 fprintf (stream, ",");
291
252b5132
RH
292 if (x & L_3)
293 {
294 fprintf (stream, "#0x%x", (unsigned) bit);
295 }
5fec0fc5 296 else if (x & (IMM | KBIT | DBIT))
252b5132
RH
297 {
298 /* Bletch. For shal #2,er0 and friends. */
5fec0fc5 299 if (*(args + 1) & SRC_IN_DST)
252b5132
RH
300 abs = 2;
301
302 fprintf (stream, "#0x%x", (unsigned) abs);
303 }
304 else if (x & REG)
305 {
306 int rn = (x & DST) ? rd : rs;
53d388d1 307
252b5132
RH
308 switch (x & SIZE)
309 {
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;
252b5132
RH
320 }
321 }
322 else if (x & MACREG)
323 {
324 fprintf (stream, "mac%c", abs ? 'l' : 'h');
325 }
326 else if (x & INC)
327 {
328 fprintf (stream, "@%s+", pregnames[rs]);
329 }
330 else if (x & DEC)
331 {
332 fprintf (stream, "@-%s", pregnames[rd]);
333 }
252b5132
RH
334 else if (x & IND)
335 {
336 int rn = (x & DST) ? rd : rs;
337 fprintf (stream, "@%s", pregnames[rn]);
338 }
252b5132
RH
339 else if (x & ABS8MEM)
340 {
341 fprintf (stream, "@0x%x:8", (unsigned) abs);
342 }
5fec0fc5 343 else if (x & (ABS | ABSJMP))
252b5132
RH
344 {
345 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
346 }
252b5132
RH
347 else if (x & MEMIND)
348 {
349 fprintf (stream, "@@%d (%x)", abs, abs);
350 }
252b5132
RH
351 else if (x & PCREL)
352 {
5fec0fc5 353 if (x & L_16)
252b5132 354 {
3903e627 355 abs += 2;
5fec0fc5 356 fprintf (stream,
53d388d1
JL
357 ".%s%d (%x)",
358 (short) abs > 0 ? "+" : "",
5fec0fc5 359 (short) abs, addr + (short) abs + 2);
252b5132 360 }
3903e627
NC
361 else
362 {
5fec0fc5 363 fprintf (stream,
53d388d1
JL
364 ".%s%d (%x)",
365 (char) abs > 0 ? "+" : "",
5fec0fc5 366 (char) abs, addr + (char) abs + 2);
3903e627 367 }
252b5132
RH
368 }
369 else if (x & DISP)
370 {
5fec0fc5 371 fprintf (stream, "@(0x%x:%d,%s)",
53d388d1 372 abs, plen, pregnames[rdisp]);
252b5132 373 }
252b5132
RH
374 else if (x & CCR)
375 {
376 fprintf (stream, "ccr");
377 }
378 else if (x & EXR)
379 {
380 fprintf (stream, "exr");
381 }
382 else
383 /* xgettext:c-format */
384 fprintf (stream, _("Hmmmm %x"), x);
53d388d1 385
252b5132
RH
386 hadone = 1;
387 args++;
388 }
389 }
53d388d1 390
252b5132
RH
391 return q->length;
392 }
252b5132 393 else
5fec0fc5
NC
394 /* xgettext:c-format */
395 fprintf (stream, _("Don't understand %x \n"), looking_for);
252b5132 396 }
53d388d1 397
252b5132
RH
398 len++;
399 nib++;
400 }
53d388d1 401
252b5132 402 fail:
c07ab2ec 403 ;
252b5132
RH
404 }
405
5fec0fc5 406 /* Fell off the end. */
252b5132
RH
407 fprintf (stream, "%02x %02x .word\tH'%x,H'%x",
408 data[0], data[1],
409 data[0], data[1]);
410 return 2;
411}
412
5fec0fc5 413int
252b5132 414print_insn_h8300 (addr, info)
5fec0fc5 415 bfd_vma addr;
3903e627 416 disassemble_info *info;
252b5132 417{
5fec0fc5 418 return bfd_h8_disassemble (addr, info, 0);
252b5132
RH
419}
420
5fec0fc5 421int
252b5132 422print_insn_h8300h (addr, info)
3903e627
NC
423 bfd_vma addr;
424 disassemble_info *info;
252b5132 425{
5fec0fc5 426 return bfd_h8_disassemble (addr, info, 1);
252b5132
RH
427}
428
5fec0fc5 429int
252b5132 430print_insn_h8300s (addr, info)
3903e627
NC
431 bfd_vma addr;
432 disassemble_info *info;
252b5132 433{
5fec0fc5 434 return bfd_h8_disassemble (addr, info, 2);
252b5132 435}
This page took 0.186444 seconds and 4 git commands to generate.