tic54x-dis.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* Disassembly routines for TMS320C54X architecture
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Contributed by Timothy Wall (twall@cygnus.com)
  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 file; see the file COPYING. If not, write to the
  15. Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include <errno.h>
  19. #include <math.h>
  20. #include <stdlib.h>
  21. #include "disassemble.h"
  22. #include "opcode/tic54x.h"
  23. #include "coff/tic54x.h"
  24. static int has_lkaddr (unsigned short, const insn_template *);
  25. static int get_insn_size (unsigned short, const insn_template *);
  26. static int print_instruction (disassemble_info *, bfd_vma,
  27. unsigned short, const char *,
  28. const enum optype [], int, int);
  29. static int print_parallel_instruction (disassemble_info *, bfd_vma,
  30. unsigned short,
  31. const insn_template *, int);
  32. static int sprint_dual_address (disassemble_info *,char [],
  33. unsigned short);
  34. static int sprint_indirect_address (disassemble_info *,char [],
  35. unsigned short);
  36. static int sprint_direct_address (disassemble_info *,char [],
  37. unsigned short);
  38. static int sprint_mmr (disassemble_info *,char [],int);
  39. static int sprint_condition (disassemble_info *,char *,unsigned short);
  40. static int sprint_cc2 (disassemble_info *,char *,unsigned short);
  41. int
  42. print_insn_tic54x (bfd_vma memaddr, disassemble_info *info)
  43. {
  44. bfd_byte opbuf[2];
  45. unsigned short opcode;
  46. int status, size;
  47. const insn_template* tm;
  48. status = (*info->read_memory_func) (memaddr, opbuf, 2, info);
  49. if (status != 0)
  50. {
  51. (*info->memory_error_func) (status, memaddr, info);
  52. return -1;
  53. }
  54. opcode = bfd_getl16 (opbuf);
  55. tm = tic54x_get_insn (info, memaddr, opcode, &size);
  56. info->bytes_per_line = 2;
  57. info->bytes_per_chunk = 2;
  58. info->octets_per_byte = 2;
  59. info->display_endian = BFD_ENDIAN_LITTLE;
  60. if (tm->flags & FL_PAR)
  61. {
  62. if (!print_parallel_instruction (info, memaddr, opcode, tm, size))
  63. return -1;
  64. }
  65. else
  66. {
  67. if (!print_instruction (info, memaddr, opcode,
  68. (char *) tm->name,
  69. tm->operand_types,
  70. size, (tm->flags & FL_EXT)))
  71. return -1;
  72. }
  73. return size * 2;
  74. }
  75. static int
  76. has_lkaddr (unsigned short memdata, const insn_template *tm)
  77. {
  78. return (IS_LKADDR (memdata)
  79. && (OPTYPE (tm->operand_types[0]) == OP_Smem
  80. || OPTYPE (tm->operand_types[1]) == OP_Smem
  81. || OPTYPE (tm->operand_types[2]) == OP_Smem
  82. || OPTYPE (tm->operand_types[1]) == OP_Sind
  83. || OPTYPE (tm->operand_types[0]) == OP_Lmem
  84. || OPTYPE (tm->operand_types[1]) == OP_Lmem));
  85. }
  86. /* always returns 1 (whether an insn template was found) since we provide an
  87. "unknown instruction" template */
  88. const insn_template*
  89. tic54x_get_insn (disassemble_info *info, bfd_vma addr,
  90. unsigned short memdata, int *size)
  91. {
  92. const insn_template *tm = NULL;
  93. for (tm = tic54x_optab; tm->name; tm++)
  94. {
  95. if (tm->opcode == (memdata & tm->mask))
  96. {
  97. /* a few opcodes span two words */
  98. if (tm->flags & FL_EXT)
  99. {
  100. /* if lk addressing is used, the second half of the opcode gets
  101. pushed one word later */
  102. bfd_byte opbuf[2];
  103. bfd_vma addr2 = addr + 1 + has_lkaddr (memdata, tm);
  104. int status = (*info->read_memory_func) (addr2, opbuf, 2, info);
  105. /* FIXME handle errors. */
  106. if (status == 0)
  107. {
  108. unsigned short data2 = bfd_getl16 (opbuf);
  109. if (tm->opcode2 == (data2 & tm->mask2))
  110. {
  111. if (size) *size = get_insn_size (memdata, tm);
  112. return tm;
  113. }
  114. }
  115. }
  116. else
  117. {
  118. if (size) *size = get_insn_size (memdata, tm);
  119. return tm;
  120. }
  121. }
  122. }
  123. for (tm = (insn_template *) tic54x_paroptab; tm->name; tm++)
  124. {
  125. if (tm->opcode == (memdata & tm->mask))
  126. {
  127. if (size) *size = get_insn_size (memdata, tm);
  128. return tm;
  129. }
  130. }
  131. if (size) *size = 1;
  132. return &tic54x_unknown_opcode;
  133. }
  134. static int
  135. get_insn_size (unsigned short memdata, const insn_template *insn)
  136. {
  137. int size;
  138. if (insn->flags & FL_PAR)
  139. {
  140. /* only non-parallel instructions support lk addressing */
  141. size = insn->words;
  142. }
  143. else
  144. {
  145. size = insn->words + has_lkaddr (memdata, insn);
  146. }
  147. return size;
  148. }
  149. int
  150. print_instruction (disassemble_info *info,
  151. bfd_vma memaddr,
  152. unsigned short opcode,
  153. const char *tm_name,
  154. const enum optype tm_operands[],
  155. int size,
  156. int ext)
  157. {
  158. static int n;
  159. /* string storage for multiple operands */
  160. char operand[4][64] = { {0},{0},{0},{0}, };
  161. bfd_byte buf[2];
  162. unsigned long opcode2 = 0;
  163. unsigned long lkaddr = 0;
  164. enum optype src = OP_None;
  165. enum optype dst = OP_None;
  166. int i, shift;
  167. char *comma = "";
  168. info->fprintf_func (info->stream, "%-7s", tm_name);
  169. if (size > 1)
  170. {
  171. int status = (*info->read_memory_func) (memaddr + 1, buf, 2, info);
  172. if (status != 0)
  173. return 0;
  174. lkaddr = opcode2 = bfd_getl16 (buf);
  175. if (size > 2)
  176. {
  177. status = (*info->read_memory_func) (memaddr + 2, buf, 2, info);
  178. if (status != 0)
  179. return 0;
  180. opcode2 = bfd_getl16 (buf);
  181. }
  182. }
  183. for (i = 0; i < MAX_OPERANDS && OPTYPE (tm_operands[i]) != OP_None; i++)
  184. {
  185. char *next_comma = ",";
  186. int optional = (tm_operands[i] & OPT) != 0;
  187. switch (OPTYPE (tm_operands[i]))
  188. {
  189. case OP_Xmem:
  190. sprint_dual_address (info, operand[i], XMEM (opcode));
  191. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  192. break;
  193. case OP_Ymem:
  194. sprint_dual_address (info, operand[i], YMEM (opcode));
  195. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  196. break;
  197. case OP_Smem:
  198. case OP_Sind:
  199. case OP_Lmem:
  200. info->fprintf_func (info->stream, "%s", comma);
  201. if (INDIRECT (opcode))
  202. {
  203. if (MOD (opcode) >= 12)
  204. {
  205. bfd_vma addr = lkaddr;
  206. int arf = ARF (opcode);
  207. int mod = MOD (opcode);
  208. if (mod == 15)
  209. info->fprintf_func (info->stream, "*(");
  210. else
  211. info->fprintf_func (info->stream, "*%sar%d(",
  212. (mod == 13 || mod == 14 ? "+" : ""),
  213. arf);
  214. (*(info->print_address_func)) ((bfd_vma) addr, info);
  215. info->fprintf_func (info->stream, ")%s",
  216. mod == 14 ? "%" : "");
  217. }
  218. else
  219. {
  220. sprint_indirect_address (info, operand[i], opcode);
  221. info->fprintf_func (info->stream, "%s", operand[i]);
  222. }
  223. }
  224. else
  225. {
  226. /* FIXME -- use labels (print_address_func) */
  227. /* in order to do this, we need to guess what DP is */
  228. sprint_direct_address (info, operand[i], opcode);
  229. info->fprintf_func (info->stream, "%s", operand[i]);
  230. }
  231. break;
  232. case OP_dmad:
  233. info->fprintf_func (info->stream, "%s", comma);
  234. (*(info->print_address_func)) ((bfd_vma) opcode2, info);
  235. break;
  236. case OP_xpmad:
  237. /* upper 7 bits of address are in the opcode */
  238. opcode2 += ((unsigned long) opcode & 0x7F) << 16;
  239. /* fall through */
  240. case OP_pmad:
  241. info->fprintf_func (info->stream, "%s", comma);
  242. (*(info->print_address_func)) ((bfd_vma) opcode2, info);
  243. break;
  244. case OP_MMRX:
  245. sprint_mmr (info, operand[i], MMRX (opcode));
  246. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  247. break;
  248. case OP_MMRY:
  249. sprint_mmr (info, operand[i], MMRY (opcode));
  250. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  251. break;
  252. case OP_MMR:
  253. sprint_mmr (info, operand[i], MMR (opcode));
  254. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  255. break;
  256. case OP_PA:
  257. sprintf (operand[i], "pa%d", (unsigned) opcode2);
  258. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  259. break;
  260. case OP_SRC:
  261. src = SRC (ext ? opcode2 : opcode) ? OP_B : OP_A;
  262. sprintf (operand[i], (src == OP_B) ? "b" : "a");
  263. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  264. break;
  265. case OP_SRC1:
  266. src = SRC1 (ext ? opcode2 : opcode) ? OP_B : OP_A;
  267. sprintf (operand[i], (src == OP_B) ? "b" : "a");
  268. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  269. break;
  270. case OP_RND:
  271. dst = DST (opcode) ? OP_B : OP_A;
  272. sprintf (operand[i], (dst == OP_B) ? "a" : "b");
  273. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  274. break;
  275. case OP_DST:
  276. dst = DST (ext ? opcode2 : opcode) ? OP_B : OP_A;
  277. if (!optional || dst != src)
  278. {
  279. sprintf (operand[i], (dst == OP_B) ? "b" : "a");
  280. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  281. }
  282. else
  283. next_comma = comma;
  284. break;
  285. case OP_B:
  286. sprintf (operand[i], "b");
  287. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  288. break;
  289. case OP_A:
  290. sprintf (operand[i], "a");
  291. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  292. break;
  293. case OP_ARX:
  294. sprintf (operand[i], "ar%d", (int) ARX (opcode));
  295. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  296. break;
  297. case OP_SHIFT:
  298. shift = SHIFT (ext ? opcode2 : opcode);
  299. if (!optional || shift != 0)
  300. {
  301. sprintf (operand[i], "%d", shift);
  302. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  303. }
  304. else
  305. next_comma = comma;
  306. break;
  307. case OP_SHFT:
  308. shift = SHFT (opcode);
  309. if (!optional || shift != 0)
  310. {
  311. sprintf (operand[i], "%d", (unsigned) shift);
  312. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  313. }
  314. else
  315. next_comma = comma;
  316. break;
  317. case OP_lk:
  318. sprintf (operand[i], "#%d", (int) (short) opcode2);
  319. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  320. break;
  321. case OP_T:
  322. sprintf (operand[i], "t");
  323. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  324. break;
  325. case OP_TS:
  326. sprintf (operand[i], "ts");
  327. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  328. break;
  329. case OP_k8:
  330. sprintf (operand[i], "%d", (int) ((signed char) (opcode & 0xFF)));
  331. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  332. break;
  333. case OP_16:
  334. sprintf (operand[i], "16");
  335. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  336. break;
  337. case OP_ASM:
  338. sprintf (operand[i], "asm");
  339. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  340. break;
  341. case OP_BITC:
  342. sprintf (operand[i], "%d", (int) (opcode & 0xF));
  343. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  344. break;
  345. case OP_CC:
  346. /* put all CC operands in the same operand */
  347. sprint_condition (info, operand[i], opcode);
  348. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  349. i = MAX_OPERANDS;
  350. break;
  351. case OP_CC2:
  352. sprint_cc2 (info, operand[i], opcode);
  353. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  354. break;
  355. case OP_CC3:
  356. {
  357. const char *code[] = { "eq", "lt", "gt", "neq" };
  358. /* Do not use sprintf with only two parameters as a
  359. compiler warning could be generated in such conditions. */
  360. sprintf (operand[i], "%s", code[CC3 (opcode)]);
  361. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  362. break;
  363. }
  364. case OP_123:
  365. {
  366. int code = (opcode >> 8) & 0x3;
  367. sprintf (operand[i], "%d", (code == 0) ? 1 : (code == 2) ? 2 : 3);
  368. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  369. break;
  370. }
  371. case OP_k5:
  372. sprintf (operand[i], "#%d", ((opcode & 0x1F) ^ 0x10) - 0x10);
  373. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  374. break;
  375. case OP_k8u:
  376. sprintf (operand[i], "#%d", (unsigned) (opcode & 0xFF));
  377. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  378. break;
  379. case OP_k3:
  380. sprintf (operand[i], "#%d", (int) (opcode & 0x7));
  381. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  382. break;
  383. case OP_lku:
  384. sprintf (operand[i], "#%d", (unsigned) opcode2);
  385. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  386. break;
  387. case OP_N:
  388. n = (opcode >> 9) & 0x1;
  389. sprintf (operand[i], "st%d", n);
  390. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  391. break;
  392. case OP_SBIT:
  393. {
  394. const char *status0[] = {
  395. "0", "1", "2", "3", "4", "5", "6", "7", "8",
  396. "ovb", "ova", "c", "tc", "13", "14", "15"
  397. };
  398. const char *status1[] = {
  399. "0", "1", "2", "3", "4",
  400. "cmpt", "frct", "c16", "sxm", "ovm", "10",
  401. "intm", "hm", "xf", "cpl", "braf"
  402. };
  403. sprintf (operand[i], "%s",
  404. n ? status1[SBIT (opcode)] : status0[SBIT (opcode)]);
  405. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  406. break;
  407. }
  408. case OP_12:
  409. sprintf (operand[i], "%d", (int) ((opcode >> 9) & 1) + 1);
  410. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  411. break;
  412. case OP_TRN:
  413. sprintf (operand[i], "trn");
  414. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  415. break;
  416. case OP_DP:
  417. sprintf (operand[i], "dp");
  418. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  419. break;
  420. case OP_k9:
  421. /* FIXME-- this is DP, print the original address? */
  422. sprintf (operand[i], "#%d", (int) (opcode & 0x1FF));
  423. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  424. break;
  425. case OP_ARP:
  426. sprintf (operand[i], "arp");
  427. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  428. break;
  429. case OP_031:
  430. sprintf (operand[i], "%d", (int) (opcode & 0x1F));
  431. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  432. break;
  433. default:
  434. sprintf (operand[i], "??? (0x%x)", tm_operands[i]);
  435. info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
  436. break;
  437. }
  438. comma = next_comma;
  439. }
  440. return 1;
  441. }
  442. static int
  443. print_parallel_instruction (disassemble_info *info,
  444. bfd_vma memaddr,
  445. unsigned short opcode,
  446. const insn_template *ptm,
  447. int size)
  448. {
  449. print_instruction (info, memaddr, opcode,
  450. ptm->name, ptm->operand_types, size, 0);
  451. info->fprintf_func (info->stream, " || ");
  452. return print_instruction (info, memaddr, opcode,
  453. ptm->parname, ptm->paroperand_types, size, 0);
  454. }
  455. static int
  456. sprint_dual_address (disassemble_info *info ATTRIBUTE_UNUSED,
  457. char buf[],
  458. unsigned short code)
  459. {
  460. const char *formats[] = {
  461. "*ar%d",
  462. "*ar%d-",
  463. "*ar%d+",
  464. "*ar%d+0%%",
  465. };
  466. return sprintf (buf, formats[XMOD (code)], XARX (code));
  467. }
  468. static int
  469. sprint_indirect_address (disassemble_info *info ATTRIBUTE_UNUSED,
  470. char buf[],
  471. unsigned short opcode)
  472. {
  473. const char *formats[] = {
  474. "*ar%d",
  475. "*ar%d-",
  476. "*ar%d+",
  477. "*+ar%d",
  478. "*ar%d-0B",
  479. "*ar%d-0",
  480. "*ar%d+0",
  481. "*ar%d+0B",
  482. "*ar%d-%%",
  483. "*ar%d-0%%",
  484. "*ar%d+%%",
  485. "*ar%d+0%%",
  486. };
  487. return sprintf (buf, formats[MOD (opcode)], ARF (opcode));
  488. }
  489. static int
  490. sprint_direct_address (disassemble_info *info ATTRIBUTE_UNUSED,
  491. char buf[],
  492. unsigned short opcode)
  493. {
  494. /* FIXME -- look up relocation if available */
  495. return sprintf (buf, "DP+0x%02x", (int) (opcode & 0x7F));
  496. }
  497. static int
  498. sprint_mmr (disassemble_info *info ATTRIBUTE_UNUSED,
  499. char buf[],
  500. int mmr)
  501. {
  502. const tic54x_symbol *reg = tic54x_mmregs;
  503. while (reg->name != NULL)
  504. {
  505. if (mmr == reg->value)
  506. {
  507. sprintf (buf, "%s", (reg + 1)->name);
  508. return 1;
  509. }
  510. ++reg;
  511. }
  512. sprintf (buf, "MMR(%d)", mmr); /* FIXME -- different targets. */
  513. return 0;
  514. }
  515. static int
  516. sprint_cc2 (disassemble_info *info ATTRIBUTE_UNUSED,
  517. char *buf,
  518. unsigned short opcode)
  519. {
  520. const char *cc2[] = {
  521. "??", "??", "ageq", "alt", "aneq", "aeq", "agt", "aleq",
  522. "??", "??", "bgeq", "blt", "bneq", "beq", "bgt", "bleq",
  523. };
  524. return sprintf (buf, "%s", cc2[opcode & 0xF]);
  525. }
  526. static int
  527. sprint_condition (disassemble_info *info ATTRIBUTE_UNUSED,
  528. char *buf,
  529. unsigned short opcode)
  530. {
  531. char *start = buf;
  532. const char *cmp[] = {
  533. "??", "??", "geq", "lt", "neq", "eq", "gt", "leq"
  534. };
  535. if (opcode & 0x40)
  536. {
  537. char acc = (opcode & 0x8) ? 'b' : 'a';
  538. if (opcode & 0x7)
  539. buf += sprintf (buf, "%c%s%s", acc, cmp[(opcode & 0x7)],
  540. (opcode & 0x20) ? ", " : "");
  541. if (opcode & 0x20)
  542. buf += sprintf (buf, "%c%s", acc, (opcode & 0x10) ? "ov" : "nov");
  543. }
  544. else if (opcode & 0x3F)
  545. {
  546. if (opcode & 0x30)
  547. buf += sprintf (buf, "%s%s",
  548. ((opcode & 0x30) == 0x30) ? "tc" : "ntc",
  549. (opcode & 0x0F) ? ", " : "");
  550. if (opcode & 0x0C)
  551. buf += sprintf (buf, "%s%s",
  552. ((opcode & 0x0C) == 0x0C) ? "c" : "nc",
  553. (opcode & 0x03) ? ", " : "");
  554. if (opcode & 0x03)
  555. buf += sprintf (buf, "%s",
  556. ((opcode & 0x03) == 0x03) ? "bio" : "nbio");
  557. }
  558. else
  559. buf += sprintf (buf, "unc");
  560. return buf - start;
  561. }