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"
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
, "$%x", value
);
77 print_normal (cd
, dis_info
, value
, attrs
, pc
, length
);
83 void ms1_cgen_print_operand
84 PARAMS ((CGEN_CPU_DESC
, int, PTR
, CGEN_FIELDS
*,
85 void const *, bfd_vma
, int));
87 /* Main entry point for printing operands.
88 XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
89 of dis-asm.h on cgen.h.
91 This function is basically just a big switch statement. Earlier versions
92 used tables to look up the function to use, but
93 - if the table contains both assembler and disassembler functions then
94 the disassembler contains much of the assembler and vice-versa,
95 - there's a lot of inlining possibilities as things grow,
96 - using a switch statement avoids the function call overhead.
98 This function could be moved into `print_insn_normal', but keeping it
99 separate makes clear the interface between `print_insn_normal' and each of
103 ms1_cgen_print_operand (cd
, opindex
, xinfo
, fields
, attrs
, pc
, length
)
108 void const *attrs ATTRIBUTE_UNUSED
;
112 disassemble_info
*info
= (disassemble_info
*) xinfo
;
116 case MS1_OPERAND_A23
:
117 print_dollarhex (cd
, info
, fields
->f_a23
, 0, pc
, length
);
119 case MS1_OPERAND_BALL
:
120 print_dollarhex (cd
, info
, fields
->f_ball
, 0, pc
, length
);
122 case MS1_OPERAND_BALL2
:
123 print_dollarhex (cd
, info
, fields
->f_ball2
, 0, pc
, length
);
125 case MS1_OPERAND_BANKADDR
:
126 print_dollarhex (cd
, info
, fields
->f_bankaddr
, 0, pc
, length
);
128 case MS1_OPERAND_BRC
:
129 print_dollarhex (cd
, info
, fields
->f_brc
, 0, pc
, length
);
131 case MS1_OPERAND_BRC2
:
132 print_dollarhex (cd
, info
, fields
->f_brc2
, 0, pc
, length
);
134 case MS1_OPERAND_CBRB
:
135 print_dollarhex (cd
, info
, fields
->f_cbrb
, 0, pc
, length
);
137 case MS1_OPERAND_CBS
:
138 print_dollarhex (cd
, info
, fields
->f_cbs
, 0, pc
, length
);
140 case MS1_OPERAND_CBX
:
141 print_dollarhex (cd
, info
, fields
->f_cbx
, 0, pc
, length
);
143 case MS1_OPERAND_CCB
:
144 print_dollarhex (cd
, info
, fields
->f_ccb
, 0, pc
, length
);
146 case MS1_OPERAND_CDB
:
147 print_dollarhex (cd
, info
, fields
->f_cdb
, 0, pc
, length
);
149 case MS1_OPERAND_CELL
:
150 print_dollarhex (cd
, info
, fields
->f_cell
, 0, pc
, length
);
152 case MS1_OPERAND_COLNUM
:
153 print_dollarhex (cd
, info
, fields
->f_colnum
, 0, pc
, length
);
155 case MS1_OPERAND_CONTNUM
:
156 print_dollarhex (cd
, info
, fields
->f_contnum
, 0, pc
, length
);
158 case MS1_OPERAND_CR
:
159 print_dollarhex (cd
, info
, fields
->f_cr
, 0, pc
, length
);
161 case MS1_OPERAND_CTXDISP
:
162 print_dollarhex (cd
, info
, fields
->f_ctxdisp
, 0, pc
, length
);
164 case MS1_OPERAND_DUP
:
165 print_dollarhex (cd
, info
, fields
->f_dup
, 0, pc
, length
);
167 case MS1_OPERAND_FBDISP
:
168 print_dollarhex (cd
, info
, fields
->f_fbdisp
, 0, pc
, length
);
170 case MS1_OPERAND_FBINCR
:
171 print_dollarhex (cd
, info
, fields
->f_fbincr
, 0, pc
, length
);
173 case MS1_OPERAND_FRDR
:
174 print_keyword (cd
, info
, & ms1_cgen_opval_h_spr
, fields
->f_dr
, 0|(1<<CGEN_OPERAND_ABS_ADDR
));
176 case MS1_OPERAND_FRDRRR
:
177 print_keyword (cd
, info
, & ms1_cgen_opval_h_spr
, fields
->f_drrr
, 0|(1<<CGEN_OPERAND_ABS_ADDR
));
179 case MS1_OPERAND_FRSR1
:
180 print_keyword (cd
, info
, & ms1_cgen_opval_h_spr
, fields
->f_sr1
, 0|(1<<CGEN_OPERAND_ABS_ADDR
));
182 case MS1_OPERAND_FRSR2
:
183 print_keyword (cd
, info
, & ms1_cgen_opval_h_spr
, fields
->f_sr2
, 0|(1<<CGEN_OPERAND_ABS_ADDR
));
185 case MS1_OPERAND_ID
:
186 print_dollarhex (cd
, info
, fields
->f_id
, 0, pc
, length
);
188 case MS1_OPERAND_IMM16
:
189 print_dollarhex (cd
, info
, fields
->f_imm16s
, 0|(1<<CGEN_OPERAND_SIGNED
), pc
, length
);
191 case MS1_OPERAND_IMM16O
:
192 print_dollarhex (cd
, info
, fields
->f_imm16s
, 0, pc
, length
);
194 case MS1_OPERAND_IMM16Z
:
195 print_dollarhex (cd
, info
, fields
->f_imm16u
, 0, pc
, length
);
197 case MS1_OPERAND_INCAMT
:
198 print_dollarhex (cd
, info
, fields
->f_incamt
, 0, pc
, length
);
200 case MS1_OPERAND_INCR
:
201 print_dollarhex (cd
, info
, fields
->f_incr
, 0, pc
, length
);
203 case MS1_OPERAND_LENGTH
:
204 print_dollarhex (cd
, info
, fields
->f_length
, 0, pc
, length
);
206 case MS1_OPERAND_MASK
:
207 print_dollarhex (cd
, info
, fields
->f_mask
, 0, pc
, length
);
209 case MS1_OPERAND_MASK1
:
210 print_dollarhex (cd
, info
, fields
->f_mask1
, 0, pc
, length
);
212 case MS1_OPERAND_MODE
:
213 print_dollarhex (cd
, info
, fields
->f_mode
, 0, pc
, length
);
215 case MS1_OPERAND_PERM
:
216 print_dollarhex (cd
, info
, fields
->f_perm
, 0, pc
, length
);
218 case MS1_OPERAND_RBBC
:
219 print_dollarhex (cd
, info
, fields
->f_rbbc
, 0, pc
, length
);
221 case MS1_OPERAND_RC
:
222 print_dollarhex (cd
, info
, fields
->f_rc
, 0, pc
, length
);
224 case MS1_OPERAND_RC1
:
225 print_dollarhex (cd
, info
, fields
->f_rc1
, 0, pc
, length
);
227 case MS1_OPERAND_RC2
:
228 print_dollarhex (cd
, info
, fields
->f_rc2
, 0, pc
, length
);
230 case MS1_OPERAND_RCNUM
:
231 print_dollarhex (cd
, info
, fields
->f_rcnum
, 0, pc
, length
);
233 case MS1_OPERAND_RDA
:
234 print_dollarhex (cd
, info
, fields
->f_rda
, 0, pc
, length
);
236 case MS1_OPERAND_ROWNUM
:
237 print_dollarhex (cd
, info
, fields
->f_rownum
, 0, pc
, length
);
239 case MS1_OPERAND_ROWNUM1
:
240 print_dollarhex (cd
, info
, fields
->f_rownum1
, 0, pc
, length
);
242 case MS1_OPERAND_ROWNUM2
:
243 print_dollarhex (cd
, info
, fields
->f_rownum2
, 0, pc
, length
);
245 case MS1_OPERAND_SIZE
:
246 print_dollarhex (cd
, info
, fields
->f_size
, 0, pc
, length
);
248 case MS1_OPERAND_TYPE
:
249 print_dollarhex (cd
, info
, fields
->f_type
, 0, pc
, length
);
251 case MS1_OPERAND_WR
:
252 print_dollarhex (cd
, info
, fields
->f_wr
, 0, pc
, length
);
254 case MS1_OPERAND_XMODE
:
255 print_dollarhex (cd
, info
, fields
->f_xmode
, 0, pc
, length
);
259 /* xgettext:c-format */
260 fprintf (stderr
, _("Unrecognized field %d while printing insn.\n"),
266 cgen_print_fn
* const ms1_cgen_print_handlers
[] =
273 ms1_cgen_init_dis (cd
)
276 ms1_cgen_init_opcode_table (cd
);
277 ms1_cgen_init_ibld_table (cd
);
278 cd
->print_handlers
= & ms1_cgen_print_handlers
[0];
279 cd
->print_operand
= ms1_cgen_print_operand
;
283 /* Default print handler. */
286 print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
290 bfd_vma pc ATTRIBUTE_UNUSED
,
291 int length ATTRIBUTE_UNUSED
)
293 disassemble_info
*info
= (disassemble_info
*) dis_info
;
295 #ifdef CGEN_PRINT_NORMAL
296 CGEN_PRINT_NORMAL (cd
, info
, value
, attrs
, pc
, length
);
299 /* Print the operand as directed by the attributes. */
300 if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SEM_ONLY
))
301 ; /* nothing to do */
302 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SIGNED
))
303 (*info
->fprintf_func
) (info
->stream
, "%ld", value
);
305 (*info
->fprintf_func
) (info
->stream
, "0x%lx", value
);
308 /* Default address handler. */
311 print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
315 bfd_vma pc ATTRIBUTE_UNUSED
,
316 int length ATTRIBUTE_UNUSED
)
318 disassemble_info
*info
= (disassemble_info
*) dis_info
;
320 #ifdef CGEN_PRINT_ADDRESS
321 CGEN_PRINT_ADDRESS (cd
, info
, value
, attrs
, pc
, length
);
324 /* Print the operand as directed by the attributes. */
325 if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SEM_ONLY
))
326 ; /* nothing to do */
327 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_PCREL_ADDR
))
328 (*info
->print_address_func
) (value
, info
);
329 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_ABS_ADDR
))
330 (*info
->print_address_func
) (value
, info
);
331 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SIGNED
))
332 (*info
->fprintf_func
) (info
->stream
, "%ld", (long) value
);
334 (*info
->fprintf_func
) (info
->stream
, "0x%lx", (long) value
);
337 /* Keyword print handler. */
340 print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
342 CGEN_KEYWORD
*keyword_table
,
344 unsigned int attrs ATTRIBUTE_UNUSED
)
346 disassemble_info
*info
= (disassemble_info
*) dis_info
;
347 const CGEN_KEYWORD_ENTRY
*ke
;
349 ke
= cgen_keyword_lookup_value (keyword_table
, value
);
351 (*info
->fprintf_func
) (info
->stream
, "%s", ke
->name
);
353 (*info
->fprintf_func
) (info
->stream
, "???");
356 /* Default insn printer.
358 DIS_INFO is defined as `void *' so the disassembler needn't know anything
359 about disassemble_info. */
362 print_insn_normal (CGEN_CPU_DESC cd
,
364 const CGEN_INSN
*insn
,
369 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
370 disassemble_info
*info
= (disassemble_info
*) dis_info
;
371 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
373 CGEN_INIT_PRINT (cd
);
375 for (syn
= CGEN_SYNTAX_STRING (syntax
); *syn
; ++syn
)
377 if (CGEN_SYNTAX_MNEMONIC_P (*syn
))
379 (*info
->fprintf_func
) (info
->stream
, "%s", CGEN_INSN_MNEMONIC (insn
));
382 if (CGEN_SYNTAX_CHAR_P (*syn
))
384 (*info
->fprintf_func
) (info
->stream
, "%c", CGEN_SYNTAX_CHAR (*syn
));
388 /* We have an operand. */
389 ms1_cgen_print_operand (cd
, CGEN_SYNTAX_FIELD (*syn
), info
,
390 fields
, CGEN_INSN_ATTRS (insn
), pc
, length
);
394 /* Subroutine of print_insn. Reads an insn into the given buffers and updates
396 Returns 0 if all is well, non-zero otherwise. */
399 read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
401 disassemble_info
*info
,
404 CGEN_EXTRACT_INFO
*ex_info
,
405 unsigned long *insn_value
)
407 int status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
410 (*info
->memory_error_func
) (status
, pc
, info
);
414 ex_info
->dis_info
= info
;
415 ex_info
->valid
= (1 << buflen
) - 1;
416 ex_info
->insn_bytes
= buf
;
418 *insn_value
= bfd_get_bits (buf
, buflen
* 8, info
->endian
== BFD_ENDIAN_BIG
);
422 /* Utility to print an insn.
423 BUF is the base part of the insn, target byte order, BUFLEN bytes long.
424 The result is the size of the insn in bytes or zero for an unknown insn
425 or -1 if an error occurs fetching data (memory_error_func will have
429 print_insn (CGEN_CPU_DESC cd
,
431 disassemble_info
*info
,
435 CGEN_INSN_INT insn_value
;
436 const CGEN_INSN_LIST
*insn_list
;
437 CGEN_EXTRACT_INFO ex_info
;
440 /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
441 basesize
= cd
->base_insn_bitsize
< buflen
* 8 ?
442 cd
->base_insn_bitsize
: buflen
* 8;
443 insn_value
= cgen_get_insn_value (cd
, buf
, basesize
);
446 /* Fill in ex_info fields like read_insn would. Don't actually call
447 read_insn, since the incoming buffer is already read (and possibly
448 modified a la m32r). */
449 ex_info
.valid
= (1 << buflen
) - 1;
450 ex_info
.dis_info
= info
;
451 ex_info
.insn_bytes
= buf
;
453 /* The instructions are stored in hash lists.
454 Pick the first one and keep trying until we find the right one. */
456 insn_list
= CGEN_DIS_LOOKUP_INSN (cd
, (char *) buf
, insn_value
);
457 while (insn_list
!= NULL
)
459 const CGEN_INSN
*insn
= insn_list
->insn
;
462 unsigned long insn_value_cropped
;
464 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
465 /* Not needed as insn shouldn't be in hash lists if not supported. */
466 /* Supported by this cpu? */
467 if (! ms1_cgen_insn_supported (cd
, insn
))
469 insn_list
= CGEN_DIS_NEXT_INSN (insn_list
);
474 /* Basic bit mask must be correct. */
475 /* ??? May wish to allow target to defer this check until the extract
478 /* Base size may exceed this instruction's size. Extract the
479 relevant part from the buffer. */
480 if ((unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) < buflen
&&
481 (unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) <= sizeof (unsigned long))
482 insn_value_cropped
= bfd_get_bits (buf
, CGEN_INSN_BITSIZE (insn
),
483 info
->endian
== BFD_ENDIAN_BIG
);
485 insn_value_cropped
= insn_value
;
487 if ((insn_value_cropped
& CGEN_INSN_BASE_MASK (insn
))
488 == CGEN_INSN_BASE_VALUE (insn
))
490 /* Printing is handled in two passes. The first pass parses the
491 machine insn and extracts the fields. The second pass prints
494 /* Make sure the entire insn is loaded into insn_value, if it
496 if (((unsigned) CGEN_INSN_BITSIZE (insn
) > cd
->base_insn_bitsize
) &&
497 (unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) <= sizeof (unsigned long))
499 unsigned long full_insn_value
;
500 int rc
= read_insn (cd
, pc
, info
, buf
,
501 CGEN_INSN_BITSIZE (insn
) / 8,
502 & ex_info
, & full_insn_value
);
505 length
= CGEN_EXTRACT_FN (cd
, insn
)
506 (cd
, insn
, &ex_info
, full_insn_value
, &fields
, pc
);
509 length
= CGEN_EXTRACT_FN (cd
, insn
)
510 (cd
, insn
, &ex_info
, insn_value_cropped
, &fields
, pc
);
512 /* length < 0 -> error */
517 CGEN_PRINT_FN (cd
, insn
) (cd
, info
, insn
, &fields
, pc
, length
);
518 /* length is in bits, result is in bytes */
523 insn_list
= CGEN_DIS_NEXT_INSN (insn_list
);
529 /* Default value for CGEN_PRINT_INSN.
530 The result is the size of the insn in bytes or zero for an unknown insn
531 or -1 if an error occured fetching bytes. */
533 #ifndef CGEN_PRINT_INSN
534 #define CGEN_PRINT_INSN default_print_insn
538 default_print_insn (CGEN_CPU_DESC cd
, bfd_vma pc
, disassemble_info
*info
)
540 bfd_byte buf
[CGEN_MAX_INSN_SIZE
];
544 /* Attempt to read the base part of the insn. */
545 buflen
= cd
->base_insn_bitsize
/ 8;
546 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
548 /* Try again with the minimum part, if min < base. */
549 if (status
!= 0 && (cd
->min_insn_bitsize
< cd
->base_insn_bitsize
))
551 buflen
= cd
->min_insn_bitsize
/ 8;
552 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
557 (*info
->memory_error_func
) (status
, pc
, info
);
561 return print_insn (cd
, pc
, info
, buf
, buflen
);
565 Print one instruction from PC on INFO->STREAM.
566 Return the size of the instruction (in bytes). */
568 typedef struct cpu_desc_list
{
569 struct cpu_desc_list
*next
;
577 print_insn_ms1 (bfd_vma pc
, disassemble_info
*info
)
579 static cpu_desc_list
*cd_list
= 0;
580 cpu_desc_list
*cl
= 0;
581 static CGEN_CPU_DESC cd
= 0;
583 static int prev_mach
;
584 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
609 isa
= CGEN_COMPUTE_ISA (info
);
611 isa
= info
->insn_sets
;
614 /* If we've switched cpu's, try to find a handle we've used before */
618 || endian
!= prev_endian
))
621 for (cl
= cd_list
; cl
; cl
= cl
->next
)
623 if (cl
->isa
== isa
&&
625 cl
->endian
== endian
)
633 /* If we haven't initialized yet, initialize the opcode table. */
636 const bfd_arch_info_type
*arch_type
= bfd_lookup_arch (arch
, mach
);
637 const char *mach_name
;
641 mach_name
= arch_type
->printable_name
;
645 prev_endian
= endian
;
646 cd
= ms1_cgen_cpu_open (CGEN_CPU_OPEN_ISAS
, prev_isa
,
647 CGEN_CPU_OPEN_BFDMACH
, mach_name
,
648 CGEN_CPU_OPEN_ENDIAN
, prev_endian
,
653 /* save this away for future reference */
654 cl
= xmalloc (sizeof (struct cpu_desc_list
));
662 ms1_cgen_init_dis (cd
);
665 /* We try to have as much common code as possible.
666 But at this point some targets need to take over. */
667 /* ??? Some targets may need a hook elsewhere. Try to avoid this,
668 but if not possible try to move this hook elsewhere rather than
670 length
= CGEN_PRINT_INSN (cd
, pc
, info
);
676 (*info
->fprintf_func
) (info
->stream
, UNKNOWN_INSN_MSG
);
677 return cd
->default_insn_bitsize
/ 8;