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