1 /* Disassembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
4 THIS FILE IS MACHINE GENERATED WITH CGEN.
5 - the resultant file is machine generated, cgen-dis.in isn't
7 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2005
8 Free Software Foundation, Inc.
10 This file is part of the GNU Binutils and GDB, the GNU debugger.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software Foundation, Inc.,
24 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
35 #include "libiberty.h"
36 #include "m32r-desc.h"
40 /* Default text to print if an instruction isn't recognized. */
41 #define UNKNOWN_INSN_MSG _("*unknown*")
43 static void print_normal
44 (CGEN_CPU_DESC, void *, long, unsigned int, bfd_vma, int);
45 static void print_address
46 (CGEN_CPU_DESC, void *, bfd_vma, unsigned int, bfd_vma, int) ATTRIBUTE_UNUSED;
47 static void print_keyword
48 (CGEN_CPU_DESC, void *, CGEN_KEYWORD *, long, unsigned int) ATTRIBUTE_UNUSED;
49 static void print_insn_normal
50 (CGEN_CPU_DESC, void *, const CGEN_INSN *, CGEN_FIELDS *, bfd_vma, int);
52 (CGEN_CPU_DESC, bfd_vma, disassemble_info *, char *, unsigned);
53 static int default_print_insn
54 (CGEN_CPU_DESC, bfd_vma, disassemble_info *) ATTRIBUTE_UNUSED;
56 (CGEN_CPU_DESC, bfd_vma, disassemble_info *, char *, int, CGEN_EXTRACT_INFO *,
59 /* -- disassembler routines inserted here */
62 static void print_hash PARAMS ((CGEN_CPU_DESC, PTR, long, unsigned, bfd_vma, int));
63 static int my_print_insn PARAMS ((CGEN_CPU_DESC, bfd_vma, disassemble_info *));
65 /* Immediate values are prefixed with '#'. */
67 #define CGEN_PRINT_NORMAL(cd, info, value, attrs, pc, length) \
70 if (CGEN_BOOL_ATTR ((attrs), CGEN_OPERAND_HASH_PREFIX)) \
71 (*info->fprintf_func) (info->stream, "#"); \
75 /* Handle '#' prefixes as operands. */
78 print_hash (cd, dis_info, value, attrs, pc, length)
79 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
81 long value ATTRIBUTE_UNUSED;
82 unsigned int attrs ATTRIBUTE_UNUSED;
83 bfd_vma pc ATTRIBUTE_UNUSED;
84 int length ATTRIBUTE_UNUSED;
86 disassemble_info *info = (disassemble_info *) dis_info;
87 (*info->fprintf_func) (info->stream, "#");
90 #undef CGEN_PRINT_INSN
91 #define CGEN_PRINT_INSN my_print_insn
94 my_print_insn (cd, pc, info)
97 disassemble_info *info;
99 char buffer[CGEN_MAX_INSN_SIZE];
102 int buflen = (pc & 3) == 0 ? 4 : 2;
103 int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG;
106 /* Read the base part of the insn. */
108 status = (*info->read_memory_func) (pc - ((!big_p && (pc & 3) != 0) ? 2 : 0),
112 (*info->memory_error_func) (status, pc, info);
117 x = (big_p ? &buf[0] : &buf[3]);
118 if ((pc & 3) == 0 && (*x & 0x80) != 0)
119 return print_insn (cd, pc, info, buf, buflen);
121 /* Print the first insn. */
124 buf += (big_p ? 0 : 2);
125 if (print_insn (cd, pc, info, buf, 2) == 0)
126 (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
127 buf += (big_p ? 2 : -2);
130 x = (big_p ? &buf[0] : &buf[1]);
134 (*info->fprintf_func) (info->stream, " || ");
138 (*info->fprintf_func) (info->stream, " -> ");
140 /* The "& 3" is to pass a consistent address.
141 Parallel insns arguably both begin on the word boundary.
142 Also, branch insns are calculated relative to the word boundary. */
143 if (print_insn (cd, pc & ~ (bfd_vma) 3, info, buf, 2) == 0)
144 (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
146 return (pc & 3) ? 2 : 4;
151 void m32r_cgen_print_operand
152 PARAMS ((CGEN_CPU_DESC, int, PTR, CGEN_FIELDS *,
153 void const *, bfd_vma, int));
155 /* Main entry point for printing operands.
156 XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
157 of dis-asm.h on cgen.h.
159 This function is basically just a big switch statement. Earlier versions
160 used tables to look up the function to use, but
161 - if the table contains both assembler and disassembler functions then
162 the disassembler contains much of the assembler and vice-versa,
163 - there's a lot of inlining possibilities as things grow,
164 - using a switch statement avoids the function call overhead.
166 This function could be moved into `print_insn_normal', but keeping it
167 separate makes clear the interface between `print_insn_normal' and each of
171 m32r_cgen_print_operand (cd, opindex, xinfo, fields, attrs, pc, length)
176 void const *attrs ATTRIBUTE_UNUSED;
180 disassemble_info *info = (disassemble_info *) xinfo;
184 case M32R_OPERAND_ACC :
185 print_keyword (cd, info, & m32r_cgen_opval_h_accums, fields->f_acc, 0);
187 case M32R_OPERAND_ACCD :
188 print_keyword (cd, info, & m32r_cgen_opval_h_accums, fields->f_accd, 0);
190 case M32R_OPERAND_ACCS :
191 print_keyword (cd, info, & m32r_cgen_opval_h_accums, fields->f_accs, 0);
193 case M32R_OPERAND_DCR :
194 print_keyword (cd, info, & m32r_cgen_opval_cr_names, fields->f_r1, 0);
196 case M32R_OPERAND_DISP16 :
197 print_address (cd, info, fields->f_disp16, 0|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
199 case M32R_OPERAND_DISP24 :
200 print_address (cd, info, fields->f_disp24, 0|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
202 case M32R_OPERAND_DISP8 :
203 print_address (cd, info, fields->f_disp8, 0|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
205 case M32R_OPERAND_DR :
206 print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r1, 0);
208 case M32R_OPERAND_HASH :
209 print_hash (cd, info, 0, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
211 case M32R_OPERAND_HI16 :
212 print_normal (cd, info, fields->f_hi16, 0|(1<<CGEN_OPERAND_SIGN_OPT), pc, length);
214 case M32R_OPERAND_IMM1 :
215 print_normal (cd, info, fields->f_imm1, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
217 case M32R_OPERAND_SCR :
218 print_keyword (cd, info, & m32r_cgen_opval_cr_names, fields->f_r2, 0);
220 case M32R_OPERAND_SIMM16 :
221 print_normal (cd, info, fields->f_simm16, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
223 case M32R_OPERAND_SIMM8 :
224 print_normal (cd, info, fields->f_simm8, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
226 case M32R_OPERAND_SLO16 :
227 print_normal (cd, info, fields->f_simm16, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
229 case M32R_OPERAND_SR :
230 print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r2, 0);
232 case M32R_OPERAND_SRC1 :
233 print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r1, 0);
235 case M32R_OPERAND_SRC2 :
236 print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r2, 0);
238 case M32R_OPERAND_UIMM16 :
239 print_normal (cd, info, fields->f_uimm16, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
241 case M32R_OPERAND_UIMM24 :
242 print_address (cd, info, fields->f_uimm24, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_ABS_ADDR), pc, length);
244 case M32R_OPERAND_UIMM3 :
245 print_normal (cd, info, fields->f_uimm3, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
247 case M32R_OPERAND_UIMM4 :
248 print_normal (cd, info, fields->f_uimm4, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
250 case M32R_OPERAND_UIMM5 :
251 print_normal (cd, info, fields->f_uimm5, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
253 case M32R_OPERAND_UIMM8 :
254 print_normal (cd, info, fields->f_uimm8, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
256 case M32R_OPERAND_ULO16 :
257 print_normal (cd, info, fields->f_uimm16, 0, pc, length);
261 /* xgettext:c-format */
262 fprintf (stderr, _("Unrecognized field %d while printing insn.\n"),
268 cgen_print_fn * const m32r_cgen_print_handlers[] =
275 m32r_cgen_init_dis (cd)
278 m32r_cgen_init_opcode_table (cd);
279 m32r_cgen_init_ibld_table (cd);
280 cd->print_handlers = & m32r_cgen_print_handlers[0];
281 cd->print_operand = m32r_cgen_print_operand;
285 /* Default print handler. */
288 print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
292 bfd_vma pc ATTRIBUTE_UNUSED,
293 int length ATTRIBUTE_UNUSED)
295 disassemble_info *info = (disassemble_info *) dis_info;
297 #ifdef CGEN_PRINT_NORMAL
298 CGEN_PRINT_NORMAL (cd, info, value, attrs, pc, length);
301 /* Print the operand as directed by the attributes. */
302 if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
303 ; /* nothing to do */
304 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
305 (*info->fprintf_func) (info->stream, "%ld", value);
307 (*info->fprintf_func) (info->stream, "0x%lx", value);
310 /* Default address handler. */
313 print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
317 bfd_vma pc ATTRIBUTE_UNUSED,
318 int length ATTRIBUTE_UNUSED)
320 disassemble_info *info = (disassemble_info *) dis_info;
322 #ifdef CGEN_PRINT_ADDRESS
323 CGEN_PRINT_ADDRESS (cd, info, value, attrs, pc, length);
326 /* Print the operand as directed by the attributes. */
327 if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
328 ; /* nothing to do */
329 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR))
330 (*info->print_address_func) (value, info);
331 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR))
332 (*info->print_address_func) (value, info);
333 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
334 (*info->fprintf_func) (info->stream, "%ld", (long) value);
336 (*info->fprintf_func) (info->stream, "0x%lx", (long) value);
339 /* Keyword print handler. */
342 print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
344 CGEN_KEYWORD *keyword_table,
346 unsigned int attrs ATTRIBUTE_UNUSED)
348 disassemble_info *info = (disassemble_info *) dis_info;
349 const CGEN_KEYWORD_ENTRY *ke;
351 ke = cgen_keyword_lookup_value (keyword_table, value);
353 (*info->fprintf_func) (info->stream, "%s", ke->name);
355 (*info->fprintf_func) (info->stream, "???");
358 /* Default insn printer.
360 DIS_INFO is defined as `void *' so the disassembler needn't know anything
361 about disassemble_info. */
364 print_insn_normal (CGEN_CPU_DESC cd,
366 const CGEN_INSN *insn,
371 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
372 disassemble_info *info = (disassemble_info *) dis_info;
373 const CGEN_SYNTAX_CHAR_TYPE *syn;
375 CGEN_INIT_PRINT (cd);
377 for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
379 if (CGEN_SYNTAX_MNEMONIC_P (*syn))
381 (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn));
384 if (CGEN_SYNTAX_CHAR_P (*syn))
386 (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn));
390 /* We have an operand. */
391 m32r_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info,
392 fields, CGEN_INSN_ATTRS (insn), pc, length);
396 /* Subroutine of print_insn. Reads an insn into the given buffers and updates
398 Returns 0 if all is well, non-zero otherwise. */
401 read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
403 disassemble_info *info,
406 CGEN_EXTRACT_INFO *ex_info,
407 unsigned long *insn_value)
409 int status = (*info->read_memory_func) (pc, buf, buflen, info);
412 (*info->memory_error_func) (status, pc, info);
416 ex_info->dis_info = info;
417 ex_info->valid = (1 << buflen) - 1;
418 ex_info->insn_bytes = buf;
420 *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG);
424 /* Utility to print an insn.
425 BUF is the base part of the insn, target byte order, BUFLEN bytes long.
426 The result is the size of the insn in bytes or zero for an unknown insn
427 or -1 if an error occurs fetching data (memory_error_func will have
431 print_insn (CGEN_CPU_DESC cd,
433 disassemble_info *info,
437 CGEN_INSN_INT insn_value;
438 const CGEN_INSN_LIST *insn_list;
439 CGEN_EXTRACT_INFO ex_info;
442 /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
443 basesize = cd->base_insn_bitsize < buflen * 8 ?
444 cd->base_insn_bitsize : buflen * 8;
445 insn_value = cgen_get_insn_value (cd, buf, basesize);
448 /* Fill in ex_info fields like read_insn would. Don't actually call
449 read_insn, since the incoming buffer is already read (and possibly
450 modified a la m32r). */
451 ex_info.valid = (1 << buflen) - 1;
452 ex_info.dis_info = info;
453 ex_info.insn_bytes = buf;
455 /* The instructions are stored in hash lists.
456 Pick the first one and keep trying until we find the right one. */
458 insn_list = CGEN_DIS_LOOKUP_INSN (cd, buf, insn_value);
459 while (insn_list != NULL)
461 const CGEN_INSN *insn = insn_list->insn;
464 unsigned long insn_value_cropped;
466 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
467 /* Not needed as insn shouldn't be in hash lists if not supported. */
468 /* Supported by this cpu? */
469 if (! m32r_cgen_insn_supported (cd, insn))
471 insn_list = CGEN_DIS_NEXT_INSN (insn_list);
476 /* Basic bit mask must be correct. */
477 /* ??? May wish to allow target to defer this check until the extract
480 /* Base size may exceed this instruction's size. Extract the
481 relevant part from the buffer. */
482 if ((unsigned) (CGEN_INSN_BITSIZE (insn) / 8) < buflen &&
483 (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
484 insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn),
485 info->endian == BFD_ENDIAN_BIG);
487 insn_value_cropped = insn_value;
489 if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn))
490 == CGEN_INSN_BASE_VALUE (insn))
492 /* Printing is handled in two passes. The first pass parses the
493 machine insn and extracts the fields. The second pass prints
496 /* Make sure the entire insn is loaded into insn_value, if it
498 if (((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize) &&
499 (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
501 unsigned long full_insn_value;
502 int rc = read_insn (cd, pc, info, buf,
503 CGEN_INSN_BITSIZE (insn) / 8,
504 & ex_info, & full_insn_value);
507 length = CGEN_EXTRACT_FN (cd, insn)
508 (cd, insn, &ex_info, full_insn_value, &fields, pc);
511 length = CGEN_EXTRACT_FN (cd, insn)
512 (cd, insn, &ex_info, insn_value_cropped, &fields, pc);
514 /* length < 0 -> error */
519 CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length);
520 /* length is in bits, result is in bytes */
525 insn_list = CGEN_DIS_NEXT_INSN (insn_list);
531 /* Default value for CGEN_PRINT_INSN.
532 The result is the size of the insn in bytes or zero for an unknown insn
533 or -1 if an error occured fetching bytes. */
535 #ifndef CGEN_PRINT_INSN
536 #define CGEN_PRINT_INSN default_print_insn
540 default_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info)
542 char buf[CGEN_MAX_INSN_SIZE];
546 /* Attempt to read the base part of the insn. */
547 buflen = cd->base_insn_bitsize / 8;
548 status = (*info->read_memory_func) (pc, buf, buflen, info);
550 /* Try again with the minimum part, if min < base. */
551 if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
553 buflen = cd->min_insn_bitsize / 8;
554 status = (*info->read_memory_func) (pc, buf, buflen, info);
559 (*info->memory_error_func) (status, pc, info);
563 return print_insn (cd, pc, info, buf, buflen);
567 Print one instruction from PC on INFO->STREAM.
568 Return the size of the instruction (in bytes). */
570 typedef struct cpu_desc_list {
571 struct cpu_desc_list *next;
579 print_insn_m32r (bfd_vma pc, disassemble_info *info)
581 static cpu_desc_list *cd_list = 0;
582 cpu_desc_list *cl = 0;
583 static CGEN_CPU_DESC cd = 0;
585 static int prev_mach;
586 static int prev_endian;
589 int endian = (info->endian == BFD_ENDIAN_BIG
591 : CGEN_ENDIAN_LITTLE);
592 enum bfd_architecture arch;
594 /* ??? gdb will set mach but leave the architecture as "unknown" */
595 #ifndef CGEN_BFD_ARCH
596 #define CGEN_BFD_ARCH bfd_arch_m32r
599 if (arch == bfd_arch_unknown)
600 arch = CGEN_BFD_ARCH;
602 /* There's no standard way to compute the machine or isa number
603 so we leave it to the target. */
604 #ifdef CGEN_COMPUTE_MACH
605 mach = CGEN_COMPUTE_MACH (info);
610 #ifdef CGEN_COMPUTE_ISA
611 isa = CGEN_COMPUTE_ISA (info);
613 isa = info->insn_sets;
616 /* If we've switched cpu's, try to find a handle we've used before */
620 || endian != prev_endian))
623 for (cl = cd_list; cl; cl = cl->next)
625 if (cl->isa == isa &&
627 cl->endian == endian)
635 /* If we haven't initialized yet, initialize the opcode table. */
638 const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach);
639 const char *mach_name;
643 mach_name = arch_type->printable_name;
647 prev_endian = endian;
648 cd = m32r_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa,
649 CGEN_CPU_OPEN_BFDMACH, mach_name,
650 CGEN_CPU_OPEN_ENDIAN, prev_endian,
655 /* save this away for future reference */
656 cl = xmalloc (sizeof (struct cpu_desc_list));
664 m32r_cgen_init_dis (cd);
667 /* We try to have as much common code as possible.
668 But at this point some targets need to take over. */
669 /* ??? Some targets may need a hook elsewhere. Try to avoid this,
670 but if not possible try to move this hook elsewhere rather than
672 length = CGEN_PRINT_INSN (cd, pc, info);
678 (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
679 return cd->default_insn_bitsize / 8;