csky-dis.c 29 KB

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