s/Linux/.../
[deliverable/binutils-gdb.git] / opcodes / z8k-dis.c
CommitLineData
252b5132 1/* Disassemble z8000 code.
7f6621cd 2 Copyright 1992, 1993, 1998, 2000, 2001
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 28\f
ec22bdda 29typedef struct {
252b5132
RH
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];
35
36 /* Nibble number of first word not yet fetched. */
37 int max_fetched;
38 bfd_vma insn_start;
39 jmp_buf bailout;
40
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;
ec22bdda 51} instr_data_s;
252b5132 52
d83c6548
AJ
53static int fetch_data PARAMS ((struct disassemble_info *, int));
54
55
252b5132
RH
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) \
ec22bdda 60 ((nibble) < ((instr_data_s *) (info->private_data))->max_fetched \
252b5132
RH
61 ? 1 : fetch_data ((info), (nibble)))
62
63static int
64fetch_data (info, nibble)
65 struct disassemble_info *info;
66 int nibble;
67{
68 unsigned char mybuf[20];
69 int status;
5c90f90d 70 instr_data_s *priv = (instr_data_s *) info->private_data;
252b5132
RH
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;
5c90f90d
KH
87 unsigned char *p = mybuf;
88
252b5132
RH
89 for (i = 0; i < nibble;)
90 {
91 priv->words[i] = (p[0] << 8) | p[1];
5c90f90d 92
252b5132
RH
93 priv->bytes[i] = *p;
94 priv->nibbles[i++] = *p >> 4;
5c90f90d 95 priv->nibbles[i++] = *p & 0xf;
252b5132
RH
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
ec22bdda 109static char *codes[16] = {
252b5132
RH
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
ec22bdda 128static char *ctrl_names[8] = {
6840198f
NC
129 "<invld>",
130 "flags",
131 "fcw",
132 "refresh",
133 "psapseg",
134 "psapoff",
135 "nspseg",
136 "nspoff"
137};
138
139static int seg_length;
d83c6548 140static int print_insn_z8k PARAMS ((bfd_vma, disassemble_info *, int));
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 *));
879db8be 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;
252b5132
RH
276}
277
278static void
279output_instr (instr_data, addr, info)
280 instr_data_s *instr_data;
d83c6548 281 unsigned long addr ATTRIBUTE_UNUSED;
252b5132
RH
282 disassemble_info *info;
283{
284 int loop, loop_limit;
285 char tmp_str[20];
286 char out_str[100];
287
288 strcpy (out_str, "\t");
289
6840198f 290 loop_limit = (z8k_table[instr_data->tabl_index].length + seg_length) * 2;
252b5132
RH
291 FETCH_DATA (info, loop_limit);
292 for (loop = 0; loop < loop_limit; loop++)
293 {
294 sprintf (tmp_str, "%x", instr_data->nibbles[loop]);
295 strcat (out_str, tmp_str);
296 }
297
298 while (loop++ < 8)
299 {
300 strcat (out_str, " ");
301 }
302
303 strcat (out_str, instr_data->instr_asmsrc);
304
305 (*info->fprintf_func) (info->stream, "%s", out_str);
306}
307
308static void
309unpack_instr (instr_data, is_segmented, info)
310 instr_data_s *instr_data;
311 int is_segmented;
312 disassemble_info *info;
313{
314 int nibl_count, loop;
315 unsigned short instr_nibl, instr_byte, instr_word;
316 long instr_long;
6840198f
NC
317 unsigned int tabl_datum, datum_class;
318 unsigned short datum_value;
252b5132
RH
319
320 nibl_count = 0;
321 loop = 0;
6840198f 322 seg_length = 0;
252b5132
RH
323 while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0)
324 {
325 FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4));
326 instr_nibl = instr_data->nibbles[nibl_count];
ec22bdda
KH
327 instr_byte = instr_data->bytes[nibl_count & ~1];
328 instr_word = instr_data->words[nibl_count & ~3];
252b5132
RH
329
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;
333
334 switch (datum_class)
335 {
252b5132
RH
336 case CLASS_DISP:
337 switch (datum_value)
338 {
339 case ARG_DISP16:
7f6621cd
KH
340 instr_data->displacement = instr_data->insn_start + 4
341 + (signed short) (instr_word & 0xffff);
252b5132
RH
342 nibl_count += 3;
343 break;
344 case ARG_DISP12:
7f6621cd 345 if (instr_word & 0x800)
ec22bdda
KH
346 {
347 /* neg. 12 bit displacement */
348 instr_data->displacement = instr_data->insn_start + 2
349 - (signed short) ((instr_word & 0xfff) | 0xf000) * 2;
350 }
7f6621cd 351 else
ec22bdda
KH
352 {
353 instr_data->displacement = instr_data->insn_start + 2
354 - (instr_word & 0x0fff) * 2;
7f6621cd 355 }
252b5132
RH
356 nibl_count += 2;
357 break;
358 default:
359 break;
360 }
361 break;
362 case CLASS_IMM:
363 switch (datum_value)
364 {
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;
404 }
405 break;
406 case CLASS_CC:
407 instr_data->cond_code = instr_nibl;
408 break;
252b5132
RH
409 case CLASS_ADDRESS:
410 if (is_segmented)
411 {
412 if (instr_nibl & 0x8)
413 {
414 FETCH_DATA (info, nibl_count + 8);
415 instr_long = (instr_data->words[nibl_count] << 16)
416 | (instr_data->words[nibl_count + 4]);
7f6621cd
KH
417 instr_data->address = ((instr_word & 0x7f00) << 8)
418 + (instr_long & 0xffff);
252b5132 419 nibl_count += 7;
7f6621cd 420 seg_length = 2;
252b5132
RH
421 }
422 else
423 {
7f6621cd
KH
424 instr_data->address = ((instr_word & 0x7f00) << 8)
425 + (instr_word & 0x00ff);
252b5132
RH
426 nibl_count += 3;
427 }
428 }
429 else
430 {
431 instr_data->address = instr_word;
432 nibl_count += 3;
433 }
434 break;
435 case CLASS_0CCC:
252b5132 436 case CLASS_1CCC:
6840198f 437 instr_data->ctrl_code = instr_nibl & 0x7;
252b5132
RH
438 break;
439 case CLASS_0DISP7:
7f6621cd
KH
440 instr_data->displacement =
441 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
252b5132
RH
442 nibl_count += 1;
443 break;
444 case CLASS_1DISP7:
7f6621cd
KH
445 instr_data->displacement =
446 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
252b5132
RH
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:
6840198f 456 instr_data->ctrl_code = instr_nibl & 0x7;
252b5132 457 break;
252b5132
RH
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;
252b5132
RH
464 case CLASS_REGN0:
465 instr_data->arg_reg[datum_value] = instr_nibl;
466 break;
7f6621cd
KH
467 case CLASS_DISP8:
468 instr_data->displacement =
469 instr_data->insn_start + 2 + (signed char) instr_byte * 2;
6840198f 470 nibl_count += 1;
7f6621cd 471 break;
252b5132 472 default:
7f6621cd 473 abort ();
252b5132
RH
474 break;
475 }
476
477 loop += 1;
478 nibl_count += 1;
479 }
480}
481
482static void
ec22bdda 483unparse_instr (instr_data, is_segmented)
252b5132 484 instr_data_s *instr_data;
6840198f 485 int is_segmented;
252b5132 486{
6840198f
NC
487 unsigned short datum_value;
488 unsigned int tabl_datum, datum_class;
252b5132
RH
489 int loop, loop_limit;
490 char out_str[80], tmp_str[25];
491
492 sprintf (out_str, "\t%s\t", z8k_table[instr_data->tabl_index].name);
493
494 loop_limit = z8k_table[instr_data->tabl_index].noperands;
495 for (loop = 0; loop < loop_limit; loop++)
496 {
497 if (loop)
498 strcat (out_str, ",");
499
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;
503
504 switch (datum_class)
505 {
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:
6840198f 522 sprintf (tmp_str, "0x%0lx", instr_data->displacement);
252b5132
RH
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:
6840198f 534 sprintf (tmp_str, "%s", ctrl_names[instr_data->ctrl_code]);
252b5132
RH
535 strcat (out_str, tmp_str);
536 break;
537 case CLASS_DA:
538 case CLASS_ADDRESS:
6840198f 539 sprintf (tmp_str, "0x%0lx", instr_data->address);
252b5132
RH
540 strcat (out_str, tmp_str);
541 break;
542 case CLASS_IR:
a5d2034a
NC
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]);
252b5132
RH
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)
a5d2034a
NC
555 sprintf (tmp_str, "rl%ld",
556 instr_data->arg_reg[datum_value] - 0x8);
252b5132 557 else
a5d2034a 558 sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
252b5132
RH
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;
6840198f 573 case CLASS_PR:
7f6621cd
KH
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]);
6840198f
NC
578 strcat (out_str, tmp_str);
579 break;
252b5132 580 default:
7f6621cd 581 abort ();
252b5132
RH
582 break;
583 }
584 }
585
586 strcpy (instr_data->instr_asmsrc, out_str);
587}
This page took 0.134909 seconds and 4 git commands to generate.