Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Disassemble h8300 instructions. |
3903e627 | 2 | Copyright (C) 1993, 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; | |
90 | struct h8_opcode *q = h8_opcodes; | |
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. */ |
252b5132 RH |
115 | while (q->name) |
116 | { | |
117 | op_type *nib; | |
118 | unsigned int len = 0; | |
119 | ||
120 | nib = q->data.nib; | |
53d388d1 | 121 | |
252b5132 RH |
122 | while (1) |
123 | { | |
124 | op_type looking_for = *nib; | |
125 | int thisnib = data[len >> 1]; | |
53d388d1 | 126 | |
252b5132 | 127 | thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf); |
53d388d1 | 128 | |
5fec0fc5 | 129 | if (looking_for < 16 && looking_for >= 0) |
252b5132 | 130 | { |
5fec0fc5 | 131 | if (looking_for != thisnib) |
252b5132 RH |
132 | goto fail; |
133 | } | |
5fec0fc5 | 134 | else |
252b5132 | 135 | { |
252b5132 RH |
136 | if ((int) looking_for & (int) B31) |
137 | { | |
53d388d1 | 138 | if (!(((int) thisnib & 0x8) != 0)) |
252b5132 | 139 | goto fail; |
53d388d1 | 140 | |
252b5132 RH |
141 | looking_for = (op_type) ((int) looking_for & ~(int) B31); |
142 | } | |
53d388d1 | 143 | |
252b5132 RH |
144 | if ((int) looking_for & (int) B30) |
145 | { | |
5fec0fc5 | 146 | if (!(((int) thisnib & 0x8) == 0)) |
252b5132 | 147 | goto fail; |
53d388d1 | 148 | |
252b5132 RH |
149 | looking_for = (op_type) ((int) looking_for & ~(int) B30); |
150 | } | |
151 | ||
152 | if (looking_for & DBIT) | |
153 | { | |
53d388d1 JL |
154 | /* Exclude adds/subs by looking at bit 0 and 2, and |
155 | make sure the operand size, either w or l, | |
156 | matches by looking at bit 1. */ | |
157 | if ((looking_for & 7) != (thisnib & 7)) | |
3903e627 | 158 | goto fail; |
53d388d1 | 159 | |
252b5132 | 160 | abs = (thisnib & 0x8) ? 2 : 1; |
5fec0fc5 NC |
161 | } |
162 | else if (looking_for & (REG | IND | INC | DEC)) | |
252b5132 RH |
163 | { |
164 | if (looking_for & SRC) | |
3903e627 | 165 | rs = thisnib; |
252b5132 | 166 | else |
3903e627 | 167 | rd = thisnib; |
252b5132 RH |
168 | } |
169 | else if (looking_for & L_16) | |
170 | { | |
171 | abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1]; | |
172 | plen = 16; | |
252b5132 | 173 | } |
3903e627 | 174 | else if (looking_for & ABSJMP) |
252b5132 | 175 | { |
5fec0fc5 | 176 | abs = (data[1] << 16) | (data[2] << 8) | (data[3]); |
252b5132 | 177 | } |
3903e627 | 178 | else if (looking_for & MEMIND) |
252b5132 RH |
179 | { |
180 | abs = data[1]; | |
181 | } | |
182 | else if (looking_for & L_32) | |
183 | { | |
184 | int i = len >> 1; | |
53d388d1 | 185 | |
252b5132 RH |
186 | abs = (data[i] << 24) |
187 | | (data[i + 1] << 16) | |
5fec0fc5 | 188 | | (data[i + 2] << 8) |
53d388d1 | 189 | | (data[i + 3]); |
252b5132 | 190 | |
3903e627 | 191 | plen = 32; |
252b5132 RH |
192 | } |
193 | else if (looking_for & L_24) | |
194 | { | |
195 | int i = len >> 1; | |
53d388d1 | 196 | |
5fec0fc5 NC |
197 | abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]); |
198 | plen = 24; | |
252b5132 RH |
199 | } |
200 | else if (looking_for & IGNORE) | |
201 | { | |
3903e627 | 202 | ; |
252b5132 RH |
203 | } |
204 | else if (looking_for & DISPREG) | |
205 | { | |
206 | rdisp = thisnib; | |
207 | } | |
208 | else if (looking_for & KBIT) | |
209 | { | |
5fec0fc5 | 210 | switch (thisnib) |
252b5132 RH |
211 | { |
212 | case 9: | |
213 | abs = 4; | |
214 | break; | |
215 | case 8: | |
216 | abs = 2; | |
217 | break; | |
218 | case 0: | |
219 | abs = 1; | |
220 | break; | |
221 | default: | |
222 | goto fail; | |
223 | } | |
224 | } | |
225 | else if (looking_for & L_8) | |
226 | { | |
5fec0fc5 | 227 | plen = 8; |
252b5132 RH |
228 | abs = data[len >> 1]; |
229 | } | |
230 | else if (looking_for & L_3) | |
231 | { | |
232 | bit = thisnib & 0x7; | |
233 | } | |
234 | else if (looking_for & L_2) | |
235 | { | |
236 | plen = 2; | |
237 | abs = thisnib & 0x3; | |
238 | } | |
239 | else if (looking_for & MACREG) | |
240 | { | |
241 | abs = (thisnib == 3); | |
242 | } | |
243 | else if (looking_for == E) | |
244 | { | |
3903e627 | 245 | int i; |
252b5132 | 246 | |
3903e627 NC |
247 | for (i = 0; i < q->length; i++) |
248 | fprintf (stream, "%02x ", data[i]); | |
53d388d1 | 249 | |
3903e627 NC |
250 | for (; i < 6; i++) |
251 | fprintf (stream, " "); | |
53d388d1 | 252 | |
252b5132 RH |
253 | fprintf (stream, "%s\t", q->name); |
254 | ||
255 | /* Gross. Disgusting. */ | |
256 | if (strcmp (q->name, "ldm.l") == 0) | |
257 | { | |
258 | int count, high; | |
259 | ||
260 | count = (data[1] >> 4) & 0x3; | |
261 | high = data[3] & 0x7; | |
262 | ||
263 | fprintf (stream, "@sp+,er%d-er%d", high - count, high); | |
264 | return q->length; | |
265 | } | |
266 | ||
267 | if (strcmp (q->name, "stm.l") == 0) | |
268 | { | |
269 | int count, low; | |
270 | ||
271 | count = (data[1] >> 4) & 0x3; | |
272 | low = data[3] & 0x7; | |
273 | ||
274 | fprintf (stream, "er%d-er%d,@-sp", low, low + count); | |
275 | return q->length; | |
276 | } | |
277 | ||
3903e627 | 278 | /* Fill in the args. */ |
252b5132 RH |
279 | { |
280 | op_type *args = q->args.nib; | |
281 | int hadone = 0; | |
282 | ||
252b5132 RH |
283 | while (*args != E) |
284 | { | |
285 | int x = *args; | |
53d388d1 | 286 | |
252b5132 RH |
287 | if (hadone) |
288 | fprintf (stream, ","); | |
289 | ||
252b5132 RH |
290 | if (x & L_3) |
291 | { | |
292 | fprintf (stream, "#0x%x", (unsigned) bit); | |
293 | } | |
5fec0fc5 | 294 | else if (x & (IMM | KBIT | DBIT)) |
252b5132 RH |
295 | { |
296 | /* Bletch. For shal #2,er0 and friends. */ | |
5fec0fc5 | 297 | if (*(args + 1) & SRC_IN_DST) |
252b5132 RH |
298 | abs = 2; |
299 | ||
300 | fprintf (stream, "#0x%x", (unsigned) abs); | |
301 | } | |
302 | else if (x & REG) | |
303 | { | |
304 | int rn = (x & DST) ? rd : rs; | |
53d388d1 | 305 | |
252b5132 RH |
306 | switch (x & SIZE) |
307 | { | |
308 | case L_8: | |
309 | fprintf (stream, "%s", regnames[rn]); | |
310 | break; | |
311 | case L_16: | |
312 | fprintf (stream, "%s", wregnames[rn]); | |
313 | break; | |
314 | case L_P: | |
315 | case L_32: | |
316 | fprintf (stream, "%s", lregnames[rn]); | |
317 | break; | |
252b5132 RH |
318 | } |
319 | } | |
320 | else if (x & MACREG) | |
321 | { | |
322 | fprintf (stream, "mac%c", abs ? 'l' : 'h'); | |
323 | } | |
324 | else if (x & INC) | |
325 | { | |
326 | fprintf (stream, "@%s+", pregnames[rs]); | |
327 | } | |
328 | else if (x & DEC) | |
329 | { | |
330 | fprintf (stream, "@-%s", pregnames[rd]); | |
331 | } | |
252b5132 RH |
332 | else if (x & IND) |
333 | { | |
334 | int rn = (x & DST) ? rd : rs; | |
335 | fprintf (stream, "@%s", pregnames[rn]); | |
336 | } | |
252b5132 RH |
337 | else if (x & ABS8MEM) |
338 | { | |
339 | fprintf (stream, "@0x%x:8", (unsigned) abs); | |
340 | } | |
5fec0fc5 | 341 | else if (x & (ABS | ABSJMP)) |
252b5132 RH |
342 | { |
343 | fprintf (stream, "@0x%x:%d", (unsigned) abs, plen); | |
344 | } | |
252b5132 RH |
345 | else if (x & MEMIND) |
346 | { | |
347 | fprintf (stream, "@@%d (%x)", abs, abs); | |
348 | } | |
252b5132 RH |
349 | else if (x & PCREL) |
350 | { | |
5fec0fc5 | 351 | if (x & L_16) |
252b5132 | 352 | { |
3903e627 | 353 | abs += 2; |
5fec0fc5 | 354 | fprintf (stream, |
53d388d1 JL |
355 | ".%s%d (%x)", |
356 | (short) abs > 0 ? "+" : "", | |
5fec0fc5 | 357 | (short) abs, addr + (short) abs + 2); |
252b5132 | 358 | } |
3903e627 NC |
359 | else |
360 | { | |
5fec0fc5 | 361 | fprintf (stream, |
53d388d1 JL |
362 | ".%s%d (%x)", |
363 | (char) abs > 0 ? "+" : "", | |
5fec0fc5 | 364 | (char) abs, addr + (char) abs + 2); |
3903e627 | 365 | } |
252b5132 RH |
366 | } |
367 | else if (x & DISP) | |
368 | { | |
5fec0fc5 | 369 | fprintf (stream, "@(0x%x:%d,%s)", |
53d388d1 | 370 | abs, plen, pregnames[rdisp]); |
252b5132 | 371 | } |
252b5132 RH |
372 | else if (x & CCR) |
373 | { | |
374 | fprintf (stream, "ccr"); | |
375 | } | |
376 | else if (x & EXR) | |
377 | { | |
378 | fprintf (stream, "exr"); | |
379 | } | |
380 | else | |
381 | /* xgettext:c-format */ | |
382 | fprintf (stream, _("Hmmmm %x"), x); | |
53d388d1 | 383 | |
252b5132 RH |
384 | hadone = 1; |
385 | args++; | |
386 | } | |
387 | } | |
53d388d1 | 388 | |
252b5132 RH |
389 | return q->length; |
390 | } | |
252b5132 | 391 | else |
5fec0fc5 NC |
392 | /* xgettext:c-format */ |
393 | fprintf (stream, _("Don't understand %x \n"), looking_for); | |
252b5132 | 394 | } |
53d388d1 | 395 | |
252b5132 RH |
396 | len++; |
397 | nib++; | |
398 | } | |
53d388d1 | 399 | |
252b5132 RH |
400 | fail: |
401 | q++; | |
402 | } | |
403 | ||
5fec0fc5 | 404 | /* Fell off the end. */ |
252b5132 RH |
405 | fprintf (stream, "%02x %02x .word\tH'%x,H'%x", |
406 | data[0], data[1], | |
407 | data[0], data[1]); | |
408 | return 2; | |
409 | } | |
410 | ||
5fec0fc5 | 411 | int |
252b5132 | 412 | print_insn_h8300 (addr, info) |
5fec0fc5 | 413 | bfd_vma addr; |
3903e627 | 414 | disassemble_info *info; |
252b5132 | 415 | { |
5fec0fc5 | 416 | return bfd_h8_disassemble (addr, info, 0); |
252b5132 RH |
417 | } |
418 | ||
5fec0fc5 | 419 | int |
252b5132 | 420 | print_insn_h8300h (addr, info) |
3903e627 NC |
421 | bfd_vma addr; |
422 | disassemble_info *info; | |
252b5132 | 423 | { |
5fec0fc5 | 424 | return bfd_h8_disassemble (addr, info, 1); |
252b5132 RH |
425 | } |
426 | ||
5fec0fc5 | 427 | int |
252b5132 | 428 | print_insn_h8300s (addr, info) |
3903e627 NC |
429 | bfd_vma addr; |
430 | disassemble_info *info; | |
252b5132 | 431 | { |
5fec0fc5 | 432 | return bfd_h8_disassemble (addr, info, 2); |
252b5132 | 433 | } |