1 /* Instruction printing code for the ARC.
2 Copyright (C) 1994-2024 Free Software Foundation, Inc.
4 Contributed by Claudiu Zissulescu (claziss@synopsys.com)
6 This file is part of libopcodes.
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. */
27 #include "opcode/arc.h"
32 #include "libiberty.h"
35 /* Structure used to iterate over, and extract the values for, operands of
38 struct arc_operand_iterator
40 /* The complete instruction value to extract operands from. */
41 unsigned long long insn
;
43 /* The LIMM if this is being tracked separately. This field is only
44 valid if we find the LIMM operand in the operand list. */
47 /* The opcode this iterator is operating on. */
48 const struct arc_opcode
*opcode
;
50 /* The index into the opcodes operand index list. */
51 const unsigned char *opidx
;
54 /* A private data used by ARC decoder. */
55 struct arc_disassemble_info
57 /* The current disassembled arc opcode. */
58 const struct arc_opcode
*opcode
;
60 /* Instruction length w/o limm field. */
63 /* TRUE if we have limm. */
66 /* LIMM value, if exists. */
69 /* Condition code, if exists. */
70 unsigned condition_code
;
73 unsigned writeback_mode
;
75 /* Number of operands. */
76 unsigned operands_count
;
78 struct arc_insn_operand operands
[MAX_INSN_ARGS
];
81 /* Globals variables. */
83 static const char * const regnames
[64] =
85 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
86 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
87 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
88 "r24", "r25", "gp", "fp", "sp", "ilink", "r30", "blink",
90 "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
91 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
92 "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
93 "r56", "r57", "r58", "r59", "lp_count", "reserved", "LIMM", "pcl"
96 static const char * const addrtypenames
[ARC_NUM_ADDRTYPES
] =
98 "bd", "jid", "lbd", "mbd", "sd", "sm", "xa", "xd",
99 "cd", "cbd", "cjid", "clbd", "cm", "csd", "cxa", "cxd"
102 static int addrtypenames_max
= ARC_NUM_ADDRTYPES
- 1;
104 static const char * const addrtypeunknown
= "unknown";
106 /* This structure keeps track which instruction class(es)
107 should be ignored durring disassembling. */
109 typedef struct skipclass
111 insn_class_t insn_class
;
112 insn_subclass_t subclass
;
113 struct skipclass
*nxt
;
114 } skipclass_t
, *linkclass
;
116 /* Intial classes of instructions to be consider first when
118 static linkclass decodelist
= NULL
;
120 /* ISA mask value enforced via disassembler info options. ARC_OPCODE_NONE
121 value means that no CPU is enforced. */
123 static unsigned enforced_isa_mask
= ARC_OPCODE_NONE
;
125 /* True if we want to print using only hex numbers. */
126 static bool print_hex
= false;
128 /* Macros section. */
131 # define pr_debug(fmt, args...) fprintf (stderr, fmt, ##args)
133 # define pr_debug(fmt, args...)
136 #define ARRANGE_ENDIAN(info, buf) \
137 (info->endian == BFD_ENDIAN_LITTLE ? bfd_getm32 (bfd_getl32 (buf)) \
140 #define BITS(word,s,e) (((word) >> (s)) & ((1ull << ((e) - (s)) << 1) - 1))
141 #define OPCODE_32BIT_INSN(word) (BITS ((word), 27, 31))
143 /* Functions implementation. */
145 /* Initialize private data. */
147 init_arc_disasm_info (struct disassemble_info
*info
)
149 struct arc_disassemble_info
*arc_infop
150 = calloc (1, sizeof (*arc_infop
));
152 if (arc_infop
== NULL
)
155 info
->private_data
= arc_infop
;
159 /* Add a new element to the decode list. */
162 add_to_decodelist (insn_class_t insn_class
,
163 insn_subclass_t subclass
)
165 linkclass t
= (linkclass
) xmalloc (sizeof (skipclass_t
));
167 t
->insn_class
= insn_class
;
168 t
->subclass
= subclass
;
173 /* Return TRUE if we need to skip the opcode from being
177 skip_this_opcode (const struct arc_opcode
*opcode
)
179 linkclass t
= decodelist
;
181 /* Check opcode for major 0x06, return if it is not in. */
182 if (arc_opcode_len (opcode
) == 4
183 && (OPCODE_32BIT_INSN (opcode
->opcode
) != 0x06
184 /* Can be an APEX extensions. */
185 && OPCODE_32BIT_INSN (opcode
->opcode
) != 0x07))
188 /* or not a known truble class. */
189 switch (opcode
->insn_class
)
202 if ((t
->insn_class
== opcode
->insn_class
)
203 && (t
->subclass
== opcode
->subclass
))
212 bfd_getm32 (unsigned int data
)
216 value
= ((data
& 0xff00) | (data
& 0xff)) << 16;
217 value
|= ((data
& 0xff0000) | (data
& 0xff000000)) >> 16;
222 special_flag_p (const char *opname
,
225 const struct arc_flag_special
*flg_spec
;
226 unsigned i
, j
, flgidx
;
228 for (i
= 0; i
< arc_num_flag_special
; i
++)
230 flg_spec
= &arc_flag_special_cases
[i
];
232 if (strcmp (opname
, flg_spec
->name
))
235 /* Found potential special case instruction. */
238 flgidx
= flg_spec
->flags
[j
];
240 break; /* End of the array. */
242 if (strcmp (flgname
, arc_flag_operands
[flgidx
].name
) == 0)
249 /* Find opcode from ARC_TABLE given the instruction described by INSN and
250 INSNLEN. The ISA_MASK restricts the possible matches in ARC_TABLE. */
252 static const struct arc_opcode
*
253 find_format_from_table (struct disassemble_info
*info
,
254 const struct arc_opcode
*arc_table
,
255 unsigned long long insn
,
256 unsigned int insn_len
,
262 const struct arc_opcode
*opcode
= NULL
;
263 const struct arc_opcode
*t_op
= NULL
;
264 const unsigned char *opidx
;
265 const unsigned char *flgidx
;
270 bool invalid
= false;
272 opcode
= &arc_table
[i
++];
274 if (!(opcode
->cpu
& isa_mask
))
277 if (arc_opcode_len (opcode
) != (int) insn_len
)
280 if ((insn
& opcode
->mask
) != opcode
->opcode
)
285 /* Possible candidate, check the operands. */
286 for (opidx
= opcode
->operands
; *opidx
; opidx
++)
289 const struct arc_operand
*operand
= &arc_operands
[*opidx
];
291 if (operand
->flags
& ARC_OPERAND_FAKE
)
294 if (operand
->extract
)
295 value
= (*operand
->extract
) (insn
, &invalid
);
297 value
= (insn
>> operand
->shift
) & ((1ull << operand
->bits
) - 1);
299 /* Check for LIMM indicator. If it is there, then make sure
300 we pick the right format. */
301 limmind
= (isa_mask
& ARC_OPCODE_ARCV2
) ? 0x1E : 0x3E;
302 if (operand
->flags
& ARC_OPERAND_IR
303 && !(operand
->flags
& ARC_OPERAND_LIMM
))
305 if ((value
== 0x3E && insn_len
== 4)
306 || (value
== limmind
&& insn_len
== 2))
313 if (operand
->flags
& ARC_OPERAND_LIMM
314 && !(operand
->flags
& ARC_OPERAND_DUPLICATE
))
318 /* Check the flags. */
319 for (flgidx
= opcode
->flags
; *flgidx
; flgidx
++)
321 /* Get a valid flag class. */
322 const struct arc_flag_class
*cl_flags
= &arc_flag_classes
[*flgidx
];
323 const unsigned *flgopridx
;
324 int foundA
= 0, foundB
= 0;
327 /* Check first the extensions. */
328 if (cl_flags
->flag_class
& F_CLASS_EXTEND
)
330 value
= (insn
& 0x1F);
331 if (arcExtMap_condCodeName (value
))
335 /* Check for the implicit flags. */
336 if (cl_flags
->flag_class
& F_CLASS_IMPLICIT
)
339 for (flgopridx
= cl_flags
->flags
; *flgopridx
; ++flgopridx
)
341 const struct arc_flag_operand
*flg_operand
=
342 &arc_flag_operands
[*flgopridx
];
344 value
= (insn
>> flg_operand
->shift
)
345 & ((1 << flg_operand
->bits
) - 1);
346 if (value
== flg_operand
->code
)
352 if (!foundA
&& foundB
)
367 if (skip_this_opcode (opcode
))
371 /* The instruction is valid. */
374 while (opcode
->mask
);
378 info
->fprintf_styled_func
379 (info
->stream
, dis_style_text
,
380 _("\nWarning: disassembly may be wrong due to "
381 "guessed opcode class choice.\n"
382 "Use -M<class[,class]> to select the correct "
383 "opcode class(es).\n\t\t\t\t"));
390 /* Find opcode for INSN, trying various different sources. The instruction
391 length in INSN_LEN will be updated if the instruction requires a LIMM
394 A pointer to the opcode is placed into OPCODE_RESULT, and ITER is
395 initialised, ready to iterate over the operands of the found opcode. If
396 the found opcode requires a LIMM then the LIMM value will be loaded into a
399 This function returns TRUE in almost all cases, FALSE is reserved to
400 indicate an error (failing to find an opcode is not an error) a returned
401 result of FALSE would indicate that the disassembler can't continue.
403 If no matching opcode is found then the returned result will be TRUE, the
404 value placed into OPCODE_RESULT will be NULL, ITER will be undefined, and
405 INSN_LEN will be unchanged.
407 If a matching opcode is found, then the returned result will be TRUE, the
408 opcode pointer is placed into OPCODE_RESULT, INSN_LEN will be increased by
409 4 if the instruction requires a LIMM, and the LIMM value will have been
410 loaded into a field of ITER. Finally, ITER will have been initialised so
411 that calls to OPERAND_ITERATOR_NEXT will iterate over the opcode's
415 find_format (bfd_vma memaddr
,
416 unsigned long long insn
,
417 unsigned int * insn_len
,
419 struct disassemble_info
* info
,
420 const struct arc_opcode
** opcode_result
,
421 struct arc_operand_iterator
* iter
)
423 const struct arc_opcode
*opcode
= NULL
;
424 bool needs_limm
= false;
425 const extInstruction_t
*einsn
, *i
;
427 struct arc_disassemble_info
*arc_infop
= info
->private_data
;
429 /* First, try the extension instructions. */
432 einsn
= arcExtMap_insn (OPCODE_32BIT_INSN (insn
), insn
);
433 for (i
= einsn
; (i
!= NULL
) && (opcode
== NULL
); i
= i
->next
)
435 const char *errmsg
= NULL
;
437 opcode
= arcExtMap_genOpcode (i
, isa_mask
, &errmsg
);
440 (*info
->fprintf_styled_func
)
441 (info
->stream
, dis_style_text
,
442 _("An error occurred while generating "
443 "the extension instruction operations"));
444 *opcode_result
= NULL
;
448 opcode
= find_format_from_table (info
, opcode
, insn
, *insn_len
,
449 isa_mask
, &needs_limm
, false);
453 /* Then, try finding the first match in the opcode table. */
455 opcode
= find_format_from_table (info
, arc_opcodes
, insn
, *insn_len
,
456 isa_mask
, &needs_limm
, true);
458 if (opcode
!= NULL
&& needs_limm
)
463 status
= (*info
->read_memory_func
) (memaddr
+ *insn_len
, buffer
,
471 limm
= ARRANGE_ENDIAN (info
, buffer
);
480 iter
->opcode
= opcode
;
481 iter
->opidx
= opcode
->operands
;
484 *opcode_result
= opcode
;
486 /* Update private data. */
487 arc_infop
->opcode
= opcode
;
488 arc_infop
->limm
= limm
;
489 arc_infop
->limm_p
= needs_limm
;
495 print_flags (const struct arc_opcode
*opcode
,
496 unsigned long long *insn
,
497 struct disassemble_info
*info
)
499 const unsigned char *flgidx
;
501 struct arc_disassemble_info
*arc_infop
= info
->private_data
;
503 /* Now extract and print the flags. */
504 for (flgidx
= opcode
->flags
; *flgidx
; flgidx
++)
506 /* Get a valid flag class. */
507 const struct arc_flag_class
*cl_flags
= &arc_flag_classes
[*flgidx
];
508 const unsigned *flgopridx
;
510 /* Check first the extensions. */
511 if (cl_flags
->flag_class
& F_CLASS_EXTEND
)
514 value
= (insn
[0] & 0x1F);
516 name
= arcExtMap_condCodeName (value
);
519 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_mnemonic
,
525 for (flgopridx
= cl_flags
->flags
; *flgopridx
; ++flgopridx
)
527 const struct arc_flag_operand
*flg_operand
=
528 &arc_flag_operands
[*flgopridx
];
530 /* Implicit flags are only used for the insn decoder. */
531 if (cl_flags
->flag_class
& F_CLASS_IMPLICIT
)
533 if (cl_flags
->flag_class
& F_CLASS_COND
)
534 arc_infop
->condition_code
= flg_operand
->code
;
535 else if (cl_flags
->flag_class
& F_CLASS_WB
)
536 arc_infop
->writeback_mode
= flg_operand
->code
;
537 else if (cl_flags
->flag_class
& F_CLASS_ZZ
)
538 info
->data_size
= flg_operand
->code
;
542 if (!flg_operand
->favail
)
545 value
= (insn
[0] >> flg_operand
->shift
)
546 & ((1 << flg_operand
->bits
) - 1);
547 if (value
== flg_operand
->code
)
549 /* FIXME!: print correctly nt/t flag. */
550 if (!special_flag_p (opcode
->name
, flg_operand
->name
))
551 (*info
->fprintf_styled_func
) (info
->stream
,
552 dis_style_mnemonic
, ".");
553 else if (info
->insn_type
== dis_dref
)
555 switch (flg_operand
->name
[0])
569 if (flg_operand
->name
[0] == 'd'
570 && flg_operand
->name
[1] == 0)
571 info
->branch_delay_insns
= 1;
573 /* Check if it is a conditional flag. */
574 if (cl_flags
->flag_class
& F_CLASS_COND
)
576 if (info
->insn_type
== dis_jsr
)
577 info
->insn_type
= dis_condjsr
;
578 else if (info
->insn_type
== dis_branch
)
579 info
->insn_type
= dis_condbranch
;
580 arc_infop
->condition_code
= flg_operand
->code
;
583 /* Check for the write back modes. */
584 if (cl_flags
->flag_class
& F_CLASS_WB
)
585 arc_infop
->writeback_mode
= flg_operand
->code
;
587 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_mnemonic
,
588 "%s", flg_operand
->name
);
595 get_auxreg (const struct arc_opcode
*opcode
,
601 const struct arc_aux_reg
*auxr
= &arc_aux_regs
[0];
603 if (opcode
->insn_class
!= AUXREG
)
606 name
= arcExtMap_auxRegName (value
);
610 for (i
= 0; i
< arc_num_aux_regs
; i
++, auxr
++)
612 if (!(auxr
->cpu
& isa_mask
))
615 if (auxr
->subclass
!= NONE
)
618 if (auxr
->address
== value
)
624 /* Convert a value representing an address type to a string used to refer to
625 the address type in assembly code. */
628 get_addrtype (int value
)
630 if (value
< 0 || value
> addrtypenames_max
)
631 return addrtypeunknown
;
633 return addrtypenames
[value
];
636 /* Calculate the instruction length for an instruction starting with MSB
637 and LSB, the most and least significant byte. The ISA_MASK is used to
638 filter the instructions considered to only those that are part of the
639 current architecture.
641 The instruction lengths are calculated from the ARC_OPCODE table, and
642 cached for later use. */
645 arc_insn_length (bfd_byte msb
, bfd_byte lsb
, struct disassemble_info
*info
)
647 bfd_byte major_opcode
= msb
>> 3;
651 case bfd_mach_arc_arc700
:
652 /* The nps400 extension set requires this special casing of the
653 instruction length calculation. Right now this is not causing any
654 problems as none of the known extensions overlap in opcode space,
655 but, if they ever do then we might need to start carrying
656 information around in the elf about which extensions are in use. */
657 if (major_opcode
== 0xb)
659 bfd_byte minor_opcode
= lsb
& 0x1f;
661 if (minor_opcode
< 4)
663 else if (minor_opcode
== 0x10 || minor_opcode
== 0x11)
666 if (major_opcode
== 0xa)
671 case bfd_mach_arc_arc600
:
672 return (major_opcode
> 0xb) ? 2 : 4;
675 case bfd_mach_arc_arcv2
:
676 return (major_opcode
> 0x7) ? 2 : 4;
684 /* Extract and return the value of OPERAND from the instruction whose value
685 is held in the array INSN. */
688 extract_operand_value (const struct arc_operand
*operand
,
689 unsigned long long insn
,
694 /* Read the limm operand, if required. */
695 if (operand
->flags
& ARC_OPERAND_LIMM
)
696 /* The second part of the instruction value will have been loaded as
697 part of the find_format call made earlier. */
701 if (operand
->extract
)
702 value
= (*operand
->extract
) (insn
, (bool *) NULL
);
705 if (operand
->flags
& ARC_OPERAND_ALIGNED32
)
707 value
= (insn
>> operand
->shift
)
708 & ((1 << (operand
->bits
- 2)) - 1);
713 value
= (insn
>> operand
->shift
) & ((1 << operand
->bits
) - 1);
715 if (operand
->flags
& ARC_OPERAND_SIGNED
)
717 int signbit
= 1 << (operand
->bits
- 1);
718 value
= (value
^ signbit
) - signbit
;
726 /* Find the next operand, and the operands value from ITER. Return TRUE if
727 there is another operand, otherwise return FALSE. If there is an
728 operand returned then the operand is placed into OPERAND, and the value
729 into VALUE. If there is no operand returned then OPERAND and VALUE are
733 operand_iterator_next (struct arc_operand_iterator
*iter
,
734 const struct arc_operand
**operand
,
737 if (*iter
->opidx
== 0)
743 *operand
= &arc_operands
[*iter
->opidx
];
744 *value
= extract_operand_value (*operand
, iter
->insn
, iter
->limm
);
750 /* Helper for parsing the options. */
753 parse_option (const char *option
)
755 if (disassembler_options_cmp (option
, "dsp") == 0)
756 add_to_decodelist (DSP
, NONE
);
758 else if (disassembler_options_cmp (option
, "spfp") == 0)
759 add_to_decodelist (FLOAT
, SPX
);
761 else if (disassembler_options_cmp (option
, "dpfp") == 0)
762 add_to_decodelist (FLOAT
, DPX
);
764 else if (disassembler_options_cmp (option
, "quarkse_em") == 0)
766 add_to_decodelist (FLOAT
, DPX
);
767 add_to_decodelist (FLOAT
, SPX
);
768 add_to_decodelist (FLOAT
, QUARKSE1
);
769 add_to_decodelist (FLOAT
, QUARKSE2
);
772 else if (disassembler_options_cmp (option
, "fpuda") == 0)
773 add_to_decodelist (FLOAT
, DPA
);
775 else if (disassembler_options_cmp (option
, "nps400") == 0)
777 add_to_decodelist (ACL
, NPS400
);
778 add_to_decodelist (ARITH
, NPS400
);
779 add_to_decodelist (BITOP
, NPS400
);
780 add_to_decodelist (BMU
, NPS400
);
781 add_to_decodelist (CONTROL
, NPS400
);
782 add_to_decodelist (DMA
, NPS400
);
783 add_to_decodelist (DPI
, NPS400
);
784 add_to_decodelist (MEMORY
, NPS400
);
785 add_to_decodelist (MISC
, NPS400
);
786 add_to_decodelist (NET
, NPS400
);
787 add_to_decodelist (PMU
, NPS400
);
788 add_to_decodelist (PROTOCOL_DECODE
, NPS400
);
789 add_to_decodelist (ULTRAIP
, NPS400
);
792 else if (disassembler_options_cmp (option
, "fpus") == 0)
794 add_to_decodelist (FLOAT
, SP
);
795 add_to_decodelist (FLOAT
, CVT
);
798 else if (disassembler_options_cmp (option
, "fpud") == 0)
800 add_to_decodelist (FLOAT
, DP
);
801 add_to_decodelist (FLOAT
, CVT
);
803 else if (startswith (option
, "hex"))
806 /* xgettext:c-format */
807 opcodes_error_handler (_("unrecognised disassembler option: %s"), option
);
810 #define ARC_CPU_TYPE_A6xx(NAME,EXTRA) \
811 { #NAME, ARC_OPCODE_ARC600, "ARC600" }
812 #define ARC_CPU_TYPE_A7xx(NAME,EXTRA) \
813 { #NAME, ARC_OPCODE_ARC700, "ARC700" }
814 #define ARC_CPU_TYPE_AV2EM(NAME,EXTRA) \
815 { #NAME, ARC_OPCODE_ARCv2EM, "ARC EM" }
816 #define ARC_CPU_TYPE_AV2HS(NAME,EXTRA) \
817 { #NAME, ARC_OPCODE_ARCv2HS, "ARC HS" }
818 #define ARC_CPU_TYPE_NONE \
821 /* A table of CPU names and opcode sets. */
822 static const struct cpu_type
830 #include "elf/arc-cpu.def"
833 /* Helper for parsing the CPU options. Accept any of the ARC architectures
834 values. OPTION should be a value passed to cpu=. */
837 parse_cpu_option (const char *option
)
841 for (i
= 0; cpu_types
[i
].name
; ++i
)
843 if (!disassembler_options_cmp (cpu_types
[i
].name
, option
))
845 return cpu_types
[i
].flags
;
849 /* xgettext:c-format */
850 opcodes_error_handler (_("unrecognised disassembler CPU option: %s"), option
);
851 return ARC_OPCODE_NONE
;
854 /* Go over the options list and parse it. */
857 parse_disassembler_options (const char *options
)
864 /* Disassembler might be reused for difference CPU's, and cpu option set for
865 the first one shouldn't be applied to second (which might not have
866 explicit cpu in its options. Therefore it is required to reset enforced
867 CPU when new options are being parsed. */
868 enforced_isa_mask
= ARC_OPCODE_NONE
;
870 FOR_EACH_DISASSEMBLER_OPTION (option
, options
)
872 /* A CPU option? Cannot use STRING_COMMA_LEN because strncmp is also a
873 preprocessor macro. */
874 if (strncmp (option
, "cpu=", 4) == 0)
875 /* Strip leading `cpu=`. */
876 enforced_isa_mask
= parse_cpu_option (option
+ 4);
878 parse_option (option
);
882 /* Return the instruction type for an instruction described by OPCODE. */
884 static enum dis_insn_type
885 arc_opcode_to_insn_type (const struct arc_opcode
*opcode
)
887 enum dis_insn_type insn_type
;
889 switch (opcode
->insn_class
)
902 if (!strncmp (opcode
->name
, "bl", 2)
903 || !strncmp (opcode
->name
, "jl", 2))
905 if (opcode
->subclass
== COND
)
906 insn_type
= dis_condjsr
;
912 if (opcode
->subclass
== COND
)
913 insn_type
= dis_condbranch
;
915 insn_type
= dis_branch
;
924 insn_type
= dis_dref
;
927 insn_type
= dis_branch
;
930 insn_type
= dis_nonbranch
;
937 /* Disassemble ARC instructions. */
940 print_insn_arc (bfd_vma memaddr
,
941 struct disassemble_info
*info
)
944 unsigned int highbyte
, lowbyte
;
946 unsigned int insn_len
;
947 unsigned long long insn
= 0;
948 unsigned isa_mask
= ARC_OPCODE_NONE
;
949 const struct arc_opcode
*opcode
;
953 const struct arc_operand
*operand
;
955 struct arc_operand_iterator iter
;
956 struct arc_disassemble_info
*arc_infop
;
957 bool rpcl
= false, rset
= false;
959 if (info
->disassembler_options
)
961 parse_disassembler_options (info
->disassembler_options
);
963 /* Avoid repeated parsing of the options. */
964 info
->disassembler_options
= NULL
;
967 if (info
->private_data
== NULL
&& !init_arc_disasm_info (info
))
970 memset (&iter
, 0, sizeof (iter
));
971 highbyte
= ((info
->endian
== BFD_ENDIAN_LITTLE
) ? 1 : 0);
972 lowbyte
= ((info
->endian
== BFD_ENDIAN_LITTLE
) ? 0 : 1);
974 /* Figure out CPU type, unless it was enforced via disassembler options. */
975 if (enforced_isa_mask
== ARC_OPCODE_NONE
)
977 Elf_Internal_Ehdr
*header
= NULL
;
979 if (info
->section
&& info
->section
->owner
)
980 header
= elf_elfheader (info
->section
->owner
);
984 case bfd_mach_arc_arc700
:
985 isa_mask
= ARC_OPCODE_ARC700
;
988 case bfd_mach_arc_arc600
:
989 isa_mask
= ARC_OPCODE_ARC600
;
992 case bfd_mach_arc_arcv2
:
994 isa_mask
= ARC_OPCODE_ARCv2EM
;
995 /* TODO: Perhaps remove definition of header since it is only used at
998 && (header
->e_flags
& EF_ARC_MACH_MSK
) == EF_ARC_CPU_ARCV2HS
)
999 isa_mask
= ARC_OPCODE_ARCv2HS
;
1004 isa_mask
= enforced_isa_mask
;
1006 if (isa_mask
== ARC_OPCODE_ARCv2HS
)
1008 /* FPU instructions are not extensions for HS. */
1009 add_to_decodelist (FLOAT
, SP
);
1010 add_to_decodelist (FLOAT
, DP
);
1011 add_to_decodelist (FLOAT
, CVT
);
1014 /* This variable may be set by the instruction decoder. It suggests
1015 the number of bytes objdump should display on a single line. If
1016 the instruction decoder sets this, it should always set it to
1017 the same value in order to get reasonable looking output. */
1018 info
->bytes_per_line
= 8;
1020 /* In the next lines, we set two info variables control the way
1021 objdump displays the raw data. For example, if bytes_per_line is
1022 8 and bytes_per_chunk is 4, the output will look like this:
1023 00: 00000000 00000000
1024 with the chunks displayed according to "display_endian". */
1026 && !(info
->section
->flags
& SEC_CODE
))
1028 /* This is not a CODE section. */
1029 switch (info
->section
->size
)
1034 size
= info
->section
->size
;
1037 size
= (info
->section
->size
& 0x01) ? 1 : 4;
1040 info
->bytes_per_chunk
= 1;
1041 info
->display_endian
= info
->endian
;
1046 info
->bytes_per_chunk
= 2;
1047 info
->display_endian
= info
->endian
;
1050 /* Read the insn into a host word. */
1051 status
= (*info
->read_memory_func
) (memaddr
, buffer
, size
, info
);
1055 (*info
->memory_error_func
) (status
, memaddr
, info
);
1060 && !(info
->section
->flags
& SEC_CODE
))
1065 data
= bfd_get_bits (buffer
, size
* 8,
1066 info
->display_endian
== BFD_ENDIAN_BIG
);
1070 (*info
->fprintf_styled_func
) (info
->stream
,
1071 dis_style_assembler_directive
,
1073 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1074 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1078 (*info
->fprintf_styled_func
) (info
->stream
,
1079 dis_style_assembler_directive
,
1081 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1082 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1086 (*info
->fprintf_styled_func
) (info
->stream
,
1087 dis_style_assembler_directive
,
1089 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1090 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1099 insn_len
= arc_insn_length (buffer
[highbyte
], buffer
[lowbyte
], info
);
1100 pr_debug ("instruction length = %d bytes\n", insn_len
);
1104 arc_infop
= info
->private_data
;
1105 arc_infop
->insn_len
= insn_len
;
1110 insn
= (buffer
[highbyte
] << 8) | buffer
[lowbyte
];
1115 /* This is a long instruction: Read the remaning 2 bytes. */
1116 status
= (*info
->read_memory_func
) (memaddr
+ 2, &buffer
[2], 2, info
);
1119 (*info
->memory_error_func
) (status
, memaddr
+ 2, info
);
1122 insn
= (unsigned long long) ARRANGE_ENDIAN (info
, buffer
);
1128 status
= (*info
->read_memory_func
) (memaddr
+ 2, &buffer
[2], 4, info
);
1131 (*info
->memory_error_func
) (status
, memaddr
+ 2, info
);
1134 insn
= (unsigned long long) ARRANGE_ENDIAN (info
, &buffer
[2]);
1135 insn
|= ((unsigned long long) buffer
[highbyte
] << 40)
1136 | ((unsigned long long) buffer
[lowbyte
] << 32);
1142 status
= (*info
->read_memory_func
) (memaddr
+ 2, &buffer
[2], 6, info
);
1145 (*info
->memory_error_func
) (status
, memaddr
+ 2, info
);
1149 ((((unsigned long long) ARRANGE_ENDIAN (info
, buffer
)) << 32)
1150 | ((unsigned long long) ARRANGE_ENDIAN (info
, &buffer
[4])));
1155 /* There is no instruction whose length is not 2, 4, 6, or 8. */
1159 pr_debug ("instruction value = %llx\n", insn
);
1161 /* Set some defaults for the insn info. */
1162 info
->insn_info_valid
= 1;
1163 info
->branch_delay_insns
= 0;
1164 info
->data_size
= 4;
1165 info
->insn_type
= dis_nonbranch
;
1169 /* FIXME to be moved in dissasemble_init_for_target. */
1170 info
->disassembler_needs_relocs
= true;
1172 /* Find the first match in the opcode table. */
1173 if (!find_format (memaddr
, insn
, &insn_len
, isa_mask
, info
, &opcode
, &iter
))
1181 (*info
->fprintf_styled_func
) (info
->stream
,
1182 dis_style_assembler_directive
,
1184 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1185 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1186 "0x%04llx", insn
& 0xffff);
1190 (*info
->fprintf_styled_func
) (info
->stream
,
1191 dis_style_assembler_directive
,
1193 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1194 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1195 "0x%08llx", insn
& 0xffffffff);
1199 (*info
->fprintf_styled_func
) (info
->stream
,
1200 dis_style_assembler_directive
,
1202 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1203 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1204 "0x%08llx", insn
& 0xffffffff);
1205 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, " ");
1206 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1207 "0x%04llx", (insn
>> 32) & 0xffff);
1211 (*info
->fprintf_styled_func
) (info
->stream
,
1212 dis_style_assembler_directive
,
1214 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1215 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1216 "0x%08llx", insn
& 0xffffffff);
1217 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, " ");
1218 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1219 "0x%08llx", (insn
>> 32));
1226 info
->insn_type
= dis_noninsn
;
1230 /* Print the mnemonic. */
1231 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_mnemonic
,
1232 "%s", opcode
->name
);
1234 /* Preselect the insn class. */
1235 info
->insn_type
= arc_opcode_to_insn_type (opcode
);
1237 pr_debug ("%s: 0x%08llx\n", opcode
->name
, opcode
->opcode
);
1239 print_flags (opcode
, &insn
, info
);
1241 if (opcode
->operands
[0] != 0)
1242 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1245 open_braket
= false;
1246 arc_infop
->operands_count
= 0;
1248 /* Now extract and print the operands. */
1251 while (operand_iterator_next (&iter
, &operand
, &value
))
1253 if (open_braket
&& (operand
->flags
& ARC_OPERAND_BRAKET
))
1255 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "]");
1256 open_braket
= false;
1260 /* Only take input from real operands. */
1261 if (ARC_OPERAND_IS_FAKE (operand
))
1264 if ((operand
->flags
& ARC_OPERAND_IGNORE
)
1265 && (operand
->flags
& ARC_OPERAND_IR
)
1269 if (operand
->flags
& ARC_OPERAND_COLON
)
1271 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, ":");
1276 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
,",");
1278 if (!open_braket
&& (operand
->flags
& ARC_OPERAND_BRAKET
))
1280 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "[");
1288 if (operand
->flags
& ARC_OPERAND_PCREL
)
1294 info
->target
= (bfd_vma
) (memaddr
& ~3) + value
;
1296 else if (!(operand
->flags
& ARC_OPERAND_IR
))
1302 /* Print the operand as directed by the flags. */
1303 if (operand
->flags
& ARC_OPERAND_IR
)
1307 assert (value
>=0 && value
< 64);
1308 rname
= arcExtMap_coreRegName (value
);
1310 rname
= regnames
[value
];
1311 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_register
,
1314 /* Check if we have a double register to print. */
1315 if (operand
->flags
& ARC_OPERAND_TRUNCATE
)
1317 if ((value
& 0x01) == 0)
1319 rname
= arcExtMap_coreRegName (value
+ 1);
1321 rname
= regnames
[value
+ 1];
1324 rname
= _("\nWarning: illegal use of double register "
1326 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_register
,
1334 else if (operand
->flags
& ARC_OPERAND_LIMM
)
1336 const char *rname
= get_auxreg (opcode
, value
, isa_mask
);
1338 if (rname
&& open_braket
)
1339 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_register
,
1343 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1345 if (info
->insn_type
== dis_branch
1346 || info
->insn_type
== dis_jsr
)
1347 info
->target
= (bfd_vma
) value
;
1350 else if (operand
->flags
& ARC_OPERAND_SIGNED
)
1352 const char *rname
= get_auxreg (opcode
, value
, isa_mask
);
1353 if (rname
&& open_braket
)
1354 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_register
,
1359 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1362 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1366 else if (operand
->flags
& ARC_OPERAND_ADDRTYPE
)
1368 const char *addrtype
= get_addrtype (value
);
1369 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_address
,
1371 /* A colon follow an address type. */
1376 if (operand
->flags
& ARC_OPERAND_TRUNCATE
1377 && !(operand
->flags
& ARC_OPERAND_ALIGNED32
)
1378 && !(operand
->flags
& ARC_OPERAND_ALIGNED16
)
1379 && value
>= 0 && value
<= 14)
1381 /* Leave/Enter mnemonics. */
1388 (*info
->fprintf_styled_func
) (info
->stream
,
1389 dis_style_register
, "r13");
1392 (*info
->fprintf_styled_func
) (info
->stream
,
1393 dis_style_register
, "r13");
1394 (*info
->fprintf_styled_func
) (info
->stream
,
1395 dis_style_text
, "-");
1396 (*info
->fprintf_styled_func
) (info
->stream
,
1397 dis_style_register
, "%s",
1398 regnames
[13 + value
- 1]);
1406 const char *rname
= get_auxreg (opcode
, value
, isa_mask
);
1407 if (rname
&& open_braket
)
1408 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_register
,
1411 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1416 if (operand
->flags
& ARC_OPERAND_LIMM
)
1418 arc_infop
->operands
[arc_infop
->operands_count
].kind
1419 = ARC_OPERAND_KIND_LIMM
;
1420 /* It is not important to have exactly the LIMM indicator
1422 arc_infop
->operands
[arc_infop
->operands_count
].value
= 63;
1426 arc_infop
->operands
[arc_infop
->operands_count
].value
= value
;
1427 arc_infop
->operands
[arc_infop
->operands_count
].kind
1428 = (operand
->flags
& ARC_OPERAND_IR
1429 ? ARC_OPERAND_KIND_REG
1430 : ARC_OPERAND_KIND_SHIMM
);
1432 arc_infop
->operands_count
++;
1435 /* Pretty print extra info for pc-relative operands. */
1438 if (info
->flags
& INSN_HAS_RELOC
)
1439 /* If the instruction has a reloc associated with it, then the
1440 offset field in the instruction will actually be the addend
1441 for the reloc. (We are using REL type relocs). In such
1442 cases, we can ignore the pc when computing addresses, since
1443 the addend is not currently pc-relative. */
1446 (*info
->fprintf_styled_func
) (info
->stream
,
1447 dis_style_comment_start
, "\t;");
1448 (*info
->print_address_func
) ((memaddr
& ~3) + vpcl
, info
);
1456 arc_get_disassembler (bfd
*abfd
)
1458 /* BFD my be absent, if opcodes is invoked from the debugger that
1459 has connected to remote target and doesn't have an ELF file. */
1462 /* Read the extension insns and registers, if any. */
1463 build_ARC_extmap (abfd
);
1469 return print_insn_arc
;
1472 /* Indices into option argument vector for options that do require
1473 an argument. Use ARC_OPTION_ARG_NONE for options that don't
1474 expect an argument. */
1477 ARC_OPTION_ARG_NONE
= -1,
1478 ARC_OPTION_ARG_ARCH
,
1482 /* Valid ARC disassembler options. */
1486 const char *description
;
1487 arc_option_arg_t arg
;
1490 { "cpu=", N_("Enforce the designated architecture while decoding."),
1491 ARC_OPTION_ARG_ARCH
},
1492 { "dsp", N_("Recognize DSP instructions."),
1493 ARC_OPTION_ARG_NONE
},
1494 { "spfp", N_("Recognize FPX SP instructions."),
1495 ARC_OPTION_ARG_NONE
},
1496 { "dpfp", N_("Recognize FPX DP instructions."),
1497 ARC_OPTION_ARG_NONE
},
1498 { "quarkse_em", N_("Recognize FPU QuarkSE-EM instructions."),
1499 ARC_OPTION_ARG_NONE
},
1500 { "fpuda", N_("Recognize double assist FPU instructions."),
1501 ARC_OPTION_ARG_NONE
},
1502 { "fpus", N_("Recognize single precision FPU instructions."),
1503 ARC_OPTION_ARG_NONE
},
1504 { "fpud", N_("Recognize double precision FPU instructions."),
1505 ARC_OPTION_ARG_NONE
},
1506 { "nps400", N_("Recognize NPS400 instructions."),
1507 ARC_OPTION_ARG_NONE
},
1508 { "hex", N_("Use only hexadecimal number to print immediates."),
1509 ARC_OPTION_ARG_NONE
}
1512 /* Populate the structure for representing ARC's disassembly options.
1513 Such a dynamic initialization is desired, because it makes the maintenance
1514 easier and also gdb uses this to enable the "disassembler-option". */
1516 const disasm_options_and_args_t
*
1517 disassembler_options_arc (void)
1519 static disasm_options_and_args_t
*opts_and_args
;
1521 if (opts_and_args
== NULL
)
1523 disasm_option_arg_t
*args
;
1524 disasm_options_t
*opts
;
1526 const size_t nr_of_options
= ARRAY_SIZE (arc_options
);
1527 /* There is a null element at the end of CPU_TYPES, therefore
1528 NR_OF_CPUS is actually 1 more and that is desired here too. */
1529 const size_t nr_of_cpus
= ARRAY_SIZE (cpu_types
);
1531 opts_and_args
= XNEW (disasm_options_and_args_t
);
1533 = XNEWVEC (disasm_option_arg_t
, ARC_OPTION_ARG_SIZE
+ 1);
1534 opts_and_args
->options
.name
1535 = XNEWVEC (const char *, nr_of_options
+ 1);
1536 opts_and_args
->options
.description
1537 = XNEWVEC (const char *, nr_of_options
+ 1);
1538 opts_and_args
->options
.arg
1539 = XNEWVEC (const disasm_option_arg_t
*, nr_of_options
+ 1);
1541 /* Populate the arguments for "cpu=" option. */
1542 args
= opts_and_args
->args
;
1543 args
[ARC_OPTION_ARG_ARCH
].name
= "ARCH";
1544 args
[ARC_OPTION_ARG_ARCH
].values
= XNEWVEC (const char *, nr_of_cpus
);
1545 for (i
= 0; i
< nr_of_cpus
; ++i
)
1546 args
[ARC_OPTION_ARG_ARCH
].values
[i
] = cpu_types
[i
].name
;
1547 args
[ARC_OPTION_ARG_SIZE
].name
= NULL
;
1548 args
[ARC_OPTION_ARG_SIZE
].values
= NULL
;
1550 /* Populate the options. */
1551 opts
= &opts_and_args
->options
;
1552 for (i
= 0; i
< nr_of_options
; ++i
)
1554 opts
->name
[i
] = arc_options
[i
].name
;
1555 opts
->description
[i
] = arc_options
[i
].description
;
1556 if (arc_options
[i
].arg
!= ARC_OPTION_ARG_NONE
)
1557 opts
->arg
[i
] = &args
[arc_options
[i
].arg
];
1559 opts
->arg
[i
] = NULL
;
1561 opts
->name
[nr_of_options
] = NULL
;
1562 opts
->description
[nr_of_options
] = NULL
;
1563 opts
->arg
[nr_of_options
] = NULL
;
1566 return opts_and_args
;
1571 print_arc_disassembler_options (FILE *stream
)
1573 const disasm_options_and_args_t
*opts_and_args
;
1574 const disasm_option_arg_t
*args
;
1575 const disasm_options_t
*opts
;
1579 opts_and_args
= disassembler_options_arc ();
1580 opts
= &opts_and_args
->options
;
1581 args
= opts_and_args
->args
;
1583 fprintf (stream
, _("\nThe following ARC specific disassembler options are"
1584 " supported for use \nwith the -M switch (multiple"
1585 " options should be separated by commas):\n"));
1587 /* Find the maximum length for printing options (and their arg name). */
1588 for (i
= 0; opts
->name
[i
] != NULL
; ++i
)
1590 size_t len
= strlen (opts
->name
[i
]);
1591 len
+= (opts
->arg
[i
]) ? strlen (opts
->arg
[i
]->name
) : 0;
1592 max_len
= (len
> max_len
) ? len
: max_len
;
1595 /* Print the options, their arg and description, if any. */
1596 for (i
= 0, ++max_len
; opts
->name
[i
] != NULL
; ++i
)
1598 fprintf (stream
, " %s", opts
->name
[i
]);
1599 if (opts
->arg
[i
] != NULL
)
1600 fprintf (stream
, "%s", opts
->arg
[i
]->name
);
1601 if (opts
->description
[i
] != NULL
)
1603 size_t len
= strlen (opts
->name
[i
]);
1604 len
+= (opts
->arg
[i
]) ? strlen (opts
->arg
[i
]->name
) : 0;
1606 "%*c %s", (int) (max_len
- len
), ' ', opts
->description
[i
]);
1608 fprintf (stream
, _("\n"));
1611 /* Print the possible values of an argument. */
1612 for (i
= 0; args
[i
].name
!= NULL
; ++i
)
1615 if (args
[i
].values
== NULL
)
1617 fprintf (stream
, _("\n\
1618 For the options above, the following values are supported for \"%s\":\n "),
1620 for (j
= 0; args
[i
].values
[j
] != NULL
; ++j
)
1622 fprintf (stream
, " %s", args
[i
].values
[j
]);
1623 len
+= strlen (args
[i
].values
[j
]) + 1;
1624 /* reset line if printed too long. */
1627 fprintf (stream
, _("\n "));
1631 fprintf (stream
, _("\n"));
1634 fprintf (stream
, _("\n"));
1637 void arc_insn_decode (bfd_vma addr
,
1638 struct disassemble_info
*info
,
1639 disassembler_ftype disasm_func
,
1640 struct arc_instruction
*insn
)
1642 const struct arc_opcode
*opcode
;
1643 struct arc_disassemble_info
*arc_infop
;
1645 /* Ensure that insn would be in the reset state. */
1646 memset (insn
, 0, sizeof (struct arc_instruction
));
1648 /* There was an error when disassembling, for example memory read error. */
1649 if (disasm_func (addr
, info
) < 0)
1651 insn
->valid
= false;
1655 assert (info
->private_data
!= NULL
);
1656 arc_infop
= info
->private_data
;
1658 insn
->length
= arc_infop
->insn_len
;;
1659 insn
->address
= addr
;
1661 /* Quick exit if memory at this address is not an instruction. */
1662 if (info
->insn_type
== dis_noninsn
)
1664 insn
->valid
= false;
1670 opcode
= (const struct arc_opcode
*) arc_infop
->opcode
;
1671 insn
->insn_class
= opcode
->insn_class
;
1672 insn
->limm_value
= arc_infop
->limm
;
1673 insn
->limm_p
= arc_infop
->limm_p
;
1675 insn
->is_control_flow
= (info
->insn_type
== dis_branch
1676 || info
->insn_type
== dis_condbranch
1677 || info
->insn_type
== dis_jsr
1678 || info
->insn_type
== dis_condjsr
);
1680 insn
->has_delay_slot
= info
->branch_delay_insns
;
1681 insn
->writeback_mode
1682 = (enum arc_ldst_writeback_mode
) arc_infop
->writeback_mode
;
1683 insn
->data_size_mode
= info
->data_size
;
1684 insn
->condition_code
= arc_infop
->condition_code
;
1685 memcpy (insn
->operands
, arc_infop
->operands
,
1686 sizeof (struct arc_insn_operand
) * MAX_INSN_ARGS
);
1687 insn
->operands_count
= arc_infop
->operands_count
;
1691 eval: (c-set-style "gnu")