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