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 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
26 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
35 #include "libiberty.h"
36 #include "ip2k-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
*, bfd_byte
*, 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
*, bfd_byte
*, int, CGEN_EXTRACT_INFO
*,
59 /* -- disassembler routines inserted here */
63 #define PRINT_FUNC_DECL(name) \
64 static void name PARAMS ((CGEN_CPU_DESC, PTR, long, unsigned int, bfd_vma, int))
66 PRINT_FUNC_DECL (print_fr
);
67 PRINT_FUNC_DECL (print_dollarhex
);
68 PRINT_FUNC_DECL (print_dollarhex8
);
69 PRINT_FUNC_DECL (print_dollarhex_addr16h
);
70 PRINT_FUNC_DECL (print_dollarhex_addr16l
);
71 PRINT_FUNC_DECL (print_dollarhex_p
);
72 PRINT_FUNC_DECL (print_dollarhex_cj
);
73 PRINT_FUNC_DECL (print_decimal
);
76 print_fr (cd
, dis_info
, value
, attrs
, pc
, length
)
77 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
80 unsigned int attrs ATTRIBUTE_UNUSED
;
81 bfd_vma pc ATTRIBUTE_UNUSED
;
82 int length ATTRIBUTE_UNUSED
;
84 disassemble_info
*info
= (disassemble_info
*) dis_info
;
85 const CGEN_KEYWORD_ENTRY
*ke
;
86 extern CGEN_KEYWORD ip2k_cgen_opval_register_names
;
90 if ( value
== 0 ) /* This is (IP) */
92 (*info
->fprintf_func
) (info
->stream
, "%s", "(IP)");
96 offsettest
= value
>> 7;
97 offsetvalue
= value
& 0x7F;
99 /* Check to see if first two bits are 10 -> (DP) */
100 if ( offsettest
== 2 )
102 if ( offsetvalue
== 0 )
103 (*info
->fprintf_func
) (info
->stream
, "%s","(DP)");
105 (*info
->fprintf_func
) (info
->stream
, "$%x%s",offsetvalue
, "(DP)");
109 /* Check to see if first two bits are 11 -> (SP) */
110 if ( offsettest
== 3 )
112 if ( offsetvalue
== 0 )
113 (*info
->fprintf_func
) (info
->stream
, "%s", "(SP)");
115 (*info
->fprintf_func
) (info
->stream
, "$%x%s", offsetvalue
,"(SP)");
119 /* Attempt to print as a register keyword. */
120 ke
= cgen_keyword_lookup_value (& ip2k_cgen_opval_register_names
, value
);
123 (*info
->fprintf_func
) (info
->stream
, "%s", ke
->name
);
127 /* Print as an address literal. */
128 (*info
->fprintf_func
) (info
->stream
, "$%02x", value
);
132 print_dollarhex (cd
, dis_info
, value
, attrs
, pc
, length
)
133 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
136 unsigned int attrs ATTRIBUTE_UNUSED
;
137 bfd_vma pc ATTRIBUTE_UNUSED
;
138 int length ATTRIBUTE_UNUSED
;
140 disassemble_info
*info
= (disassemble_info
*) dis_info
;
142 (*info
->fprintf_func
) (info
->stream
, "$%x", value
);
146 print_dollarhex8 (cd
, dis_info
, value
, attrs
, pc
, length
)
147 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
150 unsigned int attrs ATTRIBUTE_UNUSED
;
151 bfd_vma pc ATTRIBUTE_UNUSED
;
152 int length ATTRIBUTE_UNUSED
;
154 disassemble_info
*info
= (disassemble_info
*) dis_info
;
156 (*info
->fprintf_func
) (info
->stream
, "$%02x", value
);
160 print_dollarhex_addr16h (cd
, dis_info
, value
, attrs
, pc
, length
)
161 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
164 unsigned int attrs ATTRIBUTE_UNUSED
;
165 bfd_vma pc ATTRIBUTE_UNUSED
;
166 int length ATTRIBUTE_UNUSED
;
168 disassemble_info
*info
= (disassemble_info
*) dis_info
;
170 /* This is a loadh instruction. Shift the value to the left */
171 /* by 8 bits so that disassembled code will reassemble properly. */
172 value
= ((value
<< 8) & 0xFF00);
174 (*info
->fprintf_func
) (info
->stream
, "$%04x", value
);
178 print_dollarhex_addr16l (cd
, dis_info
, value
, attrs
, pc
, length
)
179 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
182 unsigned int attrs ATTRIBUTE_UNUSED
;
183 bfd_vma pc ATTRIBUTE_UNUSED
;
184 int length ATTRIBUTE_UNUSED
;
186 disassemble_info
*info
= (disassemble_info
*) dis_info
;
188 (*info
->fprintf_func
) (info
->stream
, "$%04x", value
);
192 print_dollarhex_p (cd
, dis_info
, value
, attrs
, pc
, length
)
193 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
196 unsigned int attrs ATTRIBUTE_UNUSED
;
197 bfd_vma pc ATTRIBUTE_UNUSED
;
198 int length ATTRIBUTE_UNUSED
;
200 disassemble_info
*info
= (disassemble_info
*) dis_info
;
202 value
= ((value
<< 14) & 0x1C000);
203 ;value
= (value
& 0x1FFFF);
204 (*info
->fprintf_func
) (info
->stream
, "$%05x", value
);
208 print_dollarhex_cj (cd
, dis_info
, value
, attrs
, pc
, length
)
209 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
212 unsigned int attrs ATTRIBUTE_UNUSED
;
213 bfd_vma pc ATTRIBUTE_UNUSED
;
214 int length ATTRIBUTE_UNUSED
;
216 disassemble_info
*info
= (disassemble_info
*) dis_info
;
218 value
= ((value
<< 1) & 0x1FFFF);
219 (*info
->fprintf_func
) (info
->stream
, "$%05x", value
);
224 print_decimal (cd
, dis_info
, value
, attrs
, pc
, length
)
225 CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
;
228 unsigned int attrs ATTRIBUTE_UNUSED
;
229 bfd_vma pc ATTRIBUTE_UNUSED
;
230 int length ATTRIBUTE_UNUSED
;
232 disassemble_info
*info
= (disassemble_info
*) dis_info
;
234 (*info
->fprintf_func
) (info
->stream
, "%d", value
);
241 void ip2k_cgen_print_operand
242 PARAMS ((CGEN_CPU_DESC
, int, PTR
, CGEN_FIELDS
*,
243 void const *, bfd_vma
, int));
245 /* Main entry point for printing operands.
246 XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
247 of dis-asm.h on cgen.h.
249 This function is basically just a big switch statement. Earlier versions
250 used tables to look up the function to use, but
251 - if the table contains both assembler and disassembler functions then
252 the disassembler contains much of the assembler and vice-versa,
253 - there's a lot of inlining possibilities as things grow,
254 - using a switch statement avoids the function call overhead.
256 This function could be moved into `print_insn_normal', but keeping it
257 separate makes clear the interface between `print_insn_normal' and each of
261 ip2k_cgen_print_operand (cd
, opindex
, xinfo
, fields
, attrs
, pc
, length
)
266 void const *attrs ATTRIBUTE_UNUSED
;
270 disassemble_info
*info
= (disassemble_info
*) xinfo
;
274 case IP2K_OPERAND_ADDR16CJP
:
275 print_dollarhex_cj (cd
, info
, fields
->f_addr16cjp
, 0|(1<<CGEN_OPERAND_ABS_ADDR
), pc
, length
);
277 case IP2K_OPERAND_ADDR16H
:
278 print_dollarhex_addr16h (cd
, info
, fields
->f_imm8
, 0, pc
, length
);
280 case IP2K_OPERAND_ADDR16L
:
281 print_dollarhex_addr16l (cd
, info
, fields
->f_imm8
, 0, pc
, length
);
283 case IP2K_OPERAND_ADDR16P
:
284 print_dollarhex_p (cd
, info
, fields
->f_page3
, 0, pc
, length
);
286 case IP2K_OPERAND_BITNO
:
287 print_decimal (cd
, info
, fields
->f_bitno
, 0, pc
, length
);
289 case IP2K_OPERAND_CBIT
:
290 print_normal (cd
, info
, 0, 0, pc
, length
);
292 case IP2K_OPERAND_DCBIT
:
293 print_normal (cd
, info
, 0, 0, pc
, length
);
295 case IP2K_OPERAND_FR
:
296 print_fr (cd
, info
, fields
->f_reg
, 0|(1<<CGEN_OPERAND_ABS_ADDR
), pc
, length
);
298 case IP2K_OPERAND_LIT8
:
299 print_dollarhex8 (cd
, info
, fields
->f_imm8
, 0|(1<<CGEN_OPERAND_SIGNED
), pc
, length
);
301 case IP2K_OPERAND_PABITS
:
302 print_normal (cd
, info
, 0, 0, pc
, length
);
304 case IP2K_OPERAND_RETI3
:
305 print_dollarhex (cd
, info
, fields
->f_reti3
, 0, pc
, length
);
307 case IP2K_OPERAND_ZBIT
:
308 print_normal (cd
, info
, 0, 0, pc
, length
);
312 /* xgettext:c-format */
313 fprintf (stderr
, _("Unrecognized field %d while printing insn.\n"),
319 cgen_print_fn
* const ip2k_cgen_print_handlers
[] =
326 ip2k_cgen_init_dis (cd
)
329 ip2k_cgen_init_opcode_table (cd
);
330 ip2k_cgen_init_ibld_table (cd
);
331 cd
->print_handlers
= & ip2k_cgen_print_handlers
[0];
332 cd
->print_operand
= ip2k_cgen_print_operand
;
336 /* Default print handler. */
339 print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
343 bfd_vma pc ATTRIBUTE_UNUSED
,
344 int length ATTRIBUTE_UNUSED
)
346 disassemble_info
*info
= (disassemble_info
*) dis_info
;
348 #ifdef CGEN_PRINT_NORMAL
349 CGEN_PRINT_NORMAL (cd
, info
, value
, attrs
, pc
, length
);
352 /* Print the operand as directed by the attributes. */
353 if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SEM_ONLY
))
354 ; /* nothing to do */
355 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SIGNED
))
356 (*info
->fprintf_func
) (info
->stream
, "%ld", value
);
358 (*info
->fprintf_func
) (info
->stream
, "0x%lx", value
);
361 /* Default address handler. */
364 print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
368 bfd_vma pc ATTRIBUTE_UNUSED
,
369 int length ATTRIBUTE_UNUSED
)
371 disassemble_info
*info
= (disassemble_info
*) dis_info
;
373 #ifdef CGEN_PRINT_ADDRESS
374 CGEN_PRINT_ADDRESS (cd
, info
, value
, attrs
, pc
, length
);
377 /* Print the operand as directed by the attributes. */
378 if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SEM_ONLY
))
379 ; /* nothing to do */
380 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_PCREL_ADDR
))
381 (*info
->print_address_func
) (value
, info
);
382 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_ABS_ADDR
))
383 (*info
->print_address_func
) (value
, info
);
384 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SIGNED
))
385 (*info
->fprintf_func
) (info
->stream
, "%ld", (long) value
);
387 (*info
->fprintf_func
) (info
->stream
, "0x%lx", (long) value
);
390 /* Keyword print handler. */
393 print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
395 CGEN_KEYWORD
*keyword_table
,
397 unsigned int attrs ATTRIBUTE_UNUSED
)
399 disassemble_info
*info
= (disassemble_info
*) dis_info
;
400 const CGEN_KEYWORD_ENTRY
*ke
;
402 ke
= cgen_keyword_lookup_value (keyword_table
, value
);
404 (*info
->fprintf_func
) (info
->stream
, "%s", ke
->name
);
406 (*info
->fprintf_func
) (info
->stream
, "???");
409 /* Default insn printer.
411 DIS_INFO is defined as `void *' so the disassembler needn't know anything
412 about disassemble_info. */
415 print_insn_normal (CGEN_CPU_DESC cd
,
417 const CGEN_INSN
*insn
,
422 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
423 disassemble_info
*info
= (disassemble_info
*) dis_info
;
424 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
426 CGEN_INIT_PRINT (cd
);
428 for (syn
= CGEN_SYNTAX_STRING (syntax
); *syn
; ++syn
)
430 if (CGEN_SYNTAX_MNEMONIC_P (*syn
))
432 (*info
->fprintf_func
) (info
->stream
, "%s", CGEN_INSN_MNEMONIC (insn
));
435 if (CGEN_SYNTAX_CHAR_P (*syn
))
437 (*info
->fprintf_func
) (info
->stream
, "%c", CGEN_SYNTAX_CHAR (*syn
));
441 /* We have an operand. */
442 ip2k_cgen_print_operand (cd
, CGEN_SYNTAX_FIELD (*syn
), info
,
443 fields
, CGEN_INSN_ATTRS (insn
), pc
, length
);
447 /* Subroutine of print_insn. Reads an insn into the given buffers and updates
449 Returns 0 if all is well, non-zero otherwise. */
452 read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
454 disassemble_info
*info
,
457 CGEN_EXTRACT_INFO
*ex_info
,
458 unsigned long *insn_value
)
460 int status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
463 (*info
->memory_error_func
) (status
, pc
, info
);
467 ex_info
->dis_info
= info
;
468 ex_info
->valid
= (1 << buflen
) - 1;
469 ex_info
->insn_bytes
= buf
;
471 *insn_value
= bfd_get_bits (buf
, buflen
* 8, info
->endian
== BFD_ENDIAN_BIG
);
475 /* Utility to print an insn.
476 BUF is the base part of the insn, target byte order, BUFLEN bytes long.
477 The result is the size of the insn in bytes or zero for an unknown insn
478 or -1 if an error occurs fetching data (memory_error_func will have
482 print_insn (CGEN_CPU_DESC cd
,
484 disassemble_info
*info
,
488 CGEN_INSN_INT insn_value
;
489 const CGEN_INSN_LIST
*insn_list
;
490 CGEN_EXTRACT_INFO ex_info
;
493 /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
494 basesize
= cd
->base_insn_bitsize
< buflen
* 8 ?
495 cd
->base_insn_bitsize
: buflen
* 8;
496 insn_value
= cgen_get_insn_value (cd
, buf
, basesize
);
499 /* Fill in ex_info fields like read_insn would. Don't actually call
500 read_insn, since the incoming buffer is already read (and possibly
501 modified a la m32r). */
502 ex_info
.valid
= (1 << buflen
) - 1;
503 ex_info
.dis_info
= info
;
504 ex_info
.insn_bytes
= buf
;
506 /* The instructions are stored in hash lists.
507 Pick the first one and keep trying until we find the right one. */
509 insn_list
= CGEN_DIS_LOOKUP_INSN (cd
, (char *) buf
, insn_value
);
510 while (insn_list
!= NULL
)
512 const CGEN_INSN
*insn
= insn_list
->insn
;
515 unsigned long insn_value_cropped
;
517 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
518 /* Not needed as insn shouldn't be in hash lists if not supported. */
519 /* Supported by this cpu? */
520 if (! ip2k_cgen_insn_supported (cd
, insn
))
522 insn_list
= CGEN_DIS_NEXT_INSN (insn_list
);
527 /* Basic bit mask must be correct. */
528 /* ??? May wish to allow target to defer this check until the extract
531 /* Base size may exceed this instruction's size. Extract the
532 relevant part from the buffer. */
533 if ((unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) < buflen
&&
534 (unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) <= sizeof (unsigned long))
535 insn_value_cropped
= bfd_get_bits (buf
, CGEN_INSN_BITSIZE (insn
),
536 info
->endian
== BFD_ENDIAN_BIG
);
538 insn_value_cropped
= insn_value
;
540 if ((insn_value_cropped
& CGEN_INSN_BASE_MASK (insn
))
541 == CGEN_INSN_BASE_VALUE (insn
))
543 /* Printing is handled in two passes. The first pass parses the
544 machine insn and extracts the fields. The second pass prints
547 /* Make sure the entire insn is loaded into insn_value, if it
549 if (((unsigned) CGEN_INSN_BITSIZE (insn
) > cd
->base_insn_bitsize
) &&
550 (unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) <= sizeof (unsigned long))
552 unsigned long full_insn_value
;
553 int rc
= read_insn (cd
, pc
, info
, buf
,
554 CGEN_INSN_BITSIZE (insn
) / 8,
555 & ex_info
, & full_insn_value
);
558 length
= CGEN_EXTRACT_FN (cd
, insn
)
559 (cd
, insn
, &ex_info
, full_insn_value
, &fields
, pc
);
562 length
= CGEN_EXTRACT_FN (cd
, insn
)
563 (cd
, insn
, &ex_info
, insn_value_cropped
, &fields
, pc
);
565 /* length < 0 -> error */
570 CGEN_PRINT_FN (cd
, insn
) (cd
, info
, insn
, &fields
, pc
, length
);
571 /* length is in bits, result is in bytes */
576 insn_list
= CGEN_DIS_NEXT_INSN (insn_list
);
582 /* Default value for CGEN_PRINT_INSN.
583 The result is the size of the insn in bytes or zero for an unknown insn
584 or -1 if an error occured fetching bytes. */
586 #ifndef CGEN_PRINT_INSN
587 #define CGEN_PRINT_INSN default_print_insn
591 default_print_insn (CGEN_CPU_DESC cd
, bfd_vma pc
, disassemble_info
*info
)
593 bfd_byte buf
[CGEN_MAX_INSN_SIZE
];
597 /* Attempt to read the base part of the insn. */
598 buflen
= cd
->base_insn_bitsize
/ 8;
599 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
601 /* Try again with the minimum part, if min < base. */
602 if (status
!= 0 && (cd
->min_insn_bitsize
< cd
->base_insn_bitsize
))
604 buflen
= cd
->min_insn_bitsize
/ 8;
605 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
610 (*info
->memory_error_func
) (status
, pc
, info
);
614 return print_insn (cd
, pc
, info
, buf
, buflen
);
618 Print one instruction from PC on INFO->STREAM.
619 Return the size of the instruction (in bytes). */
621 typedef struct cpu_desc_list
{
622 struct cpu_desc_list
*next
;
630 print_insn_ip2k (bfd_vma pc
, disassemble_info
*info
)
632 static cpu_desc_list
*cd_list
= 0;
633 cpu_desc_list
*cl
= 0;
634 static CGEN_CPU_DESC cd
= 0;
636 static int prev_mach
;
637 static int prev_endian
;
640 int endian
= (info
->endian
== BFD_ENDIAN_BIG
642 : CGEN_ENDIAN_LITTLE
);
643 enum bfd_architecture arch
;
645 /* ??? gdb will set mach but leave the architecture as "unknown" */
646 #ifndef CGEN_BFD_ARCH
647 #define CGEN_BFD_ARCH bfd_arch_ip2k
650 if (arch
== bfd_arch_unknown
)
651 arch
= CGEN_BFD_ARCH
;
653 /* There's no standard way to compute the machine or isa number
654 so we leave it to the target. */
655 #ifdef CGEN_COMPUTE_MACH
656 mach
= CGEN_COMPUTE_MACH (info
);
661 #ifdef CGEN_COMPUTE_ISA
662 isa
= CGEN_COMPUTE_ISA (info
);
664 isa
= info
->insn_sets
;
667 /* If we've switched cpu's, try to find a handle we've used before */
671 || endian
!= prev_endian
))
674 for (cl
= cd_list
; cl
; cl
= cl
->next
)
676 if (cl
->isa
== isa
&&
678 cl
->endian
== endian
)
686 /* If we haven't initialized yet, initialize the opcode table. */
689 const bfd_arch_info_type
*arch_type
= bfd_lookup_arch (arch
, mach
);
690 const char *mach_name
;
694 mach_name
= arch_type
->printable_name
;
698 prev_endian
= endian
;
699 cd
= ip2k_cgen_cpu_open (CGEN_CPU_OPEN_ISAS
, prev_isa
,
700 CGEN_CPU_OPEN_BFDMACH
, mach_name
,
701 CGEN_CPU_OPEN_ENDIAN
, prev_endian
,
706 /* save this away for future reference */
707 cl
= xmalloc (sizeof (struct cpu_desc_list
));
715 ip2k_cgen_init_dis (cd
);
718 /* We try to have as much common code as possible.
719 But at this point some targets need to take over. */
720 /* ??? Some targets may need a hook elsewhere. Try to avoid this,
721 but if not possible try to move this hook elsewhere rather than
723 length
= CGEN_PRINT_INSN (cd
, pc
, info
);
729 (*info
->fprintf_func
) (info
->stream
, UNKNOWN_INSN_MSG
);
730 return cd
->default_insn_bitsize
/ 8;