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, 2003, 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"
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. */
62 static void print_dollarhex (CGEN_CPU_DESC
, PTR
, long, unsigned, bfd_vma
, int);
65 print_dollarhex (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
68 unsigned int attrs ATTRIBUTE_UNUSED
,
69 bfd_vma pc ATTRIBUTE_UNUSED
,
70 int length ATTRIBUTE_UNUSED
)
72 disassemble_info
*info
= (disassemble_info
*) dis_info
;
74 info
->fprintf_func (info
->stream
, "$%lx", value
);
77 print_normal (cd
, dis_info
, value
, attrs
, pc
, length
);
83 void ms1_cgen_print_operand
84 (CGEN_CPU_DESC
, int, PTR
, CGEN_FIELDS
*, void const *, bfd_vma
, int);
86 /* Main entry point for printing operands.
87 XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
88 of dis-asm.h on cgen.h.
90 This function is basically just a big switch statement. Earlier versions
91 used tables to look up the function to use, but
92 - if the table contains both assembler and disassembler functions then
93 the disassembler contains much of the assembler and vice-versa,
94 - there's a lot of inlining possibilities as things grow,
95 - using a switch statement avoids the function call overhead.
97 This function could be moved into `print_insn_normal', but keeping it
98 separate makes clear the interface between `print_insn_normal' and each of
102 ms1_cgen_print_operand (CGEN_CPU_DESC cd
,
106 void const *attrs ATTRIBUTE_UNUSED
,
110 disassemble_info
*info
= (disassemble_info
*) xinfo
;
114 case MS1_OPERAND_A23
:
115 print_dollarhex (cd
, info
, fields
->f_a23
, 0, pc
, length
);
117 case MS1_OPERAND_BALL
:
118 print_dollarhex (cd
, info
, fields
->f_ball
, 0, pc
, length
);
120 case MS1_OPERAND_BALL2
:
121 print_dollarhex (cd
, info
, fields
->f_ball2
, 0, pc
, length
);
123 case MS1_OPERAND_BANKADDR
:
124 print_dollarhex (cd
, info
, fields
->f_bankaddr
, 0, pc
, length
);
126 case MS1_OPERAND_BRC
:
127 print_dollarhex (cd
, info
, fields
->f_brc
, 0, pc
, length
);
129 case MS1_OPERAND_BRC2
:
130 print_dollarhex (cd
, info
, fields
->f_brc2
, 0, pc
, length
);
132 case MS1_OPERAND_CBRB
:
133 print_dollarhex (cd
, info
, fields
->f_cbrb
, 0, pc
, length
);
135 case MS1_OPERAND_CBS
:
136 print_dollarhex (cd
, info
, fields
->f_cbs
, 0, pc
, length
);
138 case MS1_OPERAND_CBX
:
139 print_dollarhex (cd
, info
, fields
->f_cbx
, 0, pc
, length
);
141 case MS1_OPERAND_CCB
:
142 print_dollarhex (cd
, info
, fields
->f_ccb
, 0, pc
, length
);
144 case MS1_OPERAND_CDB
:
145 print_dollarhex (cd
, info
, fields
->f_cdb
, 0, pc
, length
);
147 case MS1_OPERAND_CELL
:
148 print_dollarhex (cd
, info
, fields
->f_cell
, 0, pc
, length
);
150 case MS1_OPERAND_COLNUM
:
151 print_dollarhex (cd
, info
, fields
->f_colnum
, 0, pc
, length
);
153 case MS1_OPERAND_CONTNUM
:
154 print_dollarhex (cd
, info
, fields
->f_contnum
, 0, pc
, length
);
156 case MS1_OPERAND_CR
:
157 print_dollarhex (cd
, info
, fields
->f_cr
, 0, pc
, length
);
159 case MS1_OPERAND_CTXDISP
:
160 print_dollarhex (cd
, info
, fields
->f_ctxdisp
, 0, pc
, length
);
162 case MS1_OPERAND_DUP
:
163 print_dollarhex (cd
, info
, fields
->f_dup
, 0, pc
, length
);
165 case MS1_OPERAND_FBDISP
:
166 print_dollarhex (cd
, info
, fields
->f_fbdisp
, 0, pc
, length
);
168 case MS1_OPERAND_FBINCR
:
169 print_dollarhex (cd
, info
, fields
->f_fbincr
, 0, pc
, length
);
171 case MS1_OPERAND_FRDR
:
172 print_keyword (cd
, info
, & ms1_cgen_opval_h_spr
, fields
->f_dr
, 0|(1<<CGEN_OPERAND_ABS_ADDR
));
174 case MS1_OPERAND_FRDRRR
:
175 print_keyword (cd
, info
, & ms1_cgen_opval_h_spr
, fields
->f_drrr
, 0|(1<<CGEN_OPERAND_ABS_ADDR
));
177 case MS1_OPERAND_FRSR1
:
178 print_keyword (cd
, info
, & ms1_cgen_opval_h_spr
, fields
->f_sr1
, 0|(1<<CGEN_OPERAND_ABS_ADDR
));
180 case MS1_OPERAND_FRSR2
:
181 print_keyword (cd
, info
, & ms1_cgen_opval_h_spr
, fields
->f_sr2
, 0|(1<<CGEN_OPERAND_ABS_ADDR
));
183 case MS1_OPERAND_ID
:
184 print_dollarhex (cd
, info
, fields
->f_id
, 0, pc
, length
);
186 case MS1_OPERAND_IMM16
:
187 print_dollarhex (cd
, info
, fields
->f_imm16s
, 0|(1<<CGEN_OPERAND_SIGNED
), pc
, length
);
189 case MS1_OPERAND_IMM16O
:
190 print_dollarhex (cd
, info
, fields
->f_imm16s
, 0, pc
, length
);
192 case MS1_OPERAND_IMM16Z
:
193 print_dollarhex (cd
, info
, fields
->f_imm16u
, 0, pc
, length
);
195 case MS1_OPERAND_INCAMT
:
196 print_dollarhex (cd
, info
, fields
->f_incamt
, 0, pc
, length
);
198 case MS1_OPERAND_INCR
:
199 print_dollarhex (cd
, info
, fields
->f_incr
, 0, pc
, length
);
201 case MS1_OPERAND_LENGTH
:
202 print_dollarhex (cd
, info
, fields
->f_length
, 0, pc
, length
);
204 case MS1_OPERAND_MASK
:
205 print_dollarhex (cd
, info
, fields
->f_mask
, 0, pc
, length
);
207 case MS1_OPERAND_MASK1
:
208 print_dollarhex (cd
, info
, fields
->f_mask1
, 0, pc
, length
);
210 case MS1_OPERAND_MODE
:
211 print_dollarhex (cd
, info
, fields
->f_mode
, 0, pc
, length
);
213 case MS1_OPERAND_PERM
:
214 print_dollarhex (cd
, info
, fields
->f_perm
, 0, pc
, length
);
216 case MS1_OPERAND_RBBC
:
217 print_dollarhex (cd
, info
, fields
->f_rbbc
, 0, pc
, length
);
219 case MS1_OPERAND_RC
:
220 print_dollarhex (cd
, info
, fields
->f_rc
, 0, pc
, length
);
222 case MS1_OPERAND_RC1
:
223 print_dollarhex (cd
, info
, fields
->f_rc1
, 0, pc
, length
);
225 case MS1_OPERAND_RC2
:
226 print_dollarhex (cd
, info
, fields
->f_rc2
, 0, pc
, length
);
228 case MS1_OPERAND_RCNUM
:
229 print_dollarhex (cd
, info
, fields
->f_rcnum
, 0, pc
, length
);
231 case MS1_OPERAND_RDA
:
232 print_dollarhex (cd
, info
, fields
->f_rda
, 0, pc
, length
);
234 case MS1_OPERAND_ROWNUM
:
235 print_dollarhex (cd
, info
, fields
->f_rownum
, 0, pc
, length
);
237 case MS1_OPERAND_ROWNUM1
:
238 print_dollarhex (cd
, info
, fields
->f_rownum1
, 0, pc
, length
);
240 case MS1_OPERAND_ROWNUM2
:
241 print_dollarhex (cd
, info
, fields
->f_rownum2
, 0, pc
, length
);
243 case MS1_OPERAND_SIZE
:
244 print_dollarhex (cd
, info
, fields
->f_size
, 0, pc
, length
);
246 case MS1_OPERAND_TYPE
:
247 print_dollarhex (cd
, info
, fields
->f_type
, 0, pc
, length
);
249 case MS1_OPERAND_WR
:
250 print_dollarhex (cd
, info
, fields
->f_wr
, 0, pc
, length
);
252 case MS1_OPERAND_XMODE
:
253 print_dollarhex (cd
, info
, fields
->f_xmode
, 0, pc
, length
);
257 /* xgettext:c-format */
258 fprintf (stderr
, _("Unrecognized field %d while printing insn.\n"),
264 cgen_print_fn
* const ms1_cgen_print_handlers
[] =
271 ms1_cgen_init_dis (CGEN_CPU_DESC cd
)
273 ms1_cgen_init_opcode_table (cd
);
274 ms1_cgen_init_ibld_table (cd
);
275 cd
->print_handlers
= & ms1_cgen_print_handlers
[0];
276 cd
->print_operand
= ms1_cgen_print_operand
;
280 /* Default print handler. */
283 print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
287 bfd_vma pc ATTRIBUTE_UNUSED
,
288 int length ATTRIBUTE_UNUSED
)
290 disassemble_info
*info
= (disassemble_info
*) dis_info
;
292 #ifdef CGEN_PRINT_NORMAL
293 CGEN_PRINT_NORMAL (cd
, info
, value
, attrs
, pc
, length
);
296 /* Print the operand as directed by the attributes. */
297 if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SEM_ONLY
))
298 ; /* nothing to do */
299 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SIGNED
))
300 (*info
->fprintf_func
) (info
->stream
, "%ld", value
);
302 (*info
->fprintf_func
) (info
->stream
, "0x%lx", value
);
305 /* Default address handler. */
308 print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
312 bfd_vma pc ATTRIBUTE_UNUSED
,
313 int length ATTRIBUTE_UNUSED
)
315 disassemble_info
*info
= (disassemble_info
*) dis_info
;
317 #ifdef CGEN_PRINT_ADDRESS
318 CGEN_PRINT_ADDRESS (cd
, info
, value
, attrs
, pc
, length
);
321 /* Print the operand as directed by the attributes. */
322 if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SEM_ONLY
))
323 ; /* Nothing to do. */
324 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_PCREL_ADDR
))
325 (*info
->print_address_func
) (value
, info
);
326 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_ABS_ADDR
))
327 (*info
->print_address_func
) (value
, info
);
328 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SIGNED
))
329 (*info
->fprintf_func
) (info
->stream
, "%ld", (long) value
);
331 (*info
->fprintf_func
) (info
->stream
, "0x%lx", (long) value
);
334 /* Keyword print handler. */
337 print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
339 CGEN_KEYWORD
*keyword_table
,
341 unsigned int attrs ATTRIBUTE_UNUSED
)
343 disassemble_info
*info
= (disassemble_info
*) dis_info
;
344 const CGEN_KEYWORD_ENTRY
*ke
;
346 ke
= cgen_keyword_lookup_value (keyword_table
, value
);
348 (*info
->fprintf_func
) (info
->stream
, "%s", ke
->name
);
350 (*info
->fprintf_func
) (info
->stream
, "???");
353 /* Default insn printer.
355 DIS_INFO is defined as `void *' so the disassembler needn't know anything
356 about disassemble_info. */
359 print_insn_normal (CGEN_CPU_DESC cd
,
361 const CGEN_INSN
*insn
,
366 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
367 disassemble_info
*info
= (disassemble_info
*) dis_info
;
368 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
370 CGEN_INIT_PRINT (cd
);
372 for (syn
= CGEN_SYNTAX_STRING (syntax
); *syn
; ++syn
)
374 if (CGEN_SYNTAX_MNEMONIC_P (*syn
))
376 (*info
->fprintf_func
) (info
->stream
, "%s", CGEN_INSN_MNEMONIC (insn
));
379 if (CGEN_SYNTAX_CHAR_P (*syn
))
381 (*info
->fprintf_func
) (info
->stream
, "%c", CGEN_SYNTAX_CHAR (*syn
));
385 /* We have an operand. */
386 ms1_cgen_print_operand (cd
, CGEN_SYNTAX_FIELD (*syn
), info
,
387 fields
, CGEN_INSN_ATTRS (insn
), pc
, length
);
391 /* Subroutine of print_insn. Reads an insn into the given buffers and updates
393 Returns 0 if all is well, non-zero otherwise. */
396 read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
398 disassemble_info
*info
,
401 CGEN_EXTRACT_INFO
*ex_info
,
402 unsigned long *insn_value
)
404 int status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
408 (*info
->memory_error_func
) (status
, pc
, info
);
412 ex_info
->dis_info
= info
;
413 ex_info
->valid
= (1 << buflen
) - 1;
414 ex_info
->insn_bytes
= buf
;
416 *insn_value
= bfd_get_bits (buf
, buflen
* 8, info
->endian
== BFD_ENDIAN_BIG
);
420 /* Utility to print an insn.
421 BUF is the base part of the insn, target byte order, BUFLEN bytes long.
422 The result is the size of the insn in bytes or zero for an unknown insn
423 or -1 if an error occurs fetching data (memory_error_func will have
427 print_insn (CGEN_CPU_DESC cd
,
429 disassemble_info
*info
,
433 CGEN_INSN_INT insn_value
;
434 const CGEN_INSN_LIST
*insn_list
;
435 CGEN_EXTRACT_INFO ex_info
;
438 /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
439 basesize
= cd
->base_insn_bitsize
< buflen
* 8 ?
440 cd
->base_insn_bitsize
: buflen
* 8;
441 insn_value
= cgen_get_insn_value (cd
, buf
, basesize
);
444 /* Fill in ex_info fields like read_insn would. Don't actually call
445 read_insn, since the incoming buffer is already read (and possibly
446 modified a la m32r). */
447 ex_info
.valid
= (1 << buflen
) - 1;
448 ex_info
.dis_info
= info
;
449 ex_info
.insn_bytes
= buf
;
451 /* The instructions are stored in hash lists.
452 Pick the first one and keep trying until we find the right one. */
454 insn_list
= CGEN_DIS_LOOKUP_INSN (cd
, (char *) buf
, insn_value
);
455 while (insn_list
!= NULL
)
457 const CGEN_INSN
*insn
= insn_list
->insn
;
460 unsigned long insn_value_cropped
;
462 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
463 /* Not needed as insn shouldn't be in hash lists if not supported. */
464 /* Supported by this cpu? */
465 if (! ms1_cgen_insn_supported (cd
, insn
))
467 insn_list
= CGEN_DIS_NEXT_INSN (insn_list
);
472 /* Basic bit mask must be correct. */
473 /* ??? May wish to allow target to defer this check until the extract
476 /* Base size may exceed this instruction's size. Extract the
477 relevant part from the buffer. */
478 if ((unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) < buflen
&&
479 (unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) <= sizeof (unsigned long))
480 insn_value_cropped
= bfd_get_bits (buf
, CGEN_INSN_BITSIZE (insn
),
481 info
->endian
== BFD_ENDIAN_BIG
);
483 insn_value_cropped
= insn_value
;
485 if ((insn_value_cropped
& CGEN_INSN_BASE_MASK (insn
))
486 == CGEN_INSN_BASE_VALUE (insn
))
488 /* Printing is handled in two passes. The first pass parses the
489 machine insn and extracts the fields. The second pass prints
492 /* Make sure the entire insn is loaded into insn_value, if it
494 if (((unsigned) CGEN_INSN_BITSIZE (insn
) > cd
->base_insn_bitsize
) &&
495 (unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) <= sizeof (unsigned long))
497 unsigned long full_insn_value
;
498 int rc
= read_insn (cd
, pc
, info
, buf
,
499 CGEN_INSN_BITSIZE (insn
) / 8,
500 & ex_info
, & full_insn_value
);
503 length
= CGEN_EXTRACT_FN (cd
, insn
)
504 (cd
, insn
, &ex_info
, full_insn_value
, &fields
, pc
);
507 length
= CGEN_EXTRACT_FN (cd
, insn
)
508 (cd
, insn
, &ex_info
, insn_value_cropped
, &fields
, pc
);
510 /* Length < 0 -> error. */
515 CGEN_PRINT_FN (cd
, insn
) (cd
, info
, insn
, &fields
, pc
, length
);
516 /* Length is in bits, result is in bytes. */
521 insn_list
= CGEN_DIS_NEXT_INSN (insn_list
);
527 /* Default value for CGEN_PRINT_INSN.
528 The result is the size of the insn in bytes or zero for an unknown insn
529 or -1 if an error occured fetching bytes. */
531 #ifndef CGEN_PRINT_INSN
532 #define CGEN_PRINT_INSN default_print_insn
536 default_print_insn (CGEN_CPU_DESC cd
, bfd_vma pc
, disassemble_info
*info
)
538 bfd_byte buf
[CGEN_MAX_INSN_SIZE
];
542 /* Attempt to read the base part of the insn. */
543 buflen
= cd
->base_insn_bitsize
/ 8;
544 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
546 /* Try again with the minimum part, if min < base. */
547 if (status
!= 0 && (cd
->min_insn_bitsize
< cd
->base_insn_bitsize
))
549 buflen
= cd
->min_insn_bitsize
/ 8;
550 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
555 (*info
->memory_error_func
) (status
, pc
, info
);
559 return print_insn (cd
, pc
, info
, buf
, buflen
);
563 Print one instruction from PC on INFO->STREAM.
564 Return the size of the instruction (in bytes). */
566 typedef struct cpu_desc_list
568 struct cpu_desc_list
*next
;
576 print_insn_ms1 (bfd_vma pc
, disassemble_info
*info
)
578 static cpu_desc_list
*cd_list
= 0;
579 cpu_desc_list
*cl
= 0;
580 static CGEN_CPU_DESC cd
= 0;
581 static CGEN_BITSET
*prev_isa
;
582 static int prev_mach
;
583 static int prev_endian
;
587 int endian
= (info
->endian
== BFD_ENDIAN_BIG
589 : CGEN_ENDIAN_LITTLE
);
590 enum bfd_architecture arch
;
592 /* ??? gdb will set mach but leave the architecture as "unknown" */
593 #ifndef CGEN_BFD_ARCH
594 #define CGEN_BFD_ARCH bfd_arch_ms1
597 if (arch
== bfd_arch_unknown
)
598 arch
= CGEN_BFD_ARCH
;
600 /* There's no standard way to compute the machine or isa number
601 so we leave it to the target. */
602 #ifdef CGEN_COMPUTE_MACH
603 mach
= CGEN_COMPUTE_MACH (info
);
608 #ifdef CGEN_COMPUTE_ISA
610 static CGEN_BITSET
*permanent_isa
;
613 permanent_isa
= cgen_bitset_create (MAX_ISAS
);
615 cgen_bitset_clear (isa
);
616 cgen_bitset_add (isa
, CGEN_COMPUTE_ISA (info
));
619 isa
= info
->insn_sets
;
622 /* If we've switched cpu's, try to find a handle we've used before */
624 && (cgen_bitset_compare (isa
, prev_isa
) != 0
626 || endian
!= prev_endian
))
629 for (cl
= cd_list
; cl
; cl
= cl
->next
)
631 if (cgen_bitset_compare (cl
->isa
, isa
) == 0 &&
633 cl
->endian
== endian
)
642 /* If we haven't initialized yet, initialize the opcode table. */
645 const bfd_arch_info_type
*arch_type
= bfd_lookup_arch (arch
, mach
);
646 const char *mach_name
;
650 mach_name
= arch_type
->printable_name
;
652 prev_isa
= cgen_bitset_copy (isa
);
654 prev_endian
= endian
;
655 cd
= ms1_cgen_cpu_open (CGEN_CPU_OPEN_ISAS
, prev_isa
,
656 CGEN_CPU_OPEN_BFDMACH
, mach_name
,
657 CGEN_CPU_OPEN_ENDIAN
, prev_endian
,
662 /* Save this away for future reference. */
663 cl
= xmalloc (sizeof (struct cpu_desc_list
));
671 ms1_cgen_init_dis (cd
);
674 /* We try to have as much common code as possible.
675 But at this point some targets need to take over. */
676 /* ??? Some targets may need a hook elsewhere. Try to avoid this,
677 but if not possible try to move this hook elsewhere rather than
679 length
= CGEN_PRINT_INSN (cd
, pc
, info
);
685 (*info
->fprintf_func
) (info
->stream
, UNKNOWN_INSN_MSG
);
686 return cd
->default_insn_bitsize
/ 8;