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" | |
26 | ||
d83c6548 AJ |
27 | static void bfd_h8_disassemble_init PARAMS ((void)); |
28 | static unsigned int bfd_h8_disassemble | |
29 | PARAMS ((bfd_vma, disassemble_info *, int)); | |
30 | ||
252b5132 | 31 | /* Run through the opcodes and sort them into order to make them easy |
3903e627 | 32 | to disassemble. */ |
252b5132 RH |
33 | static void |
34 | bfd_h8_disassemble_init () | |
35 | { | |
36 | unsigned int i; | |
252b5132 RH |
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) | |
5fec0fc5 | 45 | n1 = (int) p->data.nib[0]; |
252b5132 RH |
46 | else |
47 | n1 = 0; | |
53d388d1 | 48 | |
252b5132 | 49 | if ((int) p->data.nib[1] < 16) |
5fec0fc5 | 50 | n2 = (int) p->data.nib[1]; |
252b5132 RH |
51 | else |
52 | n2 = 0; | |
53 | ||
54 | /* Just make sure there are an even number of nibbles in it, and | |
3903e627 | 55 | that the count is the same as the length. */ |
252b5132 | 56 | for (i = 0; p->data.nib[i] != E; i++) |
53d388d1 JL |
57 | ; |
58 | ||
252b5132 RH |
59 | if (i & 1) |
60 | abort (); | |
53d388d1 | 61 | |
252b5132 RH |
62 | p->length = i / 2; |
63 | } | |
252b5132 RH |
64 | } |
65 | ||
d83c6548 | 66 | static unsigned int |
252b5132 RH |
67 | bfd_h8_disassemble (addr, info, mode) |
68 | bfd_vma addr; | |
69 | disassemble_info *info; | |
70 | int mode; | |
71 | { | |
3903e627 | 72 | /* Find the first entry in the table for this opcode. */ |
56da5fed | 73 | static const char *regnames[] = |
252b5132 RH |
74 | { |
75 | "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h", | |
3903e627 NC |
76 | "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l" |
77 | }; | |
56da5fed | 78 | static const char *wregnames[] = |
252b5132 RH |
79 | { |
80 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", | |
81 | "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" | |
3903e627 | 82 | }; |
56da5fed | 83 | static const char *lregnames[] = |
252b5132 RH |
84 | { |
85 | "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7", | |
86 | "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" | |
3903e627 | 87 | }; |
252b5132 RH |
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; | |
c07ab2ec | 95 | struct h8_opcode *q; |
56da5fed | 96 | char const **pregnames = mode != 0 ? lregnames : wregnames; |
252b5132 RH |
97 | int status; |
98 | int l; | |
5fec0fc5 | 99 | unsigned char data[20]; |
252b5132 RH |
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 | ||
3903e627 | 109 | status = info->read_memory_func (addr, data, 2, info); |
5fec0fc5 | 110 | if (status != 0) |
252b5132 | 111 | { |
3903e627 | 112 | info->memory_error_func (status, addr, info); |
252b5132 RH |
113 | return -1; |
114 | } | |
53d388d1 | 115 | |
3903e627 | 116 | for (l = 2; status == 0 && l < 10; l += 2) |
53d388d1 | 117 | status = info->read_memory_func (addr + l, data + l, 2, info); |
252b5132 | 118 | |
3903e627 | 119 | /* Find the exact opcode/arg combo. */ |
c07ab2ec | 120 | for (q = h8_opcodes; q->name; q++) |
252b5132 | 121 | { |
c07ab2ec | 122 | op_type *nib = q->data.nib; |
252b5132 RH |
123 | unsigned int len = 0; |
124 | ||
252b5132 RH |
125 | while (1) |
126 | { | |
127 | op_type looking_for = *nib; | |
128 | int thisnib = data[len >> 1]; | |
53d388d1 | 129 | |
252b5132 | 130 | thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf); |
53d388d1 | 131 | |
5fec0fc5 | 132 | if (looking_for < 16 && looking_for >= 0) |
252b5132 | 133 | { |
5fec0fc5 | 134 | if (looking_for != thisnib) |
252b5132 RH |
135 | goto fail; |
136 | } | |
5fec0fc5 | 137 | else |
252b5132 | 138 | { |
252b5132 RH |
139 | if ((int) looking_for & (int) B31) |
140 | { | |
53d388d1 | 141 | if (!(((int) thisnib & 0x8) != 0)) |
252b5132 | 142 | goto fail; |
53d388d1 | 143 | |
252b5132 RH |
144 | looking_for = (op_type) ((int) looking_for & ~(int) B31); |
145 | } | |
53d388d1 | 146 | |
252b5132 RH |
147 | if ((int) looking_for & (int) B30) |
148 | { | |
5fec0fc5 | 149 | if (!(((int) thisnib & 0x8) == 0)) |
252b5132 | 150 | goto fail; |
53d388d1 | 151 | |
252b5132 RH |
152 | looking_for = (op_type) ((int) looking_for & ~(int) B30); |
153 | } | |
154 | ||
155 | if (looking_for & DBIT) | |
156 | { | |
53d388d1 JL |
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)) | |
3903e627 | 161 | goto fail; |
53d388d1 | 162 | |
252b5132 | 163 | abs = (thisnib & 0x8) ? 2 : 1; |
5fec0fc5 NC |
164 | } |
165 | else if (looking_for & (REG | IND | INC | DEC)) | |
252b5132 RH |
166 | { |
167 | if (looking_for & SRC) | |
3903e627 | 168 | rs = thisnib; |
252b5132 | 169 | else |
3903e627 | 170 | rd = thisnib; |
252b5132 RH |
171 | } |
172 | else if (looking_for & L_16) | |
173 | { | |
174 | abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1]; | |
175 | plen = 16; | |
252b5132 | 176 | } |
3903e627 | 177 | else if (looking_for & ABSJMP) |
252b5132 | 178 | { |
5fec0fc5 | 179 | abs = (data[1] << 16) | (data[2] << 8) | (data[3]); |
252b5132 | 180 | } |
3903e627 | 181 | else if (looking_for & MEMIND) |
252b5132 RH |
182 | { |
183 | abs = data[1]; | |
184 | } | |
185 | else if (looking_for & L_32) | |
186 | { | |
187 | int i = len >> 1; | |
53d388d1 | 188 | |
252b5132 RH |
189 | abs = (data[i] << 24) |
190 | | (data[i + 1] << 16) | |
5fec0fc5 | 191 | | (data[i + 2] << 8) |
53d388d1 | 192 | | (data[i + 3]); |
252b5132 | 193 | |
3903e627 | 194 | plen = 32; |
252b5132 RH |
195 | } |
196 | else if (looking_for & L_24) | |
197 | { | |
198 | int i = len >> 1; | |
53d388d1 | 199 | |
5fec0fc5 NC |
200 | abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]); |
201 | plen = 24; | |
252b5132 RH |
202 | } |
203 | else if (looking_for & IGNORE) | |
204 | { | |
3903e627 | 205 | ; |
252b5132 RH |
206 | } |
207 | else if (looking_for & DISPREG) | |
208 | { | |
209 | rdisp = thisnib; | |
210 | } | |
211 | else if (looking_for & KBIT) | |
212 | { | |
5fec0fc5 | 213 | switch (thisnib) |
252b5132 RH |
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 | { | |
5fec0fc5 | 230 | plen = 8; |
252b5132 RH |
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 | { | |
3903e627 | 248 | int i; |
252b5132 | 249 | |
3903e627 NC |
250 | for (i = 0; i < q->length; i++) |
251 | fprintf (stream, "%02x ", data[i]); | |
53d388d1 | 252 | |
3903e627 NC |
253 | for (; i < 6; i++) |
254 | fprintf (stream, " "); | |
53d388d1 | 255 | |
252b5132 RH |
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 | ||
3903e627 | 281 | /* Fill in the args. */ |
252b5132 RH |
282 | { |
283 | op_type *args = q->args.nib; | |
284 | int hadone = 0; | |
285 | ||
252b5132 RH |
286 | while (*args != E) |
287 | { | |
288 | int x = *args; | |
53d388d1 | 289 | |
252b5132 RH |
290 | if (hadone) |
291 | fprintf (stream, ","); | |
292 | ||
252b5132 RH |
293 | if (x & L_3) |
294 | { | |
295 | fprintf (stream, "#0x%x", (unsigned) bit); | |
296 | } | |
5fec0fc5 | 297 | else if (x & (IMM | KBIT | DBIT)) |
252b5132 RH |
298 | { |
299 | /* Bletch. For shal #2,er0 and friends. */ | |
5fec0fc5 | 300 | if (*(args + 1) & SRC_IN_DST) |
252b5132 RH |
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; | |
53d388d1 | 308 | |
252b5132 RH |
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; | |
252b5132 RH |
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 | } | |
252b5132 RH |
335 | else if (x & IND) |
336 | { | |
337 | int rn = (x & DST) ? rd : rs; | |
338 | fprintf (stream, "@%s", pregnames[rn]); | |
339 | } | |
252b5132 RH |
340 | else if (x & ABS8MEM) |
341 | { | |
342 | fprintf (stream, "@0x%x:8", (unsigned) abs); | |
343 | } | |
5fec0fc5 | 344 | else if (x & (ABS | ABSJMP)) |
252b5132 RH |
345 | { |
346 | fprintf (stream, "@0x%x:%d", (unsigned) abs, plen); | |
347 | } | |
252b5132 RH |
348 | else if (x & MEMIND) |
349 | { | |
350 | fprintf (stream, "@@%d (%x)", abs, abs); | |
351 | } | |
252b5132 RH |
352 | else if (x & PCREL) |
353 | { | |
5fec0fc5 | 354 | if (x & L_16) |
252b5132 | 355 | { |
3903e627 | 356 | abs += 2; |
5fec0fc5 | 357 | fprintf (stream, |
53d388d1 JL |
358 | ".%s%d (%x)", |
359 | (short) abs > 0 ? "+" : "", | |
5fec0fc5 | 360 | (short) abs, addr + (short) abs + 2); |
252b5132 | 361 | } |
3903e627 NC |
362 | else |
363 | { | |
5fec0fc5 | 364 | fprintf (stream, |
53d388d1 JL |
365 | ".%s%d (%x)", |
366 | (char) abs > 0 ? "+" : "", | |
5fec0fc5 | 367 | (char) abs, addr + (char) abs + 2); |
3903e627 | 368 | } |
252b5132 RH |
369 | } |
370 | else if (x & DISP) | |
371 | { | |
5fec0fc5 | 372 | fprintf (stream, "@(0x%x:%d,%s)", |
53d388d1 | 373 | abs, plen, pregnames[rdisp]); |
252b5132 | 374 | } |
252b5132 RH |
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); | |
53d388d1 | 386 | |
252b5132 RH |
387 | hadone = 1; |
388 | args++; | |
389 | } | |
390 | } | |
53d388d1 | 391 | |
252b5132 RH |
392 | return q->length; |
393 | } | |
252b5132 | 394 | else |
5fec0fc5 NC |
395 | /* xgettext:c-format */ |
396 | fprintf (stream, _("Don't understand %x \n"), looking_for); | |
252b5132 | 397 | } |
53d388d1 | 398 | |
252b5132 RH |
399 | len++; |
400 | nib++; | |
401 | } | |
53d388d1 | 402 | |
252b5132 | 403 | fail: |
c07ab2ec | 404 | ; |
252b5132 RH |
405 | } |
406 | ||
5fec0fc5 | 407 | /* Fell off the end. */ |
252b5132 RH |
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 | ||
5fec0fc5 | 414 | int |
252b5132 | 415 | print_insn_h8300 (addr, info) |
5fec0fc5 | 416 | bfd_vma addr; |
3903e627 | 417 | disassemble_info *info; |
252b5132 | 418 | { |
5fec0fc5 | 419 | return bfd_h8_disassemble (addr, info, 0); |
252b5132 RH |
420 | } |
421 | ||
5fec0fc5 | 422 | int |
252b5132 | 423 | print_insn_h8300h (addr, info) |
3903e627 NC |
424 | bfd_vma addr; |
425 | disassemble_info *info; | |
252b5132 | 426 | { |
5fec0fc5 | 427 | return bfd_h8_disassemble (addr, info, 1); |
252b5132 RH |
428 | } |
429 | ||
5fec0fc5 | 430 | int |
252b5132 | 431 | print_insn_h8300s (addr, info) |
3903e627 NC |
432 | bfd_vma addr; |
433 | disassemble_info *info; | |
252b5132 | 434 | { |
5fec0fc5 | 435 | return bfd_h8_disassemble (addr, info, 2); |
252b5132 | 436 | } |