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