1 /* Disassembler code for CRIS.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed by Axis Communications AB, Lund, Sweden.
4 Written by Hans-Peter Nilsson.
6 This file is part of the GNU opcodes library.
8 This library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
25 #include "opcode/cris.h"
26 #include "libiberty.h"
28 /* No instruction will be disassembled longer than this. In theory, and
29 in silicon, address prefixes can be cascaded. In practice, cascading
30 is not used by GCC, and not supported by the assembler. */
31 #ifndef MAX_BYTES_PER_CRIS_INSN
32 #define MAX_BYTES_PER_CRIS_INSN 8
35 /* Whether or not to decode prefixes, folding it into the following
36 instruction. FIXME: Make this optional later. */
38 #define PARSE_PREFIX 1
41 /* Sometimes we prefix all registers with this character. */
42 #define REGISTER_PREFIX_CHAR '$'
44 /* Whether or not to trace the following sequence:
49 This is the assembly form of a switch-statement in C.
50 The "sub is optional. If there is none, then X will be zero.
51 X is the value of the first case,
52 Y is the number of cases (including default).
54 This results in case offsets printed on the form:
55 case N: -> case_address
56 where N is an estimation on the corresponding 'case' operand in C,
57 and case_address is where execution of that case continues after the
58 sequence presented above.
60 The old style of output was to print the offsets as instructions,
61 which made it hard to follow "case"-constructs in the disassembly,
62 and caused a lot of annoying warnings about undefined instructions.
64 FIXME: Make this optional later. */
66 #define TRACE_CASE (disdata->trace_case)
69 enum cris_disass_family
70 { cris_dis_v0_v10
, cris_dis_common_v10_v32
, cris_dis_v32
};
72 /* Stored in the disasm_info->private_data member. */
73 struct cris_disasm_data
75 /* Whether to print something less confusing if we find something
76 matching a switch-construct. */
79 /* Whether this code is flagged as crisv32. FIXME: Should be an enum
80 that includes "compatible". */
81 enum cris_disass_family distype
;
84 /* Value of first element in switch. */
85 static long case_offset
= 0;
87 /* How many more case-offsets to print. */
88 static long case_offset_counter
= 0;
90 /* Number of case offsets. */
91 static long no_of_case_offsets
= 0;
93 /* Candidate for next case_offset. */
94 static long last_immediate
= 0;
96 static int cris_constraint
97 (const char *, unsigned, unsigned, struct cris_disasm_data
*);
99 /* Parse disassembler options and store state in info. FIXME: For the
100 time being, we abuse static variables. */
103 cris_parse_disassembler_options (disassemble_info
*info
,
104 enum cris_disass_family distype
)
106 struct cris_disasm_data
*disdata
;
108 info
->private_data
= calloc (1, sizeof (struct cris_disasm_data
));
109 disdata
= (struct cris_disasm_data
*) info
->private_data
;
115 = (info
->disassembler_options
== NULL
116 || (strcmp (info
->disassembler_options
, "nocase") != 0));
118 disdata
->distype
= distype
;
122 static const struct cris_spec_reg
*
123 spec_reg_info (unsigned int sreg
, enum cris_disass_family distype
)
127 for (i
= 0; cris_spec_regs
[i
].name
!= NULL
; i
++)
129 if (cris_spec_regs
[i
].number
== sreg
)
131 if (distype
== cris_dis_v32
)
132 switch (cris_spec_regs
[i
].applicable_version
)
134 case cris_ver_warning
:
135 case cris_ver_version_all
:
140 /* No ambiguous sizes or register names with CRISv32. */
141 if (cris_spec_regs
[i
].warning
== NULL
)
142 return &cris_spec_regs
[i
];
146 else if (cris_spec_regs
[i
].applicable_version
!= cris_ver_v32p
)
147 return &cris_spec_regs
[i
];
154 /* Return the number of bits in the argument. */
157 number_of_bits (unsigned int val
)
161 for (bits
= 0; val
!= 0; val
&= val
- 1)
167 /* Get an entry in the opcode-table. */
169 static const struct cris_opcode
*
170 get_opcode_entry (unsigned int insn
,
171 unsigned int prefix_insn
,
172 struct cris_disasm_data
*disdata
)
174 /* For non-prefixed insns, we keep a table of pointers, indexed by the
175 insn code. Each entry is initialized when found to be NULL. */
176 static const struct cris_opcode
**opc_table
= NULL
;
178 const struct cris_opcode
*max_matchedp
= NULL
;
179 const struct cris_opcode
**prefix_opc_table
= NULL
;
181 /* We hold a table for each prefix that need to be handled differently. */
182 static const struct cris_opcode
**dip_prefixes
= NULL
;
183 static const struct cris_opcode
**bdapq_m1_prefixes
= NULL
;
184 static const struct cris_opcode
**bdapq_m2_prefixes
= NULL
;
185 static const struct cris_opcode
**bdapq_m4_prefixes
= NULL
;
186 static const struct cris_opcode
**rest_prefixes
= NULL
;
188 /* Allocate and clear the opcode-table. */
189 if (opc_table
== NULL
)
191 opc_table
= malloc (65536 * sizeof (opc_table
[0]));
192 if (opc_table
== NULL
)
195 memset (opc_table
, 0, 65536 * sizeof (const struct cris_opcode
*));
198 = malloc (65536 * sizeof (const struct cris_opcode
**));
199 if (dip_prefixes
== NULL
)
202 memset (dip_prefixes
, 0, 65536 * sizeof (dip_prefixes
[0]));
205 = malloc (65536 * sizeof (const struct cris_opcode
**));
206 if (bdapq_m1_prefixes
== NULL
)
209 memset (bdapq_m1_prefixes
, 0, 65536 * sizeof (bdapq_m1_prefixes
[0]));
212 = malloc (65536 * sizeof (const struct cris_opcode
**));
213 if (bdapq_m2_prefixes
== NULL
)
216 memset (bdapq_m2_prefixes
, 0, 65536 * sizeof (bdapq_m2_prefixes
[0]));
219 = malloc (65536 * sizeof (const struct cris_opcode
**));
220 if (bdapq_m4_prefixes
== NULL
)
223 memset (bdapq_m4_prefixes
, 0, 65536 * sizeof (bdapq_m4_prefixes
[0]));
226 = malloc (65536 * sizeof (const struct cris_opcode
**));
227 if (rest_prefixes
== NULL
)
230 memset (rest_prefixes
, 0, 65536 * sizeof (rest_prefixes
[0]));
233 /* Get the right table if this is a prefix.
234 This code is connected to cris_constraints in that it knows what
235 prefixes play a role in recognition of patterns; the necessary
236 state is reflected by which table is used. If constraints
237 involving match or non-match of prefix insns are changed, then this
238 probably needs changing too. */
239 if (prefix_insn
!= NO_CRIS_PREFIX
)
241 const struct cris_opcode
*popcodep
242 = (opc_table
[prefix_insn
] != NULL
243 ? opc_table
[prefix_insn
]
244 : get_opcode_entry (prefix_insn
, NO_CRIS_PREFIX
, disdata
));
246 if (popcodep
== NULL
)
249 if (popcodep
->match
== BDAP_QUICK_OPCODE
)
251 /* Since some offsets are recognized with "push" macros, we
252 have to have different tables for them. */
253 int offset
= (prefix_insn
& 255);
261 prefix_opc_table
= bdapq_m4_prefixes
;
265 prefix_opc_table
= bdapq_m2_prefixes
;
269 prefix_opc_table
= bdapq_m1_prefixes
;
273 prefix_opc_table
= rest_prefixes
;
277 else if (popcodep
->match
== DIP_OPCODE
)
278 /* We don't allow postincrement when the prefix is DIP, so use a
279 different table for DIP. */
280 prefix_opc_table
= dip_prefixes
;
282 prefix_opc_table
= rest_prefixes
;
285 if (prefix_insn
!= NO_CRIS_PREFIX
286 && prefix_opc_table
[insn
] != NULL
)
287 max_matchedp
= prefix_opc_table
[insn
];
288 else if (prefix_insn
== NO_CRIS_PREFIX
&& opc_table
[insn
] != NULL
)
289 max_matchedp
= opc_table
[insn
];
292 const struct cris_opcode
*opcodep
;
293 int max_level_of_match
= -1;
295 for (opcodep
= cris_opcodes
;
296 opcodep
->name
!= NULL
;
301 if (disdata
->distype
== cris_dis_v32
)
303 switch (opcodep
->applicable_version
)
305 case cris_ver_version_all
:
311 case cris_ver_sim_v0_10
:
314 case cris_ver_warning
:
331 switch (opcodep
->applicable_version
)
333 case cris_ver_version_all
:
340 case cris_ver_sim_v0_10
:
342 case cris_ver_warning
:
355 /* We give a double lead for bits matching the template in
356 cris_opcodes. Not even, because then "move p8,r10" would
357 be given 2 bits lead over "clear.d r10". When there's a
358 tie, the first entry in the table wins. This is
359 deliberate, to avoid a more complicated recognition
361 if ((opcodep
->match
& insn
) == opcodep
->match
362 && (opcodep
->lose
& insn
) == 0
364 = cris_constraint (opcodep
->args
,
370 += 2 * number_of_bits (opcodep
->match
372 > max_level_of_match
))
374 max_matchedp
= opcodep
;
375 max_level_of_match
= level_of_match
;
377 /* If there was a full match, never mind looking
379 if (level_of_match
>= 2 * 16)
383 /* Fill in the new entry.
385 If there are changes to the opcode-table involving prefixes, and
386 disassembly then does not work correctly, try removing the
387 else-clause below that fills in the prefix-table. If that
388 helps, you need to change the prefix_opc_table setting above, or
389 something related. */
390 if (prefix_insn
== NO_CRIS_PREFIX
)
391 opc_table
[insn
] = max_matchedp
;
393 prefix_opc_table
[insn
] = max_matchedp
;
399 /* Return -1 if the constraints of a bitwise-matched instruction say
400 that there is no match. Otherwise return a nonnegative number
401 indicating the confidence in the match (higher is better). */
404 cris_constraint (const char *cs
,
406 unsigned int prefix_insn
,
407 struct cris_disasm_data
*disdata
)
414 for (s
= cs
; *s
; s
++)
418 /* Do not recognize "pop" if there's a prefix and then only for
420 if (prefix_insn
!= NO_CRIS_PREFIX
421 || disdata
->distype
!= cris_dis_v0_v10
)
426 /* Not recognized at disassembly. */
430 /* Size modifier for "clear", i.e. special register 0, 4 or 8.
431 Check that it is one of them. Only special register 12 could
432 be mismatched, but checking for matches is more logical than
433 checking for mismatches when there are only a few cases. */
434 tmp
= ((insn
>> 12) & 0xf);
435 if (tmp
!= 0 && tmp
!= 4 && tmp
!= 8)
440 if ((insn
& 0x30) == 0x30)
445 /* A prefix operand without side-effect. */
446 if (prefix_insn
!= NO_CRIS_PREFIX
&& (insn
& 0x400) == 0)
457 /* If this is a prefixed insn with postincrement (side-effect),
458 the prefix must not be DIP. */
459 if (prefix_insn
!= NO_CRIS_PREFIX
)
463 const struct cris_opcode
*prefix_opcodep
464 = get_opcode_entry (prefix_insn
, NO_CRIS_PREFIX
, disdata
);
466 if (prefix_opcodep
->match
== DIP_OPCODE
)
475 /* If we don't fall through, then the prefix is ok. */
478 /* A "push" prefix. Check for valid "push" size.
479 In case of special register, it may be != 4. */
480 if (prefix_insn
!= NO_CRIS_PREFIX
)
482 /* Match the prefix insn to BDAPQ. */
483 const struct cris_opcode
*prefix_opcodep
484 = get_opcode_entry (prefix_insn
, NO_CRIS_PREFIX
, disdata
);
486 if (prefix_opcodep
->match
== BDAP_QUICK_OPCODE
)
488 int pushsize
= (prefix_insn
& 255);
495 unsigned int spec_reg
= (insn
>> 12) & 15;
496 const struct cris_spec_reg
*sregp
497 = spec_reg_info (spec_reg
, disdata
->distype
);
499 /* For a special-register, the "prefix size" must
500 match the size of the register. */
501 if (sregp
&& sregp
->reg_size
== (unsigned int) -pushsize
)
504 else if (s
[1] == 'R')
506 if ((insn
& 0x30) == 0x20 && pushsize
== -4)
509 /* FIXME: Should abort here; next constraint letter
510 *must* be 'P' or 'R'. */
516 retval
= (((insn
>> 12) & 15) == (insn
& 15));
525 const struct cris_spec_reg
*sregp
526 = spec_reg_info ((insn
>> 12) & 15, disdata
->distype
);
528 /* Since we match four bits, we will give a value of 4-1 = 3
529 in a match. If there is a corresponding exact match of a
530 special register in another pattern, it will get a value of
531 4, which will be higher. This should be correct in that an
532 exact pattern would match better than a general pattern.
534 Note that there is a reason for not returning zero; the
535 pattern for "clear" is partly matched in the bit-pattern
536 (the two lower bits must be zero), while the bit-pattern
537 for a move from a special register is matched in the
538 register constraint. */
550 if (prefix_insn
!= NO_CRIS_PREFIX
&& ! prefix_ok
)
556 /* Format number as hex with a leading "0x" into outbuffer. */
559 format_hex (unsigned long number
,
561 struct cris_disasm_data
*disdata
)
563 /* Truncate negative numbers on >32-bit hosts. */
564 number
&= 0xffffffff;
566 /* Save this value for the "case" support. */
568 last_immediate
= number
;
570 return outbuffer
+ sprintf (outbuffer
, "0x%lx", number
);
573 /* Format number as decimal into outbuffer. Parameter signedp says
574 whether the number should be formatted as signed (!= 0) or
578 format_dec (long number
, char *outbuffer
, int signedp
)
580 last_immediate
= number
;
581 return outbuffer
+ sprintf (outbuffer
, signedp
? "%ld" : "%lu", number
);
584 /* Format the name of the general register regno into outbuffer. */
587 format_reg (struct cris_disasm_data
*disdata
,
590 bool with_reg_prefix
)
593 *outbuffer
++ = REGISTER_PREFIX_CHAR
;
598 /* For v32, there is no context in which we output PC. */
599 if (disdata
->distype
== cris_dis_v32
)
600 outbuffer
= stpcpy (outbuffer
, "acr");
602 outbuffer
= stpcpy (outbuffer
, "pc");
606 outbuffer
= stpcpy (outbuffer
, "sp");
610 outbuffer
+= sprintf (outbuffer
, "r%d", regno
);
617 /* Format the name of a support register into outbuffer. */
620 format_sup_reg (unsigned int regno
,
622 bool with_reg_prefix
)
627 *outbuffer
++ = REGISTER_PREFIX_CHAR
;
629 for (i
= 0; cris_support_regs
[i
].name
!= NULL
; i
++)
630 if (cris_support_regs
[i
].number
== regno
)
631 return stpcpy (outbuffer
, cris_support_regs
[i
].name
);
633 /* There's supposed to be register names covering all numbers, though
634 some may be generic names. */
635 return stpcpy (outbuffer
, "format_sup_reg-BUG");
638 /* Return the length of an instruction. */
641 bytes_to_skip (unsigned int insn
,
642 const struct cris_opcode
*matchedp
,
643 enum cris_disass_family distype
,
644 const struct cris_opcode
*prefix_matchedp
)
646 /* Each insn is a word plus "immediate" operands. */
647 unsigned to_skip
= 2;
648 const char *template_name
= (const char *) matchedp
->args
;
651 for (s
= template_name
; *s
; s
++)
652 if ((*s
== 's' || *s
== 'N' || *s
== 'Y')
653 && (insn
& 0x400) && (insn
& 15) == 15
654 && prefix_matchedp
== NULL
)
656 /* Immediate via [pc+], so we have to check the size of the
658 int mode_size
= 1 << ((insn
>> 4) & (*template_name
== 'z' ? 1 : 3));
660 if (matchedp
->imm_oprnd_size
== SIZE_FIX_32
)
662 else if (matchedp
->imm_oprnd_size
== SIZE_SPEC_REG
)
664 const struct cris_spec_reg
*sregp
665 = spec_reg_info ((insn
>> 12) & 15, distype
);
667 /* FIXME: Improve error handling; should have been caught
672 /* PC is incremented by two, not one, for a byte. Except on
673 CRISv32, where constants are always DWORD-size for
674 special registers. */
676 distype
== cris_dis_v32
? 4 : (sregp
->reg_size
+ 1) & ~1;
679 to_skip
+= (mode_size
+ 1) & ~1;
689 /* Print condition code flags. */
692 print_flags (struct cris_disasm_data
*disdata
, unsigned int insn
, char *cp
)
694 /* Use the v8 (Etrax 100) flag definitions for disassembly.
695 The differences with v0 (Etrax 1..4) vs. Svinto are:
698 FIXME: Emit v0..v3 flag names somehow. */
699 static const char v8_fnames
[] = "cvznxibm";
700 static const char v32_fnames
[] = "cvznxiup";
702 = disdata
->distype
== cris_dis_v32
? v32_fnames
: v8_fnames
;
704 unsigned char flagbits
= (((insn
>> 8) & 0xf0) | (insn
& 15));
707 for (i
= 0; i
< 8; i
++)
708 if (flagbits
& (1 << i
))
714 /* Print out an insn with its operands, and update the info->insn_type
715 fields. The prefix_opcodep and the rest hold a prefix insn that is
716 supposed to be output as an address mode. */
719 print_with_operands (const struct cris_opcode
*opcodep
,
721 unsigned char *buffer
,
723 disassemble_info
*info
,
724 /* If a prefix insn was before this insn (and is supposed
725 to be output as an address), here is a description of
727 const struct cris_opcode
*prefix_opcodep
,
728 unsigned int prefix_insn
,
729 unsigned char *prefix_buffer
,
730 bool with_reg_prefix
)
732 /* Get a buffer of somewhat reasonable size where we store
733 intermediate parts of the insn. */
734 char temp
[sizeof (".d [$r13=$r12-2147483648],$r10") * 2];
736 static const char mode_char
[] = "bwd?";
739 struct cris_disasm_data
*disdata
740 = (struct cris_disasm_data
*) info
->private_data
;
742 /* Print out the name first thing we do. */
743 (*info
->fprintf_func
) (info
->stream
, "%s", opcodep
->name
);
748 /* Ignore any prefix indicator. */
752 if (*s
== 'm' || *s
== 'M' || *s
== 'z')
756 /* Get the size-letter. */
758 ? (insn
& 0x8000 ? 'd'
759 : insn
& 0x4000 ? 'w' : 'b')
760 : mode_char
[(insn
>> 4) & (*s
== 'z' ? 1 : 3)];
762 /* Ignore the size and the space character that follows. */
766 /* Add a space if this isn't a long-branch, because for those will add
767 the condition part of the name later. */
768 if (opcodep
->match
!= (BRANCH_PC_LOW
+ BRANCH_INCR_HIGH
* 256))
771 /* Fill in the insn-type if deducible from the name (and there's no
773 if (opcodep
->name
[0] == 'j')
775 if (startswith (opcodep
->name
, "jsr"))
776 /* It's "jsr" or "jsrc". */
777 info
->insn_type
= dis_jsr
;
779 /* Any other jump-type insn is considered a branch. */
780 info
->insn_type
= dis_branch
;
783 /* We might know some more fields right now. */
784 info
->branch_delay_insns
= opcodep
->delayed
;
786 /* Handle operands. */
792 tp
= format_sup_reg ((insn
>> 12) & 15, tp
, with_reg_prefix
);
797 *tp
++ = REGISTER_PREFIX_CHAR
;
810 /* Ignore at this point; used at earlier stages to avoid
811 recognition if there's a prefix at something that in other
812 ways looks like a "pop". */
816 /* Ignore. This is an optional ".d " on the large one of
821 /* This was the prefix that made this a "push". We've already
822 handled it by recognizing it, so signal that the prefix is
823 handled by setting it to NULL. */
824 prefix_opcodep
= NULL
;
829 tp
= format_reg (disdata
, insn
& 15, tp
, with_reg_prefix
);
833 tp
= format_reg (disdata
, (insn
>> 12) & 15, tp
, with_reg_prefix
);
838 /* Like N but pc-relative to the start of the insn. */
839 int32_t number
= (buffer
[2] + buffer
[3] * 256 + buffer
[4] * 65536
840 + buffer
[5] * 0x1000000u
);
842 /* Finish off and output previous formatted bytes. */
845 (*info
->fprintf_func
) (info
->stream
, "%s", temp
);
848 (*info
->print_address_func
) (addr
+ number
, info
);
854 /* Like n but the offset is bits <3:0> in the instruction. */
855 unsigned int number
= (buffer
[0] & 0xf) * 2;
857 /* Finish off and output previous formatted bytes. */
860 (*info
->fprintf_func
) (info
->stream
, "%s", temp
);
863 (*info
->print_address_func
) (addr
+ number
, info
);
872 /* Any "normal" memory operand. */
873 if ((insn
& 0x400) && (insn
& 15) == 15 && prefix_opcodep
== NULL
)
875 /* We're looking at [pc+], i.e. we need to output an immediate
876 number, where the size can depend on different things. */
879 = ((*cs
== 'z' && (insn
& 0x20))
880 || opcodep
->match
== BDAP_QUICK_OPCODE
);
883 if (opcodep
->imm_oprnd_size
== SIZE_FIX_32
)
885 else if (opcodep
->imm_oprnd_size
== SIZE_SPEC_REG
)
887 const struct cris_spec_reg
*sregp
888 = spec_reg_info ((insn
>> 12) & 15, disdata
->distype
);
890 /* A NULL return should have been as a non-match earlier,
891 so catch it as an internal error in the error-case
894 /* Whatever non-valid size. */
897 /* PC is always incremented by a multiple of two.
898 For CRISv32, immediates are always 4 bytes for
899 special registers. */
900 nbytes
= disdata
->distype
== cris_dis_v32
901 ? 4 : (sregp
->reg_size
+ 1) & ~1;
905 int mode_size
= 1 << ((insn
>> 4) & (*cs
== 'z' ? 1 : 3));
917 if (signedp
&& number
> 127)
922 number
= buffer
[2] + buffer
[3] * 256;
923 if (signedp
&& number
> 32767)
928 number
= (buffer
[2] + buffer
[3] * 256 + buffer
[4] * 65536
929 + buffer
[5] * 0x1000000u
);
938 if ((*cs
== 'z' && (insn
& 0x20))
939 || (opcodep
->match
== BDAP_QUICK_OPCODE
940 && (nbytes
<= 2 || buffer
[1 + nbytes
] == 0)))
941 tp
= format_dec (number
, tp
, signedp
);
944 unsigned int highbyte
= (number
>> 24) & 0xff;
946 /* Either output this as an address or as a number. If it's
947 a dword with the same high-byte as the address of the
948 insn, assume it's an address, and also if it's a non-zero
949 non-0xff high-byte. If this is a jsr or a jump, then
950 it's definitely an address. */
952 && (highbyte
== ((addr
>> 24) & 0xff)
953 || (highbyte
!= 0 && highbyte
!= 0xff)
954 || info
->insn_type
== dis_branch
955 || info
->insn_type
== dis_jsr
))
957 /* Finish off and output previous formatted bytes. */
961 (*info
->fprintf_func
) (info
->stream
, "%s", temp
);
963 (*info
->print_address_func
) ((bfd_vma
) number
, info
);
965 info
->target
= number
;
968 tp
= format_hex (number
, tp
, disdata
);
973 /* Not an immediate number. Then this is a (possibly
974 prefixed) memory operand. */
975 if (info
->insn_type
!= dis_nonbranch
)
979 & (opcodep
->args
[0] == 'z' ? 1 : 3));
981 info
->insn_type
= dis_dref
;
982 info
->flags
|= CRIS_DIS_FLAG_MEMREF
;
984 if (opcodep
->imm_oprnd_size
== SIZE_FIX_32
)
986 else if (opcodep
->imm_oprnd_size
== SIZE_SPEC_REG
)
988 const struct cris_spec_reg
*sregp
989 = spec_reg_info ((insn
>> 12) & 15, disdata
->distype
);
991 /* FIXME: Improve error handling; should have been caught
996 size
= sregp
->reg_size
;
1001 info
->data_size
= size
;
1007 /* We don't match dip with a postincremented field
1008 as a side-effect address mode. */
1009 && ((insn
& 0x400) == 0
1010 || prefix_opcodep
->match
!= DIP_OPCODE
))
1014 tp
= format_reg (disdata
, insn
& 15, tp
, with_reg_prefix
);
1019 /* We mainly ignore the prefix format string when the
1020 address-mode syntax is output. */
1021 switch (prefix_opcodep
->match
)
1024 /* It's [r], [r+] or [pc+]. */
1025 if ((prefix_insn
& 0x400) && (prefix_insn
& 15) == 15)
1027 /* It's [pc+]. This cannot possibly be anything
1029 int32_t number
= (prefix_buffer
[2]
1030 + prefix_buffer
[3] * 256
1031 + prefix_buffer
[4] * 65536
1032 + prefix_buffer
[5] * 0x1000000u
);
1034 info
->target
= (bfd_vma
) number
;
1036 /* Finish off and output previous formatted
1041 (*info
->fprintf_func
) (info
->stream
, "%s", temp
);
1043 (*info
->print_address_func
) ((bfd_vma
) number
, info
);
1047 /* For a memref in an address, we use target2.
1048 In this case, target is zero. */
1050 |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
1051 | CRIS_DIS_FLAG_MEM_TARGET2_MEM
);
1053 info
->target2
= prefix_insn
& 15;
1056 tp
= format_reg (disdata
, prefix_insn
& 15, tp
,
1058 if (prefix_insn
& 0x400)
1064 case BDAP_QUICK_OPCODE
:
1068 number
= prefix_buffer
[0];
1072 /* Output "reg+num" or, if num < 0, "reg-num". */
1073 tp
= format_reg (disdata
, (prefix_insn
>> 12) & 15, tp
,
1077 tp
= format_dec (number
, tp
, 1);
1079 info
->flags
|= CRIS_DIS_FLAG_MEM_TARGET_IS_REG
;
1080 info
->target
= (prefix_insn
>> 12) & 15;
1081 info
->target2
= (bfd_vma
) number
;
1086 /* Output "r+R.m". */
1087 tp
= format_reg (disdata
, prefix_insn
& 15, tp
,
1090 tp
= format_reg (disdata
, (prefix_insn
>> 12) & 15, tp
,
1093 *tp
++ = mode_char
[(prefix_insn
>> 4) & 3];
1096 |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
1097 | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
1099 | ((prefix_insn
& 0x8000)
1100 ? CRIS_DIS_FLAG_MEM_TARGET2_MULT4
1101 : ((prefix_insn
& 0x8000)
1102 ? CRIS_DIS_FLAG_MEM_TARGET2_MULT2
: 0)));
1104 /* Is it the casejump? It's a "adds.w [pc+r%d.w],pc". */
1105 if (insn
== 0xf83f && (prefix_insn
& ~0xf000) == 0x55f)
1106 /* Then start interpreting data as offsets. */
1107 case_offset_counter
= no_of_case_offsets
;
1110 case BDAP_INDIR_OPCODE
:
1111 /* Output "r+s.m", or, if "s" is [pc+], "r+s" or
1113 tp
= format_reg (disdata
, (prefix_insn
>> 12) & 15, tp
,
1116 if ((prefix_insn
& 0x400) && (prefix_insn
& 15) == 15)
1119 unsigned int nbytes
;
1121 /* It's a value. Get its size. */
1122 int mode_size
= 1 << ((prefix_insn
>> 4) & 3);
1132 number
= prefix_buffer
[2];
1138 number
= prefix_buffer
[2] + prefix_buffer
[3] * 256;
1144 number
= (prefix_buffer
[2] + prefix_buffer
[3] * 256
1145 + prefix_buffer
[4] * 65536
1146 + prefix_buffer
[5] * 0x1000000u
);
1155 info
->flags
|= CRIS_DIS_FLAG_MEM_TARGET_IS_REG
;
1156 info
->target2
= (bfd_vma
) number
;
1158 /* If the size is dword, then assume it's an
1162 /* Finish off and output previous formatted
1167 (*info
->fprintf_func
) (info
->stream
, "%s", temp
);
1169 (*info
->print_address_func
) ((bfd_vma
) number
, info
);
1175 tp
= format_dec (number
, tp
, 1);
1180 /* Output "r+[R].m" or "r+[R+].m". */
1183 tp
= format_reg (disdata
, prefix_insn
& 15, tp
,
1185 if (prefix_insn
& 0x400)
1189 *tp
++ = mode_char
[(prefix_insn
>> 4) & 3];
1192 |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
1193 | CRIS_DIS_FLAG_MEM_TARGET2_MEM
1194 | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
1196 | (((prefix_insn
>> 4) == 2)
1198 : (((prefix_insn
>> 4) & 3) == 1
1199 ? CRIS_DIS_FLAG_MEM_TARGET2_MEM_WORD
1200 : CRIS_DIS_FLAG_MEM_TARGET2_MEM_BYTE
)));
1205 (*info
->fprintf_func
) (info
->stream
, "?prefix-bug");
1208 /* To mark that the prefix is used, reset it. */
1209 prefix_opcodep
= NULL
;
1213 tp
= format_reg (disdata
, insn
& 15, tp
, with_reg_prefix
);
1215 info
->flags
|= CRIS_DIS_FLAG_MEM_TARGET_IS_REG
;
1216 info
->target
= insn
& 15;
1226 tp
= format_reg (disdata
, (insn
>> 12) & 15, tp
, with_reg_prefix
);
1228 *tp
++ = mode_char
[(insn
>> 4) & 3];
1232 tp
= format_dec (insn
& 63, tp
, 0);
1237 int where
= buffer
[2] + buffer
[3] * 256;
1242 where
+= addr
+ ((disdata
->distype
== cris_dis_v32
) ? 0 : 4);
1244 if (insn
== BA_PC_INCR_OPCODE
)
1245 info
->insn_type
= dis_branch
;
1247 info
->insn_type
= dis_condbranch
;
1249 info
->target
= (bfd_vma
) where
;
1253 (*info
->fprintf_func
) (info
->stream
, "%s%s ",
1254 temp
, cris_cc_strings
[insn
>> 12]);
1256 (*info
->print_address_func
) ((bfd_vma
) where
, info
);
1261 tp
= format_dec (insn
& 31, tp
, 0);
1265 tp
= format_dec (insn
& 15, tp
, 0);
1270 long offset
= insn
& 0xfe;
1276 if (opcodep
->match
== BA_QUICK_OPCODE
)
1277 info
->insn_type
= dis_branch
;
1279 info
->insn_type
= dis_condbranch
;
1281 target
= addr
+ ((disdata
->distype
== cris_dis_v32
) ? 0 : 2) + offset
;
1282 info
->target
= target
;
1285 (*info
->fprintf_func
) (info
->stream
, "%s", temp
);
1286 (*info
->print_address_func
) (target
, info
);
1293 long number
= buffer
[0];
1296 number
= number
- 256;
1298 tp
= format_dec (number
, tp
, 1);
1300 tp
= format_reg (disdata
, (insn
>> 12) & 15, tp
, with_reg_prefix
);
1305 tp
= print_flags (disdata
, insn
, tp
);
1309 tp
= format_dec ((insn
& 32) ? (insn
& 31) | ~31L : insn
& 31, tp
, 1);
1314 const struct cris_spec_reg
*sregp
1315 = spec_reg_info ((insn
>> 12) & 15, disdata
->distype
);
1317 if (sregp
->name
== NULL
)
1318 /* Should have been caught as a non-match eariler. */
1322 if (with_reg_prefix
)
1323 *tp
++ = REGISTER_PREFIX_CHAR
;
1324 strcpy (tp
, sregp
->name
);
1339 (*info
->fprintf_func
) (info
->stream
, " (OOPS unused prefix \"%s: %s\")",
1340 prefix_opcodep
->name
, prefix_opcodep
->args
);
1342 (*info
->fprintf_func
) (info
->stream
, "%s", temp
);
1344 /* Get info for matching case-tables, if we don't have any active.
1345 We assume that the last constant seen is used; either in the insn
1346 itself or in a "move.d const,rN, sub.d rN,rM"-like sequence. */
1347 if (TRACE_CASE
&& case_offset_counter
== 0)
1349 if (startswith (opcodep
->name
, "sub"))
1350 case_offset
= last_immediate
;
1352 /* It could also be an "add", if there are negative case-values. */
1353 else if (startswith (opcodep
->name
, "add"))
1354 /* The first case is the negated operand to the add. */
1355 case_offset
= -last_immediate
;
1357 /* A bound insn will tell us the number of cases. */
1358 else if (startswith (opcodep
->name
, "bound"))
1359 no_of_case_offsets
= last_immediate
+ 1;
1361 /* A jump or jsr or branch breaks the chain of insns for a
1362 case-table, so assume default first-case again. */
1363 else if (info
->insn_type
== dis_jsr
1364 || info
->insn_type
== dis_branch
1365 || info
->insn_type
== dis_condbranch
)
1371 /* Print the CRIS instruction at address memaddr on stream. Returns
1372 length of the instruction, in bytes. Prefix register names with `$' if
1376 print_insn_cris_generic (bfd_vma memaddr
,
1377 disassemble_info
*info
,
1378 bool with_reg_prefix
)
1382 const struct cris_opcode
*matchedp
;
1384 struct cris_disasm_data
*disdata
1385 = (struct cris_disasm_data
*) info
->private_data
;
1387 /* No instruction will be disassembled as longer than this number of
1388 bytes; stacked prefixes will not be expanded. */
1389 unsigned char buffer
[MAX_BYTES_PER_CRIS_INSN
];
1390 unsigned char *bufp
;
1394 /* There will be an "out of range" error after the last instruction.
1395 Reading pairs of bytes in decreasing number, we hope that we will get
1396 at least the amount that we will consume.
1398 If we can't get any data, or we do not get enough data, we print
1399 the error message. */
1401 for (nbytes
= MAX_BYTES_PER_CRIS_INSN
; nbytes
> 0; nbytes
-= 2)
1403 status
= (*info
->read_memory_func
) (memaddr
, buffer
, nbytes
, info
);
1408 /* If we did not get all we asked for, then clear the rest.
1409 Hopefully this makes a reproducible result in case of errors. */
1410 if (nbytes
!= MAX_BYTES_PER_CRIS_INSN
)
1411 memset (buffer
+ nbytes
, 0, MAX_BYTES_PER_CRIS_INSN
- nbytes
);
1416 /* Set some defaults for the insn info. */
1417 info
->insn_info_valid
= 1;
1418 info
->branch_delay_insns
= 0;
1419 info
->data_size
= 0;
1420 info
->insn_type
= dis_nonbranch
;
1425 /* If we got any data, disassemble it. */
1430 insn
= bufp
[0] + bufp
[1] * 256;
1432 /* If we're in a case-table, don't disassemble the offsets. */
1433 if (TRACE_CASE
&& case_offset_counter
!= 0)
1435 info
->insn_type
= dis_noninsn
;
1438 /* If to print data as offsets, then shortcut here. */
1439 (*info
->fprintf_func
) (info
->stream
, "case %ld%s: -> ",
1440 case_offset
+ no_of_case_offsets
1441 - case_offset_counter
,
1442 case_offset_counter
== 1 ? "/default" :
1445 (*info
->print_address_func
) ((bfd_vma
)
1448 - (no_of_case_offsets
1449 - case_offset_counter
)
1451 case_offset_counter
--;
1453 /* The default case start (without a "sub" or "add") must be
1455 if (case_offset_counter
== 0)
1460 /* We're often called to disassemble zeroes. While this is a
1461 valid "bcc .+2" insn, it is also useless enough and enough
1462 of a nuiscance that we will just output "bcc .+2" for it
1463 and signal it as a noninsn. */
1464 (*info
->fprintf_func
) (info
->stream
,
1465 disdata
->distype
== cris_dis_v32
1466 ? "bcc ." : "bcc .+2");
1467 info
->insn_type
= dis_noninsn
;
1472 const struct cris_opcode
*prefix_opcodep
= NULL
;
1473 unsigned char *prefix_buffer
= bufp
;
1474 unsigned int prefix_insn
= insn
;
1475 int prefix_size
= 0;
1477 matchedp
= get_opcode_entry (insn
, NO_CRIS_PREFIX
, disdata
);
1479 /* Check if we're supposed to write out prefixes as address
1480 modes and if this was a prefix. */
1481 if (matchedp
!= NULL
&& PARSE_PREFIX
&& matchedp
->args
[0] == 'p')
1483 /* If it's a prefix, put it into the prefix vars and get the
1485 prefix_size
= bytes_to_skip (prefix_insn
, matchedp
,
1486 disdata
->distype
, NULL
);
1487 prefix_opcodep
= matchedp
;
1489 insn
= bufp
[prefix_size
] + bufp
[prefix_size
+ 1] * 256;
1490 matchedp
= get_opcode_entry (insn
, prefix_insn
, disdata
);
1492 if (matchedp
!= NULL
)
1494 addr
+= prefix_size
;
1495 bufp
+= prefix_size
;
1496 advance
+= prefix_size
;
1500 /* The "main" insn wasn't valid, at least not when
1501 prefixed. Put back things enough to output the
1502 prefix insn only, as a normal insn. */
1503 matchedp
= prefix_opcodep
;
1505 prefix_opcodep
= NULL
;
1509 if (matchedp
== NULL
)
1511 (*info
->fprintf_func
) (info
->stream
, "??0x%x", insn
);
1514 info
->insn_type
= dis_noninsn
;
1519 += bytes_to_skip (insn
, matchedp
, disdata
->distype
,
1522 /* The info_type and assorted fields will be set according
1524 print_with_operands (matchedp
, insn
, bufp
, addr
, info
,
1525 prefix_opcodep
, prefix_insn
,
1526 prefix_buffer
, with_reg_prefix
);
1531 info
->insn_type
= dis_noninsn
;
1533 /* If we read less than MAX_BYTES_PER_CRIS_INSN, i.e. we got an error
1534 status when reading that much, and the insn decoding indicated a
1535 length exceeding what we read, there is an error. */
1536 if (status
!= 0 && (nbytes
== 0 || advance
> nbytes
))
1538 (*info
->memory_error_func
) (status
, memaddr
, info
);
1542 /* Max supported insn size with one folded prefix insn. */
1543 info
->bytes_per_line
= MAX_BYTES_PER_CRIS_INSN
;
1545 /* I would like to set this to a fixed value larger than the actual
1546 number of bytes to print in order to avoid spaces between bytes,
1547 but objdump.c (2.9.1) does not like that, so we print 16-bit
1548 chunks, which is the next choice. */
1549 info
->bytes_per_chunk
= 2;
1551 /* Printing bytes in order of increasing addresses makes sense,
1552 especially on a little-endian target.
1553 This is completely the opposite of what you think; setting this to
1554 BFD_ENDIAN_LITTLE will print bytes in order N..0 rather than the 0..N
1556 info
->display_endian
= BFD_ENDIAN_BIG
;
1561 /* Disassemble, prefixing register names with `$'. CRIS v0..v10. */
1564 print_insn_cris_with_register_prefix (bfd_vma vma
,
1565 disassemble_info
*info
)
1567 if (info
->private_data
== NULL
1568 && !cris_parse_disassembler_options (info
, cris_dis_v0_v10
))
1570 return print_insn_cris_generic (vma
, info
, true);
1573 /* Disassemble, prefixing register names with `$'. CRIS v32. */
1576 print_insn_crisv32_with_register_prefix (bfd_vma vma
,
1577 disassemble_info
*info
)
1579 if (info
->private_data
== NULL
1580 && !cris_parse_disassembler_options (info
, cris_dis_v32
))
1582 return print_insn_cris_generic (vma
, info
, true);
1585 /* Disassemble, prefixing register names with `$'.
1586 Common v10 and v32 subset. */
1589 print_insn_crisv10_v32_with_register_prefix (bfd_vma vma
,
1590 disassemble_info
*info
)
1592 if (info
->private_data
== NULL
1593 && !cris_parse_disassembler_options (info
, cris_dis_common_v10_v32
))
1595 return print_insn_cris_generic (vma
, info
, true);
1598 /* Disassemble, no prefixes on register names. CRIS v0..v10. */
1601 print_insn_cris_without_register_prefix (bfd_vma vma
,
1602 disassemble_info
*info
)
1604 if (info
->private_data
== NULL
1605 && !cris_parse_disassembler_options (info
, cris_dis_v0_v10
))
1607 return print_insn_cris_generic (vma
, info
, false);
1610 /* Disassemble, no prefixes on register names. CRIS v32. */
1613 print_insn_crisv32_without_register_prefix (bfd_vma vma
,
1614 disassemble_info
*info
)
1616 if (info
->private_data
== NULL
1617 && !cris_parse_disassembler_options (info
, cris_dis_v32
))
1619 return print_insn_cris_generic (vma
, info
, false);
1622 /* Disassemble, no prefixes on register names.
1623 Common v10 and v32 subset. */
1626 print_insn_crisv10_v32_without_register_prefix (bfd_vma vma
,
1627 disassemble_info
*info
)
1629 if (info
->private_data
== NULL
1630 && !cris_parse_disassembler_options (info
, cris_dis_common_v10_v32
))
1632 return print_insn_cris_generic (vma
, info
, false);
1635 /* Return a disassembler-function that prints registers with a `$' prefix,
1636 or one that prints registers without a prefix.
1637 FIXME: We should improve the solution to avoid the multitude of
1638 functions seen above. */
1641 cris_get_disassembler (bfd
*abfd
)
1643 /* If there's no bfd in sight, we return what is valid as input in all
1644 contexts if fed back to the assembler: disassembly *with* register
1645 prefix. Unfortunately this will be totally wrong for v32. */
1647 return print_insn_cris_with_register_prefix
;
1649 if (bfd_get_symbol_leading_char (abfd
) == 0)
1651 if (bfd_get_mach (abfd
) == bfd_mach_cris_v32
)
1652 return print_insn_crisv32_with_register_prefix
;
1653 if (bfd_get_mach (abfd
) == bfd_mach_cris_v10_v32
)
1654 return print_insn_crisv10_v32_with_register_prefix
;
1656 /* We default to v10. This may be specifically specified in the
1657 bfd mach, but is also the default setting. */
1658 return print_insn_cris_with_register_prefix
;
1661 if (bfd_get_mach (abfd
) == bfd_mach_cris_v32
)
1662 return print_insn_crisv32_without_register_prefix
;
1663 if (bfd_get_mach (abfd
) == bfd_mach_cris_v10_v32
)
1664 return print_insn_crisv10_v32_without_register_prefix
;
1665 return print_insn_cris_without_register_prefix
;
1669 eval: (c-set-style "gnu")