[PATCH 16/57][Arm][GAS] Add support for MVE instructions: vdup, vddup, vdwdup, vidup...
[binutils-gdb.git] / opcodes / csky-dis.c
blobefd1f75052b341664c2fb3d0d738256d398578d6
1 /* C-SKY disassembler.
2 Copyright (C) 1988-2019 Free Software Foundation, Inc.
3 Contributed by C-SKY Microsystems and Mentor Graphics.
5 This file is part of the GNU opcodes library.
7 This library 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, or (at your option)
10 any later version.
12 It is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 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,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "config.h"
24 #include <stdio.h>
25 #include "bfd_stdint.h"
26 #include "disassemble.h"
27 #include "elf-bfd.h"
28 #include "opcode/csky.h"
29 #include "libiberty.h"
30 #include "csky-opc.h"
31 #include "floatformat.h"
33 #define CSKY_INST_TYPE unsigned long
34 #define HAS_SUB_OPERAND (unsigned int)0xffffffff
36 enum sym_type
38 CUR_TEXT,
39 CUR_DATA
42 struct csky_dis_info
44 /* Mem to disassemble. */
45 bfd_vma mem;
46 /* Disassemble info. */
47 disassemble_info *info;
48 /* Opcode information. */
49 struct csky_opcode_info const *opinfo;
50 /* The value of operand to show. */
51 int value;
52 /* Whether to look up/print a symbol name. */
53 int need_output_symbol;
54 } dis_info;
57 enum sym_type last_type;
58 int last_map_sym = 1;
59 bfd_vma last_map_addr = 0;
61 /* Only for objdump tool. */
62 #define INIT_MACH_FLAG 0xffffffff
63 #define BINARY_MACH_FLAG 0x0
65 static unsigned int mach_flag = INIT_MACH_FLAG;
67 static void
68 print_insn_data (bfd_vma pc ATTRIBUTE_UNUSED,
69 struct disassemble_info *info,
70 long given)
72 switch (info->bytes_per_chunk)
74 case 1:
75 info->fprintf_func (info->stream, ".byte\t0x%02lx", given);
76 break;
77 case 2:
78 info->fprintf_func (info->stream, ".short\t0x%04lx", given);
79 break;
80 case 4:
81 info->fprintf_func (info->stream, ".long\t0x%08lx", given);
82 break;
83 default:
84 abort ();
88 static int
89 get_sym_code_type (struct disassemble_info *info,
90 int n,
91 enum sym_type *sym_type)
93 const char *name;
94 name = bfd_asymbol_name (info->symtab[n]);
95 if (name[0] == '$' && (name[1] == 't' || name[1] == 'd')
96 && (name[2] == 0 || name[2] == '.'))
98 *sym_type = ((name[1] == 't') ? CUR_TEXT : CUR_DATA);
99 return TRUE;
101 return FALSE;
104 static int
105 csky_get_operand_mask (struct operand const *oprnd)
107 int mask = 0;
108 if (oprnd->mask == HAS_SUB_OPERAND)
110 struct soperand *sop = (struct soperand *)oprnd;
111 mask |= csky_get_operand_mask (&sop->subs[0]);
112 mask |= csky_get_operand_mask (&sop->subs[1]);
113 return mask;
115 return oprnd->mask;
118 static int
119 csky_get_mask (struct csky_opcode_info const *pinfo)
121 int i = 0;
122 int mask = 0;
123 /* List type. */
124 if (pinfo->operand_num == -1)
125 mask |= csky_get_operand_mask (&pinfo->oprnd.oprnds[i]);
126 else
127 for (; i < pinfo->operand_num; i++)
128 mask |= csky_get_operand_mask (&pinfo->oprnd.oprnds[i]);
130 mask = ~mask;
131 return mask;
134 static unsigned int
135 csky_chars_to_number (unsigned char * buf, int n)
137 if (n == 0)
138 abort ();
139 int i;
140 int val = 0;
142 if (dis_info.info->endian == BFD_ENDIAN_BIG)
143 while (n--)
144 val |= buf[n] << (n*8);
145 else
146 for (i = 0; i < n; i++)
147 val |= buf[i] << (i*8);
148 return val;
151 static struct csky_opcode const *g_opcodeP;
153 static struct csky_opcode const *
154 csky_find_inst_info (struct csky_opcode_info const **pinfo,
155 CSKY_INST_TYPE inst, int length)
157 int i;
158 unsigned int mask;
159 struct csky_opcode const *p;
161 p = g_opcodeP;
162 while (p->mnemonic)
164 /* Get the opcode mask. */
165 for (i = 0; i < OP_TABLE_NUM; i++)
166 if (length == 2)
168 mask = csky_get_mask (&p->op16[i]);
169 if (mask != 0 && (inst & mask) == p->op16[i].opcode)
171 *pinfo = &p->op16[i];
172 g_opcodeP = p;
173 return p;
176 else if (length == 4)
178 mask = csky_get_mask (&p->op32[i]);
179 if (mask != 0
180 && ((unsigned long)(inst & mask)
181 == (unsigned long)p->op32[i].opcode))
183 *pinfo = &p->op32[i];
184 g_opcodeP = p;
185 return p;
188 p++;
191 return NULL;
194 static bfd_boolean
195 is_extern_symbol (struct disassemble_info *info, int addr)
197 unsigned int rel_count = 0;
199 if (info->section == NULL)
200 return 0;
201 if ((info->section->flags & SEC_RELOC) != 0) /* Fit .o file. */
203 struct reloc_cache_entry *pt = info->section->relocation;
204 for (; rel_count < info->section->reloc_count; rel_count++, pt++)
205 if ((long unsigned int)addr == pt->address)
206 return TRUE;
207 return FALSE;
209 return FALSE;
213 /* Suppress printing of mapping symbols emitted by the assembler to mark
214 the beginning of code and data sequences. */
216 bfd_boolean
217 csky_symbol_is_valid (asymbol *sym,
218 struct disassemble_info *info ATTRIBUTE_UNUSED)
220 const char *name;
222 if (sym == NULL)
223 return FALSE;
224 name = bfd_asymbol_name (sym);
225 return name && *name != '$';
228 disassembler_ftype
229 csky_get_disassembler (bfd *abfd)
231 if (abfd != NULL)
232 mach_flag = elf_elfheader (abfd)->e_flags;
233 return print_insn_csky;
236 static int
237 csky_output_operand (char *str, struct operand const *oprnd,
238 CSKY_INST_TYPE inst, int reloc ATTRIBUTE_UNUSED)
240 int ret = 0;;
241 int bit = 0;
242 int result = 0;
243 bfd_vma value;
244 int mask = oprnd->mask;
245 int max = 0;
246 char buf[128];
248 /* Get operand value with mask. */
249 value = inst & mask;
250 for (; mask; mask >>= 1, value >>=1)
251 if (mask & 0x1)
253 result |= ((value & 0x1) << bit);
254 max |= (1 << bit);
255 bit++;
257 value = result;
259 /* Here is general instructions that have no reloc. */
260 switch (oprnd->type)
262 case OPRND_TYPE_CTRLREG:
263 if (IS_CSKY_V1 (mach_flag))
265 /* In V1 only cr0-cr12 have alias names. */
266 if (value <= 12)
267 strcat (str, csky_ctrl_regs[value].name);
268 /* Others using crn(n > 12). */
269 else if (value <= 30)
271 sprintf (buf, "cr%d", (int)value);
272 strcat (str, buf);
274 else
275 return -1;
277 else
279 int sel;
280 int crx;
281 sel = value >> 5;
282 crx = value & 0x1f;
283 sprintf (buf, "cr<%d, %d>", crx, sel);
284 strcat (str, buf);
286 break;
287 case OPRND_TYPE_DUMMY_REG:
288 mask = dis_info.opinfo->oprnd.oprnds[0].mask;
289 value = inst & mask;
290 for (; mask; mask >>= 1, value >>=1)
291 if (mask & 0x1)
293 result |= ((value & 0x1) << bit);
294 bit++;
296 value = result;
297 strcat (str, csky_general_reg[value]);
298 break;
299 case OPRND_TYPE_GREG0_7:
300 case OPRND_TYPE_GREG0_15:
301 case OPRND_TYPE_GREG16_31:
302 case OPRND_TYPE_REGnsplr:
303 case OPRND_TYPE_AREG:
304 if (IS_CSKY_V2 (mach_flag) && value == 14)
305 strcat (str, "sp");
306 else
307 strcat (str, csky_general_reg[value]);
308 dis_info.value = value;
309 break;
310 case OPRND_TYPE_CPREG:
311 strcat (str, csky_cp_reg[value]);
312 break;
313 case OPRND_TYPE_FREG:
314 sprintf (buf, "fr%d", (int)value);
315 strcat (str, buf);
316 break;
317 case OPRND_TYPE_VREG:
318 sprintf (buf, "vr%d", (int)value);
319 strcat (str, buf);
320 break;
321 case OPRND_TYPE_CPCREG:
322 strcat (str, csky_cp_creg[value]);
323 break;
324 case OPRND_TYPE_CPIDX:
325 strcat (str, csky_cp_idx[value]);
326 break;
327 case OPRND_TYPE_IMM2b_JMPIX:
328 value = (value + 2) << 3;
329 sprintf (buf, "%d", (int)value);
330 strcat (str, buf);
331 break;
332 case OPRND_TYPE_IMM_LDST:
333 case OPRND_TYPE_IMM_FLDST:
334 value <<= oprnd->shift;
335 sprintf (buf, "0x%x", (unsigned int)value);
336 strcat (str, buf);
337 break;
338 case OPRND_TYPE_IMM7b_LS2:
339 case OPRND_TYPE_IMM8b_LS2:
340 sprintf (buf, "%d", (int)(value << 2));
341 strcat (str, buf);
342 ret = 0;
343 break;
344 case OPRND_TYPE_IMM5b_BMASKI:
345 if ((value != 0) && (value > 31 || value < 8))
347 ret = -1;
348 break;
350 sprintf (buf, "%d", (int)value);
351 strcat (str, buf);
352 ret = 0;
353 break;
354 case OPRND_TYPE_IMM5b_1_31:
355 if (value > 31 || value < 1)
357 ret = -1;
358 break;
360 sprintf (buf, "%d", (int)value);
361 strcat (str, buf);
362 ret = 0;
363 break;
364 case OPRND_TYPE_IMM5b_7_31:
365 if (value > 31 || value < 7)
367 ret = -1;
368 break;
370 sprintf (buf, "%d", (int)value);
371 strcat (str, buf);
372 ret = 0;
373 break;
374 case OPRND_TYPE_MSB2SIZE:
375 case OPRND_TYPE_LSB2SIZE:
377 static int size;
378 if (oprnd->type == OPRND_TYPE_MSB2SIZE)
379 size = value;
380 else
382 str[strlen (str) - 2] = '\0';
383 sprintf (buf, "%d, %d", (int)(size + value), (int)value);
384 strcat (str, buf);
386 break;
388 case OPRND_TYPE_IMM1b:
389 case OPRND_TYPE_IMM2b:
390 case OPRND_TYPE_IMM4b:
391 case OPRND_TYPE_IMM5b:
392 case OPRND_TYPE_IMM7b:
393 case OPRND_TYPE_IMM8b:
394 case OPRND_TYPE_IMM12b:
395 case OPRND_TYPE_IMM15b:
396 case OPRND_TYPE_IMM16b:
397 case OPRND_TYPE_IMM16b_MOVIH:
398 case OPRND_TYPE_IMM16b_ORI:
399 sprintf (buf, "%d", (int)value);
400 strcat (str, buf);
401 ret = 0;
402 break;
403 case OPRND_TYPE_OFF8b:
404 case OPRND_TYPE_OFF16b:
406 unsigned char ibytes[4];
407 int shift = oprnd->shift;
408 int status;
409 unsigned int mem_val;
411 dis_info.info->stop_vma = 0;
413 value = ((dis_info.mem + (value << shift)
414 + ((IS_CSKY_V1 (mach_flag)) ? 2 : 0))
415 & 0xfffffffc);
416 status = dis_info.info->read_memory_func (value, ibytes, 4,
417 dis_info.info);
418 if (status != 0)
420 dis_info.info->memory_error_func (status, dis_info.mem,
421 dis_info.info);
422 return -1;
424 mem_val = csky_chars_to_number (ibytes, 4);
425 /* Remove [] around literal value to match ABI syntax. */
426 sprintf (buf, "0x%X", mem_val);
427 strcat (str, buf);
428 /* For jmpi/jsri, we'll try to get a symbol for the target. */
429 if (dis_info.info->print_address_func && mem_val != 0)
431 dis_info.value = mem_val;
432 dis_info.need_output_symbol = 1;
434 else
436 sprintf (buf, "\t// from address pool at 0x%x",
437 (unsigned int)value);
438 strcat (str, buf);
440 break;
442 case OPRND_TYPE_BLOOP_OFF4b:
443 case OPRND_TYPE_BLOOP_OFF12b:
444 case OPRND_TYPE_OFF11b:
445 case OPRND_TYPE_OFF16b_LSL1:
446 case OPRND_TYPE_IMM_OFF18b:
447 case OPRND_TYPE_OFF26b:
449 int shift = oprnd->shift;
450 if (value & ((max >> 1) + 1))
451 value |= ~max;
452 if (is_extern_symbol (dis_info.info, dis_info.mem))
453 value = 0;
454 else if (IS_CSKY_V1 (mach_flag))
455 value = dis_info.mem + 2 + (value << shift);
456 else
457 value = dis_info.mem + (value << shift);
458 dis_info.need_output_symbol = 1;
459 dis_info.value= value;
460 sprintf (buf, "0x%x", (unsigned int)value);
461 strcat (str, buf);
462 break;
464 case OPRND_TYPE_CONSTANT:
465 case OPRND_TYPE_FCONSTANT:
467 int shift = oprnd->shift;
468 char ibytes[8];
469 int status;
470 bfd_vma addr;
471 int nbytes;
473 dis_info.info->stop_vma = 0;
474 value <<= shift;
476 if (IS_CSKY_V1 (mach_flag))
477 addr = (dis_info.mem + 2 + value) & 0xfffffffc;
478 else
479 addr = (dis_info.mem + value) & 0xfffffffc;
481 if (oprnd->type == OPRND_TYPE_FCONSTANT
482 && dis_info.opinfo->opcode != CSKYV2_INST_FLRW)
483 nbytes = 8;
484 else
485 nbytes = 4;
487 status = dis_info.info->read_memory_func (addr, (bfd_byte *)ibytes,
488 nbytes, dis_info.info);
489 if (status != 0)
490 /* Address out of bounds. -> lrw rx, [pc, 0ffset]. */
491 sprintf (buf, "[pc, %d]\t// from address pool at %x", (int)value,
492 (unsigned int)addr);
493 else
495 dis_info.value = addr;
496 value = csky_chars_to_number ((unsigned char *)ibytes, 4);
499 if (oprnd->type == OPRND_TYPE_FCONSTANT)
501 double f;
503 if (dis_info.opinfo->opcode == CSKYV2_INST_FLRW)
504 /* flrws. */
505 floatformat_to_double ((dis_info.info->endian == BFD_ENDIAN_BIG
506 ? &floatformat_ieee_single_big
507 : &floatformat_ieee_single_little),
508 ibytes, &f);
509 else
510 floatformat_to_double ((dis_info.info->endian == BFD_ENDIAN_BIG
511 ? &floatformat_ieee_double_big
512 : &floatformat_ieee_double_little),
513 ibytes, &f);
514 sprintf (buf, "%f", f);
516 else
518 dis_info.need_output_symbol = 1;
519 sprintf (buf, "0x%x", (unsigned int)value);
522 strcat (str, buf);
523 break;
525 case OPRND_TYPE_ELRW_CONSTANT:
527 int shift = oprnd->shift;
528 char ibytes[4];
529 int status;
530 bfd_vma addr;
531 dis_info.info->stop_vma = 0;
533 value = 0x80 + ((~value) & 0x7f);
535 value = value << shift;
536 addr = (dis_info.mem + value) & 0xfffffffc;
538 status = dis_info.info->read_memory_func (addr, (bfd_byte *)ibytes,
539 4, dis_info.info);
540 if (status != 0)
541 /* Address out of bounds. -> lrw rx, [pc, 0ffset]. */
542 sprintf (buf, "[pc, %d]\t// from address pool at %x", (int) value,
543 (unsigned int)addr);
544 else
546 dis_info.value = addr;
547 value = csky_chars_to_number ((unsigned char *)ibytes, 4);
548 dis_info.need_output_symbol = 1;
549 sprintf (buf, "0x%x", (unsigned int)value);
552 strcat (str, buf);
553 break;
555 case OPRND_TYPE_SFLOAT:
556 case OPRND_TYPE_DFLOAT:
558 /* This is for fmovis/fmovid, which have an internal 13-bit
559 encoding that they convert to single/double precision
560 (respectively). We'll convert the 13-bit encoding to an IEEE
561 double and then to host double format to print it.
562 Sign bit: bit 20.
563 4-bit exponent: bits 19:16, biased by 11.
564 8-bit mantissa: split between 24:21 and 7:4. */
565 uint64_t imm4;
566 uint64_t imm8;
567 uint64_t dbnum;
568 unsigned char valbytes[8];
569 double fvalue;
571 imm4 = ((inst >> 16) & 0xf);
572 imm4 = (uint64_t)(1023 - (imm4 - 11)) << 52;
574 imm8 = (uint64_t)((inst >> 4) & 0xf) << 44;
575 imm8 |= (uint64_t)((inst >> 21) & 0xf) << 48;
577 dbnum = (uint64_t)((inst >> 20) & 1) << 63;
578 dbnum |= imm4 | imm8;
580 /* Do this a byte at a time so we don't have to
581 worry about the host's endianness. */
582 valbytes[0] = dbnum & 0xff;
583 valbytes[1] = (dbnum >> 8) & 0xff;
584 valbytes[2] = (dbnum >> 16) & 0xff;
585 valbytes[3] = (dbnum >> 24) & 0xff;
586 valbytes[4] = (dbnum >> 32) & 0xff;
587 valbytes[5] = (dbnum >> 40) & 0xff;
588 valbytes[6] = (dbnum >> 48) & 0xff;
589 valbytes[7] = (dbnum >> 56) & 0xff;
591 floatformat_to_double (&floatformat_ieee_double_little, valbytes,
592 &fvalue);
594 sprintf (buf, "%f", fvalue);
595 strcat (str, buf);
596 break;
598 case OPRND_TYPE_LABEL_WITH_BRACKET:
599 sprintf (buf, "[0x%x]", (unsigned int)value);
600 strcat (str, buf);
601 strcat (str, "\t// the offset is based on .data");
602 break;
603 case OPRND_TYPE_OIMM3b:
604 case OPRND_TYPE_OIMM4b:
605 case OPRND_TYPE_OIMM5b:
606 case OPRND_TYPE_OIMM5b_IDLY:
607 case OPRND_TYPE_OIMM8b:
608 case OPRND_TYPE_OIMM12b:
609 case OPRND_TYPE_OIMM16b:
610 case OPRND_TYPE_OIMM18b:
611 value += 1;
612 sprintf (buf, "%d", (int)value);
613 strcat (str, buf);
614 break;
615 case OPRND_TYPE_OIMM5b_BMASKI:
616 if (value > 32 || value < 16)
618 ret = -1;
619 break;
621 sprintf (buf, "%d", (int)(value + 1));
622 strcat (str, buf);
623 ret = 0;
624 break;
625 case OPRND_TYPE_FREGLIST_DASH:
626 if (IS_CSKY_V2 (mach_flag))
628 int vrx = value & 0xf;
629 int vry = vrx + (value >> 4);
630 sprintf (buf, "fr%d-fr%d", vrx, vry);
631 strcat (str, buf);
633 break;
634 case OPRND_TYPE_REGLIST_DASH:
635 if (IS_CSKY_V1 (mach_flag))
637 strcat (str, csky_general_reg[value]);
638 strcat (str, "-r15");
640 else
642 strcat (str, csky_general_reg[value >> 5]);
643 strcat (str, "-");
644 strcat (str, csky_general_reg[(value & 0x1f) + (value >> 5)]);
646 break;
647 case OPRND_TYPE_PSR_BITS_LIST:
649 struct psrbit const *bits;
650 int first_oprnd = TRUE;
651 int i = 0;
652 if (IS_CSKY_V1 (mach_flag))
654 if (value == 0)
656 strcat (str, "af");
657 break;
659 bits = cskyv1_psr_bits;
661 else
662 bits = cskyv2_psr_bits;
663 while (value != 0 && bits[i].name != NULL)
665 if (value & bits[i].value)
667 if (!first_oprnd)
668 strcat (str, ", ");
669 strcat (str, bits[i].name);
670 value &= ~bits[i].value;
671 first_oprnd = FALSE;
673 i++;
675 break;
677 case OPRND_TYPE_REGbsp:
678 if (IS_CSKY_V1 (mach_flag))
679 strcat (str, "(sp)");
680 else
681 strcat (str, "(sp)");
682 break;
683 case OPRND_TYPE_REGsp:
684 if (IS_CSKY_V1 (mach_flag))
685 strcat (str, "sp");
686 else
687 strcat (str, "sp");
688 break;
689 case OPRND_TYPE_REGnr4_r7:
690 case OPRND_TYPE_AREG_WITH_BRACKET:
691 if (IS_CSKY_V1 (mach_flag) && (value < 4 || value > 7))
693 strcat (str, "(");
694 strcat (str, csky_general_reg[value]);
695 strcat (str, ")");
697 else
699 strcat (str, "(");
700 strcat (str, csky_general_reg[value]);
701 strcat (str, ")");
703 break;
704 case OPRND_TYPE_AREG_WITH_LSHIFT:
705 strcat (str, csky_general_reg[value >> 5]);
706 strcat (str, " << ");
707 if ((value & 0x1f) == 0x1)
708 strcat (str, "0");
709 else if ((value & 0x1f) == 0x2)
710 strcat (str, "1");
711 else if ((value & 0x1f) == 0x4)
712 strcat (str, "2");
713 else if ((value & 0x1f) == 0x8)
714 strcat (str, "3");
715 break;
716 case OPRND_TYPE_AREG_WITH_LSHIFT_FPU:
717 strcat (str, csky_general_reg[value >> 2]);
718 strcat (str, " << ");
719 if ((value & 0x3) == 0x0)
720 strcat (str, "0");
721 else if ((value & 0x3) == 0x1)
722 strcat (str, "1");
723 else if ((value & 0x3) == 0x2)
724 strcat (str, "2");
725 else if ((value & 0x3) == 0x3)
726 strcat (str, "3");
727 break;
728 case OPRND_TYPE_FREG_WITH_INDEX:
730 unsigned freg_val = value & 0xf;
731 unsigned index_val = (value >> 4) & 0xf;
732 sprintf (buf, "vr%d[%d]", freg_val, index_val);
733 strcat(str, buf);
734 break;
736 case OPRND_TYPE_REGr4_r7:
737 if (IS_CSKY_V1 (mach_flag))
738 strcat (str, "r4-r7");
739 break;
740 case OPRND_TYPE_CONST1:
741 strcat (str, "1");
742 break;
743 case OPRND_TYPE_REG_r1a:
744 case OPRND_TYPE_REG_r1b:
745 strcat (str, "r1");
746 break;
747 case OPRND_TYPE_REG_r28:
748 strcat (str, "r28");
749 break;
750 case OPRND_TYPE_REGLIST_DASH_COMMA:
751 /* 16-bit reglist. */
752 if (value & 0xf)
754 strcat (str, "r4");
755 if ((value & 0xf) > 1)
757 strcat (str, "-");
758 strcat (str, csky_general_reg[(value & 0xf) + 3]);
760 if (value & ~0xf)
761 strcat (str, ", ");
763 if (value & 0x10)
765 /* r15. */
766 strcat (str, "r15");
767 if (value & ~0x1f)
768 strcat (str, ", ");
770 if (dis_info.opinfo->oprnd.oprnds[0].mask != OPRND_MASK_0_4)
772 /* 32bits reglist. */
773 value >>= 5;
774 if (value & 0x3)
776 strcat (str, "r16");
777 if ((value & 0x7) > 1)
779 strcat (str, "-");
780 strcat (str, csky_general_reg[(value & 0xf) + 15]);
782 if (value & ~0x7)
783 strcat (str, ", ");
785 if (value & 0x8)
786 /* r15. */
787 strcat (str, "r28");
789 break;
790 case OPRND_TYPE_UNCOND10b:
791 case OPRND_TYPE_UNCOND16b:
792 case OPRND_TYPE_COND10b:
793 case OPRND_TYPE_COND16b:
795 int shift = oprnd->shift;
797 if (value & ((max >> 1) + 1))
798 value |= ~max;
799 if (is_extern_symbol (dis_info.info, dis_info.mem))
800 value = 0;
801 else
802 value = dis_info.mem + (value << shift);
803 sprintf (buf, "0x%x", (unsigned int)value);
804 strcat (str, buf);
805 dis_info.need_output_symbol = 1;
806 dis_info.value = value;
808 break;
810 default:
811 ret = -1;
812 break;
814 return ret;
817 static int
818 csky_print_operand (char *str, struct operand const *oprnd,
819 CSKY_INST_TYPE inst, int reloc)
821 int ret = -1;
822 char *lc = "";
823 char *rc = "";
824 if (oprnd->mask == HAS_SUB_OPERAND)
826 struct soperand *sop = (struct soperand *)oprnd;
827 if (oprnd->type == OPRND_TYPE_BRACKET)
829 lc = "(";
830 rc = ")";
832 else if (oprnd->type == OPRND_TYPE_ABRACKET)
834 lc = "<";
835 rc = ">";
837 strcat (str, lc);
838 ret = csky_print_operand (str, &sop->subs[0], inst, reloc);
839 if (ret)
840 return ret;
841 strcat (str, ", ");
842 ret = csky_print_operand (str, &sop->subs[1], inst, reloc);
843 strcat (str, rc);
844 return ret;
846 return csky_output_operand (str, oprnd, inst, reloc);
849 static int
850 csky_print_operands (char *str, struct csky_opcode_info const *pinfo,
851 struct disassemble_info *info, CSKY_INST_TYPE inst,
852 int reloc)
854 int i = 0;
855 int ret = 0;
856 if (pinfo->operand_num)
857 strcat (str, " \t");
858 if (pinfo->operand_num == -1)
860 ret = csky_print_operand (str, &pinfo->oprnd.oprnds[i], inst, reloc);
861 if (ret)
862 return ret;
864 else
865 for (; i < pinfo->operand_num; i++)
867 if (i != 0)
868 strcat (str, ", ");
869 ret = csky_print_operand (str, &pinfo->oprnd.oprnds[i], inst, reloc);
870 if (ret)
871 return ret;
873 info->fprintf_func (info->stream, "%s", str);
874 if (dis_info.need_output_symbol)
876 info->fprintf_func (info->stream, "\t// ");
877 info->print_address_func (dis_info.value, dis_info.info);
879 return 0;
882 static void
883 number_to_chars_littleendian (char *buf, CSKY_INST_TYPE val, int n)
885 if (n <= 0)
886 abort ();
887 while (n--)
889 *buf++ = val & 0xff;
890 val >>= 8;
894 #define CSKY_READ_DATA() \
896 status = info->read_memory_func (memaddr, buf, 2, info); \
897 if (status) \
899 info->memory_error_func (status, memaddr, info); \
900 return -1; \
902 if (info->endian == BFD_ENDIAN_BIG) \
903 inst |= (buf[0] << 8) | buf[1]; \
904 else if (info->endian == BFD_ENDIAN_LITTLE) \
905 inst |= (buf[1] << 8) | buf[0]; \
906 else \
907 abort(); \
908 info->bytes_per_chunk += 2; \
909 memaddr += 2; \
913 print_insn_csky (bfd_vma memaddr, struct disassemble_info *info)
915 unsigned char buf[4];
916 CSKY_INST_TYPE inst = 0;
917 int status;
918 char str[256];
919 long given;
920 int is_data = FALSE;
921 void (*printer) (bfd_vma, struct disassemble_info *, long);
922 unsigned int size = 4;
924 memset (str, 0, sizeof (str));
925 info->bytes_per_chunk = 0;
926 info->bytes_per_chunk = 0;
927 dis_info.mem = memaddr;
928 dis_info.info = info;
929 dis_info.need_output_symbol = 0;
930 if (mach_flag != INIT_MACH_FLAG && mach_flag != BINARY_MACH_FLAG)
931 info->mach = mach_flag;
932 else if (mach_flag == INIT_MACH_FLAG)
933 mach_flag = info->mach;
935 if (mach_flag == BINARY_MACH_FLAG && info->endian == BFD_ENDIAN_UNKNOWN)
936 info->endian = BFD_ENDIAN_LITTLE;
938 /* First check the full symtab for a mapping symbol, even if there
939 are no usable non-mapping symbols for this address. */
940 if (info->symtab_size != 0
941 && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour)
943 bfd_vma addr;
944 int n;
945 int last_sym = -1;
946 enum sym_type type = CUR_TEXT;
948 if (memaddr <= last_map_addr)
949 last_map_sym = -1;
950 /* Start scanning at the start of the function, or wherever
951 we finished last time. */
952 n = 0;
953 if (n < last_map_sym)
954 n = last_map_sym;
956 /* Scan up to the location being disassembled. */
957 for (; n < info->symtab_size; n++)
959 addr = bfd_asymbol_value (info->symtab[n]);
960 if (addr > memaddr)
961 break;
962 if ((info->section == NULL
963 || info->section == info->symtab[n]->section)
964 && get_sym_code_type (info, n, &type))
965 last_sym = n;
967 last_map_sym = last_sym;
968 last_type = type;
969 is_data = (last_type == CUR_DATA);
970 if (is_data)
972 size = 4 - ( memaddr & 3);
973 for (n = last_sym + 1; n < info->symtab_size; n++)
975 addr = bfd_asymbol_value (info->symtab[n]);
976 if (addr > memaddr)
978 if (addr - memaddr < size)
979 size = addr - memaddr;
980 break;
983 /* If the next symbol is after three bytes, we need to
984 print only part of the data, so that we can use either
985 .byte or .short. */
986 if (size == 3)
987 size = (memaddr & 1) ? 1 : 2;
990 info->bytes_per_line = 4;
992 if (is_data)
994 int i;
996 /* Size was already set above. */
997 info->bytes_per_chunk = size;
998 printer = print_insn_data;
1000 status = info->read_memory_func (memaddr, (bfd_byte *) buf, size, info);
1001 given = 0;
1002 if (info->endian == BFD_ENDIAN_LITTLE)
1003 for (i = size - 1; i >= 0; i--)
1004 given = buf[i] | (given << 8);
1005 else
1006 for (i = 0; i < (int) size; i++)
1007 given = buf[i] | (given << 8);
1009 printer (memaddr, info, given);
1010 return info->bytes_per_chunk;
1013 /* Handle instructions. */
1014 CSKY_READ_DATA();
1015 if ((inst & 0xc000) == 0xc000 && IS_CSKY_V2 (mach_flag))
1017 /* It's a 32-bit instruction. */
1018 inst <<= 16;
1019 CSKY_READ_DATA();
1020 if (info->buffer && (info->endian == BFD_ENDIAN_LITTLE))
1022 char* src = (char *)(info->buffer
1023 + ((memaddr - 4 - info->buffer_vma)
1024 * info->octets_per_byte));
1025 if (info->endian == BFD_ENDIAN_LITTLE)
1026 number_to_chars_littleendian (src, inst, 4);
1030 if (IS_CSKY_V1 (mach_flag))
1031 g_opcodeP = csky_v1_opcodes;
1032 else
1033 g_opcodeP = csky_v2_opcodes;
1037 struct csky_opcode const *op;
1038 struct csky_opcode_info const *pinfo = NULL;
1039 int reloc;
1041 memset (str, 0, sizeof (str));
1042 op = csky_find_inst_info (&pinfo, inst, info->bytes_per_chunk);
1043 if (!op)
1045 if (IS_CSKY_V1 (mach_flag))
1046 info->fprintf_func (info->stream, ".short: 0x%04x",
1047 (unsigned short)inst);
1048 else
1049 info->fprintf_func (info->stream, ".long: 0x%08x",
1050 (unsigned int)inst);
1051 return info->bytes_per_chunk;
1054 if (info->bytes_per_chunk == 2)
1055 reloc = op->reloc16;
1056 else
1057 reloc = op->reloc32;
1058 dis_info.opinfo = pinfo;
1059 strcat (str, op->mnemonic);
1061 if (csky_print_operands (str, pinfo, info, inst, reloc))
1062 g_opcodeP++;
1063 else
1064 break;
1065 } while (1);
1067 return info->bytes_per_chunk;