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