1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2024 Free Software Foundation, Inc.
3 Contributed by Andes Technology Corporation.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "disassemble.h"
28 #include "libiberty.h"
32 #include "nds32-asm.h"
33 #include "opcode/nds32.h"
35 /* Get fields macro define. */
36 #define MASK_OP(insn, mask) ((insn) & (0x3f << 25 | (mask)))
38 /* For mapping symbol. */
49 struct nds32_private_data
51 /* Whether any mapping symbols are present in the provided symbol
52 table. -1 if we do not know yet, otherwise 0 or 1. */
53 int has_mapping_symbols
;
55 /* Track the last type (although this doesn't seem to be useful). */
56 enum map_type last_mapping_type
;
58 /* Tracking symbol table information. */
59 int last_symbol_index
;
63 /* Default text to print if an instruction isn't recognized. */
64 #define UNKNOWN_INSN_MSG _("*unknown*")
65 #define NDS32_PARSE_INSN16 0x01
66 #define NDS32_PARSE_INSN32 0x02
68 static uint32_t nds32_mask_opcode (uint32_t);
69 static void nds32_special_opcode (uint32_t, struct nds32_opcode
**);
70 static int get_mapping_symbol_type (struct disassemble_info
*, int,
72 static int is_mapping_symbol (struct disassemble_info
*, int,
75 /* Hash function for disassemble. */
77 static htab_t opcode_htab
;
79 /* Find the value map register name. */
81 static const keyword_t
*
82 nds32_find_reg_keyword (const keyword_t
*reg
, int value
)
87 while (reg
->name
!= NULL
&& reg
->value
!= value
)
91 if (reg
->name
== NULL
)
97 nds32_parse_audio_ext (const field_t
*pfd
,
98 disassemble_info
*info
, uint32_t insn
)
100 fprintf_ftype func
= info
->fprintf_func
;
101 void *stream
= info
->stream
;
102 const keyword_t
*psys_reg
;
103 int int_value
, new_value
;
105 if (pfd
->hw_res
== HW_INT
|| pfd
->hw_res
== HW_UINT
)
107 if (pfd
->hw_res
== HW_INT
)
108 int_value
= (unsigned) N32_IMMS (insn
>> pfd
->bitpos
,
109 pfd
->bitsize
) << pfd
->shift
;
111 int_value
= __GF (insn
, pfd
->bitpos
, pfd
->bitsize
) << pfd
->shift
;
114 func (stream
, "#%d", int_value
);
116 func (stream
, "#0x%x", int_value
);
120 __GF (insn
, pfd
->bitpos
, pfd
->bitsize
) << pfd
->shift
;
121 new_value
= int_value
;
122 psys_reg
= (keyword_t
*) nds32_keywords
[pfd
->hw_res
];
124 /* p = bit[4].bit[1:0], r = bit[4].bit[3:2]. */
125 if (strcmp (pfd
->name
, "im5_i") == 0)
127 new_value
= int_value
& 0x03;
128 new_value
|= ((int_value
& 0x10) >> 2);
130 else if (strcmp (pfd
->name
, "im5_m") == 0)
132 new_value
= ((int_value
& 0x1C) >> 2);
134 /* p = 0.bit[1:0], r = 0.bit[3:2]. */
135 /* q = 1.bit[1:0], s = 1.bit[5:4]. */
136 else if (strcmp (pfd
->name
, "im6_iq") == 0)
140 else if (strcmp (pfd
->name
, "im6_ms") == 0)
144 /* Rt CONCAT(c, t21, t0). */
145 else if (strcmp (pfd
->name
, "a_rt21") == 0)
147 new_value
= (insn
& 0x00000020) >> 5;
148 new_value
|= (insn
& 0x00000C00) >> 9;
149 new_value
|= (insn
& 0x00008000) >> 12;
151 else if (strcmp (pfd
->name
, "a_rte") == 0)
153 new_value
= (insn
& 0x00000C00) >> 9;
154 new_value
|= (insn
& 0x00008000) >> 12;
156 else if (strcmp (pfd
->name
, "a_rte1") == 0)
158 new_value
= (insn
& 0x00000C00) >> 9;
159 new_value
|= (insn
& 0x00008000) >> 12;
162 else if (strcmp (pfd
->name
, "a_rte69") == 0)
164 new_value
= int_value
<< 1;
166 else if (strcmp (pfd
->name
, "a_rte69_1") == 0)
168 new_value
= int_value
<< 1;
172 psys_reg
= nds32_find_reg_keyword (psys_reg
, new_value
);
174 func (stream
, "???");
176 func (stream
, "$%s", psys_reg
->name
);
179 /* Match instruction opcode with keyword table. */
182 match_field (char *name
)
187 for (k
= 0; k
< NDS32_CORE_COUNT
; k
++)
189 pfd
= (field_t
*) nds32_field_table
[k
];
192 if (pfd
->name
== NULL
)
194 if (strcmp (name
, pfd
->name
) == 0)
203 /* Dump instruction. If the opcode is unknown, return FALSE. */
206 nds32_parse_opcode (struct nds32_opcode
*opc
, bfd_vma pc ATTRIBUTE_UNUSED
,
207 disassemble_info
*info
, uint32_t insn
,
211 fprintf_ftype func
= info
->fprintf_func
;
212 void *stream
= info
->stream
;
213 const char *pstr_src
;
216 unsigned int push25gpr
= 0, lsmwRb
, lsmwRe
, lsmwEnb4
, checkbit
, i
;
217 int int_value
, ifthe1st
= 1;
219 const keyword_t
*psys_reg
;
223 func (stream
, UNKNOWN_INSN_MSG
);
227 pstr_src
= opc
->instruction
;
230 func (stream
, "%s", opc
->opcode
);
233 /* NDS32_PARSE_INSN16. */
234 if (parse_mode
& NDS32_PARSE_INSN16
)
236 func (stream
, "%s ", opc
->opcode
);
239 /* NDS32_PARSE_INSN32. */
243 if (op
== N32_OP6_LSMW
)
244 func (stream
, "%s.", opc
->opcode
);
245 else if (strstr (opc
->instruction
, "tito"))
246 func (stream
, "%s", opc
->opcode
);
248 func (stream
, "%s\t", opc
->opcode
);
259 /* Compare with nds32_operand_fields[].name. */
260 pstr_tmp
= &tmp_string
[0];
263 if ((*pstr_src
== ',') || (*pstr_src
== ' ')
264 || (*pstr_src
== '{') || (*pstr_src
== '}')
265 || (*pstr_src
== '[') || (*pstr_src
== ']')
266 || (*pstr_src
== '(') || (*pstr_src
== ')')
267 || (*pstr_src
== '+') || (*pstr_src
== '<'))
269 *pstr_tmp
++ = *pstr_src
++;
273 if ((pfd
= match_field (&tmp_string
[0])) == NULL
)
277 if (parse_mode
& NDS32_PARSE_INSN16
)
279 if (pfd
->hw_res
== HW_GPR
)
282 __GF (insn
, pfd
->bitpos
, pfd
->bitsize
) << pfd
->shift
;
284 if ((opc
->value
== 0xfc00) || (opc
->value
== 0xfc80))
289 int_value
= (6 + (0x01 << int_value
));
290 push25gpr
= int_value
;
292 else if (strcmp (pfd
->name
, "rt4") == 0)
294 int_value
= nds32_r45map
[int_value
];
296 func (stream
, "$%s", nds32_keyword_gpr
[int_value
].name
);
298 else if ((pfd
->hw_res
== HW_INT
) || (pfd
->hw_res
== HW_UINT
))
300 if (pfd
->hw_res
== HW_INT
)
302 = (unsigned) N32_IMMS (insn
>> pfd
->bitpos
,
303 pfd
->bitsize
) << pfd
->shift
;
306 __GF (insn
, pfd
->bitpos
, pfd
->bitsize
) << pfd
->shift
;
309 if (opc
->value
== 0xfa00)
312 func (stream
, "#0x%x", int_value
);
315 else if (opc
->value
== 0xb200)
317 int_value
= 0 - (128 - int_value
);
318 func (stream
, "#%d", int_value
);
320 /* beqz38/bnez38/beqs38/bnes38/j8/beqzs8/bnezs8. */
321 else if ((opc
->value
== 0xc000) || (opc
->value
== 0xc800)
322 || (opc
->value
== 0xd000) || (opc
->value
== 0xd800)
323 || (opc
->value
== 0xd500) || (opc
->value
== 0xe800)
324 || (opc
->value
== 0xe900))
326 info
->print_address_func (int_value
+ pc
, info
);
329 else if ((opc
->value
== 0xfc00) || (opc
->value
== 0xfc80))
331 func (stream
, "#%d ! {$r6", int_value
);
333 func (stream
, "~$%s", nds32_keyword_gpr
[push25gpr
].name
);
334 func (stream
, ", $fp, $gp, $lp}");
336 else if (pfd
->hw_res
== HW_INT
)
339 func (stream
, "#%d", int_value
);
341 func (stream
, "#0x%x", int_value
);
343 else /* if (pfd->hw_res == HW_UINT). */
346 func (stream
, "#%u", int_value
);
348 func (stream
, "#0x%x", int_value
);
354 else if (op
== N32_OP6_AEXT
)
356 nds32_parse_audio_ext (pfd
, info
, insn
);
359 else if (pfd
->hw_res
< HW_INT
)
362 __GF (insn
, pfd
->bitpos
, pfd
->bitsize
) << pfd
->shift
;
364 psys_reg
= *(nds32_keyword_table
[pfd
->hw_res
>> 8]
365 + (pfd
->hw_res
& 0xff));
367 psys_reg
= nds32_find_reg_keyword (psys_reg
, int_value
);
368 /* For HW_SR, dump the index when it can't
369 map the register name. */
370 if (!psys_reg
&& pfd
->hw_res
== HW_SR
)
371 func (stream
, "%d", int_value
);
373 func (stream
, "???");
376 if (pfd
->hw_res
== HW_GPR
|| pfd
->hw_res
== HW_CPR
377 || pfd
->hw_res
== HW_FDR
|| pfd
->hw_res
== HW_FSR
378 || pfd
->hw_res
== HW_DXR
|| pfd
->hw_res
== HW_SR
379 || pfd
->hw_res
== HW_USR
)
380 func (stream
, "$%s", psys_reg
->name
);
381 else if (pfd
->hw_res
== HW_DTITON
382 || pfd
->hw_res
== HW_DTITOFF
)
383 func (stream
, ".%s", psys_reg
->name
);
385 func (stream
, "%s", psys_reg
->name
);
388 else if ((pfd
->hw_res
== HW_INT
) || (pfd
->hw_res
== HW_UINT
))
390 if (pfd
->hw_res
== HW_INT
)
391 int_value
= (unsigned) N32_IMMS (insn
>> pfd
->bitpos
,
392 pfd
->bitsize
) << pfd
->shift
;
395 __GF (insn
, pfd
->bitpos
, pfd
->bitsize
) << pfd
->shift
;
397 if ((op
== N32_OP6_BR1
) || (op
== N32_OP6_BR2
))
399 info
->print_address_func (int_value
+ pc
, info
);
401 else if ((op
== N32_OP6_BR3
) && (pfd
->bitpos
== 0))
403 info
->print_address_func (int_value
+ pc
, info
);
405 else if (op
== N32_OP6_JI
)
407 /* FIXME: Handle relocation. */
408 if (info
->flags
& INSN_HAS_RELOC
)
410 info
->print_address_func (int_value
+ pc
, info
);
412 else if (op
== N32_OP6_LSMW
)
414 /* lmw.adm/smw.adm. */
415 func (stream
, "#0x%x ! {", int_value
);
416 lsmwEnb4
= int_value
;
417 lsmwRb
= ((insn
>> 20) & 0x1F);
418 lsmwRe
= ((insn
>> 10) & 0x1F);
420 /* If [Rb, Re] specifies at least one register,
421 Rb(4,0) <= Re(4,0) and 0 <= Rb(4,0), Re(4,0) < 28.
422 Disassembling does not consider this currently because of
423 the convience comparing with bsp320. */
424 if (lsmwRb
!= 31 || lsmwRe
!= 31)
426 func (stream
, "$%s", nds32_keyword_gpr
[lsmwRb
].name
);
427 if (lsmwRb
!= lsmwRe
)
428 func (stream
, "~$%s", nds32_keyword_gpr
[lsmwRe
].name
);
433 /* $fp, $gp, $lp, $sp. */
435 for (i
= 0; i
< 4; i
++)
437 if (lsmwEnb4
& checkbit
)
442 func (stream
, "$%s", nds32_keyword_gpr
[28 + i
].name
);
445 func (stream
, ", $%s", nds32_keyword_gpr
[28 + i
].name
);
452 else if (pfd
->hw_res
== HW_INT
)
455 func (stream
, "#%d", int_value
);
457 func (stream
, "#0x%x", int_value
);
459 else /* if (pfd->hw_res == HW_UINT). */
462 func (stream
, "#%u", int_value
);
464 func (stream
, "#0x%x", int_value
);
480 func (stream
, " + ");
485 if (pstr_src
[1] == '<')
487 func (stream
, " << ");
498 func (stream
, "%c", *pstr_src
++);
504 /* Filter instructions with some bits must be fixed. */
507 nds32_filter_unknown_insn (uint32_t insn
, struct nds32_opcode
**opc
)
512 switch ((*opc
)->value
)
517 if (__GF (insn
, 6, 2) != 0 || __GF (insn
, 15, 10) != 0)
521 if (__GF (insn
, 7, 18) != 0)
526 if (__GF (insn
, 5, 5) != 0)
530 if (__GF (insn
, 20, 5) != 0)
534 if (__GF (insn
, 5, 3) != 0 || __GF (insn
, 15, 5) != 0)
556 if (__GF (insn
, 5, 5) != 0)
562 if (__GF (insn
, 5, 20) != 0)
569 print_insn32 (bfd_vma pc
, disassemble_info
*info
, uint32_t insn
,
572 /* Get the final correct opcode and parse. */
573 struct nds32_opcode
*opc
;
574 uint32_t opcode
= nds32_mask_opcode (insn
);
575 opc
= (struct nds32_opcode
*) htab_find (opcode_htab
, &opcode
);
577 nds32_special_opcode (insn
, &opc
);
578 nds32_filter_unknown_insn (insn
, &opc
);
579 nds32_parse_opcode (opc
, pc
, info
, insn
, parse_mode
);
583 print_insn16 (bfd_vma pc
, disassemble_info
*info
,
584 uint32_t insn
, uint32_t parse_mode
)
586 struct nds32_opcode
*opc
;
589 /* Get highest 7 bit in default. */
590 unsigned int mask
= 0xfe00;
592 /* Classify 16-bit instruction to 4 sets by bit 13 and 14. */
593 switch (__GF (insn
, 13, 2))
597 if (__GF (insn
, 11, 2) == 0)
600 /* ifret16 = mov55 $sp, $sp*/
601 if (__GF (insn
, 0, 11) == 0x3ff)
604 else if (__GF (insn
, 9, 4) == 0xb)
609 if (__GF (insn
, 11, 2) == 0x3)
614 /* Exclude beqz38, bnez38, beqs38, and bnes38. */
615 if (__GF (insn
, 12, 1) == 0x1
616 && __GF (insn
, 8, 3) == 0x5)
618 if (__GF (insn
, 11, 1) == 0x0)
625 switch (__GF (insn
, 11, 2))
629 if (__GF (insn
, 9, 2) == 0x0)
632 else if (__GF(insn
, 10, 1) == 0x1)
636 /* lwi37.sp swi37.sp */
640 if (__GF (insn
, 8, 3) == 0x5)
642 else if (__GF (insn
, 8, 3) == 0x4)
644 else if (__GF (insn
, 9 , 2) == 0x3)
650 opcode
= insn
& mask
;
651 opc
= (struct nds32_opcode
*) htab_find (opcode_htab
, &opcode
);
653 nds32_special_opcode (insn
, &opc
);
654 /* Get the final correct opcode and parse it. */
655 nds32_parse_opcode (opc
, pc
, info
, insn
, parse_mode
);
659 htab_hash_hash (const void *p
)
661 return (*(unsigned int *) p
) % 49;
665 htab_hash_eq (const void *p
, const void *q
)
667 uint32_t pinsn
= ((struct nds32_opcode
*) p
)->value
;
668 uint32_t qinsn
= *((uint32_t *) q
);
670 return (pinsn
== qinsn
);
673 /* Get the format of instruction. */
676 nds32_mask_opcode (uint32_t insn
)
678 uint32_t opcode
= N32_OP6 (insn
);
700 case N32_OP6_LBSI_BI
:
701 case N32_OP6_LHSI_BI
:
702 case N32_OP6_LWSI_BI
:
714 return MASK_OP (insn
, 0);
717 if (__GF (insn
, 0, 7) == (N32_ALU2_FFBI
| N32_BIT (6)))
718 return MASK_OP (insn
, 0x7f);
719 else if (__GF (insn
, 0, 7) == (N32_ALU2_MFUSR
| N32_BIT (6))
720 || __GF (insn
, 0, 7) == (N32_ALU2_MTUSR
| N32_BIT (6)))
722 return MASK_OP (insn
, 0xf81ff);
723 else if (__GF (insn
, 0, 10) == (N32_ALU2_ONEOP
| N32_BIT (7)))
726 if (__GF (insn
, 12, 3) == 4)
727 return MASK_OP (insn
, 0x73ff);
728 return MASK_OP (insn
, 0x7fff);
730 return MASK_OP (insn
, 0x3ff);
733 return MASK_OP (insn
, 0x1f);
735 return MASK_OP (insn
, 0xff);
737 return MASK_OP (insn
, 0x7f);
739 return MASK_OP (insn
, 0x23);
742 return MASK_OP (insn
, 0x1 << 19);
744 if (__GF (insn
, 18, 2) == 0x3)
745 return MASK_OP (insn
, 0x7 << 17);
746 return MASK_OP (insn
, 0x3 << 18);
748 return MASK_OP (insn
, 0x1 << 24);
753 return MASK_OP (insn
, 0x1 << 12);
755 return MASK_OP (insn
, 0x1 << 24);
757 return MASK_OP (insn
, 0x1 << 14);
759 if (__GF (insn
, 16, 4) == 0)
760 return MASK_OP (insn
, 0x1ff << 16);
762 return MASK_OP (insn
, 0xf << 16);
764 return MASK_OP (insn
, 0x1 << 19);
766 switch (__GF (insn
, 0, 5))
769 /* SETGIE and SETEND */
770 if (__GF (insn
, 5, 5) == 0x1 || __GF (insn
, 5, 5) == 0x2)
771 return MASK_OP (insn
, 0x1fffff);
772 return MASK_OP (insn
, 0x1f);
774 if (__GF (insn
, 5, 5) == 5 || __GF (insn
, 5, 5) == 7)
776 return MASK_OP (insn
, 0x3ff);
777 return MASK_OP (insn
, 0x1f);
779 return MASK_OP (insn
, 0x1f);
782 if (__GF (insn
, 4, 2) == 0)
785 switch (__GF (insn
, 0, 4))
789 /* FS1/F2OP FD1/F2OP */
790 if (__GF (insn
, 6, 4) == 0xf)
791 return MASK_OP (insn
, 0x7fff);
793 return MASK_OP (insn
, 0x3ff);
797 return MASK_OP (insn
, 0x3ff);
801 if (__GF (insn
, 6, 4) == 0xc)
802 return MASK_OP (insn
, 0x7fff);
804 return MASK_OP (insn
, 0x3ff);
806 return MASK_OP (insn
, 0xff);
809 else if (__GF (insn
, 0, 2) == 0)
810 return MASK_OP (insn
, 0xf);
811 return MASK_OP (insn
, 0xcf);
814 switch (__GF (insn
, 23, 2))
817 if (__GF (insn
, 5, 4) == 0)
818 /* AMxxx AMAyyS AMyyS AMAWzS AMWzS */
819 return MASK_OP (insn
, (0x1f << 20) | 0x1ff);
820 else if (__GF (insn
, 5, 4) == 1)
821 /* ALR ASR ALA ASA AUPI */
822 return MASK_OP (insn
, (0x1f << 20) | (0xf << 5));
823 else if (__GF (insn
, 20, 3) == 0 && __GF (insn
, 6, 3) == 1)
825 return MASK_OP (insn
, (0x1f << 20) | (0x7 << 6));
826 else if (__GF (insn
, 20 ,3) == 2 && __GF (insn
, 6, 3) == 1)
828 return MASK_OP (insn
, (0x1f << 20) | (0xf << 5));
829 else if (__GF (insn
, 20 ,3) == 3 && __GF (insn
, 6, 3) == 1)
830 /* AMTAR AMTAR2 AMFAR AMFAR2 */
831 return MASK_OP (insn
, (0x1f << 20) | (0x1f << 5));
832 else if (__GF (insn
, 7, 2) == 3)
834 return MASK_OP (insn
, (0x1f << 20) | (0x3 << 7));
835 else if (__GF (insn
, 6, 3) == 2)
837 return MASK_OP (insn
, (0x1f << 20) | (0xf << 5));
839 /* AmxxxL.l AmxxxL2.S AMxxxL2.L */
840 return MASK_OP (insn
, (0x1f << 20) | (0x7 << 6));
842 if (__GF (insn
, 20, 3) == 0)
844 return MASK_OP (insn
, (0x1f << 20) | (0x1 << 5));
845 else if (__GF (insn
, 20, 3) == 1)
846 /* AMTARI Ix AMTARI Mx */
847 return MASK_OP (insn
, (0x1f << 20));
848 else if (__GF (insn
, 6, 3) == 2)
849 /* AMAWzSl.S AMWzSl.S */
850 return MASK_OP (insn
, (0x1f << 20) | (0xf << 5));
851 else if (__GF (insn
, 7, 2) == 3)
852 /* AMAWzSSA AMWzSSA */
853 return MASK_OP (insn
, (0x1f << 20) | (0x3 << 7));
855 /* AMAWzSL.L AMAWzSL2.S AMAWzSL2.L
856 AMWzSL.L AMWzSL.L AMWzSL2.S */
857 return MASK_OP (insn
, (0x1f << 20) | (0x7 << 6));
859 if (__GF (insn
, 6, 3) == 2)
860 /* AMAyySl.S AMWyySl.S */
861 return MASK_OP (insn
, (0x1f << 20) | (0xf << 5));
862 else if (__GF (insn
, 7, 2) == 3)
863 /* AMAWyySSA AMWyySSA */
864 return MASK_OP (insn
, (0x1f << 20) | (0x3 << 7));
866 /* AMAWyySL.L AMAWyySL2.S AMAWyySL2.L
867 AMWyySL.L AMWyySL.L AMWyySL2.S */
868 return MASK_OP (insn
, (0x1f << 20) | (0x7 << 6));
870 return MASK_OP (insn
, 0x1f << 20);
876 /* Define cctl subtype. */
877 static char *cctl_subtype
[] =
880 "st0", "st0", "st0", "st2", "st2", "st3", "st3", "st4",
881 "st1", "st1", "st1", "st0", "st0", NULL
, NULL
, "st5",
883 "st0", NULL
, NULL
, "st2", "st2", "st3", "st3", NULL
,
884 "st1", NULL
, NULL
, "st0", "st0", NULL
, NULL
, NULL
887 /* Check the subset of opcode. */
890 nds32_special_opcode (uint32_t insn
, struct nds32_opcode
**opc
)
898 /* Check if special case. */
899 switch ((*opc
)->value
)
905 case FPU_RA_IMMBI (LWC
):
906 case FPU_RA_IMMBI (SWC
):
907 case FPU_RA_IMMBI (LDC
):
908 case FPU_RA_IMMBI (SDC
):
909 /* Check if cp0 => FPU. */
910 if (__GF (insn
, 13, 2) == 0)
912 while (!((*opc
)->attr
& ATTR (FPU
)) && (*opc
)->next
)
921 /* Check if (add/add_slli) (sub/sub_slli) (and/and_slli). */
922 if (N32_SH5(insn
) != 0)
927 if (__GF (insn
, 10, 15) == 0)
931 string
= cctl_subtype
[__GF (insn
, 5, 5)];
935 case JREG (JR
) | JREG_RET
:
936 if (__GF (insn
, 8, 2) != 0)
943 if (__GF (insn
, 0, 9) == 0)
950 while (strstr ((*opc
)->opcode
, string
) == NULL
951 && strstr ((*opc
)->instruction
, string
) == NULL
&& (*opc
)->next
)
956 /* Classify instruction is COP or FPU. */
958 if (op
== N32_OP6_COP
&& __GF (insn
, 4, 2) != 0)
960 while (((*opc
)->attr
& ATTR (FPU
)) != 0 && (*opc
)->next
)
966 print_insn_nds32 (bfd_vma pc
, disassemble_info
*info
)
970 bfd_byte buf_data
[16];
975 int last_symbol_index
= -1;
979 struct nds32_private_data
*private_data
;
981 enum map_type mapping_type
= MAP_CODE
;
983 if (info
->private_data
== NULL
)
985 /* Note: remain lifecycle throughout whole execution. */
986 static struct nds32_private_data
private;
987 private.has_mapping_symbols
= -1; /* unknown yet. */
988 private.last_symbol_index
= -1;
989 private.last_addr
= 0;
990 info
->private_data
= &private;
992 private_data
= info
->private_data
;
994 if (info
->symtab_size
!= 0)
1001 start
= info
->symtab_pos
;
1002 if (start
< private_data
->last_symbol_index
)
1003 start
= private_data
->last_symbol_index
;
1009 if (private_data
->has_mapping_symbols
!= 0
1010 && ((strncmp (".text", info
->section
->name
, 5) == 0)))
1012 for (n
= start
; n
< info
->symtab_size
; n
++)
1014 addr
= bfd_asymbol_value (info
->symtab
[n
]);
1017 if (get_mapping_symbol_type (info
, n
, &mapping_type
))
1019 last_symbol_index
= n
;
1025 private_data
->has_mapping_symbols
= 1;
1026 else if (!found
&& private_data
->has_mapping_symbols
== -1)
1028 /* Make sure there are no any mapping symbol. */
1029 for (n
= 0; n
< info
->symtab_size
; n
++)
1031 if (is_mapping_symbol (info
, n
, &mapping_type
))
1033 private_data
->has_mapping_symbols
= -1;
1037 if (private_data
->has_mapping_symbols
== -1)
1038 private_data
->has_mapping_symbols
= 0;
1041 private_data
->last_symbol_index
= last_symbol_index
;
1042 private_data
->last_mapping_type
= mapping_type
;
1043 is_data
= (private_data
->last_mapping_type
== MAP_DATA0
1044 || private_data
->last_mapping_type
== MAP_DATA1
1045 || private_data
->last_mapping_type
== MAP_DATA2
1046 || private_data
->last_mapping_type
== MAP_DATA3
1047 || private_data
->last_mapping_type
== MAP_DATA4
);
1051 /* Wonder data or instruction. */
1056 /* Fix corner case: there is no next mapping symbol,
1057 let mapping type decides size */
1059 if (last_symbol_index
+ 1 >= info
->symtab_size
)
1061 if (mapping_type
== MAP_DATA0
)
1063 if (mapping_type
== MAP_DATA1
)
1065 if (mapping_type
== MAP_DATA2
)
1067 if (mapping_type
== MAP_DATA3
)
1069 if (mapping_type
== MAP_DATA4
)
1072 for (n
= last_symbol_index
+ 1; n
< info
->symtab_size
; n
++)
1074 addr
= bfd_asymbol_value (info
->symtab
[n
]);
1076 enum map_type fake_mapping_type
;
1077 if (get_mapping_symbol_type (info
, n
, &fake_mapping_type
)
1079 && ((info
->section
== NULL
)
1080 || (info
->section
== info
->symtab
[n
]->section
)))
1081 && (addr
- pc
< size
))
1089 size
= (pc
& 1) ? 1 : 2;
1091 /* Read bytes from BFD. */
1092 info
->read_memory_func (pc
, buf_data
, size
, info
);
1095 /* Start assembling data. */
1096 /* Little endian of data. */
1097 if (info
->endian
== BFD_ENDIAN_LITTLE
)
1099 for (i1
= size
- 1;; i1
--)
1102 given1
= buf_data
[i1
] | (given1
<< 8);
1104 given
= buf_data
[i1
] | (given
<< 8);
1112 /* Big endian of data. */
1113 for (i1
= 0; i1
< size
; i1
++)
1116 given
= buf_data
[i1
] | (given
<< 8);
1118 given1
= buf_data
[i1
] | (given1
<< 8);
1122 info
->bytes_per_line
= 4;
1125 info
->fprintf_func (info
->stream
, ".qword\t0x%016" PRIx64
"%016" PRIx64
,
1128 info
->fprintf_func (info
->stream
, ".dword\t0x%016" PRIx64
, given
);
1130 info
->fprintf_func (info
->stream
, ".word\t0x%08" PRIx64
, given
);
1134 if (mapping_type
== MAP_DATA0
)
1135 info
->fprintf_func (info
->stream
, ".byte\t0x%02" PRIx64
,
1138 info
->fprintf_func (info
->stream
, ".short\t0x%04" PRIx64
, given
);
1143 info
->fprintf_func (info
->stream
, ".byte\t0x%02" PRIx64
, given
);
1150 status
= info
->read_memory_func (pc
, buf
, 4, info
);
1153 /* For the last 16-bit instruction. */
1155 status
= info
->read_memory_func (pc
, buf
, 2, info
);
1158 (*info
->memory_error_func
) (status
, pc
, info
);
1165 insn
= bfd_getb32 (buf
);
1166 /* 16-bit instruction. */
1167 if (insn
& 0x80000000)
1169 print_insn16 (pc
, info
, (insn
>> 16), NDS32_PARSE_INSN16
);
1173 /* 32-bit instructions. */
1175 print_insn32 (pc
, info
, insn
, NDS32_PARSE_INSN32
);
1177 info
->fprintf_func (info
->stream
,
1178 _("insufficient data to decode instruction"));
1182 /* Ignore disassembling unnecessary name. */
1185 nds32_symbol_is_valid (asymbol
*sym
,
1186 struct disassemble_info
*info ATTRIBUTE_UNUSED
)
1193 name
= bfd_asymbol_name (sym
);
1195 /* Mapping symbol is invalid. */
1202 nds32_add_opcode_hash_table (unsigned indx
)
1206 opc
= nds32_opcode_table
[indx
];
1210 while (opc
->opcode
!= NULL
)
1214 slot
= (opcode_t
**) htab_find_slot
1215 (opcode_htab
, &opc
->value
, INSERT
);
1218 /* This is the new one. */
1225 /* Already exists. Append to the list. */
1237 disassemble_init_nds32 (struct disassemble_info
*info
)
1239 static unsigned init_done
= 0;
1242 /* Set up symbol checking function. */
1243 info
->symbol_is_valid
= nds32_symbol_is_valid
;
1245 /* Only need to initialize once:
1246 High level will call this function for every object file.
1247 For example, when disassemble all members of a library. */
1251 /* Setup main core. */
1252 nds32_keyword_table
[NDS32_MAIN_CORE
] = &nds32_keywords
[0];
1253 nds32_opcode_table
[NDS32_MAIN_CORE
] = &nds32_opcodes
[0];
1254 nds32_field_table
[NDS32_MAIN_CORE
] = &nds32_operand_fields
[0];
1256 /* Build opcode table. */
1257 opcode_htab
= htab_create_alloc (1024, htab_hash_hash
, htab_hash_eq
,
1258 NULL
, xcalloc
, free
);
1260 for (k
= 0; k
< NDS32_CORE_COUNT
; k
++)
1263 nds32_add_opcode_hash_table (k
);
1270 is_mapping_symbol (struct disassemble_info
*info
, int n
,
1271 enum map_type
*map_type
)
1273 const char *name
= NULL
;
1275 /* Get symbol name. */
1276 name
= bfd_asymbol_name (info
->symtab
[n
]);
1280 *map_type
= MAP_CODE
;
1283 else if (name
[1] == 'd' && name
[2] == '0')
1285 *map_type
= MAP_DATA0
;
1288 else if (name
[1] == 'd' && name
[2] == '1')
1290 *map_type
= MAP_DATA1
;
1293 else if (name
[1] == 'd' && name
[2] == '2')
1295 *map_type
= MAP_DATA2
;
1298 else if (name
[1] == 'd' && name
[2] == '3')
1300 *map_type
= MAP_DATA3
;
1303 else if (name
[1] == 'd' && name
[2] == '4')
1305 *map_type
= MAP_DATA4
;
1313 get_mapping_symbol_type (struct disassemble_info
*info
, int n
,
1314 enum map_type
*map_type
)
1316 /* If the symbol is in a different section, ignore it. */
1317 if (info
->section
!= NULL
1318 && info
->section
!= info
->symtab
[n
]->section
)
1321 return is_mapping_symbol (info
, n
, map_type
);