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