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