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