Fix assembly of "callt 0x3f"
[binutils.git] / opcodes / z8k-dis.c
blobd375e6908d223fc4170487d9ebe5cf6d58d1a11a
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 unsigned short instr_nibl;
213 unsigned short tabl_datum, datum_class, datum_value;
215 nibl_matched = 0;
216 tabl_index = 0;
217 while (!nibl_matched && z8k_table[tabl_index].name)
219 nibl_matched = 1;
220 for (nibl_index = 0;
221 nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched;
222 nibl_index++)
224 if ((nibl_index % 4) == 0)
225 /* Fetch one word at a time. */
226 FETCH_DATA (info, nibl_index + 4);
227 instr_nibl = nibbles[nibl_index];
229 tabl_datum = z8k_table[tabl_index].byte_info[nibl_index];
230 datum_class = tabl_datum & CLASS_MASK;
231 datum_value = ~CLASS_MASK & tabl_datum;
233 switch (datum_class)
235 case CLASS_BIT:
236 if (datum_value != instr_nibl)
237 nibl_matched = 0;
238 break;
239 case CLASS_IGNORE:
240 break;
241 case CLASS_00II:
242 if (!((~instr_nibl) & 0x4))
243 nibl_matched = 0;
244 break;
245 case CLASS_01II:
246 if (!(instr_nibl & 0x4))
247 nibl_matched = 0;
248 break;
249 case CLASS_0CCC:
250 if (!((~instr_nibl) & 0x8))
251 nibl_matched = 0;
252 break;
253 case CLASS_1CCC:
254 if (!(instr_nibl & 0x8))
255 nibl_matched = 0;
256 break;
257 case CLASS_0DISP7:
258 if (!((~instr_nibl) & 0x8))
259 nibl_matched = 0;
260 nibl_index += 1;
261 break;
262 case CLASS_1DISP7:
263 if (!(instr_nibl & 0x8))
264 nibl_matched = 0;
265 nibl_index += 1;
266 break;
267 case CLASS_REGN0:
268 if (instr_nibl == 0)
269 nibl_matched = 0;
270 break;
271 case CLASS_BIT_1OR2:
272 if ((instr_nibl | 0x2) != (datum_value | 0x2))
273 nibl_matched = 0;
274 break;
275 default:
276 break;
280 if (nibl_matched)
281 return tabl_index;
283 tabl_index++;
285 return -1;
288 static void
289 output_instr (instr_data, addr, info)
290 instr_data_s *instr_data;
291 unsigned long addr ATTRIBUTE_UNUSED;
292 disassemble_info *info;
294 int num_bytes;
295 char out_str[100];
297 out_str[0] = 0;
299 num_bytes = (z8k_table[instr_data->tabl_index].length + seg_length) * 2;
300 FETCH_DATA (info, num_bytes);
302 strcat (out_str, instr_data->instr_asmsrc);
304 (*info->fprintf_func) (info->stream, "%s", out_str);
307 static void
308 unpack_instr (instr_data, is_segmented, info)
309 instr_data_s *instr_data;
310 int is_segmented;
311 disassemble_info *info;
313 int nibl_count, loop;
314 unsigned short instr_nibl, instr_byte, instr_word;
315 long instr_long;
316 unsigned int tabl_datum, datum_class;
317 unsigned short datum_value;
319 nibl_count = 0;
320 loop = 0;
321 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)
346 /* Negative 12 bit displacement. */
347 instr_data->displacement = instr_data->insn_start + 2
348 - (signed short) ((instr_word & 0xfff) | 0xf000) * 2;
349 else
350 instr_data->displacement = instr_data->insn_start + 2
351 - (instr_word & 0x0fff) * 2;
353 nibl_count += 2;
354 break;
355 default:
356 break;
358 break;
359 case CLASS_IMM:
360 switch (datum_value)
362 case ARG_IMM4:
363 instr_data->immediate = instr_nibl;
364 break;
365 case ARG_NIM4:
366 instr_data->immediate = (- instr_nibl) & 0xf;
367 break;
368 case ARG_NIM8:
369 instr_data->immediate = (- instr_byte) & 0xff;
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_IGNORE:
456 case CLASS_BIT:
457 instr_data->ctrl_code = instr_nibl & 0x7;
458 break;
459 case CLASS_FLAGS:
460 instr_data->flags = instr_nibl;
461 break;
462 case CLASS_REG:
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 case CLASS_DISP8:
469 instr_data->displacement =
470 instr_data->insn_start + 2 + (signed char) instr_byte * 2;
471 nibl_count += 1;
472 break;
473 case CLASS_BIT_1OR2:
474 instr_data->immediate = ((instr_nibl >> 1) & 0x1) + 1;
475 nibl_count += 1;
476 break;
477 default:
478 abort ();
479 break;
482 loop += 1;
483 nibl_count += 1;
487 static void
488 unparse_instr (instr_data, is_segmented)
489 instr_data_s *instr_data;
490 int is_segmented;
492 unsigned short datum_value;
493 unsigned int tabl_datum, datum_class;
494 int loop, loop_limit;
495 char out_str[80], tmp_str[25];
497 sprintf (out_str, "%s\t", z8k_table[instr_data->tabl_index].name);
499 loop_limit = z8k_table[instr_data->tabl_index].noperands;
500 for (loop = 0; loop < loop_limit; loop++)
502 if (loop)
503 strcat (out_str, ",");
505 tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop];
506 datum_class = tabl_datum & CLASS_MASK;
507 datum_value = tabl_datum & ~CLASS_MASK;
509 switch (datum_class)
511 case CLASS_X:
512 sprintf (tmp_str, "0x%0lx(r%ld)", instr_data->address,
513 instr_data->arg_reg[datum_value]);
514 strcat (out_str, tmp_str);
515 break;
516 case CLASS_BA:
517 if (is_segmented)
518 sprintf (tmp_str, "rr%ld(#%lx)", instr_data->arg_reg[datum_value],
519 instr_data->immediate);
520 else
521 sprintf (tmp_str, "r%ld(#%lx)", instr_data->arg_reg[datum_value],
522 instr_data->immediate);
523 strcat (out_str, tmp_str);
524 break;
525 case CLASS_BX:
526 if (is_segmented)
527 sprintf (tmp_str, "rr%ld(r%ld)", instr_data->arg_reg[datum_value],
528 instr_data->arg_reg[ARG_RX]);
529 else
530 sprintf (tmp_str, "r%ld(r%ld)", instr_data->arg_reg[datum_value],
531 instr_data->arg_reg[ARG_RX]);
532 strcat (out_str, tmp_str);
533 break;
534 case CLASS_DISP:
535 sprintf (tmp_str, "0x%0lx", instr_data->displacement);
536 strcat (out_str, tmp_str);
537 break;
538 case CLASS_IMM:
539 sprintf (tmp_str, "#0x%0lx", instr_data->immediate);
540 strcat (out_str, tmp_str);
541 break;
542 case CLASS_CC:
543 sprintf (tmp_str, "%s", codes[instr_data->cond_code]);
544 strcat (out_str, tmp_str);
545 break;
546 case CLASS_CTRL:
547 sprintf (tmp_str, "%s", ctrl_names[instr_data->ctrl_code]);
548 strcat (out_str, tmp_str);
549 break;
550 case CLASS_DA:
551 case CLASS_ADDRESS:
552 sprintf (tmp_str, "0x%0lx", instr_data->address);
553 strcat (out_str, tmp_str);
554 break;
555 case CLASS_IR:
556 if (is_segmented)
557 sprintf (tmp_str, "@rr%ld", instr_data->arg_reg[datum_value]);
558 else
559 sprintf (tmp_str, "@r%ld", instr_data->arg_reg[datum_value]);
560 strcat (out_str, tmp_str);
561 break;
562 case CLASS_FLAGS:
563 sprintf (tmp_str, "0x%0lx", instr_data->flags);
564 strcat (out_str, tmp_str);
565 break;
566 case CLASS_REG_BYTE:
567 if (instr_data->arg_reg[datum_value] >= 0x8)
568 sprintf (tmp_str, "rl%ld",
569 instr_data->arg_reg[datum_value] - 0x8);
570 else
571 sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
572 strcat (out_str, tmp_str);
573 break;
574 case CLASS_REG_WORD:
575 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
576 strcat (out_str, tmp_str);
577 break;
578 case CLASS_REG_QUAD:
579 sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]);
580 strcat (out_str, tmp_str);
581 break;
582 case CLASS_REG_LONG:
583 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
584 strcat (out_str, tmp_str);
585 break;
586 case CLASS_PR:
587 if (is_segmented)
588 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
589 else
590 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
591 strcat (out_str, tmp_str);
592 break;
593 default:
594 abort ();
595 break;
599 strcpy (instr_data->instr_asmsrc, out_str);