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 Free Software Foundation, Inc.
9 This file is part of the GNU Binutils and GDB, the GNU debugger.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation, Inc.,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
34 #include "m32r-desc.h"
38 /* Default text to print if an instruction isn't recognized. */
39 #define UNKNOWN_INSN_MSG _("*unknown*")
41 static void print_normal
42 PARAMS ((CGEN_CPU_DESC, PTR, long, unsigned int, bfd_vma, int));
43 static void print_address
44 PARAMS ((CGEN_CPU_DESC, PTR, bfd_vma, unsigned int, bfd_vma, int));
45 static void print_keyword
46 PARAMS ((CGEN_CPU_DESC, PTR, CGEN_KEYWORD *, long, unsigned int));
47 static void print_insn_normal
48 PARAMS ((CGEN_CPU_DESC, PTR, const CGEN_INSN *, CGEN_FIELDS *,
51 PARAMS ((CGEN_CPU_DESC, bfd_vma, disassemble_info *, char *, int));
52 static void print_hash
53 PARAMS ((CGEN_CPU_DESC, PTR, long, unsigned, bfd_vma, int));
54 static int my_print_insn
55 PARAMS ((CGEN_CPU_DESC, bfd_vma, disassemble_info *));
56 void m32r_cgen_print_operand
57 PARAMS ((CGEN_CPU_DESC, int, PTR, CGEN_FIELDS *, void const *, bfd_vma, int));
59 PARAMS ((CGEN_CPU_DESC, bfd_vma, disassemble_info *, char *, int,
60 CGEN_EXTRACT_INFO *, unsigned long *));
62 /* -- disassembler routines inserted here */
66 /* Immediate values are prefixed with '#'. */
68 #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, "#"); \
74 /* Handle '#' prefixes as operands. */
77 print_hash (cd, dis_info, value, attrs, pc, length)
78 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
80 long value ATTRIBUTE_UNUSED;
81 unsigned int attrs ATTRIBUTE_UNUSED;
82 bfd_vma pc ATTRIBUTE_UNUSED;
83 int length ATTRIBUTE_UNUSED;
85 disassemble_info *info = (disassemble_info *) dis_info;
86 (*info->fprintf_func) (info->stream, "#");
89 #undef CGEN_PRINT_INSN
90 #define CGEN_PRINT_INSN my_print_insn
93 my_print_insn (cd, pc, info)
96 disassemble_info *info;
98 char buffer[CGEN_MAX_INSN_SIZE];
101 int buflen = (pc & 3) == 0 ? 4 : 2;
103 /* Read the base part of the insn. */
105 status = (*info->read_memory_func) (pc, buf, buflen, info);
108 (*info->memory_error_func) (status, pc, info);
113 if ((pc & 3) == 0 && (buf[0] & 0x80) != 0)
114 return print_insn (cd, pc, info, buf, buflen);
116 /* Print the first insn. */
119 if (print_insn (cd, pc, info, buf, 2) == 0)
120 (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
127 (*info->fprintf_func) (info->stream, " || ");
131 (*info->fprintf_func) (info->stream, " -> ");
133 /* The "& 3" is to pass a consistent address.
134 Parallel insns arguably both begin on the word boundary.
135 Also, branch insns are calculated relative to the word boundary. */
136 if (print_insn (cd, pc & ~ (bfd_vma) 3, info, buf, 2) == 0)
137 (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
139 return (pc & 3) ? 2 : 4;
144 /* Main entry point for printing operands.
145 XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
146 of dis-asm.h on cgen.h.
148 This function is basically just a big switch statement. Earlier versions
149 used tables to look up the function to use, but
150 - if the table contains both assembler and disassembler functions then
151 the disassembler contains much of the assembler and vice-versa,
152 - there's a lot of inlining possibilities as things grow,
153 - using a switch statement avoids the function call overhead.
155 This function could be moved into `print_insn_normal', but keeping it
156 separate makes clear the interface between `print_insn_normal' and each of
161 m32r_cgen_print_operand (cd, opindex, xinfo, fields, attrs, pc, length)
166 void const *attrs ATTRIBUTE_UNUSED;
170 disassemble_info *info = (disassemble_info *) xinfo;
174 case M32R_OPERAND_ACC :
175 print_keyword (cd, info, & m32r_cgen_opval_h_accums, fields->f_acc, 0);
177 case M32R_OPERAND_ACCD :
178 print_keyword (cd, info, & m32r_cgen_opval_h_accums, fields->f_accd, 0);
180 case M32R_OPERAND_ACCS :
181 print_keyword (cd, info, & m32r_cgen_opval_h_accums, fields->f_accs, 0);
183 case M32R_OPERAND_DCR :
184 print_keyword (cd, info, & m32r_cgen_opval_cr_names, fields->f_r1, 0);
186 case M32R_OPERAND_DISP16 :
187 print_address (cd, info, fields->f_disp16, 0|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
189 case M32R_OPERAND_DISP24 :
190 print_address (cd, info, fields->f_disp24, 0|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
192 case M32R_OPERAND_DISP8 :
193 print_address (cd, info, fields->f_disp8, 0|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
195 case M32R_OPERAND_DR :
196 print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r1, 0);
198 case M32R_OPERAND_HASH :
199 print_hash (cd, info, 0, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
201 case M32R_OPERAND_HI16 :
202 print_normal (cd, info, fields->f_hi16, 0|(1<<CGEN_OPERAND_SIGN_OPT), pc, length);
204 case M32R_OPERAND_IMM1 :
205 print_normal (cd, info, fields->f_imm1, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
207 case M32R_OPERAND_SCR :
208 print_keyword (cd, info, & m32r_cgen_opval_cr_names, fields->f_r2, 0);
210 case M32R_OPERAND_SIMM16 :
211 print_normal (cd, info, fields->f_simm16, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
213 case M32R_OPERAND_SIMM8 :
214 print_normal (cd, info, fields->f_simm8, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
216 case M32R_OPERAND_SLO16 :
217 print_normal (cd, info, fields->f_simm16, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
219 case M32R_OPERAND_SR :
220 print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r2, 0);
222 case M32R_OPERAND_SRC1 :
223 print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r1, 0);
225 case M32R_OPERAND_SRC2 :
226 print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r2, 0);
228 case M32R_OPERAND_UIMM16 :
229 print_normal (cd, info, fields->f_uimm16, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
231 case M32R_OPERAND_UIMM24 :
232 print_address (cd, info, fields->f_uimm24, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_ABS_ADDR), pc, length);
234 case M32R_OPERAND_UIMM4 :
235 print_normal (cd, info, fields->f_uimm4, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
237 case M32R_OPERAND_UIMM5 :
238 print_normal (cd, info, fields->f_uimm5, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
240 case M32R_OPERAND_ULO16 :
241 print_normal (cd, info, fields->f_uimm16, 0, pc, length);
245 /* xgettext:c-format */
246 fprintf (stderr, _("Unrecognized field %d while printing insn.\n"),
252 cgen_print_fn * const m32r_cgen_print_handlers[] =
259 m32r_cgen_init_dis (cd)
262 m32r_cgen_init_opcode_table (cd);
263 m32r_cgen_init_ibld_table (cd);
264 cd->print_handlers = & m32r_cgen_print_handlers[0];
265 cd->print_operand = m32r_cgen_print_operand;
269 /* Default print handler. */
272 print_normal (cd, dis_info, value, attrs, pc, length)
273 #ifdef CGEN_PRINT_NORMAL
274 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
276 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
281 #ifdef CGEN_PRINT_NORMAL
282 bfd_vma pc ATTRIBUTE_UNUSED;
283 int length ATTRIBUTE_UNUSED;
285 bfd_vma pc ATTRIBUTE_UNUSED;
286 int length ATTRIBUTE_UNUSED;
289 disassemble_info *info = (disassemble_info *) dis_info;
291 #ifdef CGEN_PRINT_NORMAL
292 CGEN_PRINT_NORMAL (cd, info, value, attrs, pc, length);
295 /* Print the operand as directed by the attributes. */
296 if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
297 ; /* nothing to do */
298 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
299 (*info->fprintf_func) (info->stream, "%ld", value);
301 (*info->fprintf_func) (info->stream, "0x%lx", value);
304 /* Default address handler. */
307 print_address (cd, dis_info, value, attrs, pc, length)
308 #ifdef CGEN_PRINT_NORMAL
309 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
311 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
316 #ifdef CGEN_PRINT_NORMAL
317 bfd_vma pc ATTRIBUTE_UNUSED;
318 int length ATTRIBUTE_UNUSED;
320 bfd_vma pc ATTRIBUTE_UNUSED;
321 int length ATTRIBUTE_UNUSED;
324 disassemble_info *info = (disassemble_info *) dis_info;
326 #ifdef CGEN_PRINT_ADDRESS
327 CGEN_PRINT_ADDRESS (cd, info, value, attrs, pc, length);
330 /* Print the operand as directed by the attributes. */
331 if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
332 ; /* nothing to do */
333 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR))
334 (*info->print_address_func) (value, info);
335 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR))
336 (*info->print_address_func) (value, info);
337 else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
338 (*info->fprintf_func) (info->stream, "%ld", (long) value);
340 (*info->fprintf_func) (info->stream, "0x%lx", (long) value);
343 /* Keyword print handler. */
346 print_keyword (cd, dis_info, keyword_table, value, attrs)
347 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
349 CGEN_KEYWORD *keyword_table;
351 unsigned int attrs ATTRIBUTE_UNUSED;
353 disassemble_info *info = (disassemble_info *) dis_info;
354 const CGEN_KEYWORD_ENTRY *ke;
356 ke = cgen_keyword_lookup_value (keyword_table, value);
358 (*info->fprintf_func) (info->stream, "%s", ke->name);
360 (*info->fprintf_func) (info->stream, "???");
363 /* Default insn printer.
365 DIS_INFO is defined as `PTR' so the disassembler needn't know anything
366 about disassemble_info. */
369 print_insn_normal (cd, dis_info, insn, fields, pc, length)
372 const CGEN_INSN *insn;
377 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
378 disassemble_info *info = (disassemble_info *) dis_info;
379 const CGEN_SYNTAX_CHAR_TYPE *syn;
381 CGEN_INIT_PRINT (cd);
383 for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
385 if (CGEN_SYNTAX_MNEMONIC_P (*syn))
387 (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn));
390 if (CGEN_SYNTAX_CHAR_P (*syn))
392 (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn));
396 /* We have an operand. */
397 m32r_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info,
398 fields, CGEN_INSN_ATTRS (insn), pc, length);
402 /* Subroutine of print_insn. Reads an insn into the given buffers and updates
404 Returns 0 if all is well, non-zero otherwise. */
406 read_insn (cd, pc, info, buf, buflen, ex_info, insn_value)
407 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
409 disassemble_info *info;
412 CGEN_EXTRACT_INFO *ex_info;
413 unsigned long *insn_value;
415 int status = (*info->read_memory_func) (pc, buf, buflen, info);
418 (*info->memory_error_func) (status, pc, info);
422 ex_info->dis_info = info;
423 ex_info->valid = (1 << buflen) - 1;
424 ex_info->insn_bytes = buf;
426 *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG);
430 /* Utility to print an insn.
431 BUF is the base part of the insn, target byte order, BUFLEN bytes long.
432 The result is the size of the insn in bytes or zero for an unknown insn
433 or -1 if an error occurs fetching data (memory_error_func will have
437 print_insn (cd, pc, info, buf, buflen)
440 disassemble_info *info;
444 CGEN_INSN_INT insn_value;
445 const CGEN_INSN_LIST *insn_list;
446 CGEN_EXTRACT_INFO ex_info;
448 /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
449 insn_value = cgen_get_insn_value (cd, buf, buflen * 8);
451 /* Fill in ex_info fields like read_insn would. Don't actually call
452 read_insn, since the incoming buffer is already read (and possibly
453 modified a la m32r). */
454 ex_info.valid = (1 << buflen) - 1;
455 ex_info.dis_info = info;
456 ex_info.insn_bytes = buf;
458 /* The instructions are stored in hash lists.
459 Pick the first one and keep trying until we find the right one. */
461 insn_list = CGEN_DIS_LOOKUP_INSN (cd, buf, insn_value);
462 while (insn_list != NULL)
464 const CGEN_INSN *insn = insn_list->insn;
467 unsigned long insn_value_cropped;
469 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
470 /* not needed as insn shouldn't be in hash lists if not supported */
471 /* Supported by this cpu? */
472 if (! m32r_cgen_insn_supported (cd, insn))
474 insn_list = CGEN_DIS_NEXT_INSN (insn_list);
479 /* Basic bit mask must be correct. */
480 /* ??? May wish to allow target to defer this check until the extract
483 /* Base size may exceed this instruction's size. Extract the
484 relevant part from the buffer. */
485 if ((CGEN_INSN_BITSIZE (insn) / 8) < buflen
486 && (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
487 insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn),
488 info->endian == BFD_ENDIAN_BIG);
490 insn_value_cropped = insn_value;
492 if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn))
493 == CGEN_INSN_BASE_VALUE (insn))
495 /* Printing is handled in two passes. The first pass parses the
496 machine insn and extracts the fields. The second pass prints
499 /* Make sure the entire insn is loaded into insn_value, if it
501 if ((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize
502 && ((unsigned) CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
504 unsigned long full_insn_value;
505 int rc = read_insn (cd, pc, info, buf,
506 CGEN_INSN_BITSIZE (insn) / 8,
507 & ex_info, & full_insn_value);
510 length = CGEN_EXTRACT_FN (cd, insn)
511 (cd, insn, &ex_info, full_insn_value, &fields, pc);
514 length = CGEN_EXTRACT_FN (cd, insn)
515 (cd, insn, &ex_info, insn_value_cropped, &fields, pc);
517 /* length < 0 -> error */
522 CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length);
523 /* length is in bits, result is in bytes */
528 insn_list = CGEN_DIS_NEXT_INSN (insn_list);
534 /* Default value for CGEN_PRINT_INSN.
535 The result is the size of the insn in bytes or zero for an unknown insn
536 or -1 if an error occured fetching bytes. */
538 #ifndef CGEN_PRINT_INSN
539 #define CGEN_PRINT_INSN default_print_insn
541 static int default_print_insn
542 PARAMS ((CGEN_CPU_DESC, bfd_vma, disassemble_info *));
545 default_print_insn (cd, pc, info)
548 disassemble_info *info;
550 char buf[CGEN_MAX_INSN_SIZE];
554 /* Attempt to read the base part of the insn. */
555 buflen = cd->base_insn_bitsize / 8;
556 status = (*info->read_memory_func) (pc, buf, buflen, info);
558 /* Try again with the minimum part, if min < base. */
559 if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
561 buflen = cd->min_insn_bitsize / 8;
562 status = (*info->read_memory_func) (pc, buf, buflen, info);
567 (*info->memory_error_func) (status, pc, info);
571 return print_insn (cd, pc, info, buf, buflen);
576 Print one instruction from PC on INFO->STREAM.
577 Return the size of the instruction (in bytes). */
580 print_insn_m32r (pc, info)
582 disassemble_info *info;
584 static CGEN_CPU_DESC cd = 0;
586 static int prev_mach;
587 static int prev_endian;
590 int endian = (info->endian == BFD_ENDIAN_BIG
592 : CGEN_ENDIAN_LITTLE);
593 enum bfd_architecture arch;
595 /* ??? gdb will set mach but leave the architecture as "unknown" */
596 #ifndef CGEN_BFD_ARCH
597 #define CGEN_BFD_ARCH bfd_arch_m32r
600 if (arch == bfd_arch_unknown)
601 arch = CGEN_BFD_ARCH;
603 /* There's no standard way to compute the machine or isa number
604 so we leave it to the target. */
605 #ifdef CGEN_COMPUTE_MACH
606 mach = CGEN_COMPUTE_MACH (info);
611 #ifdef CGEN_COMPUTE_ISA
612 isa = CGEN_COMPUTE_ISA (info);
617 /* If we've switched cpu's, close the current table and open a new one. */
621 || endian != prev_endian))
623 m32r_cgen_cpu_close (cd);
627 /* If we haven't initialized yet, initialize the opcode table. */
630 const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach);
631 const char *mach_name;
635 mach_name = arch_type->printable_name;
639 prev_endian = endian;
640 cd = m32r_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa,
641 CGEN_CPU_OPEN_BFDMACH, mach_name,
642 CGEN_CPU_OPEN_ENDIAN, prev_endian,
646 m32r_cgen_init_dis (cd);
649 /* We try to have as much common code as possible.
650 But at this point some targets need to take over. */
651 /* ??? Some targets may need a hook elsewhere. Try to avoid this,
652 but if not possible try to move this hook elsewhere rather than
654 length = CGEN_PRINT_INSN (cd, pc, info);
660 (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
661 return cd->default_insn_bitsize / 8;