Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Disassemble h8300 instructions. |
56da5fed AM |
2 | Copyright 1993, 1994, 1996, 1998, 2000, 2001, 2002 |
3 | Free Software Foundation, Inc. | |
252b5132 RH |
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 | ||
0d8dfecf | 21 | #include "sysdep.h" |
252b5132 RH |
22 | #define h8_opcodes h8ops |
23 | #include "opcode/h8300.h" | |
24 | #include "dis-asm.h" | |
25 | #include "opintl.h" | |
a3e64b75 KD |
26 | #include "libiberty.h" |
27 | ||
28 | struct h8_instruction | |
29 | { | |
30 | int length; | |
31 | const struct h8_opcode *opcode; | |
32 | }; | |
33 | ||
34 | struct h8_instruction *h8_instructions; | |
252b5132 | 35 | |
d83c6548 AJ |
36 | static void bfd_h8_disassemble_init PARAMS ((void)); |
37 | static unsigned int bfd_h8_disassemble | |
38 | PARAMS ((bfd_vma, disassemble_info *, int)); | |
39 | ||
252b5132 | 40 | /* Run through the opcodes and sort them into order to make them easy |
3903e627 | 41 | to disassemble. */ |
252b5132 RH |
42 | static void |
43 | bfd_h8_disassemble_init () | |
44 | { | |
45 | unsigned int i; | |
a3e64b75 KD |
46 | unsigned int nopcodes; |
47 | const struct h8_opcode *p; | |
48 | struct h8_instruction *pi; | |
252b5132 | 49 | |
a3e64b75 | 50 | nopcodes = sizeof (h8_opcodes) / sizeof (struct h8_opcode); |
b34976b6 | 51 | |
a3e64b75 KD |
52 | h8_instructions = (struct h8_instruction *) |
53 | xmalloc (nopcodes * sizeof (struct h8_instruction)); | |
54 | ||
55 | for (p = h8_opcodes, pi = h8_instructions; p->name; p++, pi++) | |
252b5132 RH |
56 | { |
57 | int n1 = 0; | |
58 | int n2 = 0; | |
59 | ||
60 | if ((int) p->data.nib[0] < 16) | |
5fec0fc5 | 61 | n1 = (int) p->data.nib[0]; |
252b5132 RH |
62 | else |
63 | n1 = 0; | |
53d388d1 | 64 | |
252b5132 | 65 | if ((int) p->data.nib[1] < 16) |
5fec0fc5 | 66 | n2 = (int) p->data.nib[1]; |
252b5132 RH |
67 | else |
68 | n2 = 0; | |
69 | ||
70 | /* Just make sure there are an even number of nibbles in it, and | |
3903e627 | 71 | that the count is the same as the length. */ |
252b5132 | 72 | for (i = 0; p->data.nib[i] != E; i++) |
53d388d1 JL |
73 | ; |
74 | ||
252b5132 RH |
75 | if (i & 1) |
76 | abort (); | |
53d388d1 | 77 | |
a3e64b75 KD |
78 | pi->length = i / 2; |
79 | pi->opcode = p; | |
252b5132 | 80 | } |
a3e64b75 KD |
81 | |
82 | /* Add entry for the NULL vector terminator. */ | |
83 | pi->length = 0; | |
84 | pi->opcode = p; | |
252b5132 RH |
85 | } |
86 | ||
d83c6548 | 87 | static unsigned int |
252b5132 RH |
88 | bfd_h8_disassemble (addr, info, mode) |
89 | bfd_vma addr; | |
90 | disassemble_info *info; | |
91 | int mode; | |
92 | { | |
3903e627 | 93 | /* Find the first entry in the table for this opcode. */ |
56da5fed | 94 | static const char *regnames[] = |
252b5132 RH |
95 | { |
96 | "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h", | |
3903e627 NC |
97 | "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l" |
98 | }; | |
56da5fed | 99 | static const char *wregnames[] = |
252b5132 RH |
100 | { |
101 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", | |
102 | "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" | |
3903e627 | 103 | }; |
56da5fed | 104 | static const char *lregnames[] = |
252b5132 RH |
105 | { |
106 | "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7", | |
107 | "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" | |
3903e627 | 108 | }; |
252b5132 RH |
109 | int rs = 0; |
110 | int rd = 0; | |
111 | int rdisp = 0; | |
112 | int abs = 0; | |
113 | int bit = 0; | |
114 | int plen = 0; | |
b34976b6 | 115 | static bfd_boolean init = 0; |
a3e64b75 | 116 | const struct h8_instruction *qi; |
56da5fed | 117 | char const **pregnames = mode != 0 ? lregnames : wregnames; |
252b5132 RH |
118 | int status; |
119 | int l; | |
5fec0fc5 | 120 | unsigned char data[20]; |
252b5132 RH |
121 | void *stream = info->stream; |
122 | fprintf_ftype fprintf = info->fprintf_func; | |
123 | ||
124 | if (!init) | |
125 | { | |
126 | bfd_h8_disassemble_init (); | |
127 | init = 1; | |
128 | } | |
129 | ||
3903e627 | 130 | status = info->read_memory_func (addr, data, 2, info); |
5fec0fc5 | 131 | if (status != 0) |
252b5132 | 132 | { |
3903e627 | 133 | info->memory_error_func (status, addr, info); |
252b5132 RH |
134 | return -1; |
135 | } | |
53d388d1 | 136 | |
3903e627 | 137 | for (l = 2; status == 0 && l < 10; l += 2) |
53d388d1 | 138 | status = info->read_memory_func (addr + l, data + l, 2, info); |
252b5132 | 139 | |
3903e627 | 140 | /* Find the exact opcode/arg combo. */ |
a3e64b75 | 141 | for (qi = h8_instructions; qi->opcode->name; qi++) |
252b5132 | 142 | { |
a3e64b75 | 143 | const struct h8_opcode *q = qi->opcode; |
c07ab2ec | 144 | op_type *nib = q->data.nib; |
252b5132 RH |
145 | unsigned int len = 0; |
146 | ||
252b5132 RH |
147 | while (1) |
148 | { | |
149 | op_type looking_for = *nib; | |
150 | int thisnib = data[len >> 1]; | |
53d388d1 | 151 | |
252b5132 | 152 | thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf); |
53d388d1 | 153 | |
5fec0fc5 | 154 | if (looking_for < 16 && looking_for >= 0) |
252b5132 | 155 | { |
5fec0fc5 | 156 | if (looking_for != thisnib) |
252b5132 RH |
157 | goto fail; |
158 | } | |
5fec0fc5 | 159 | else |
252b5132 | 160 | { |
252b5132 RH |
161 | if ((int) looking_for & (int) B31) |
162 | { | |
53d388d1 | 163 | if (!(((int) thisnib & 0x8) != 0)) |
252b5132 | 164 | goto fail; |
53d388d1 | 165 | |
252b5132 RH |
166 | looking_for = (op_type) ((int) looking_for & ~(int) B31); |
167 | } | |
53d388d1 | 168 | |
252b5132 RH |
169 | if ((int) looking_for & (int) B30) |
170 | { | |
5fec0fc5 | 171 | if (!(((int) thisnib & 0x8) == 0)) |
252b5132 | 172 | goto fail; |
53d388d1 | 173 | |
252b5132 RH |
174 | looking_for = (op_type) ((int) looking_for & ~(int) B30); |
175 | } | |
176 | ||
177 | if (looking_for & DBIT) | |
178 | { | |
53d388d1 JL |
179 | /* Exclude adds/subs by looking at bit 0 and 2, and |
180 | make sure the operand size, either w or l, | |
181 | matches by looking at bit 1. */ | |
182 | if ((looking_for & 7) != (thisnib & 7)) | |
3903e627 | 183 | goto fail; |
53d388d1 | 184 | |
252b5132 | 185 | abs = (thisnib & 0x8) ? 2 : 1; |
5fec0fc5 NC |
186 | } |
187 | else if (looking_for & (REG | IND | INC | DEC)) | |
252b5132 RH |
188 | { |
189 | if (looking_for & SRC) | |
3903e627 | 190 | rs = thisnib; |
252b5132 | 191 | else |
3903e627 | 192 | rd = thisnib; |
252b5132 RH |
193 | } |
194 | else if (looking_for & L_16) | |
195 | { | |
196 | abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1]; | |
197 | plen = 16; | |
252b5132 | 198 | } |
3903e627 | 199 | else if (looking_for & ABSJMP) |
252b5132 | 200 | { |
5fec0fc5 | 201 | abs = (data[1] << 16) | (data[2] << 8) | (data[3]); |
252b5132 | 202 | } |
3903e627 | 203 | else if (looking_for & MEMIND) |
252b5132 RH |
204 | { |
205 | abs = data[1]; | |
206 | } | |
207 | else if (looking_for & L_32) | |
208 | { | |
209 | int i = len >> 1; | |
53d388d1 | 210 | |
252b5132 RH |
211 | abs = (data[i] << 24) |
212 | | (data[i + 1] << 16) | |
5fec0fc5 | 213 | | (data[i + 2] << 8) |
53d388d1 | 214 | | (data[i + 3]); |
252b5132 | 215 | |
3903e627 | 216 | plen = 32; |
252b5132 RH |
217 | } |
218 | else if (looking_for & L_24) | |
219 | { | |
220 | int i = len >> 1; | |
53d388d1 | 221 | |
5fec0fc5 NC |
222 | abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]); |
223 | plen = 24; | |
252b5132 RH |
224 | } |
225 | else if (looking_for & IGNORE) | |
226 | { | |
3903e627 | 227 | ; |
252b5132 RH |
228 | } |
229 | else if (looking_for & DISPREG) | |
230 | { | |
231 | rdisp = thisnib; | |
232 | } | |
233 | else if (looking_for & KBIT) | |
234 | { | |
5fec0fc5 | 235 | switch (thisnib) |
252b5132 RH |
236 | { |
237 | case 9: | |
238 | abs = 4; | |
239 | break; | |
240 | case 8: | |
241 | abs = 2; | |
242 | break; | |
243 | case 0: | |
244 | abs = 1; | |
245 | break; | |
246 | default: | |
247 | goto fail; | |
248 | } | |
249 | } | |
250 | else if (looking_for & L_8) | |
251 | { | |
5fec0fc5 | 252 | plen = 8; |
252b5132 RH |
253 | abs = data[len >> 1]; |
254 | } | |
255 | else if (looking_for & L_3) | |
256 | { | |
257 | bit = thisnib & 0x7; | |
258 | } | |
259 | else if (looking_for & L_2) | |
260 | { | |
261 | plen = 2; | |
262 | abs = thisnib & 0x3; | |
263 | } | |
264 | else if (looking_for & MACREG) | |
265 | { | |
266 | abs = (thisnib == 3); | |
267 | } | |
268 | else if (looking_for == E) | |
269 | { | |
3903e627 | 270 | int i; |
252b5132 | 271 | |
a3e64b75 | 272 | for (i = 0; i < qi->length; i++) |
3903e627 | 273 | fprintf (stream, "%02x ", data[i]); |
53d388d1 | 274 | |
3903e627 NC |
275 | for (; i < 6; i++) |
276 | fprintf (stream, " "); | |
53d388d1 | 277 | |
252b5132 RH |
278 | fprintf (stream, "%s\t", q->name); |
279 | ||
280 | /* Gross. Disgusting. */ | |
281 | if (strcmp (q->name, "ldm.l") == 0) | |
282 | { | |
283 | int count, high; | |
284 | ||
285 | count = (data[1] >> 4) & 0x3; | |
286 | high = data[3] & 0x7; | |
287 | ||
288 | fprintf (stream, "@sp+,er%d-er%d", high - count, high); | |
a3e64b75 | 289 | return qi->length; |
252b5132 RH |
290 | } |
291 | ||
292 | if (strcmp (q->name, "stm.l") == 0) | |
293 | { | |
294 | int count, low; | |
295 | ||
296 | count = (data[1] >> 4) & 0x3; | |
297 | low = data[3] & 0x7; | |
298 | ||
299 | fprintf (stream, "er%d-er%d,@-sp", low, low + count); | |
a3e64b75 | 300 | return qi->length; |
252b5132 RH |
301 | } |
302 | ||
3903e627 | 303 | /* Fill in the args. */ |
252b5132 RH |
304 | { |
305 | op_type *args = q->args.nib; | |
306 | int hadone = 0; | |
307 | ||
252b5132 RH |
308 | while (*args != E) |
309 | { | |
310 | int x = *args; | |
53d388d1 | 311 | |
252b5132 RH |
312 | if (hadone) |
313 | fprintf (stream, ","); | |
314 | ||
252b5132 RH |
315 | if (x & L_3) |
316 | { | |
317 | fprintf (stream, "#0x%x", (unsigned) bit); | |
318 | } | |
5fec0fc5 | 319 | else if (x & (IMM | KBIT | DBIT)) |
252b5132 RH |
320 | { |
321 | /* Bletch. For shal #2,er0 and friends. */ | |
5fec0fc5 | 322 | if (*(args + 1) & SRC_IN_DST) |
252b5132 RH |
323 | abs = 2; |
324 | ||
325 | fprintf (stream, "#0x%x", (unsigned) abs); | |
326 | } | |
327 | else if (x & REG) | |
328 | { | |
329 | int rn = (x & DST) ? rd : rs; | |
53d388d1 | 330 | |
252b5132 RH |
331 | switch (x & SIZE) |
332 | { | |
333 | case L_8: | |
334 | fprintf (stream, "%s", regnames[rn]); | |
335 | break; | |
336 | case L_16: | |
337 | fprintf (stream, "%s", wregnames[rn]); | |
338 | break; | |
339 | case L_P: | |
340 | case L_32: | |
341 | fprintf (stream, "%s", lregnames[rn]); | |
342 | break; | |
252b5132 RH |
343 | } |
344 | } | |
345 | else if (x & MACREG) | |
346 | { | |
347 | fprintf (stream, "mac%c", abs ? 'l' : 'h'); | |
348 | } | |
349 | else if (x & INC) | |
350 | { | |
351 | fprintf (stream, "@%s+", pregnames[rs]); | |
352 | } | |
353 | else if (x & DEC) | |
354 | { | |
355 | fprintf (stream, "@-%s", pregnames[rd]); | |
356 | } | |
252b5132 RH |
357 | else if (x & IND) |
358 | { | |
359 | int rn = (x & DST) ? rd : rs; | |
360 | fprintf (stream, "@%s", pregnames[rn]); | |
361 | } | |
252b5132 RH |
362 | else if (x & ABS8MEM) |
363 | { | |
364 | fprintf (stream, "@0x%x:8", (unsigned) abs); | |
365 | } | |
5fec0fc5 | 366 | else if (x & (ABS | ABSJMP)) |
252b5132 RH |
367 | { |
368 | fprintf (stream, "@0x%x:%d", (unsigned) abs, plen); | |
369 | } | |
252b5132 RH |
370 | else if (x & MEMIND) |
371 | { | |
372 | fprintf (stream, "@@%d (%x)", abs, abs); | |
373 | } | |
252b5132 RH |
374 | else if (x & PCREL) |
375 | { | |
5fec0fc5 | 376 | if (x & L_16) |
252b5132 | 377 | { |
3903e627 | 378 | abs += 2; |
5fec0fc5 | 379 | fprintf (stream, |
53d388d1 JL |
380 | ".%s%d (%x)", |
381 | (short) abs > 0 ? "+" : "", | |
5fec0fc5 | 382 | (short) abs, addr + (short) abs + 2); |
252b5132 | 383 | } |
3903e627 NC |
384 | else |
385 | { | |
5fec0fc5 | 386 | fprintf (stream, |
53d388d1 JL |
387 | ".%s%d (%x)", |
388 | (char) abs > 0 ? "+" : "", | |
5fec0fc5 | 389 | (char) abs, addr + (char) abs + 2); |
3903e627 | 390 | } |
252b5132 RH |
391 | } |
392 | else if (x & DISP) | |
393 | { | |
5fec0fc5 | 394 | fprintf (stream, "@(0x%x:%d,%s)", |
53d388d1 | 395 | abs, plen, pregnames[rdisp]); |
252b5132 | 396 | } |
252b5132 RH |
397 | else if (x & CCR) |
398 | { | |
399 | fprintf (stream, "ccr"); | |
400 | } | |
401 | else if (x & EXR) | |
402 | { | |
403 | fprintf (stream, "exr"); | |
404 | } | |
405 | else | |
406 | /* xgettext:c-format */ | |
407 | fprintf (stream, _("Hmmmm %x"), x); | |
53d388d1 | 408 | |
252b5132 RH |
409 | hadone = 1; |
410 | args++; | |
411 | } | |
412 | } | |
53d388d1 | 413 | |
a3e64b75 | 414 | return qi->length; |
252b5132 | 415 | } |
252b5132 | 416 | else |
5fec0fc5 NC |
417 | /* xgettext:c-format */ |
418 | fprintf (stream, _("Don't understand %x \n"), looking_for); | |
252b5132 | 419 | } |
53d388d1 | 420 | |
252b5132 RH |
421 | len++; |
422 | nib++; | |
423 | } | |
53d388d1 | 424 | |
252b5132 | 425 | fail: |
c07ab2ec | 426 | ; |
252b5132 RH |
427 | } |
428 | ||
5fec0fc5 | 429 | /* Fell off the end. */ |
252b5132 RH |
430 | fprintf (stream, "%02x %02x .word\tH'%x,H'%x", |
431 | data[0], data[1], | |
432 | data[0], data[1]); | |
433 | return 2; | |
434 | } | |
435 | ||
5fec0fc5 | 436 | int |
252b5132 | 437 | print_insn_h8300 (addr, info) |
5fec0fc5 | 438 | bfd_vma addr; |
3903e627 | 439 | disassemble_info *info; |
252b5132 | 440 | { |
5fec0fc5 | 441 | return bfd_h8_disassemble (addr, info, 0); |
252b5132 RH |
442 | } |
443 | ||
5fec0fc5 | 444 | int |
252b5132 | 445 | print_insn_h8300h (addr, info) |
3903e627 NC |
446 | bfd_vma addr; |
447 | disassemble_info *info; | |
252b5132 | 448 | { |
5fec0fc5 | 449 | return bfd_h8_disassemble (addr, info, 1); |
252b5132 RH |
450 | } |
451 | ||
5fec0fc5 | 452 | int |
252b5132 | 453 | print_insn_h8300s (addr, info) |
3903e627 NC |
454 | bfd_vma addr; |
455 | disassemble_info *info; | |
252b5132 | 456 | { |
5fec0fc5 | 457 | return bfd_h8_disassemble (addr, info, 2); |
252b5132 | 458 | } |