tic30-dis.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /* Disassembly routines for TMS320C30 architecture
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Steven Haworth (steve@pm.cse.rmit.edu.au)
  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 "disassemble.h"
  21. #include "opcode/tic30.h"
  22. #define NORMAL_INSN 1
  23. #define PARALLEL_INSN 2
  24. /* Gets the type of instruction based on the top 2 or 3 bits of the
  25. instruction word. */
  26. #define GET_TYPE(insn) (insn & 0x80000000 ? insn & 0xC0000000 : insn & 0xE0000000)
  27. /* Instruction types. */
  28. #define TWO_OPERAND_1 0x00000000
  29. #define TWO_OPERAND_2 0x40000000
  30. #define THREE_OPERAND 0x20000000
  31. #define PAR_STORE 0xC0000000
  32. #define MUL_ADDS 0x80000000
  33. #define BRANCHES 0x60000000
  34. /* Specific instruction id bits. */
  35. #define NORMAL_IDEN 0x1F800000
  36. #define PAR_STORE_IDEN 0x3E000000
  37. #define MUL_ADD_IDEN 0x2C000000
  38. #define BR_IMM_IDEN 0x1F000000
  39. #define BR_COND_IDEN 0x1C3F0000
  40. /* Addressing modes. */
  41. #define AM_REGISTER 0x00000000
  42. #define AM_DIRECT 0x00200000
  43. #define AM_INDIRECT 0x00400000
  44. #define AM_IMM 0x00600000
  45. #define P_FIELD 0x03000000
  46. #define REG_AR0 0x08
  47. #define LDP_INSN 0x08700000
  48. /* TMS320C30 program counter for current instruction. */
  49. static unsigned int _pc;
  50. struct instruction
  51. {
  52. int type;
  53. insn_template *tm;
  54. partemplate *ptm;
  55. };
  56. static int
  57. get_tic30_instruction (unsigned long insn_word, struct instruction *insn)
  58. {
  59. switch (GET_TYPE (insn_word))
  60. {
  61. case TWO_OPERAND_1:
  62. case TWO_OPERAND_2:
  63. case THREE_OPERAND:
  64. insn->type = NORMAL_INSN;
  65. {
  66. insn_template *current_optab = (insn_template *) tic30_optab;
  67. for (; current_optab < tic30_optab_end; current_optab++)
  68. {
  69. if (GET_TYPE (current_optab->base_opcode) == GET_TYPE (insn_word))
  70. {
  71. if (current_optab->operands == 0)
  72. {
  73. if (current_optab->base_opcode == insn_word)
  74. {
  75. insn->tm = current_optab;
  76. break;
  77. }
  78. }
  79. else if ((current_optab->base_opcode & NORMAL_IDEN) == (insn_word & NORMAL_IDEN))
  80. {
  81. insn->tm = current_optab;
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. break;
  88. case PAR_STORE:
  89. insn->type = PARALLEL_INSN;
  90. {
  91. partemplate *current_optab = (partemplate *) tic30_paroptab;
  92. for (; current_optab < tic30_paroptab_end; current_optab++)
  93. {
  94. if (GET_TYPE (current_optab->base_opcode) == GET_TYPE (insn_word))
  95. {
  96. if ((current_optab->base_opcode & PAR_STORE_IDEN)
  97. == (insn_word & PAR_STORE_IDEN))
  98. {
  99. insn->ptm = current_optab;
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. break;
  106. case MUL_ADDS:
  107. insn->type = PARALLEL_INSN;
  108. {
  109. partemplate *current_optab = (partemplate *) tic30_paroptab;
  110. for (; current_optab < tic30_paroptab_end; current_optab++)
  111. {
  112. if (GET_TYPE (current_optab->base_opcode) == GET_TYPE (insn_word))
  113. {
  114. if ((current_optab->base_opcode & MUL_ADD_IDEN)
  115. == (insn_word & MUL_ADD_IDEN))
  116. {
  117. insn->ptm = current_optab;
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. break;
  124. case BRANCHES:
  125. insn->type = NORMAL_INSN;
  126. {
  127. insn_template *current_optab = (insn_template *) tic30_optab;
  128. for (; current_optab < tic30_optab_end; current_optab++)
  129. {
  130. if (GET_TYPE (current_optab->base_opcode) == GET_TYPE (insn_word))
  131. {
  132. if (current_optab->operand_types[0] & Imm24)
  133. {
  134. if ((current_optab->base_opcode & BR_IMM_IDEN)
  135. == (insn_word & BR_IMM_IDEN))
  136. {
  137. insn->tm = current_optab;
  138. break;
  139. }
  140. }
  141. else if (current_optab->operands > 0)
  142. {
  143. if ((current_optab->base_opcode & BR_COND_IDEN)
  144. == (insn_word & BR_COND_IDEN))
  145. {
  146. insn->tm = current_optab;
  147. break;
  148. }
  149. }
  150. else
  151. {
  152. if ((current_optab->base_opcode & (BR_COND_IDEN | 0x00800000))
  153. == (insn_word & (BR_COND_IDEN | 0x00800000)))
  154. {
  155. insn->tm = current_optab;
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. break;
  163. default:
  164. return 0;
  165. }
  166. return 1;
  167. }
  168. #define OPERAND_BUFFER_LEN 15
  169. static int
  170. get_register_operand (unsigned char fragment, char *buffer)
  171. {
  172. const reg *current_reg = tic30_regtab;
  173. if (buffer == NULL)
  174. return 0;
  175. for (; current_reg < tic30_regtab_end; current_reg++)
  176. {
  177. if ((fragment & 0x1F) == current_reg->opcode)
  178. {
  179. strncpy (buffer, current_reg->name, OPERAND_BUFFER_LEN - 1);
  180. buffer[OPERAND_BUFFER_LEN - 1] = 0;
  181. return 1;
  182. }
  183. }
  184. return 0;
  185. }
  186. static int
  187. get_indirect_operand (unsigned short fragment,
  188. int size,
  189. char *buffer)
  190. {
  191. unsigned char mod;
  192. unsigned arnum;
  193. unsigned char disp;
  194. if (buffer == NULL)
  195. return 0;
  196. /* Determine which bits identify the sections of the indirect
  197. operand based on the size in bytes. */
  198. switch (size)
  199. {
  200. case 1:
  201. mod = (fragment & 0x00F8) >> 3;
  202. arnum = (fragment & 0x0007);
  203. disp = 0;
  204. break;
  205. case 2:
  206. mod = (fragment & 0xF800) >> 11;
  207. arnum = (fragment & 0x0700) >> 8;
  208. disp = (fragment & 0x00FF);
  209. break;
  210. default:
  211. return 0;
  212. }
  213. {
  214. const ind_addr_type *current_ind = tic30_indaddr_tab;
  215. for (; current_ind < tic30_indaddrtab_end; current_ind++)
  216. {
  217. if (current_ind->modfield == mod)
  218. {
  219. if (current_ind->displacement == IMPLIED_DISP && size == 2)
  220. continue;
  221. else
  222. {
  223. size_t i, len;
  224. int bufcnt;
  225. len = strlen (current_ind->syntax);
  226. for (i = 0, bufcnt = 0; i < len; i++, bufcnt++)
  227. {
  228. buffer[bufcnt] = current_ind->syntax[i];
  229. if (bufcnt > 0
  230. && bufcnt < OPERAND_BUFFER_LEN - 1
  231. && buffer[bufcnt - 1] == 'a'
  232. && buffer[bufcnt] == 'r')
  233. buffer[++bufcnt] = arnum + '0';
  234. if (bufcnt < OPERAND_BUFFER_LEN - 1
  235. && buffer[bufcnt] == '('
  236. && current_ind->displacement == DISP_REQUIRED)
  237. {
  238. snprintf (buffer + (bufcnt + 1),
  239. OPERAND_BUFFER_LEN - (bufcnt + 1),
  240. "%u", disp);
  241. bufcnt += strlen (buffer + (bufcnt + 1));
  242. }
  243. }
  244. buffer[bufcnt + 1] = '\0';
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. return 1;
  251. }
  252. static int
  253. cnvt_tmsfloat_ieee (unsigned long tmsfloat, int size, float *ieeefloat)
  254. {
  255. unsigned long exponent, sign, mant;
  256. union
  257. {
  258. unsigned long l;
  259. float f;
  260. } val;
  261. if (size == 2)
  262. {
  263. if ((tmsfloat & 0x0000F000) == 0x00008000)
  264. tmsfloat = 0x80000000;
  265. else
  266. {
  267. tmsfloat <<= 16;
  268. tmsfloat = (long) tmsfloat >> 4;
  269. }
  270. }
  271. exponent = tmsfloat & 0xFF000000;
  272. if (exponent == 0x80000000)
  273. {
  274. *ieeefloat = 0.0;
  275. return 1;
  276. }
  277. exponent += 0x7F000000;
  278. sign = (tmsfloat & 0x00800000) << 8;
  279. mant = tmsfloat & 0x007FFFFF;
  280. if (exponent == 0xFF000000)
  281. {
  282. if (mant == 0)
  283. *ieeefloat = ERANGE;
  284. #ifdef HUGE_VALF
  285. if (sign == 0)
  286. *ieeefloat = HUGE_VALF;
  287. else
  288. *ieeefloat = -HUGE_VALF;
  289. #else
  290. if (sign == 0)
  291. *ieeefloat = 1.0 / 0.0;
  292. else
  293. *ieeefloat = -1.0 / 0.0;
  294. #endif
  295. return 1;
  296. }
  297. exponent >>= 1;
  298. if (sign)
  299. {
  300. mant = (~mant) & 0x007FFFFF;
  301. mant += 1;
  302. exponent += mant & 0x00800000;
  303. exponent &= 0x7F800000;
  304. mant &= 0x007FFFFF;
  305. }
  306. if (tmsfloat == 0x80000000)
  307. sign = mant = exponent = 0;
  308. tmsfloat = sign | exponent | mant;
  309. val.l = tmsfloat;
  310. *ieeefloat = val.f;
  311. return 1;
  312. }
  313. static int
  314. print_two_operand (disassemble_info *info,
  315. unsigned long insn_word,
  316. struct instruction *insn)
  317. {
  318. char name[12];
  319. char operand[2][OPERAND_BUFFER_LEN] =
  320. {
  321. {0},
  322. {0}
  323. };
  324. float f_number;
  325. if (insn->tm == NULL)
  326. return 0;
  327. strcpy (name, insn->tm->name);
  328. if (insn->tm->opcode_modifier == AddressMode)
  329. {
  330. int src_op, dest_op;
  331. /* Determine whether instruction is a store or a normal instruction. */
  332. if ((insn->tm->operand_types[1] & (Direct | Indirect))
  333. == (Direct | Indirect))
  334. {
  335. src_op = 1;
  336. dest_op = 0;
  337. }
  338. else
  339. {
  340. src_op = 0;
  341. dest_op = 1;
  342. }
  343. /* Get the destination register. */
  344. if (insn->tm->operands == 2)
  345. get_register_operand ((insn_word & 0x001F0000) >> 16, operand[dest_op]);
  346. /* Get the source operand based on addressing mode. */
  347. switch (insn_word & AddressMode)
  348. {
  349. case AM_REGISTER:
  350. /* Check for the NOP instruction before getting the operand. */
  351. if ((insn->tm->operand_types[0] & NotReq) == 0)
  352. get_register_operand ((insn_word & 0x0000001F), operand[src_op]);
  353. break;
  354. case AM_DIRECT:
  355. sprintf (operand[src_op], "@0x%lX", (insn_word & 0x0000FFFF));
  356. break;
  357. case AM_INDIRECT:
  358. get_indirect_operand ((insn_word & 0x0000FFFF), 2, operand[src_op]);
  359. break;
  360. case AM_IMM:
  361. /* Get the value of the immediate operand based on variable type. */
  362. switch (insn->tm->imm_arg_type)
  363. {
  364. case Imm_Float:
  365. cnvt_tmsfloat_ieee ((insn_word & 0x0000FFFF), 2, &f_number);
  366. sprintf (operand[src_op], "%2.2f", f_number);
  367. break;
  368. case Imm_SInt:
  369. sprintf (operand[src_op], "%d", (short) (insn_word & 0x0000FFFF));
  370. break;
  371. case Imm_UInt:
  372. sprintf (operand[src_op], "%lu", (insn_word & 0x0000FFFF));
  373. break;
  374. default:
  375. return 0;
  376. }
  377. /* Handle special case for LDP instruction. */
  378. if ((insn_word & 0xFFFFFF00) == LDP_INSN)
  379. {
  380. strcpy (name, "ldp");
  381. sprintf (operand[0], "0x%06lX", (insn_word & 0x000000FF) << 16);
  382. operand[1][0] = '\0';
  383. }
  384. }
  385. }
  386. /* Handle case for stack and rotate instructions. */
  387. else if (insn->tm->operands == 1)
  388. {
  389. if (insn->tm->opcode_modifier == StackOp)
  390. get_register_operand ((insn_word & 0x001F0000) >> 16, operand[0]);
  391. }
  392. /* Output instruction to stream. */
  393. info->fprintf_func (info->stream, " %s %s%c%s", name,
  394. operand[0][0] ? operand[0] : "",
  395. operand[1][0] ? ',' : ' ',
  396. operand[1][0] ? operand[1] : "");
  397. return 1;
  398. }
  399. static int
  400. print_three_operand (disassemble_info *info,
  401. unsigned long insn_word,
  402. struct instruction *insn)
  403. {
  404. char operand[3][OPERAND_BUFFER_LEN] =
  405. {
  406. {0},
  407. {0},
  408. {0}
  409. };
  410. if (insn->tm == NULL)
  411. return 0;
  412. switch (insn_word & AddressMode)
  413. {
  414. case AM_REGISTER:
  415. get_register_operand ((insn_word & 0x000000FF), operand[0]);
  416. get_register_operand ((insn_word & 0x0000FF00) >> 8, operand[1]);
  417. break;
  418. case AM_DIRECT:
  419. get_register_operand ((insn_word & 0x000000FF), operand[0]);
  420. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1]);
  421. break;
  422. case AM_INDIRECT:
  423. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0]);
  424. get_register_operand ((insn_word & 0x0000FF00) >> 8, operand[1]);
  425. break;
  426. case AM_IMM:
  427. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0]);
  428. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1]);
  429. break;
  430. default:
  431. return 0;
  432. }
  433. if (insn->tm->operands == 3)
  434. get_register_operand ((insn_word & 0x001F0000) >> 16, operand[2]);
  435. info->fprintf_func (info->stream, " %s %s,%s%c%s", insn->tm->name,
  436. operand[0], operand[1],
  437. operand[2][0] ? ',' : ' ',
  438. operand[2][0] ? operand[2] : "");
  439. return 1;
  440. }
  441. static int
  442. print_par_insn (disassemble_info *info,
  443. unsigned long insn_word,
  444. struct instruction *insn)
  445. {
  446. size_t i, len;
  447. char *name1, *name2;
  448. char operand[2][3][OPERAND_BUFFER_LEN] =
  449. {
  450. {
  451. {0},
  452. {0},
  453. {0}
  454. },
  455. {
  456. {0},
  457. {0},
  458. {0}
  459. }
  460. };
  461. if (insn->ptm == NULL)
  462. return 0;
  463. /* Parse out the names of each of the parallel instructions from the
  464. q_insn1_insn2 format. */
  465. name1 = (char *) strdup (insn->ptm->name + 2);
  466. name2 = "";
  467. len = strlen (name1);
  468. for (i = 0; i < len; i++)
  469. {
  470. if (name1[i] == '_')
  471. {
  472. name2 = &name1[i + 1];
  473. name1[i] = '\0';
  474. break;
  475. }
  476. }
  477. /* Get the operands of the instruction based on the operand order. */
  478. switch (insn->ptm->oporder)
  479. {
  480. case OO_4op1:
  481. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][0]);
  482. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][1]);
  483. get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
  484. get_register_operand ((insn_word >> 22) & 0x07, operand[0][1]);
  485. break;
  486. case OO_4op2:
  487. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][0]);
  488. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][0]);
  489. get_register_operand ((insn_word >> 19) & 0x07, operand[1][1]);
  490. get_register_operand ((insn_word >> 22) & 0x07, operand[0][1]);
  491. break;
  492. case OO_4op3:
  493. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][1]);
  494. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][1]);
  495. get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
  496. get_register_operand ((insn_word >> 22) & 0x07, operand[0][0]);
  497. break;
  498. case OO_5op1:
  499. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][0]);
  500. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][1]);
  501. get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
  502. get_register_operand ((insn_word >> 19) & 0x07, operand[0][1]);
  503. get_register_operand ((insn_word >> 22) & 0x07, operand[0][2]);
  504. break;
  505. case OO_5op2:
  506. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][1]);
  507. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][1]);
  508. get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
  509. get_register_operand ((insn_word >> 19) & 0x07, operand[0][0]);
  510. get_register_operand ((insn_word >> 22) & 0x07, operand[0][2]);
  511. break;
  512. case OO_PField:
  513. if (insn_word & 0x00800000)
  514. get_register_operand (0x01, operand[0][2]);
  515. else
  516. get_register_operand (0x00, operand[0][2]);
  517. if (insn_word & 0x00400000)
  518. get_register_operand (0x03, operand[1][2]);
  519. else
  520. get_register_operand (0x02, operand[1][2]);
  521. switch (insn_word & P_FIELD)
  522. {
  523. case 0x00000000:
  524. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][1]);
  525. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[0][0]);
  526. get_register_operand ((insn_word >> 16) & 0x07, operand[1][1]);
  527. get_register_operand ((insn_word >> 19) & 0x07, operand[1][0]);
  528. break;
  529. case 0x01000000:
  530. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[1][0]);
  531. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[0][0]);
  532. get_register_operand ((insn_word >> 16) & 0x07, operand[1][1]);
  533. get_register_operand ((insn_word >> 19) & 0x07, operand[0][1]);
  534. break;
  535. case 0x02000000:
  536. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[1][1]);
  537. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][0]);
  538. get_register_operand ((insn_word >> 16) & 0x07, operand[0][1]);
  539. get_register_operand ((insn_word >> 19) & 0x07, operand[0][0]);
  540. break;
  541. case 0x03000000:
  542. get_indirect_operand ((insn_word & 0x000000FF), 1, operand[1][1]);
  543. get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[0][0]);
  544. get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
  545. get_register_operand ((insn_word >> 19) & 0x07, operand[0][1]);
  546. break;
  547. }
  548. break;
  549. default:
  550. return 0;
  551. }
  552. info->fprintf_func (info->stream, " %s %s,%s%c%s", name1,
  553. operand[0][0], operand[0][1],
  554. operand[0][2][0] ? ',' : ' ',
  555. operand[0][2][0] ? operand[0][2] : "");
  556. info->fprintf_func (info->stream, "\n\t\t\t|| %s %s,%s%c%s", name2,
  557. operand[1][0], operand[1][1],
  558. operand[1][2][0] ? ',' : ' ',
  559. operand[1][2][0] ? operand[1][2] : "");
  560. free (name1);
  561. return 1;
  562. }
  563. static int
  564. print_branch (disassemble_info *info,
  565. unsigned long insn_word,
  566. struct instruction *insn)
  567. {
  568. char operand[2][OPERAND_BUFFER_LEN] =
  569. {
  570. {0},
  571. {0}
  572. };
  573. unsigned long address;
  574. int print_label = 0;
  575. if (insn->tm == NULL)
  576. return 0;
  577. /* Get the operands for 24-bit immediate jumps. */
  578. if (insn->tm->operand_types[0] & Imm24)
  579. {
  580. address = insn_word & 0x00FFFFFF;
  581. sprintf (operand[0], "0x%lX", address);
  582. print_label = 1;
  583. }
  584. /* Get the operand for the trap instruction. */
  585. else if (insn->tm->operand_types[0] & IVector)
  586. {
  587. address = insn_word & 0x0000001F;
  588. sprintf (operand[0], "0x%lX", address);
  589. }
  590. else
  591. {
  592. address = insn_word & 0x0000FFFF;
  593. /* Get the operands for the DB instructions. */
  594. if (insn->tm->operands == 2)
  595. {
  596. get_register_operand (((insn_word & 0x01C00000) >> 22) + REG_AR0, operand[0]);
  597. if (insn_word & PCRel)
  598. {
  599. sprintf (operand[1], "%d", (short) address);
  600. print_label = 1;
  601. }
  602. else
  603. get_register_operand (insn_word & 0x0000001F, operand[1]);
  604. }
  605. /* Get the operands for the standard branches. */
  606. else if (insn->tm->operands == 1)
  607. {
  608. if (insn_word & PCRel)
  609. {
  610. address = (short) address;
  611. sprintf (operand[0], "%ld", address);
  612. print_label = 1;
  613. }
  614. else
  615. get_register_operand (insn_word & 0x0000001F, operand[0]);
  616. }
  617. }
  618. info->fprintf_func (info->stream, " %s %s%c%s", insn->tm->name,
  619. operand[0][0] ? operand[0] : "",
  620. operand[1][0] ? ',' : ' ',
  621. operand[1][0] ? operand[1] : "");
  622. /* Print destination of branch in relation to current symbol. */
  623. if (print_label && info->symbols)
  624. {
  625. asymbol *sym = *info->symbols;
  626. if ((insn->tm->opcode_modifier == PCRel) && (insn_word & PCRel))
  627. {
  628. address = (_pc + 1 + (short) address) - ((sym->section->vma + sym->value) / 4);
  629. /* Check for delayed instruction, if so adjust destination. */
  630. if (insn_word & 0x00200000)
  631. address += 2;
  632. }
  633. else
  634. {
  635. address -= ((sym->section->vma + sym->value) / 4);
  636. }
  637. if (address == 0)
  638. info->fprintf_func (info->stream, " <%s>", sym->name);
  639. else
  640. info->fprintf_func (info->stream, " <%s %c %lu>", sym->name,
  641. ((short) address < 0) ? '-' : '+',
  642. address);
  643. }
  644. return 1;
  645. }
  646. int
  647. print_insn_tic30 (bfd_vma pc, disassemble_info *info)
  648. {
  649. unsigned long insn_word;
  650. struct instruction insn = { 0, NULL, NULL };
  651. bfd_vma bufaddr = pc - info->buffer_vma;
  652. if (bufaddr + 3 >= info->buffer_length)
  653. return -1;
  654. /* Obtain the current instruction word from the buffer. */
  655. insn_word = (((unsigned) *(info->buffer + bufaddr) << 24)
  656. | (*(info->buffer + bufaddr + 1) << 16)
  657. | (*(info->buffer + bufaddr + 2) << 8)
  658. | *(info->buffer + bufaddr + 3));
  659. _pc = pc / 4;
  660. /* Get the instruction referred to by the current instruction word
  661. and print it out based on its type. */
  662. if (!get_tic30_instruction (insn_word, &insn))
  663. return -1;
  664. switch (GET_TYPE (insn_word))
  665. {
  666. case TWO_OPERAND_1:
  667. case TWO_OPERAND_2:
  668. if (!print_two_operand (info, insn_word, &insn))
  669. return -1;
  670. break;
  671. case THREE_OPERAND:
  672. if (!print_three_operand (info, insn_word, &insn))
  673. return -1;
  674. break;
  675. case PAR_STORE:
  676. case MUL_ADDS:
  677. if (!print_par_insn (info, insn_word, &insn))
  678. return -1;
  679. break;
  680. case BRANCHES:
  681. if (!print_branch (info, insn_word, &insn))
  682. return -1;
  683. break;
  684. }
  685. return 4;
  686. }