* elf32-mips.c (gprel32_with_gp): Remove useless N64 ABI case.
[binutils.git] / opcodes / z8k-dis.c
blob69d1792312acfd586848f98164c3c4bb0359b0c6
1 /* Disassemble z8000 code.
2 Copyright 1992, 1993, 1998, 2000, 2001, 2002
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,
20 USA. */
22 #include "sysdep.h"
23 #include "dis-asm.h"
25 #define DEFINE_TABLE
26 #include "z8k-opc.h"
28 #include <setjmp.h>
30 typedef struct
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];
38 /* Nibble number of first word not yet fetched. */
39 int max_fetched;
40 bfd_vma insn_start;
41 jmp_buf bailout;
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;
54 instr_data_s;
56 static int fetch_data PARAMS ((struct disassemble_info *, int));
59 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
60 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
61 on error. */
62 #define FETCH_DATA(info, nibble) \
63 ((nibble) < ((instr_data_s *) (info->private_data))->max_fetched \
64 ? 1 : fetch_data ((info), (nibble)))
66 static int
67 fetch_data (info, nibble)
68 struct disassemble_info *info;
69 int nibble;
71 unsigned char mybuf[20];
72 int status;
73 instr_data_s *priv = (instr_data_s *) info->private_data;
75 if ((nibble % 4) != 0)
76 abort ();
78 status = (*info->read_memory_func) (priv->insn_start,
79 (bfd_byte *) mybuf,
80 nibble / 2,
81 info);
82 if (status != 0)
84 (*info->memory_error_func) (status, priv->insn_start, info);
85 longjmp (priv->bailout, 1);
89 int i;
90 unsigned char *p = mybuf;
92 for (i = 0; i < nibble;)
94 priv->words[i] = (p[0] << 8) | p[1];
96 priv->bytes[i] = *p;
97 priv->nibbles[i++] = *p >> 4;
98 priv->nibbles[i++] = *p & 0xf;
100 ++p;
101 priv->bytes[i] = *p;
102 priv->nibbles[i++] = *p >> 4;
103 priv->nibbles[i++] = *p & 0xf;
105 ++p;
108 priv->max_fetched = nibble;
109 return 1;
112 static char *codes[16] =
114 "f",
115 "lt",
116 "le",
117 "ule",
118 "ov/pe",
119 "mi",
120 "eq",
121 "c/ult",
122 "t",
123 "ge",
124 "gt",
125 "ugt",
126 "nov/po",
127 "pl",
128 "ne",
129 "nc/uge"
132 static char *ctrl_names[8] =
134 "<invld>",
135 "flags",
136 "fcw",
137 "refresh",
138 "psapseg",
139 "psapoff",
140 "nspseg",
141 "nspoff"
144 static int seg_length;
145 static int print_insn_z8k PARAMS ((bfd_vma, disassemble_info *, int));
146 int z8k_lookup_instr PARAMS ((unsigned char *, disassemble_info *));
147 static void output_instr
148 PARAMS ((instr_data_s *, unsigned long, disassemble_info *));
149 static void unpack_instr PARAMS ((instr_data_s *, int, disassemble_info *));
150 static void unparse_instr PARAMS ((instr_data_s *, int));
152 static int
153 print_insn_z8k (addr, info, is_segmented)
154 bfd_vma addr;
155 disassemble_info *info;
156 int is_segmented;
158 instr_data_s instr_data;
160 info->private_data = (PTR) &instr_data;
161 instr_data.max_fetched = 0;
162 instr_data.insn_start = addr;
163 if (setjmp (instr_data.bailout) != 0)
164 /* Error return. */
165 return -1;
167 info->bytes_per_chunk = 2;
168 info->bytes_per_line = 6;
169 info->display_endian = BFD_ENDIAN_BIG;
171 instr_data.tabl_index = z8k_lookup_instr (instr_data.nibbles, info);
172 if (instr_data.tabl_index > 0)
174 unpack_instr (&instr_data, is_segmented, info);
175 unparse_instr (&instr_data, is_segmented);
176 output_instr (&instr_data, addr, info);
177 return z8k_table[instr_data.tabl_index].length + seg_length;
179 else
181 FETCH_DATA (info, 4);
182 (*info->fprintf_func) (info->stream, ".word %02x%02x",
183 instr_data.bytes[0], instr_data.bytes[2]);
184 return 2;
189 print_insn_z8001 (addr, info)
190 bfd_vma addr;
191 disassemble_info *info;
193 return print_insn_z8k (addr, info, 1);
197 print_insn_z8002 (addr, info)
198 bfd_vma addr;
199 disassemble_info *info;
201 return print_insn_z8k (addr, info, 0);
205 z8k_lookup_instr (nibbles, info)
206 unsigned char *nibbles;
207 disassemble_info *info;
210 int nibl_index, tabl_index;
211 int nibl_matched;
212 int need_fetch = 0;
213 unsigned short instr_nibl;
214 unsigned short tabl_datum, datum_class, datum_value;
216 nibl_matched = 0;
217 tabl_index = 0;
218 FETCH_DATA (info, 4);
219 while (!nibl_matched && z8k_table[tabl_index].name)
221 nibl_matched = 1;
222 for (nibl_index = 0;
223 nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched;
224 nibl_index++)
226 if ((nibl_index % 4) == 0)
228 /* Fetch data only if it isn't already there. */
229 if (nibl_index >= 4 || (nibl_index < 4 && need_fetch))
230 FETCH_DATA (info, nibl_index + 4); /* Fetch one word at a time. */
231 if (nibl_index < 4)
232 need_fetch = 0;
233 else
234 need_fetch = 1;
236 instr_nibl = nibbles[nibl_index];
238 tabl_datum = z8k_table[tabl_index].byte_info[nibl_index];
239 datum_class = tabl_datum & CLASS_MASK;
240 datum_value = ~CLASS_MASK & tabl_datum;
242 switch (datum_class)
244 case CLASS_BIT:
245 if (datum_value != instr_nibl)
246 nibl_matched = 0;
247 break;
248 case CLASS_IGNORE:
249 break;
250 case CLASS_00II:
251 if (!((~instr_nibl) & 0x4))
252 nibl_matched = 0;
253 break;
254 case CLASS_01II:
255 if (!(instr_nibl & 0x4))
256 nibl_matched = 0;
257 break;
258 case CLASS_0CCC:
259 if (!((~instr_nibl) & 0x8))
260 nibl_matched = 0;
261 break;
262 case CLASS_1CCC:
263 if (!(instr_nibl & 0x8))
264 nibl_matched = 0;
265 break;
266 case CLASS_0DISP7:
267 if (!((~instr_nibl) & 0x8))
268 nibl_matched = 0;
269 nibl_index += 1;
270 break;
271 case CLASS_1DISP7:
272 if (!(instr_nibl & 0x8))
273 nibl_matched = 0;
274 nibl_index += 1;
275 break;
276 case CLASS_REGN0:
277 if (instr_nibl == 0)
278 nibl_matched = 0;
279 break;
280 case CLASS_BIT_1OR2:
281 if ((instr_nibl | 0x2) != (datum_value | 0x2))
282 nibl_matched = 0;
283 break;
284 default:
285 break;
289 if (nibl_matched)
290 return tabl_index;
292 tabl_index++;
294 return -1;
297 static void
298 output_instr (instr_data, addr, info)
299 instr_data_s *instr_data;
300 unsigned long addr ATTRIBUTE_UNUSED;
301 disassemble_info *info;
303 int num_bytes;
304 char out_str[100];
306 out_str[0] = 0;
308 num_bytes = (z8k_table[instr_data->tabl_index].length + seg_length) * 2;
309 FETCH_DATA (info, num_bytes);
311 strcat (out_str, instr_data->instr_asmsrc);
313 (*info->fprintf_func) (info->stream, "%s", out_str);
316 static void
317 unpack_instr (instr_data, is_segmented, info)
318 instr_data_s *instr_data;
319 int is_segmented;
320 disassemble_info *info;
322 int nibl_count, loop;
323 unsigned short instr_nibl, instr_byte, instr_word;
324 long instr_long;
325 unsigned int tabl_datum, datum_class;
326 unsigned short datum_value;
328 nibl_count = 0;
329 loop = 0;
330 seg_length = 0;
332 while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0)
334 FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4));
335 instr_nibl = instr_data->nibbles[nibl_count];
336 instr_byte = instr_data->bytes[nibl_count & ~1];
337 instr_word = instr_data->words[nibl_count & ~3];
339 tabl_datum = z8k_table[instr_data->tabl_index].byte_info[loop];
340 datum_class = tabl_datum & CLASS_MASK;
341 datum_value = tabl_datum & ~CLASS_MASK;
343 switch (datum_class)
345 case CLASS_DISP:
346 switch (datum_value)
348 case ARG_DISP16:
349 instr_data->displacement = instr_data->insn_start + 4
350 + (signed short) (instr_word & 0xffff);
351 nibl_count += 3;
352 break;
353 case ARG_DISP12:
354 if (instr_word & 0x800)
355 /* Negative 12 bit displacement. */
356 instr_data->displacement = instr_data->insn_start + 2
357 - (signed short) ((instr_word & 0xfff) | 0xf000) * 2;
358 else
359 instr_data->displacement = instr_data->insn_start + 2
360 - (instr_word & 0x0fff) * 2;
362 nibl_count += 2;
363 break;
364 default:
365 break;
367 break;
368 case CLASS_IMM:
369 switch (datum_value)
371 case ARG_IMM4:
372 instr_data->immediate = instr_nibl;
373 break;
374 case ARG_NIM4:
375 instr_data->immediate = (- instr_nibl) & 0xf;
376 break;
377 case ARG_NIM8:
378 instr_data->immediate = (- instr_byte) & 0xff;
379 nibl_count += 1;
380 break;
381 case ARG_IMM8:
382 instr_data->immediate = instr_byte;
383 nibl_count += 1;
384 break;
385 case ARG_IMM16:
386 instr_data->immediate = instr_word;
387 nibl_count += 3;
388 break;
389 case ARG_IMM32:
390 FETCH_DATA (info, nibl_count + 8);
391 instr_long = (instr_data->words[nibl_count] << 16)
392 | (instr_data->words[nibl_count + 4]);
393 instr_data->immediate = instr_long;
394 nibl_count += 7;
395 break;
396 case ARG_IMMN:
397 instr_data->immediate = instr_nibl - 1;
398 break;
399 case ARG_IMM4M1:
400 instr_data->immediate = instr_nibl + 1;
401 break;
402 case ARG_IMM_1:
403 instr_data->immediate = 1;
404 break;
405 case ARG_IMM_2:
406 instr_data->immediate = 2;
407 break;
408 case ARG_IMM2:
409 instr_data->immediate = instr_nibl & 0x3;
410 break;
411 default:
412 break;
414 break;
415 case CLASS_CC:
416 instr_data->cond_code = instr_nibl;
417 break;
418 case CLASS_ADDRESS:
419 if (is_segmented)
421 if (instr_nibl & 0x8)
423 FETCH_DATA (info, nibl_count + 8);
424 instr_long = (instr_data->words[nibl_count] << 16)
425 | (instr_data->words[nibl_count + 4]);
426 instr_data->address = ((instr_word & 0x7f00) << 16)
427 + (instr_long & 0xffff);
428 nibl_count += 7;
429 seg_length = 2;
431 else
433 instr_data->address = ((instr_word & 0x7f00) << 16)
434 + (instr_word & 0x00ff);
435 nibl_count += 3;
438 else
440 instr_data->address = instr_word;
441 nibl_count += 3;
443 break;
444 case CLASS_0CCC:
445 case CLASS_1CCC:
446 instr_data->ctrl_code = instr_nibl & 0x7;
447 break;
448 case CLASS_0DISP7:
449 instr_data->displacement =
450 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
451 nibl_count += 1;
452 break;
453 case CLASS_1DISP7:
454 instr_data->displacement =
455 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
456 nibl_count += 1;
457 break;
458 case CLASS_01II:
459 instr_data->interrupts = instr_nibl & 0x3;
460 break;
461 case CLASS_00II:
462 instr_data->interrupts = instr_nibl & 0x3;
463 break;
464 case CLASS_IGNORE:
465 case CLASS_BIT:
466 instr_data->ctrl_code = instr_nibl & 0x7;
467 break;
468 case CLASS_FLAGS:
469 instr_data->flags = instr_nibl;
470 break;
471 case CLASS_REG:
472 instr_data->arg_reg[datum_value] = instr_nibl;
473 break;
474 case CLASS_REGN0:
475 instr_data->arg_reg[datum_value] = instr_nibl;
476 break;
477 case CLASS_DISP8:
478 instr_data->displacement =
479 instr_data->insn_start + 2 + (signed char) instr_byte * 2;
480 nibl_count += 1;
481 break;
482 case CLASS_BIT_1OR2:
483 instr_data->immediate = ((instr_nibl >> 1) & 0x1) + 1;
484 nibl_count += 1;
485 break;
486 default:
487 abort ();
488 break;
491 loop += 1;
492 nibl_count += 1;
496 static char *intr_names[] = {
497 "all", /* 0 */
498 "vi", /* 1 */
499 "nvi", /* 2 */
500 "none" /* 3 */
503 static void
504 unparse_instr (instr_data, is_segmented)
505 instr_data_s *instr_data;
506 int is_segmented;
508 unsigned short datum_value;
509 unsigned int tabl_datum, datum_class;
510 int loop, loop_limit;
511 char out_str[80], tmp_str[25];
513 sprintf (out_str, "%s\t", z8k_table[instr_data->tabl_index].name);
515 loop_limit = z8k_table[instr_data->tabl_index].noperands;
516 for (loop = 0; loop < loop_limit; loop++)
518 if (loop)
519 strcat (out_str, ",");
521 tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop];
522 datum_class = tabl_datum & CLASS_MASK;
523 datum_value = tabl_datum & ~CLASS_MASK;
525 switch (datum_class)
527 case CLASS_X:
528 sprintf (tmp_str, "0x%0lx(r%ld)", instr_data->address,
529 instr_data->arg_reg[datum_value]);
530 strcat (out_str, tmp_str);
531 break;
532 case CLASS_BA:
533 if (is_segmented)
534 sprintf (tmp_str, "rr%ld(#0x%lx)", instr_data->arg_reg[datum_value],
535 instr_data->immediate);
536 else
537 sprintf (tmp_str, "r%ld(#0x%lx)", instr_data->arg_reg[datum_value],
538 instr_data->immediate);
539 strcat (out_str, tmp_str);
540 break;
541 case CLASS_BX:
542 if (is_segmented)
543 sprintf (tmp_str, "rr%ld(r%ld)", instr_data->arg_reg[datum_value],
544 instr_data->arg_reg[ARG_RX]);
545 else
546 sprintf (tmp_str, "r%ld(r%ld)", instr_data->arg_reg[datum_value],
547 instr_data->arg_reg[ARG_RX]);
548 strcat (out_str, tmp_str);
549 break;
550 case CLASS_DISP:
551 sprintf (tmp_str, "0x%0lx", instr_data->displacement);
552 strcat (out_str, tmp_str);
553 break;
554 case CLASS_IMM:
555 if (datum_value == ARG_IMM2) /* True with EI/DI instructions only. */
557 sprintf (tmp_str, "%s", intr_names[instr_data->interrupts]);
558 strcat (out_str, tmp_str);
559 break;
561 sprintf (tmp_str, "#0x%0lx", instr_data->immediate);
562 strcat (out_str, tmp_str);
563 break;
564 case CLASS_CC:
565 sprintf (tmp_str, "%s", codes[instr_data->cond_code]);
566 strcat (out_str, tmp_str);
567 break;
568 case CLASS_CTRL:
569 sprintf (tmp_str, "%s", ctrl_names[instr_data->ctrl_code]);
570 strcat (out_str, tmp_str);
571 break;
572 case CLASS_DA:
573 case CLASS_ADDRESS:
574 sprintf (tmp_str, "0x%0lx", instr_data->address);
575 strcat (out_str, tmp_str);
576 break;
577 case CLASS_IR:
578 if (is_segmented)
579 sprintf (tmp_str, "@rr%ld", instr_data->arg_reg[datum_value]);
580 else
581 sprintf (tmp_str, "@r%ld", instr_data->arg_reg[datum_value]);
582 strcat (out_str, tmp_str);
583 break;
584 case CLASS_FLAGS:
585 sprintf (tmp_str, "0x%0lx", instr_data->flags);
586 strcat (out_str, tmp_str);
587 break;
588 case CLASS_REG_BYTE:
589 if (instr_data->arg_reg[datum_value] >= 0x8)
590 sprintf (tmp_str, "rl%ld",
591 instr_data->arg_reg[datum_value] - 0x8);
592 else
593 sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
594 strcat (out_str, tmp_str);
595 break;
596 case CLASS_REG_WORD:
597 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
598 strcat (out_str, tmp_str);
599 break;
600 case CLASS_REG_QUAD:
601 sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]);
602 strcat (out_str, tmp_str);
603 break;
604 case CLASS_REG_LONG:
605 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
606 strcat (out_str, tmp_str);
607 break;
608 case CLASS_PR:
609 if (is_segmented)
610 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
611 else
612 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
613 strcat (out_str, tmp_str);
614 break;
615 default:
616 abort ();
617 break;
621 strcpy (instr_data->instr_asmsrc, out_str);