Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* Disassemble z8000 code. |
2 | Copyright 1992, 1993, 1995, 1998 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GNU Binutils. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
19 | ||
252b5132 RH |
20 | #include "sysdep.h" |
21 | #include "dis-asm.h" | |
22 | ||
23 | #define DEFINE_TABLE | |
24 | #include "z8k-opc.h" | |
25 | ||
26 | \f | |
27 | #include <setjmp.h> | |
28 | ||
29 | \f | |
30 | typedef struct | |
31 | { | |
32 | /* These are all indexed by nibble number (i.e only every other entry | |
33 | of bytes is used, and every 4th entry of words). */ | |
34 | unsigned char nibbles[24]; | |
35 | unsigned char bytes[24]; | |
36 | unsigned short words[24]; | |
37 | ||
38 | /* Nibble number of first word not yet fetched. */ | |
39 | int max_fetched; | |
40 | bfd_vma insn_start; | |
41 | jmp_buf bailout; | |
42 | ||
43 | long tabl_index; | |
44 | char instr_asmsrc[80]; | |
45 | unsigned long arg_reg[0x0f]; | |
46 | unsigned long immediate; | |
47 | unsigned long displacement; | |
48 | unsigned long address; | |
49 | unsigned long cond_code; | |
50 | unsigned long ctrl_code; | |
51 | unsigned long flags; | |
52 | unsigned long interrupts; | |
53 | } | |
54 | instr_data_s; | |
55 | ||
56 | /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive) | |
57 | to ADDR (exclusive) are valid. Returns 1 for success, longjmps | |
58 | on error. */ | |
59 | #define FETCH_DATA(info, nibble) \ | |
60 | ((nibble) < ((instr_data_s *)(info->private_data))->max_fetched \ | |
61 | ? 1 : fetch_data ((info), (nibble))) | |
62 | ||
63 | static int | |
64 | fetch_data (info, nibble) | |
65 | struct disassemble_info *info; | |
66 | int nibble; | |
67 | { | |
68 | unsigned char mybuf[20]; | |
69 | int status; | |
70 | instr_data_s *priv = (instr_data_s *)info->private_data; | |
71 | ||
72 | if ((nibble % 4) != 0) | |
73 | abort (); | |
74 | ||
75 | status = (*info->read_memory_func) (priv->insn_start, | |
76 | (bfd_byte *) mybuf, | |
77 | nibble / 2, | |
78 | info); | |
79 | if (status != 0) | |
80 | { | |
81 | (*info->memory_error_func) (status, priv->insn_start, info); | |
82 | longjmp (priv->bailout, 1); | |
83 | } | |
84 | ||
85 | { | |
86 | int i; | |
87 | unsigned char *p = mybuf ; | |
88 | ||
89 | for (i = 0; i < nibble;) | |
90 | { | |
91 | priv->words[i] = (p[0] << 8) | p[1]; | |
92 | ||
93 | priv->bytes[i] = *p; | |
94 | priv->nibbles[i++] = *p >> 4; | |
95 | priv->nibbles[i++] = *p &0xf; | |
96 | ||
97 | ++p; | |
98 | priv->bytes[i] = *p; | |
99 | priv->nibbles[i++] = *p >> 4; | |
100 | priv->nibbles[i++] = *p & 0xf; | |
101 | ||
102 | ++p; | |
103 | } | |
104 | } | |
105 | priv->max_fetched = nibble; | |
106 | return 1; | |
107 | } | |
108 | ||
109 | static char *codes[16] = | |
110 | { | |
111 | "f", | |
112 | "lt", | |
113 | "le", | |
114 | "ule", | |
115 | "ov/pe", | |
116 | "mi", | |
117 | "eq", | |
118 | "c/ult", | |
119 | "t", | |
120 | "ge", | |
121 | "gt", | |
122 | "ugt", | |
123 | "nov/po", | |
124 | "pl", | |
125 | "ne", | |
126 | "nc/uge" | |
127 | }; | |
128 | ||
129 | int z8k_lookup_instr PARAMS ((unsigned char*, disassemble_info *)); | |
130 | static void output_instr | |
131 | PARAMS ((instr_data_s *, unsigned long, disassemble_info *)); | |
132 | static void unpack_instr PARAMS ((instr_data_s *, int, disassemble_info *)); | |
133 | static void unparse_instr PARAMS ((instr_data_s *)); | |
134 | ||
135 | static int | |
136 | print_insn_z8k (addr, info, is_segmented) | |
137 | bfd_vma addr; | |
138 | disassemble_info *info; | |
139 | int is_segmented; | |
140 | { | |
141 | instr_data_s instr_data; | |
142 | ||
143 | info->private_data = (PTR) &instr_data; | |
144 | instr_data.max_fetched = 0; | |
145 | instr_data.insn_start = addr; | |
146 | if (setjmp (instr_data.bailout) != 0) | |
147 | /* Error return. */ | |
148 | return -1; | |
149 | ||
150 | instr_data.tabl_index = z8k_lookup_instr (instr_data.nibbles, info); | |
151 | if (instr_data.tabl_index > 0) | |
152 | { | |
153 | unpack_instr (&instr_data, is_segmented, info); | |
154 | unparse_instr (&instr_data); | |
155 | output_instr (&instr_data, addr, info); | |
156 | return z8k_table[instr_data.tabl_index].length; | |
157 | } | |
158 | else | |
159 | { | |
160 | FETCH_DATA (info, 4); | |
161 | (*info->fprintf_func) (info->stream, ".word %02x%02x", | |
162 | instr_data.bytes[0], instr_data.bytes[2]); | |
163 | return 2; | |
164 | } | |
165 | } | |
166 | ||
167 | int | |
168 | print_insn_z8001 (addr, info) | |
169 | bfd_vma addr; | |
170 | disassemble_info *info; | |
171 | { | |
172 | return print_insn_z8k (addr, info, 1); | |
173 | } | |
174 | ||
175 | int | |
176 | print_insn_z8002 (addr, info) | |
177 | bfd_vma addr; | |
178 | disassemble_info *info; | |
179 | { | |
180 | return print_insn_z8k (addr, info, 0); | |
181 | } | |
182 | ||
183 | int | |
184 | z8k_lookup_instr (nibbles, info) | |
185 | unsigned char *nibbles; | |
186 | disassemble_info *info; | |
187 | { | |
188 | ||
189 | int nibl_index, tabl_index; | |
190 | int nibl_matched; | |
191 | unsigned short instr_nibl; | |
192 | unsigned short tabl_datum, datum_class, datum_value; | |
193 | ||
194 | nibl_matched = 0; | |
195 | tabl_index = 0; | |
196 | while (!nibl_matched && z8k_table[tabl_index].name) | |
197 | { | |
198 | nibl_matched = 1; | |
199 | for (nibl_index = 0; nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched; nibl_index++) | |
200 | { | |
201 | if ((nibl_index % 4) == 0) | |
202 | /* Fetch one word at a time. */ | |
203 | FETCH_DATA (info, nibl_index + 4); | |
204 | instr_nibl = nibbles[nibl_index]; | |
205 | ||
206 | tabl_datum = z8k_table[tabl_index].byte_info[nibl_index]; | |
207 | datum_class = tabl_datum & CLASS_MASK; | |
208 | datum_value = ~CLASS_MASK & tabl_datum; | |
209 | ||
210 | switch (datum_class) | |
211 | { | |
212 | case CLASS_BIT: | |
213 | if (datum_value != instr_nibl) | |
214 | nibl_matched = 0; | |
215 | break; | |
216 | case CLASS_00II: | |
217 | if (!((~instr_nibl) & 0x4)) | |
218 | nibl_matched = 0; | |
219 | break; | |
220 | case CLASS_01II: | |
221 | if (!(instr_nibl & 0x4)) | |
222 | nibl_matched = 0; | |
223 | break; | |
224 | case CLASS_0CCC: | |
225 | if (!((~instr_nibl) & 0x8)) | |
226 | nibl_matched = 0; | |
227 | break; | |
228 | case CLASS_1CCC: | |
229 | if (!(instr_nibl & 0x8)) | |
230 | nibl_matched = 0; | |
231 | break; | |
232 | case CLASS_0DISP7: | |
233 | if (!((~instr_nibl) & 0x8)) | |
234 | nibl_matched = 0; | |
235 | nibl_index += 1; | |
236 | break; | |
237 | case CLASS_1DISP7: | |
238 | if (!(instr_nibl & 0x8)) | |
239 | nibl_matched = 0; | |
240 | nibl_index += 1; | |
241 | break; | |
242 | case CLASS_REGN0: | |
243 | if (instr_nibl == 0) | |
244 | nibl_matched = 0; | |
245 | break; | |
246 | case CLASS_BIT_1OR2: | |
247 | if ((instr_nibl | 0x2) != (datum_value | 0x2)) | |
248 | nibl_matched = 0; | |
249 | break; | |
250 | default: | |
251 | break; | |
252 | } | |
253 | } | |
254 | if (nibl_matched) | |
255 | { | |
256 | return tabl_index; | |
257 | } | |
258 | ||
259 | tabl_index++; | |
260 | } | |
261 | return -1; | |
262 | ||
263 | } | |
264 | ||
265 | static void | |
266 | output_instr (instr_data, addr, info) | |
267 | instr_data_s *instr_data; | |
268 | unsigned long addr; | |
269 | disassemble_info *info; | |
270 | { | |
271 | int loop, loop_limit; | |
272 | char tmp_str[20]; | |
273 | char out_str[100]; | |
274 | ||
275 | strcpy (out_str, "\t"); | |
276 | ||
277 | loop_limit = z8k_table[instr_data->tabl_index].length * 2; | |
278 | FETCH_DATA (info, loop_limit); | |
279 | for (loop = 0; loop < loop_limit; loop++) | |
280 | { | |
281 | sprintf (tmp_str, "%x", instr_data->nibbles[loop]); | |
282 | strcat (out_str, tmp_str); | |
283 | } | |
284 | ||
285 | while (loop++ < 8) | |
286 | { | |
287 | strcat (out_str, " "); | |
288 | } | |
289 | ||
290 | strcat (out_str, instr_data->instr_asmsrc); | |
291 | ||
292 | (*info->fprintf_func) (info->stream, "%s", out_str); | |
293 | } | |
294 | ||
295 | static void | |
296 | unpack_instr (instr_data, is_segmented, info) | |
297 | instr_data_s *instr_data; | |
298 | int is_segmented; | |
299 | disassemble_info *info; | |
300 | { | |
301 | int nibl_count, loop; | |
302 | unsigned short instr_nibl, instr_byte, instr_word; | |
303 | long instr_long; | |
304 | unsigned short tabl_datum, datum_class, datum_value; | |
305 | ||
306 | nibl_count = 0; | |
307 | loop = 0; | |
308 | while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0) | |
309 | { | |
310 | FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4)); | |
311 | instr_nibl = instr_data->nibbles[nibl_count]; | |
312 | instr_byte = instr_data->bytes[nibl_count]; | |
313 | instr_word = instr_data->words[nibl_count]; | |
314 | ||
315 | tabl_datum = z8k_table[instr_data->tabl_index].byte_info[loop]; | |
316 | datum_class = tabl_datum & CLASS_MASK; | |
317 | datum_value = tabl_datum & ~CLASS_MASK; | |
318 | ||
319 | switch (datum_class) | |
320 | { | |
321 | case CLASS_X: | |
322 | instr_data->address = instr_nibl; | |
323 | break; | |
324 | case CLASS_BA: | |
325 | instr_data->displacement = instr_nibl; | |
326 | break; | |
327 | case CLASS_BX: | |
328 | instr_data->arg_reg[datum_value] = instr_nibl; | |
329 | break; | |
330 | case CLASS_DISP: | |
331 | switch (datum_value) | |
332 | { | |
333 | case ARG_DISP16: | |
334 | instr_data->displacement = instr_word; | |
335 | nibl_count += 3; | |
336 | break; | |
337 | case ARG_DISP12: | |
338 | instr_data->displacement = instr_word & 0x0fff; | |
339 | nibl_count += 2; | |
340 | break; | |
341 | default: | |
342 | break; | |
343 | } | |
344 | break; | |
345 | case CLASS_IMM: | |
346 | switch (datum_value) | |
347 | { | |
348 | case ARG_IMM4: | |
349 | instr_data->immediate = instr_nibl; | |
350 | break; | |
351 | case ARG_NIM8: | |
352 | instr_data->immediate = (-instr_byte); | |
353 | nibl_count += 1; | |
354 | break; | |
355 | case ARG_IMM8: | |
356 | instr_data->immediate = instr_byte; | |
357 | nibl_count += 1; | |
358 | break; | |
359 | case ARG_IMM16: | |
360 | instr_data->immediate = instr_word; | |
361 | nibl_count += 3; | |
362 | break; | |
363 | case ARG_IMM32: | |
364 | FETCH_DATA (info, nibl_count + 8); | |
365 | instr_long = (instr_data->words[nibl_count] << 16) | |
366 | | (instr_data->words[nibl_count + 4]); | |
367 | instr_data->immediate = instr_long; | |
368 | nibl_count += 7; | |
369 | break; | |
370 | case ARG_IMMN: | |
371 | instr_data->immediate = instr_nibl - 1; | |
372 | break; | |
373 | case ARG_IMM4M1: | |
374 | instr_data->immediate = instr_nibl + 1; | |
375 | break; | |
376 | case ARG_IMM_1: | |
377 | instr_data->immediate = 1; | |
378 | break; | |
379 | case ARG_IMM_2: | |
380 | instr_data->immediate = 2; | |
381 | break; | |
382 | case ARG_IMM2: | |
383 | instr_data->immediate = instr_nibl & 0x3; | |
384 | break; | |
385 | default: | |
386 | break; | |
387 | } | |
388 | break; | |
389 | case CLASS_CC: | |
390 | instr_data->cond_code = instr_nibl; | |
391 | break; | |
392 | case CLASS_CTRL: | |
393 | instr_data->ctrl_code = instr_nibl; | |
394 | break; | |
395 | case CLASS_DA: | |
396 | case CLASS_ADDRESS: | |
397 | if (is_segmented) | |
398 | { | |
399 | if (instr_nibl & 0x8) | |
400 | { | |
401 | FETCH_DATA (info, nibl_count + 8); | |
402 | instr_long = (instr_data->words[nibl_count] << 16) | |
403 | | (instr_data->words[nibl_count + 4]); | |
404 | instr_data->address = ((instr_word & 0x7f00) << 8) + | |
405 | (instr_long & 0xffff); | |
406 | nibl_count += 7; | |
407 | } | |
408 | else | |
409 | { | |
410 | instr_data->address = ((instr_word & 0x7f00) << 8) + | |
411 | (instr_word & 0x00ff); | |
412 | nibl_count += 3; | |
413 | } | |
414 | } | |
415 | else | |
416 | { | |
417 | instr_data->address = instr_word; | |
418 | nibl_count += 3; | |
419 | } | |
420 | break; | |
421 | case CLASS_0CCC: | |
422 | instr_data->cond_code = instr_nibl & 0x7; | |
423 | break; | |
424 | case CLASS_1CCC: | |
425 | instr_data->cond_code = instr_nibl & 0x7; | |
426 | break; | |
427 | case CLASS_0DISP7: | |
428 | instr_data->displacement = instr_byte & 0x7f; | |
429 | nibl_count += 1; | |
430 | break; | |
431 | case CLASS_1DISP7: | |
432 | instr_data->displacement = instr_byte & 0x7f; | |
433 | nibl_count += 1; | |
434 | break; | |
435 | case CLASS_01II: | |
436 | instr_data->interrupts = instr_nibl & 0x3; | |
437 | break; | |
438 | case CLASS_00II: | |
439 | instr_data->interrupts = instr_nibl & 0x3; | |
440 | break; | |
441 | case CLASS_BIT: | |
442 | /* do nothing */ | |
443 | break; | |
444 | case CLASS_IR: | |
445 | instr_data->arg_reg[datum_value] = instr_nibl; | |
446 | break; | |
447 | case CLASS_FLAGS: | |
448 | instr_data->flags = instr_nibl; | |
449 | break; | |
450 | case CLASS_REG: | |
451 | instr_data->arg_reg[datum_value] = instr_nibl; | |
452 | break; | |
453 | case CLASS_REG_BYTE: | |
454 | instr_data->arg_reg[datum_value] = instr_nibl; | |
455 | break; | |
456 | case CLASS_REG_WORD: | |
457 | instr_data->arg_reg[datum_value] = instr_nibl; | |
458 | break; | |
459 | case CLASS_REG_QUAD: | |
460 | instr_data->arg_reg[datum_value] = instr_nibl; | |
461 | break; | |
462 | case CLASS_REG_LONG: | |
463 | instr_data->arg_reg[datum_value] = instr_nibl; | |
464 | break; | |
465 | case CLASS_REGN0: | |
466 | instr_data->arg_reg[datum_value] = instr_nibl; | |
467 | break; | |
468 | default: | |
469 | break; | |
470 | } | |
471 | ||
472 | loop += 1; | |
473 | nibl_count += 1; | |
474 | } | |
475 | } | |
476 | ||
477 | static void | |
478 | unparse_instr (instr_data) | |
479 | instr_data_s *instr_data; | |
480 | { | |
481 | unsigned short tabl_datum, datum_class, datum_value; | |
482 | int loop, loop_limit; | |
483 | char out_str[80], tmp_str[25]; | |
484 | ||
485 | sprintf (out_str, "\t%s\t", z8k_table[instr_data->tabl_index].name); | |
486 | ||
487 | loop_limit = z8k_table[instr_data->tabl_index].noperands; | |
488 | for (loop = 0; loop < loop_limit; loop++) | |
489 | { | |
490 | if (loop) | |
491 | strcat (out_str, ","); | |
492 | ||
493 | tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop]; | |
494 | datum_class = tabl_datum & CLASS_MASK; | |
495 | datum_value = tabl_datum & ~CLASS_MASK; | |
496 | ||
497 | switch (datum_class) | |
498 | { | |
499 | case CLASS_X: | |
500 | sprintf (tmp_str, "0x%0lx(R%ld)", instr_data->address, | |
501 | instr_data->arg_reg[datum_value]); | |
502 | strcat (out_str, tmp_str); | |
503 | break; | |
504 | case CLASS_BA: | |
505 | sprintf (tmp_str, "r%ld(#%lx)", instr_data->arg_reg[datum_value], | |
506 | instr_data->immediate); | |
507 | strcat (out_str, tmp_str); | |
508 | break; | |
509 | case CLASS_BX: | |
510 | sprintf (tmp_str, "r%ld(R%ld)", instr_data->arg_reg[datum_value], | |
511 | instr_data->arg_reg[ARG_RX]); | |
512 | strcat (out_str, tmp_str); | |
513 | break; | |
514 | case CLASS_DISP: | |
515 | sprintf (tmp_str, "#0x%0lx", instr_data->displacement); | |
516 | strcat (out_str, tmp_str); | |
517 | break; | |
518 | case CLASS_IMM: | |
519 | sprintf (tmp_str, "#0x%0lx", instr_data->immediate); | |
520 | strcat (out_str, tmp_str); | |
521 | break; | |
522 | case CLASS_CC: | |
523 | sprintf (tmp_str, "%s", codes[instr_data->cond_code]); | |
524 | strcat (out_str, tmp_str); | |
525 | break; | |
526 | case CLASS_CTRL: | |
527 | sprintf (tmp_str, "0x%0lx", instr_data->ctrl_code); | |
528 | strcat (out_str, tmp_str); | |
529 | break; | |
530 | case CLASS_DA: | |
531 | case CLASS_ADDRESS: | |
532 | sprintf (tmp_str, "#0x%0lx", instr_data->address); | |
533 | strcat (out_str, tmp_str); | |
534 | break; | |
535 | case CLASS_IR: | |
536 | sprintf (tmp_str, "@R%ld", instr_data->arg_reg[datum_value]); | |
537 | strcat (out_str, tmp_str); | |
538 | break; | |
539 | case CLASS_FLAGS: | |
540 | sprintf (tmp_str, "0x%0lx", instr_data->flags); | |
541 | strcat (out_str, tmp_str); | |
542 | break; | |
543 | case CLASS_REG_BYTE: | |
544 | if (instr_data->arg_reg[datum_value] >= 0x8) | |
545 | { | |
546 | sprintf (tmp_str, "rl%ld", | |
547 | instr_data->arg_reg[datum_value] - 0x8); | |
548 | } | |
549 | else | |
550 | { | |
551 | sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]); | |
552 | } | |
553 | strcat (out_str, tmp_str); | |
554 | break; | |
555 | case CLASS_REG_WORD: | |
556 | sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]); | |
557 | strcat (out_str, tmp_str); | |
558 | break; | |
559 | case CLASS_REG_QUAD: | |
560 | sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]); | |
561 | strcat (out_str, tmp_str); | |
562 | break; | |
563 | case CLASS_REG_LONG: | |
564 | sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]); | |
565 | strcat (out_str, tmp_str); | |
566 | break; | |
567 | default: | |
568 | break; | |
569 | } | |
570 | } | |
571 | ||
572 | strcpy (instr_data->instr_asmsrc, out_str); | |
573 | } |