* mcore-dis.c: Fix formatting.
[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
RH
52
53/* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
54 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
55 on error. */
56#define FETCH_DATA(info, nibble) \
ec22bdda 57 ((nibble) < ((instr_data_s *) (info->private_data))->max_fetched \
252b5132
RH
58 ? 1 : fetch_data ((info), (nibble)))
59
60static int
61fetch_data (info, nibble)
62 struct disassemble_info *info;
63 int nibble;
64{
65 unsigned char mybuf[20];
66 int status;
5c90f90d 67 instr_data_s *priv = (instr_data_s *) info->private_data;
252b5132
RH
68
69 if ((nibble % 4) != 0)
70 abort ();
71
72 status = (*info->read_memory_func) (priv->insn_start,
73 (bfd_byte *) mybuf,
74 nibble / 2,
75 info);
76 if (status != 0)
77 {
78 (*info->memory_error_func) (status, priv->insn_start, info);
79 longjmp (priv->bailout, 1);
80 }
81
82 {
83 int i;
5c90f90d
KH
84 unsigned char *p = mybuf;
85
252b5132
RH
86 for (i = 0; i < nibble;)
87 {
88 priv->words[i] = (p[0] << 8) | p[1];
5c90f90d 89
252b5132
RH
90 priv->bytes[i] = *p;
91 priv->nibbles[i++] = *p >> 4;
5c90f90d 92 priv->nibbles[i++] = *p & 0xf;
252b5132
RH
93
94 ++p;
95 priv->bytes[i] = *p;
96 priv->nibbles[i++] = *p >> 4;
97 priv->nibbles[i++] = *p & 0xf;
98
99 ++p;
100 }
101 }
102 priv->max_fetched = nibble;
103 return 1;
104}
105
ec22bdda 106static char *codes[16] = {
252b5132
RH
107 "f",
108 "lt",
109 "le",
110 "ule",
111 "ov/pe",
112 "mi",
113 "eq",
114 "c/ult",
115 "t",
116 "ge",
117 "gt",
118 "ugt",
119 "nov/po",
120 "pl",
121 "ne",
122 "nc/uge"
123};
124
ec22bdda 125static char *ctrl_names[8] = {
6840198f
NC
126 "<invld>",
127 "flags",
128 "fcw",
129 "refresh",
130 "psapseg",
131 "psapoff",
132 "nspseg",
133 "nspoff"
134};
135
136static int seg_length;
5c90f90d 137int z8k_lookup_instr PARAMS ((unsigned char *, disassemble_info *));
252b5132
RH
138static void output_instr
139 PARAMS ((instr_data_s *, unsigned long, disassemble_info *));
140static void unpack_instr PARAMS ((instr_data_s *, int, disassemble_info *));
879db8be 141static void unparse_instr PARAMS ((instr_data_s *, int));
252b5132
RH
142
143static int
144print_insn_z8k (addr, info, is_segmented)
145 bfd_vma addr;
146 disassemble_info *info;
147 int is_segmented;
148{
149 instr_data_s instr_data;
150
151 info->private_data = (PTR) &instr_data;
152 instr_data.max_fetched = 0;
153 instr_data.insn_start = addr;
154 if (setjmp (instr_data.bailout) != 0)
155 /* Error return. */
156 return -1;
157
158 instr_data.tabl_index = z8k_lookup_instr (instr_data.nibbles, info);
159 if (instr_data.tabl_index > 0)
160 {
161 unpack_instr (&instr_data, is_segmented, info);
6840198f 162 unparse_instr (&instr_data, is_segmented);
252b5132 163 output_instr (&instr_data, addr, info);
6840198f 164 return z8k_table[instr_data.tabl_index].length + seg_length;
252b5132
RH
165 }
166 else
167 {
168 FETCH_DATA (info, 4);
169 (*info->fprintf_func) (info->stream, ".word %02x%02x",
170 instr_data.bytes[0], instr_data.bytes[2]);
171 return 2;
172 }
173}
174
175int
176print_insn_z8001 (addr, info)
177 bfd_vma addr;
178 disassemble_info *info;
179{
180 return print_insn_z8k (addr, info, 1);
181}
182
183int
184print_insn_z8002 (addr, info)
185 bfd_vma addr;
186 disassemble_info *info;
187{
188 return print_insn_z8k (addr, info, 0);
189}
190
191int
192z8k_lookup_instr (nibbles, info)
193 unsigned char *nibbles;
194 disassemble_info *info;
195{
196
197 int nibl_index, tabl_index;
198 int nibl_matched;
199 unsigned short instr_nibl;
200 unsigned short tabl_datum, datum_class, datum_value;
201
202 nibl_matched = 0;
203 tabl_index = 0;
204 while (!nibl_matched && z8k_table[tabl_index].name)
205 {
206 nibl_matched = 1;
5c90f90d
KH
207 for (nibl_index = 0;
208 nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched;
209 nibl_index++)
252b5132
RH
210 {
211 if ((nibl_index % 4) == 0)
212 /* Fetch one word at a time. */
213 FETCH_DATA (info, nibl_index + 4);
214 instr_nibl = nibbles[nibl_index];
215
216 tabl_datum = z8k_table[tabl_index].byte_info[nibl_index];
217 datum_class = tabl_datum & CLASS_MASK;
218 datum_value = ~CLASS_MASK & tabl_datum;
219
220 switch (datum_class)
221 {
222 case CLASS_BIT:
223 if (datum_value != instr_nibl)
224 nibl_matched = 0;
225 break;
226 case CLASS_00II:
227 if (!((~instr_nibl) & 0x4))
228 nibl_matched = 0;
229 break;
230 case CLASS_01II:
231 if (!(instr_nibl & 0x4))
232 nibl_matched = 0;
233 break;
234 case CLASS_0CCC:
235 if (!((~instr_nibl) & 0x8))
236 nibl_matched = 0;
237 break;
238 case CLASS_1CCC:
239 if (!(instr_nibl & 0x8))
240 nibl_matched = 0;
241 break;
242 case CLASS_0DISP7:
243 if (!((~instr_nibl) & 0x8))
244 nibl_matched = 0;
245 nibl_index += 1;
246 break;
247 case CLASS_1DISP7:
248 if (!(instr_nibl & 0x8))
249 nibl_matched = 0;
250 nibl_index += 1;
251 break;
252 case CLASS_REGN0:
253 if (instr_nibl == 0)
254 nibl_matched = 0;
255 break;
256 case CLASS_BIT_1OR2:
257 if ((instr_nibl | 0x2) != (datum_value | 0x2))
258 nibl_matched = 0;
259 break;
260 default:
261 break;
262 }
263 }
264 if (nibl_matched)
265 {
266 return tabl_index;
267 }
268
269 tabl_index++;
270 }
271 return -1;
252b5132
RH
272}
273
274static void
275output_instr (instr_data, addr, info)
276 instr_data_s *instr_data;
277 unsigned long addr;
278 disassemble_info *info;
279{
280 int loop, loop_limit;
281 char tmp_str[20];
282 char out_str[100];
283
284 strcpy (out_str, "\t");
285
6840198f 286 loop_limit = (z8k_table[instr_data->tabl_index].length + seg_length) * 2;
252b5132
RH
287 FETCH_DATA (info, loop_limit);
288 for (loop = 0; loop < loop_limit; loop++)
289 {
290 sprintf (tmp_str, "%x", instr_data->nibbles[loop]);
291 strcat (out_str, tmp_str);
292 }
293
294 while (loop++ < 8)
295 {
296 strcat (out_str, " ");
297 }
298
299 strcat (out_str, instr_data->instr_asmsrc);
300
301 (*info->fprintf_func) (info->stream, "%s", out_str);
302}
303
304static void
305unpack_instr (instr_data, is_segmented, info)
306 instr_data_s *instr_data;
307 int is_segmented;
308 disassemble_info *info;
309{
310 int nibl_count, loop;
311 unsigned short instr_nibl, instr_byte, instr_word;
312 long instr_long;
6840198f
NC
313 unsigned int tabl_datum, datum_class;
314 unsigned short datum_value;
252b5132
RH
315
316 nibl_count = 0;
317 loop = 0;
6840198f 318 seg_length = 0;
252b5132
RH
319 while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0)
320 {
321 FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4));
322 instr_nibl = instr_data->nibbles[nibl_count];
ec22bdda
KH
323 instr_byte = instr_data->bytes[nibl_count & ~1];
324 instr_word = instr_data->words[nibl_count & ~3];
252b5132
RH
325
326 tabl_datum = z8k_table[instr_data->tabl_index].byte_info[loop];
327 datum_class = tabl_datum & CLASS_MASK;
328 datum_value = tabl_datum & ~CLASS_MASK;
329
330 switch (datum_class)
331 {
252b5132
RH
332 case CLASS_DISP:
333 switch (datum_value)
334 {
335 case ARG_DISP16:
7f6621cd
KH
336 instr_data->displacement = instr_data->insn_start + 4
337 + (signed short) (instr_word & 0xffff);
252b5132
RH
338 nibl_count += 3;
339 break;
340 case ARG_DISP12:
7f6621cd 341 if (instr_word & 0x800)
ec22bdda
KH
342 {
343 /* neg. 12 bit displacement */
344 instr_data->displacement = instr_data->insn_start + 2
345 - (signed short) ((instr_word & 0xfff) | 0xf000) * 2;
346 }
7f6621cd 347 else
ec22bdda
KH
348 {
349 instr_data->displacement = instr_data->insn_start + 2
350 - (instr_word & 0x0fff) * 2;
7f6621cd 351 }
252b5132
RH
352 nibl_count += 2;
353 break;
354 default:
355 break;
356 }
357 break;
358 case CLASS_IMM:
359 switch (datum_value)
360 {
361 case ARG_IMM4:
362 instr_data->immediate = instr_nibl;
363 break;
364 case ARG_NIM8:
365 instr_data->immediate = (-instr_byte);
366 nibl_count += 1;
367 break;
368 case ARG_IMM8:
369 instr_data->immediate = instr_byte;
370 nibl_count += 1;
371 break;
372 case ARG_IMM16:
373 instr_data->immediate = instr_word;
374 nibl_count += 3;
375 break;
376 case ARG_IMM32:
377 FETCH_DATA (info, nibl_count + 8);
378 instr_long = (instr_data->words[nibl_count] << 16)
379 | (instr_data->words[nibl_count + 4]);
380 instr_data->immediate = instr_long;
381 nibl_count += 7;
382 break;
383 case ARG_IMMN:
384 instr_data->immediate = instr_nibl - 1;
385 break;
386 case ARG_IMM4M1:
387 instr_data->immediate = instr_nibl + 1;
388 break;
389 case ARG_IMM_1:
390 instr_data->immediate = 1;
391 break;
392 case ARG_IMM_2:
393 instr_data->immediate = 2;
394 break;
395 case ARG_IMM2:
396 instr_data->immediate = instr_nibl & 0x3;
397 break;
398 default:
399 break;
400 }
401 break;
402 case CLASS_CC:
403 instr_data->cond_code = instr_nibl;
404 break;
252b5132
RH
405 case CLASS_ADDRESS:
406 if (is_segmented)
407 {
408 if (instr_nibl & 0x8)
409 {
410 FETCH_DATA (info, nibl_count + 8);
411 instr_long = (instr_data->words[nibl_count] << 16)
412 | (instr_data->words[nibl_count + 4]);
7f6621cd
KH
413 instr_data->address = ((instr_word & 0x7f00) << 8)
414 + (instr_long & 0xffff);
252b5132 415 nibl_count += 7;
7f6621cd 416 seg_length = 2;
252b5132
RH
417 }
418 else
419 {
7f6621cd
KH
420 instr_data->address = ((instr_word & 0x7f00) << 8)
421 + (instr_word & 0x00ff);
252b5132
RH
422 nibl_count += 3;
423 }
424 }
425 else
426 {
427 instr_data->address = instr_word;
428 nibl_count += 3;
429 }
430 break;
431 case CLASS_0CCC:
252b5132 432 case CLASS_1CCC:
6840198f 433 instr_data->ctrl_code = instr_nibl & 0x7;
252b5132
RH
434 break;
435 case CLASS_0DISP7:
7f6621cd
KH
436 instr_data->displacement =
437 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
252b5132
RH
438 nibl_count += 1;
439 break;
440 case CLASS_1DISP7:
7f6621cd
KH
441 instr_data->displacement =
442 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
252b5132
RH
443 nibl_count += 1;
444 break;
445 case CLASS_01II:
446 instr_data->interrupts = instr_nibl & 0x3;
447 break;
448 case CLASS_00II:
449 instr_data->interrupts = instr_nibl & 0x3;
450 break;
451 case CLASS_BIT:
6840198f 452 instr_data->ctrl_code = instr_nibl & 0x7;
252b5132 453 break;
252b5132
RH
454 case CLASS_FLAGS:
455 instr_data->flags = instr_nibl;
456 break;
457 case CLASS_REG:
458 instr_data->arg_reg[datum_value] = instr_nibl;
459 break;
252b5132
RH
460 case CLASS_REGN0:
461 instr_data->arg_reg[datum_value] = instr_nibl;
462 break;
7f6621cd
KH
463 case CLASS_DISP8:
464 instr_data->displacement =
465 instr_data->insn_start + 2 + (signed char) instr_byte * 2;
6840198f 466 nibl_count += 1;
7f6621cd 467 break;
252b5132 468 default:
7f6621cd 469 abort ();
252b5132
RH
470 break;
471 }
472
473 loop += 1;
474 nibl_count += 1;
475 }
476}
477
478static void
ec22bdda 479unparse_instr (instr_data, is_segmented)
252b5132 480 instr_data_s *instr_data;
6840198f 481 int is_segmented;
252b5132 482{
6840198f
NC
483 unsigned short datum_value;
484 unsigned int tabl_datum, datum_class;
252b5132
RH
485 int loop, loop_limit;
486 char out_str[80], tmp_str[25];
487
488 sprintf (out_str, "\t%s\t", z8k_table[instr_data->tabl_index].name);
489
490 loop_limit = z8k_table[instr_data->tabl_index].noperands;
491 for (loop = 0; loop < loop_limit; loop++)
492 {
493 if (loop)
494 strcat (out_str, ",");
495
496 tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop];
497 datum_class = tabl_datum & CLASS_MASK;
498 datum_value = tabl_datum & ~CLASS_MASK;
499
500 switch (datum_class)
501 {
502 case CLASS_X:
503 sprintf (tmp_str, "0x%0lx(R%ld)", instr_data->address,
504 instr_data->arg_reg[datum_value]);
505 strcat (out_str, tmp_str);
506 break;
507 case CLASS_BA:
508 sprintf (tmp_str, "r%ld(#%lx)", instr_data->arg_reg[datum_value],
509 instr_data->immediate);
510 strcat (out_str, tmp_str);
511 break;
512 case CLASS_BX:
513 sprintf (tmp_str, "r%ld(R%ld)", instr_data->arg_reg[datum_value],
514 instr_data->arg_reg[ARG_RX]);
515 strcat (out_str, tmp_str);
516 break;
517 case CLASS_DISP:
6840198f 518 sprintf (tmp_str, "0x%0lx", instr_data->displacement);
252b5132
RH
519 strcat (out_str, tmp_str);
520 break;
521 case CLASS_IMM:
522 sprintf (tmp_str, "#0x%0lx", instr_data->immediate);
523 strcat (out_str, tmp_str);
524 break;
525 case CLASS_CC:
526 sprintf (tmp_str, "%s", codes[instr_data->cond_code]);
527 strcat (out_str, tmp_str);
528 break;
529 case CLASS_CTRL:
6840198f 530 sprintf (tmp_str, "%s", ctrl_names[instr_data->ctrl_code]);
252b5132
RH
531 strcat (out_str, tmp_str);
532 break;
533 case CLASS_DA:
534 case CLASS_ADDRESS:
6840198f 535 sprintf (tmp_str, "0x%0lx", instr_data->address);
252b5132
RH
536 strcat (out_str, tmp_str);
537 break;
538 case CLASS_IR:
539 sprintf (tmp_str, "@R%ld", instr_data->arg_reg[datum_value]);
540 strcat (out_str, tmp_str);
541 break;
542 case CLASS_FLAGS:
543 sprintf (tmp_str, "0x%0lx", instr_data->flags);
544 strcat (out_str, tmp_str);
545 break;
546 case CLASS_REG_BYTE:
547 if (instr_data->arg_reg[datum_value] >= 0x8)
548 {
549 sprintf (tmp_str, "rl%ld",
550 instr_data->arg_reg[datum_value] - 0x8);
551 }
552 else
553 {
554 sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
555 }
556 strcat (out_str, tmp_str);
557 break;
558 case CLASS_REG_WORD:
559 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
560 strcat (out_str, tmp_str);
561 break;
562 case CLASS_REG_QUAD:
563 sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]);
564 strcat (out_str, tmp_str);
565 break;
566 case CLASS_REG_LONG:
567 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
568 strcat (out_str, tmp_str);
569 break;
6840198f 570 case CLASS_PR:
7f6621cd
KH
571 if (is_segmented)
572 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
573 else
574 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
6840198f
NC
575 strcat (out_str, tmp_str);
576 break;
252b5132 577 default:
7f6621cd 578 abort ();
252b5132
RH
579 break;
580 }
581 }
582
583 strcpy (instr_data->instr_asmsrc, out_str);
584}
This page took 0.144903 seconds and 4 git commands to generate.