1 /* DO NOT EDIT! -*- buffer-read-only: t -*- vi:set ro: */
2 /* Disassembler interface for targets using CGEN. -*- C -*-
3 CGEN: Cpu tools GENerator
5 THIS FILE IS MACHINE GENERATED WITH CGEN.
6 - the resultant file is machine generated, cgen-dis.in isn't
8 Copyright (C) 1996-2020 Free Software Foundation, Inc.
10 This file is part of libopcodes.
12 This library 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 3, or (at your option)
17 It is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 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.
32 #include "disassemble.h"
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. */
63 /* We need to customize the disassembler a bit:
64 - Use 8 bytes per line by default.
67 #define CGEN_PRINT_INSN bpf_print_insn
70 bpf_print_insn (CGEN_CPU_DESC cd
, bfd_vma pc
, disassemble_info
*info
)
72 bfd_byte buf
[CGEN_MAX_INSN_SIZE
];
76 info
->bytes_per_chunk
= 1;
77 info
->bytes_per_line
= 8;
78 info
->endian_code
= BFD_ENDIAN_BIG
;
80 /* Attempt to read the base part of the insn. */
81 buflen
= cd
->base_insn_bitsize
/ 8;
82 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
84 /* Try again with the minimum part, if min < base. */
85 if (status
!= 0 && (cd
->min_insn_bitsize
< cd
->base_insn_bitsize
))
87 buflen
= cd
->min_insn_bitsize
/ 8;
88 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
93 (*info
->memory_error_func
) (status
, pc
, info
);
97 return print_insn (cd
, pc
, info
, buf
, buflen
);
100 /* Signed immediates should be printed in hexadecimal. */
103 print_immediate (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
106 unsigned int attrs ATTRIBUTE_UNUSED
,
107 bfd_vma pc ATTRIBUTE_UNUSED
,
108 int length ATTRIBUTE_UNUSED
)
110 disassemble_info
*info
= (disassemble_info
*) dis_info
;
113 (*info
->fprintf_func
) (info
->stream
, "%" PRId64
, value
);
115 (*info
->fprintf_func
) (info
->stream
, "%#" PRIx64
, value
);
117 /* This is to avoid -Wunused-function for print_normal. */
119 print_normal (cd
, dis_info
, value
, attrs
, pc
, length
);
122 /* Endianness bit sizes should be printed in decimal. */
125 print_endsize (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
128 unsigned int attrs ATTRIBUTE_UNUSED
,
129 bfd_vma pc ATTRIBUTE_UNUSED
,
130 int length ATTRIBUTE_UNUSED
)
132 disassemble_info
*info
= (disassemble_info
*) dis_info
;
133 (*info
->fprintf_func
) (info
->stream
, "%lu", value
);
139 void bpf_cgen_print_operand
140 (CGEN_CPU_DESC
, int, PTR
, CGEN_FIELDS
*, void const *, bfd_vma
, int);
142 /* Main entry point for printing operands.
143 XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
144 of dis-asm.h on cgen.h.
146 This function is basically just a big switch statement. Earlier versions
147 used tables to look up the function to use, but
148 - if the table contains both assembler and disassembler functions then
149 the disassembler contains much of the assembler and vice-versa,
150 - there's a lot of inlining possibilities as things grow,
151 - using a switch statement avoids the function call overhead.
153 This function could be moved into `print_insn_normal', but keeping it
154 separate makes clear the interface between `print_insn_normal' and each of
158 bpf_cgen_print_operand (CGEN_CPU_DESC cd
,
162 void const *attrs ATTRIBUTE_UNUSED
,
166 disassemble_info
*info
= (disassemble_info
*) xinfo
;
170 case BPF_OPERAND_DISP16
:
171 print_normal (cd
, info
, fields
->f_offset16
, 0|(1<<CGEN_OPERAND_SIGNED
)|(1<<CGEN_OPERAND_PCREL_ADDR
), pc
, length
);
173 case BPF_OPERAND_DISP32
:
174 print_normal (cd
, info
, fields
->f_imm32
, 0|(1<<CGEN_OPERAND_SIGNED
)|(1<<CGEN_OPERAND_PCREL_ADDR
), pc
, length
);
176 case BPF_OPERAND_DSTBE
:
177 print_keyword (cd
, info
, & bpf_cgen_opval_h_gpr
, fields
->f_dstbe
, 0);
179 case BPF_OPERAND_DSTLE
:
180 print_keyword (cd
, info
, & bpf_cgen_opval_h_gpr
, fields
->f_dstle
, 0);
182 case BPF_OPERAND_ENDSIZE
:
183 print_endsize (cd
, info
, fields
->f_imm32
, 0, pc
, length
);
185 case BPF_OPERAND_IMM32
:
186 print_immediate (cd
, info
, fields
->f_imm32
, 0|(1<<CGEN_OPERAND_SIGNED
), pc
, length
);
188 case BPF_OPERAND_IMM64
:
189 print_immediate (cd
, info
, fields
->f_imm64
, 0|(1<<CGEN_OPERAND_SIGNED
)|(1<<CGEN_OPERAND_VIRTUAL
), pc
, length
);
191 case BPF_OPERAND_OFFSET16
:
192 print_immediate (cd
, info
, fields
->f_offset16
, 0|(1<<CGEN_OPERAND_SIGNED
), pc
, length
);
194 case BPF_OPERAND_SRCBE
:
195 print_keyword (cd
, info
, & bpf_cgen_opval_h_gpr
, fields
->f_srcbe
, 0);
197 case BPF_OPERAND_SRCLE
:
198 print_keyword (cd
, info
, & bpf_cgen_opval_h_gpr
, fields
->f_srcle
, 0);
202 /* xgettext:c-format */
203 opcodes_error_handler
204 (_("internal error: unrecognized field %d while printing insn"),
210 cgen_print_fn
* const bpf_cgen_print_handlers
[] =
217 bpf_cgen_init_dis (CGEN_CPU_DESC cd
)
219 bpf_cgen_init_opcode_table (cd
);
220 bpf_cgen_init_ibld_table (cd
);
221 cd
->print_handlers
= & bpf_cgen_print_handlers
[0];
222 cd
->print_operand
= bpf_cgen_print_operand
;
226 /* Default print handler. */
229 print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
233 bfd_vma pc ATTRIBUTE_UNUSED
,
234 int length ATTRIBUTE_UNUSED
)
236 disassemble_info
*info
= (disassemble_info
*) dis_info
;
238 /* Print the operand as directed by the attributes. */
239 if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SEM_ONLY
))
240 ; /* nothing to do */
241 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SIGNED
))
242 (*info
->fprintf_func
) (info
->stream
, "%ld", value
);
244 (*info
->fprintf_func
) (info
->stream
, "0x%lx", value
);
247 /* Default address handler. */
250 print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
254 bfd_vma pc ATTRIBUTE_UNUSED
,
255 int length ATTRIBUTE_UNUSED
)
257 disassemble_info
*info
= (disassemble_info
*) dis_info
;
259 /* Print the operand as directed by the attributes. */
260 if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SEM_ONLY
))
261 ; /* Nothing to do. */
262 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_PCREL_ADDR
))
263 (*info
->print_address_func
) (value
, info
);
264 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_ABS_ADDR
))
265 (*info
->print_address_func
) (value
, info
);
266 else if (CGEN_BOOL_ATTR (attrs
, CGEN_OPERAND_SIGNED
))
267 (*info
->fprintf_func
) (info
->stream
, "%ld", (long) value
);
269 (*info
->fprintf_func
) (info
->stream
, "0x%lx", (long) value
);
272 /* Keyword print handler. */
275 print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
277 CGEN_KEYWORD
*keyword_table
,
279 unsigned int attrs ATTRIBUTE_UNUSED
)
281 disassemble_info
*info
= (disassemble_info
*) dis_info
;
282 const CGEN_KEYWORD_ENTRY
*ke
;
284 ke
= cgen_keyword_lookup_value (keyword_table
, value
);
286 (*info
->fprintf_func
) (info
->stream
, "%s", ke
->name
);
288 (*info
->fprintf_func
) (info
->stream
, "???");
291 /* Default insn printer.
293 DIS_INFO is defined as `void *' so the disassembler needn't know anything
294 about disassemble_info. */
297 print_insn_normal (CGEN_CPU_DESC cd
,
299 const CGEN_INSN
*insn
,
304 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
305 disassemble_info
*info
= (disassemble_info
*) dis_info
;
306 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
308 CGEN_INIT_PRINT (cd
);
310 for (syn
= CGEN_SYNTAX_STRING (syntax
); *syn
; ++syn
)
312 if (CGEN_SYNTAX_MNEMONIC_P (*syn
))
314 (*info
->fprintf_func
) (info
->stream
, "%s", CGEN_INSN_MNEMONIC (insn
));
317 if (CGEN_SYNTAX_CHAR_P (*syn
))
319 (*info
->fprintf_func
) (info
->stream
, "%c", CGEN_SYNTAX_CHAR (*syn
));
323 /* We have an operand. */
324 bpf_cgen_print_operand (cd
, CGEN_SYNTAX_FIELD (*syn
), info
,
325 fields
, CGEN_INSN_ATTRS (insn
), pc
, length
);
329 /* Subroutine of print_insn. Reads an insn into the given buffers and updates
331 Returns 0 if all is well, non-zero otherwise. */
334 read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED
,
336 disassemble_info
*info
,
339 CGEN_EXTRACT_INFO
*ex_info
,
340 unsigned long *insn_value
)
342 int status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
346 (*info
->memory_error_func
) (status
, pc
, info
);
350 ex_info
->dis_info
= info
;
351 ex_info
->valid
= (1 << buflen
) - 1;
352 ex_info
->insn_bytes
= buf
;
354 *insn_value
= bfd_get_bits (buf
, buflen
* 8, info
->endian
== BFD_ENDIAN_BIG
);
358 /* Utility to print an insn.
359 BUF is the base part of the insn, target byte order, BUFLEN bytes long.
360 The result is the size of the insn in bytes or zero for an unknown insn
361 or -1 if an error occurs fetching data (memory_error_func will have
365 print_insn (CGEN_CPU_DESC cd
,
367 disassemble_info
*info
,
371 CGEN_INSN_INT insn_value
;
372 const CGEN_INSN_LIST
*insn_list
;
373 CGEN_EXTRACT_INFO ex_info
;
376 /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
377 basesize
= cd
->base_insn_bitsize
< buflen
* 8 ?
378 cd
->base_insn_bitsize
: buflen
* 8;
379 insn_value
= cgen_get_insn_value (cd
, buf
, basesize
);
382 /* Fill in ex_info fields like read_insn would. Don't actually call
383 read_insn, since the incoming buffer is already read (and possibly
384 modified a la m32r). */
385 ex_info
.valid
= (1 << buflen
) - 1;
386 ex_info
.dis_info
= info
;
387 ex_info
.insn_bytes
= buf
;
389 /* The instructions are stored in hash lists.
390 Pick the first one and keep trying until we find the right one. */
392 insn_list
= CGEN_DIS_LOOKUP_INSN (cd
, (char *) buf
, insn_value
);
393 while (insn_list
!= NULL
)
395 const CGEN_INSN
*insn
= insn_list
->insn
;
398 unsigned long insn_value_cropped
;
400 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
401 /* Not needed as insn shouldn't be in hash lists if not supported. */
402 /* Supported by this cpu? */
403 if (! bpf_cgen_insn_supported (cd
, insn
))
405 insn_list
= CGEN_DIS_NEXT_INSN (insn_list
);
410 /* Basic bit mask must be correct. */
411 /* ??? May wish to allow target to defer this check until the extract
414 /* Base size may exceed this instruction's size. Extract the
415 relevant part from the buffer. */
416 if ((unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) < buflen
&&
417 (unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) <= sizeof (unsigned long))
418 insn_value_cropped
= bfd_get_bits (buf
, CGEN_INSN_BITSIZE (insn
),
419 info
->endian
== BFD_ENDIAN_BIG
);
421 insn_value_cropped
= insn_value
;
423 if ((insn_value_cropped
& CGEN_INSN_BASE_MASK (insn
))
424 == CGEN_INSN_BASE_VALUE (insn
))
426 /* Printing is handled in two passes. The first pass parses the
427 machine insn and extracts the fields. The second pass prints
430 /* Make sure the entire insn is loaded into insn_value, if it
432 if (((unsigned) CGEN_INSN_BITSIZE (insn
) > cd
->base_insn_bitsize
) &&
433 (unsigned) (CGEN_INSN_BITSIZE (insn
) / 8) <= sizeof (unsigned long))
435 unsigned long full_insn_value
;
436 int rc
= read_insn (cd
, pc
, info
, buf
,
437 CGEN_INSN_BITSIZE (insn
) / 8,
438 & ex_info
, & full_insn_value
);
441 length
= CGEN_EXTRACT_FN (cd
, insn
)
442 (cd
, insn
, &ex_info
, full_insn_value
, &fields
, pc
);
445 length
= CGEN_EXTRACT_FN (cd
, insn
)
446 (cd
, insn
, &ex_info
, insn_value_cropped
, &fields
, pc
);
448 /* Length < 0 -> error. */
453 CGEN_PRINT_FN (cd
, insn
) (cd
, info
, insn
, &fields
, pc
, length
);
454 /* Length is in bits, result is in bytes. */
459 insn_list
= CGEN_DIS_NEXT_INSN (insn_list
);
465 /* Default value for CGEN_PRINT_INSN.
466 The result is the size of the insn in bytes or zero for an unknown insn
467 or -1 if an error occured fetching bytes. */
469 #ifndef CGEN_PRINT_INSN
470 #define CGEN_PRINT_INSN default_print_insn
474 default_print_insn (CGEN_CPU_DESC cd
, bfd_vma pc
, disassemble_info
*info
)
476 bfd_byte buf
[CGEN_MAX_INSN_SIZE
];
480 /* Attempt to read the base part of the insn. */
481 buflen
= cd
->base_insn_bitsize
/ 8;
482 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
484 /* Try again with the minimum part, if min < base. */
485 if (status
!= 0 && (cd
->min_insn_bitsize
< cd
->base_insn_bitsize
))
487 buflen
= cd
->min_insn_bitsize
/ 8;
488 status
= (*info
->read_memory_func
) (pc
, buf
, buflen
, info
);
493 (*info
->memory_error_func
) (status
, pc
, info
);
497 return print_insn (cd
, pc
, info
, buf
, buflen
);
501 Print one instruction from PC on INFO->STREAM.
502 Return the size of the instruction (in bytes). */
504 typedef struct cpu_desc_list
506 struct cpu_desc_list
*next
;
514 print_insn_bpf (bfd_vma pc
, disassemble_info
*info
)
516 static cpu_desc_list
*cd_list
= 0;
517 cpu_desc_list
*cl
= 0;
518 static CGEN_CPU_DESC cd
= 0;
519 static CGEN_BITSET
*prev_isa
;
520 static int prev_mach
;
521 static int prev_endian
;
525 int endian
= (info
->endian
== BFD_ENDIAN_BIG
527 : CGEN_ENDIAN_LITTLE
);
528 enum bfd_architecture arch
;
530 /* ??? gdb will set mach but leave the architecture as "unknown" */
531 #ifndef CGEN_BFD_ARCH
532 #define CGEN_BFD_ARCH bfd_arch_bpf
535 if (arch
== bfd_arch_unknown
)
536 arch
= CGEN_BFD_ARCH
;
538 /* There's no standard way to compute the machine or isa number
539 so we leave it to the target. */
540 #ifdef CGEN_COMPUTE_MACH
541 mach
= CGEN_COMPUTE_MACH (info
);
546 #ifdef CGEN_COMPUTE_ISA
548 static CGEN_BITSET
*permanent_isa
;
551 permanent_isa
= cgen_bitset_create (MAX_ISAS
);
553 cgen_bitset_clear (isa
);
554 cgen_bitset_add (isa
, CGEN_COMPUTE_ISA (info
));
557 isa
= info
->private_data
;
560 /* If we've switched cpu's, try to find a handle we've used before */
562 && (cgen_bitset_compare (isa
, prev_isa
) != 0
564 || endian
!= prev_endian
))
567 for (cl
= cd_list
; cl
; cl
= cl
->next
)
569 if (cgen_bitset_compare (cl
->isa
, isa
) == 0 &&
571 cl
->endian
== endian
)
580 /* If we haven't initialized yet, initialize the opcode table. */
583 const bfd_arch_info_type
*arch_type
= bfd_lookup_arch (arch
, mach
);
584 const char *mach_name
;
588 mach_name
= arch_type
->printable_name
;
590 prev_isa
= cgen_bitset_copy (isa
);
592 prev_endian
= endian
;
593 cd
= bpf_cgen_cpu_open (CGEN_CPU_OPEN_ISAS
, prev_isa
,
594 CGEN_CPU_OPEN_BFDMACH
, mach_name
,
595 CGEN_CPU_OPEN_ENDIAN
, prev_endian
,
600 /* Save this away for future reference. */
601 cl
= xmalloc (sizeof (struct cpu_desc_list
));
609 bpf_cgen_init_dis (cd
);
612 /* We try to have as much common code as possible.
613 But at this point some targets need to take over. */
614 /* ??? Some targets may need a hook elsewhere. Try to avoid this,
615 but if not possible try to move this hook elsewhere rather than
617 length
= CGEN_PRINT_INSN (cd
, pc
, info
);
623 (*info
->fprintf_func
) (info
->stream
, UNKNOWN_INSN_MSG
);
624 return cd
->default_insn_bitsize
/ 8;