crx-dis.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /* Disassembler code for CRX.
  2. Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. Contributed by Tomer Levi, NSC, Israel.
  4. Written by Tomer Levi.
  5. This file is part of the GNU opcodes library.
  6. This library is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. It is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  13. License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "sysdep.h"
  19. #include "disassemble.h"
  20. #include "opcode/crx.h"
  21. /* String to print when opcode was not matched. */
  22. #define ILLEGAL "illegal"
  23. /* Escape to 16-bit immediate. */
  24. #define ESCAPE_16_BIT 0xE
  25. /* Extract 'n_bits' from 'a' starting from offset 'offs'. */
  26. #define EXTRACT(a, offs, n_bits) \
  27. (((a) >> (offs)) & ((2ull << (n_bits - 1)) - 1))
  28. /* Set Bit Mask - a mask to set all bits starting from offset 'offs'. */
  29. #define SBM(offs) ((-1u << (offs)) & 0xffffffff)
  30. typedef unsigned long dwordU;
  31. typedef unsigned short wordU;
  32. typedef struct
  33. {
  34. dwordU val;
  35. int nbits;
  36. } parameter;
  37. /* Structure to hold valid 'cinv' instruction options. */
  38. typedef struct
  39. {
  40. /* Cinv printed string. */
  41. char *str;
  42. /* Value corresponding to the string. */
  43. unsigned int value;
  44. }
  45. cinv_entry;
  46. /* CRX 'cinv' options. */
  47. static const cinv_entry crx_cinvs[] =
  48. {
  49. {"[i]", 2}, {"[i,u]", 3}, {"[d]", 4}, {"[d,u]", 5},
  50. {"[d,i]", 6}, {"[d,i,u]", 7}, {"[b]", 8},
  51. {"[b,i]", 10}, {"[b,i,u]", 11}, {"[b,d]", 12},
  52. {"[b,d,u]", 13}, {"[b,d,i]", 14}, {"[b,d,i,u]", 15}
  53. };
  54. /* Enum to distinguish different registers argument types. */
  55. typedef enum REG_ARG_TYPE
  56. {
  57. /* General purpose register (r<N>). */
  58. REG_ARG = 0,
  59. /* User register (u<N>). */
  60. USER_REG_ARG,
  61. /* CO-Processor register (c<N>). */
  62. COP_ARG,
  63. /* CO-Processor special register (cs<N>). */
  64. COPS_ARG
  65. }
  66. REG_ARG_TYPE;
  67. /* Number of valid 'cinv' instruction options. */
  68. static int NUMCINVS = ((sizeof crx_cinvs)/(sizeof crx_cinvs[0]));
  69. /* Current opcode table entry we're disassembling. */
  70. static const inst *instruction;
  71. /* Current instruction we're disassembling. */
  72. static ins currInsn;
  73. /* The current instruction is read into 3 consecutive words. */
  74. static wordU words[3];
  75. /* Contains all words in appropriate order. */
  76. static ULONGLONG allWords;
  77. /* Holds the current processed argument number. */
  78. static int processing_argument_number;
  79. /* Nonzero means a CST4 instruction. */
  80. static int cst4flag;
  81. /* Nonzero means the instruction's original size is
  82. incremented (escape sequence is used). */
  83. static int size_changed;
  84. /* Retrieve the number of operands for the current assembled instruction. */
  85. static int
  86. get_number_of_operands (void)
  87. {
  88. int i;
  89. for (i = 0; i < MAX_OPERANDS && instruction->operands[i].op_type; i++)
  90. ;
  91. return i;
  92. }
  93. /* Return the bit size for a given operand. */
  94. static int
  95. getbits (operand_type op)
  96. {
  97. if (op < MAX_OPRD)
  98. return crx_optab[op].bit_size;
  99. else
  100. return 0;
  101. }
  102. /* Return the argument type of a given operand. */
  103. static argtype
  104. getargtype (operand_type op)
  105. {
  106. if (op < MAX_OPRD)
  107. return crx_optab[op].arg_type;
  108. else
  109. return nullargs;
  110. }
  111. /* Given the trap index in dispatch table, return its name.
  112. This routine is used when disassembling the 'excp' instruction. */
  113. static char *
  114. gettrapstring (unsigned int trap_index)
  115. {
  116. const trap_entry *trap;
  117. for (trap = crx_traps; trap < crx_traps + NUMTRAPS; trap++)
  118. if (trap->entry == trap_index)
  119. return trap->name;
  120. return ILLEGAL;
  121. }
  122. /* Given a 'cinv' instruction constant operand, return its corresponding string.
  123. This routine is used when disassembling the 'cinv' instruction. */
  124. static char *
  125. getcinvstring (unsigned int num)
  126. {
  127. const cinv_entry *cinv;
  128. for (cinv = crx_cinvs; cinv < (crx_cinvs + NUMCINVS); cinv++)
  129. if (cinv->value == num)
  130. return cinv->str;
  131. return ILLEGAL;
  132. }
  133. /* Given a register enum value, retrieve its name. */
  134. static char *
  135. getregname (reg r)
  136. {
  137. const reg_entry * regentry = &crx_regtab[r];
  138. if (regentry->type != CRX_R_REGTYPE)
  139. return ILLEGAL;
  140. else
  141. return regentry->name;
  142. }
  143. /* Given a coprocessor register enum value, retrieve its name. */
  144. static char *
  145. getcopregname (copreg r, reg_type type)
  146. {
  147. const reg_entry * regentry;
  148. if (type == CRX_C_REGTYPE)
  149. regentry = &crx_copregtab[r];
  150. else if (type == CRX_CS_REGTYPE)
  151. regentry = &crx_copregtab[r+(cs0-c0)];
  152. else
  153. return ILLEGAL;
  154. return regentry->name;
  155. }
  156. /* Getting a processor register name. */
  157. static char *
  158. getprocregname (int reg_index)
  159. {
  160. const reg_entry *r;
  161. for (r = crx_regtab; r < crx_regtab + NUMREGS; r++)
  162. if (r->image == reg_index)
  163. return r->name;
  164. return "ILLEGAL REGISTER";
  165. }
  166. /* Get the power of two for a given integer. */
  167. static int
  168. powerof2 (int x)
  169. {
  170. int product, i;
  171. for (i = 0, product = 1; i < x; i++)
  172. product *= 2;
  173. return product;
  174. }
  175. /* Transform a register bit mask to a register list. */
  176. static void
  177. getregliststring (int mask, char *string, enum REG_ARG_TYPE core_cop)
  178. {
  179. char temp_string[16];
  180. int i;
  181. string[0] = '{';
  182. string[1] = '\0';
  183. /* A zero mask means HI/LO registers. */
  184. if (mask == 0)
  185. {
  186. if (core_cop == USER_REG_ARG)
  187. strcat (string, "ulo,uhi");
  188. else
  189. strcat (string, "lo,hi");
  190. }
  191. else
  192. {
  193. for (i = 0; i < 16; i++)
  194. {
  195. if (mask & 0x1)
  196. {
  197. switch (core_cop)
  198. {
  199. case REG_ARG:
  200. sprintf (temp_string, "r%d", i);
  201. break;
  202. case USER_REG_ARG:
  203. sprintf (temp_string, "u%d", i);
  204. break;
  205. case COP_ARG:
  206. sprintf (temp_string, "c%d", i);
  207. break;
  208. case COPS_ARG:
  209. sprintf (temp_string, "cs%d", i);
  210. break;
  211. default:
  212. break;
  213. }
  214. strcat (string, temp_string);
  215. if (mask & 0xfffe)
  216. strcat (string, ",");
  217. }
  218. mask >>= 1;
  219. }
  220. }
  221. strcat (string, "}");
  222. }
  223. /* START and END are relating 'allWords' struct, which is 48 bits size.
  224. START|--------|END
  225. +---------+---------+---------+---------+
  226. | | V | A | L |
  227. +---------+---------+---------+---------+
  228. 0 16 32 48
  229. words [0] [1] [2] */
  230. static parameter
  231. makelongparameter (ULONGLONG val, int start, int end)
  232. {
  233. parameter p;
  234. p.val = (dwordU) EXTRACT(val, 48 - end, end - start);
  235. p.nbits = end - start;
  236. return p;
  237. }
  238. /* Build a mask of the instruction's 'constant' opcode,
  239. based on the instruction's printing flags. */
  240. static unsigned int
  241. build_mask (void)
  242. {
  243. unsigned int print_flags;
  244. unsigned int mask;
  245. print_flags = instruction->flags & FMT_CRX;
  246. switch (print_flags)
  247. {
  248. case FMT_1:
  249. mask = 0xF0F00000;
  250. break;
  251. case FMT_2:
  252. mask = 0xFFF0FF00;
  253. break;
  254. case FMT_3:
  255. mask = 0xFFF00F00;
  256. break;
  257. case FMT_4:
  258. mask = 0xFFF0F000;
  259. break;
  260. case FMT_5:
  261. mask = 0xFFF0FFF0;
  262. break;
  263. default:
  264. mask = SBM(instruction->match_bits);
  265. break;
  266. }
  267. return mask;
  268. }
  269. /* Search for a matching opcode. Return 1 for success, 0 for failure. */
  270. static int
  271. match_opcode (void)
  272. {
  273. unsigned int mask;
  274. /* The instruction 'constant' opcode doewsn't exceed 32 bits. */
  275. unsigned int doubleWord = words[1] + ((unsigned) words[0] << 16);
  276. /* Start searching from end of instruction table. */
  277. instruction = &crx_instruction[NUMOPCODES - 2];
  278. /* Loop over instruction table until a full match is found. */
  279. while (instruction >= crx_instruction)
  280. {
  281. mask = build_mask ();
  282. if ((doubleWord & mask) == BIN(instruction->match, instruction->match_bits))
  283. return 1;
  284. else
  285. instruction--;
  286. }
  287. return 0;
  288. }
  289. /* Set the proper parameter value for different type of arguments. */
  290. static void
  291. make_argument (argument * a, int start_bits)
  292. {
  293. int inst_bit_size, total_size;
  294. parameter p;
  295. if ((instruction->size == 3) && a->size >= 16)
  296. inst_bit_size = 48;
  297. else
  298. inst_bit_size = 32;
  299. switch (a->type)
  300. {
  301. case arg_copr:
  302. case arg_copsr:
  303. p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
  304. inst_bit_size - start_bits);
  305. a->cr = p.val;
  306. break;
  307. case arg_r:
  308. p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
  309. inst_bit_size - start_bits);
  310. a->r = p.val;
  311. break;
  312. case arg_ic:
  313. p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
  314. inst_bit_size - start_bits);
  315. if ((p.nbits == 4) && cst4flag)
  316. {
  317. if (IS_INSN_TYPE (CMPBR_INS) && (p.val == ESCAPE_16_BIT))
  318. {
  319. /* A special case, where the value is actually stored
  320. in the last 4 bits. */
  321. p = makelongparameter (allWords, 44, 48);
  322. /* The size of the instruction should be incremented. */
  323. size_changed = 1;
  324. }
  325. if (p.val == 6)
  326. p.val = -1;
  327. else if (p.val == 13)
  328. p.val = 48;
  329. else if (p.val == 5)
  330. p.val = -4;
  331. else if (p.val == 10)
  332. p.val = 32;
  333. else if (p.val == 11)
  334. p.val = 20;
  335. else if (p.val == 9)
  336. p.val = 16;
  337. }
  338. a->constant = p.val;
  339. break;
  340. case arg_idxr:
  341. a->scale = 0;
  342. total_size = a->size + 10; /* sizeof(rbase + ridx + scl2) = 10. */
  343. p = makelongparameter (allWords, inst_bit_size - total_size,
  344. inst_bit_size - (total_size - 4));
  345. a->r = p.val;
  346. p = makelongparameter (allWords, inst_bit_size - (total_size - 4),
  347. inst_bit_size - (total_size - 8));
  348. a->i_r = p.val;
  349. p = makelongparameter (allWords, inst_bit_size - (total_size - 8),
  350. inst_bit_size - (total_size - 10));
  351. a->scale = p.val;
  352. p = makelongparameter (allWords, inst_bit_size - (total_size - 10),
  353. inst_bit_size);
  354. a->constant = p.val;
  355. break;
  356. case arg_rbase:
  357. p = makelongparameter (allWords, inst_bit_size - (start_bits + 4),
  358. inst_bit_size - start_bits);
  359. a->r = p.val;
  360. break;
  361. case arg_cr:
  362. if (a->size <= 8)
  363. {
  364. p = makelongparameter (allWords, inst_bit_size - (start_bits + 4),
  365. inst_bit_size - start_bits);
  366. a->r = p.val;
  367. /* Case for opc4 r dispu rbase. */
  368. p = makelongparameter (allWords, inst_bit_size - (start_bits + 8),
  369. inst_bit_size - (start_bits + 4));
  370. }
  371. else
  372. {
  373. /* The 'rbase' start_bits is always relative to a 32-bit data type. */
  374. p = makelongparameter (allWords, 32 - (start_bits + 4),
  375. 32 - start_bits);
  376. a->r = p.val;
  377. p = makelongparameter (allWords, 32 - start_bits,
  378. inst_bit_size);
  379. }
  380. if ((p.nbits == 4) && cst4flag)
  381. {
  382. if (instruction->flags & DISPUW4)
  383. p.val *= 2;
  384. else if (instruction->flags & DISPUD4)
  385. p.val *= 4;
  386. }
  387. a->constant = p.val;
  388. break;
  389. case arg_c:
  390. p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
  391. inst_bit_size - start_bits);
  392. a->constant = p.val;
  393. break;
  394. default:
  395. break;
  396. }
  397. }
  398. /* Print a single argument. */
  399. static void
  400. print_arg (argument *a, bfd_vma memaddr, struct disassemble_info *info)
  401. {
  402. ULONGLONG longdisp, mask;
  403. int sign_flag = 0;
  404. int relative = 0;
  405. bfd_vma number;
  406. int op_index = 0;
  407. char string[200];
  408. PTR stream = info->stream;
  409. fprintf_ftype func = info->fprintf_func;
  410. switch (a->type)
  411. {
  412. case arg_copr:
  413. func (stream, "%s", getcopregname (a->cr, CRX_C_REGTYPE));
  414. break;
  415. case arg_copsr:
  416. func (stream, "%s", getcopregname (a->cr, CRX_CS_REGTYPE));
  417. break;
  418. case arg_r:
  419. if (IS_INSN_MNEMONIC ("mtpr") || IS_INSN_MNEMONIC ("mfpr"))
  420. func (stream, "%s", getprocregname (a->r));
  421. else
  422. func (stream, "%s", getregname (a->r));
  423. break;
  424. case arg_ic:
  425. if (IS_INSN_MNEMONIC ("excp"))
  426. func (stream, "%s", gettrapstring (a->constant));
  427. else if (IS_INSN_MNEMONIC ("cinv"))
  428. func (stream, "%s", getcinvstring (a->constant));
  429. else if (INST_HAS_REG_LIST)
  430. {
  431. REG_ARG_TYPE reg_arg_type = IS_INSN_TYPE (COP_REG_INS) ?
  432. COP_ARG : IS_INSN_TYPE (COPS_REG_INS) ?
  433. COPS_ARG : (instruction->flags & USER_REG) ?
  434. USER_REG_ARG : REG_ARG;
  435. if ((reg_arg_type == COP_ARG) || (reg_arg_type == COPS_ARG))
  436. {
  437. /* Check for proper argument number. */
  438. if (processing_argument_number == 2)
  439. {
  440. getregliststring (a->constant, string, reg_arg_type);
  441. func (stream, "%s", string);
  442. }
  443. else
  444. func (stream, "$0x%lx", a->constant & 0xffffffff);
  445. }
  446. else
  447. {
  448. getregliststring (a->constant, string, reg_arg_type);
  449. func (stream, "%s", string);
  450. }
  451. }
  452. else
  453. func (stream, "$0x%lx", a->constant & 0xffffffff);
  454. break;
  455. case arg_idxr:
  456. func (stream, "0x%lx(%s,%s,%d)", a->constant & 0xffffffff,
  457. getregname (a->r), getregname (a->i_r), powerof2 (a->scale));
  458. break;
  459. case arg_rbase:
  460. func (stream, "(%s)", getregname (a->r));
  461. break;
  462. case arg_cr:
  463. func (stream, "0x%lx(%s)", a->constant & 0xffffffff, getregname (a->r));
  464. if (IS_INSN_TYPE (LD_STOR_INS_INC))
  465. func (stream, "+");
  466. break;
  467. case arg_c:
  468. /* Removed the *2 part as because implicit zeros are no more required.
  469. Have to fix this as this needs a bit of extension in terms of branchins.
  470. Have to add support for cmp and branch instructions. */
  471. if (IS_INSN_TYPE (BRANCH_INS) || IS_INSN_MNEMONIC ("bal")
  472. || IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (DCR_BRANCH_INS)
  473. || IS_INSN_TYPE (COP_BRANCH_INS))
  474. {
  475. relative = 1;
  476. longdisp = a->constant;
  477. longdisp <<= 1;
  478. switch (a->size)
  479. {
  480. case 8:
  481. case 16:
  482. case 24:
  483. case 32:
  484. mask = ((LONGLONG) 1 << a->size) - 1;
  485. if (longdisp & ((ULONGLONG) 1 << a->size))
  486. {
  487. sign_flag = 1;
  488. longdisp = ~(longdisp) + 1;
  489. }
  490. a->constant = (unsigned long int) (longdisp & mask);
  491. break;
  492. default:
  493. func (stream,
  494. "Wrong offset used in branch/bal instruction");
  495. break;
  496. }
  497. }
  498. /* For branch Neq instruction it is 2*offset + 2. */
  499. else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
  500. a->constant = 2 * a->constant + 2;
  501. else if (IS_INSN_TYPE (LD_STOR_INS_INC)
  502. || IS_INSN_TYPE (LD_STOR_INS)
  503. || IS_INSN_TYPE (STOR_IMM_INS)
  504. || IS_INSN_TYPE (CSTBIT_INS))
  505. {
  506. op_index = instruction->flags & REVERSE_MATCH ? 0 : 1;
  507. if (instruction->operands[op_index].op_type == abs16)
  508. a->constant |= 0xFFFF0000;
  509. }
  510. func (stream, "%s", "0x");
  511. number = (relative ? memaddr : 0)
  512. + (sign_flag ? -a->constant : a->constant);
  513. (*info->print_address_func) (number, info);
  514. break;
  515. default:
  516. break;
  517. }
  518. }
  519. /* Print all the arguments of CURRINSN instruction. */
  520. static void
  521. print_arguments (ins *currentInsn, bfd_vma memaddr, struct disassemble_info *info)
  522. {
  523. int i;
  524. for (i = 0; i < currentInsn->nargs; i++)
  525. {
  526. processing_argument_number = i;
  527. print_arg (&currentInsn->arg[i], memaddr, info);
  528. if (i != currentInsn->nargs - 1)
  529. info->fprintf_func (info->stream, ", ");
  530. }
  531. }
  532. /* Build the instruction's arguments. */
  533. static void
  534. make_instruction (void)
  535. {
  536. int i;
  537. unsigned int shift;
  538. for (i = 0; i < currInsn.nargs; i++)
  539. {
  540. argument a;
  541. memset (&a, 0, sizeof (a));
  542. a.type = getargtype (instruction->operands[i].op_type);
  543. if (instruction->operands[i].op_type == cst4
  544. || instruction->operands[i].op_type == rbase_dispu4)
  545. cst4flag = 1;
  546. a.size = getbits (instruction->operands[i].op_type);
  547. shift = instruction->operands[i].shift;
  548. make_argument (&a, shift);
  549. currInsn.arg[i] = a;
  550. }
  551. /* Calculate instruction size (in bytes). */
  552. currInsn.size = instruction->size + (size_changed ? 1 : 0);
  553. /* Now in bits. */
  554. currInsn.size *= 2;
  555. }
  556. /* Retrieve a single word from a given memory address. */
  557. static wordU
  558. get_word_at_PC (bfd_vma memaddr, struct disassemble_info *info)
  559. {
  560. bfd_byte buffer[4];
  561. int status;
  562. wordU insn = 0;
  563. status = info->read_memory_func (memaddr, buffer, 2, info);
  564. if (status == 0)
  565. insn = (wordU) bfd_getl16 (buffer);
  566. return insn;
  567. }
  568. /* Retrieve multiple words (3) from a given memory address. */
  569. static void
  570. get_words_at_PC (bfd_vma memaddr, struct disassemble_info *info)
  571. {
  572. int i;
  573. bfd_vma mem;
  574. for (i = 0, mem = memaddr; i < 3; i++, mem += 2)
  575. words[i] = get_word_at_PC (mem, info);
  576. allWords =
  577. ((ULONGLONG) words[0] << 32) + ((unsigned long) words[1] << 16) + words[2];
  578. }
  579. /* Prints the instruction by calling print_arguments after proper matching. */
  580. int
  581. print_insn_crx (bfd_vma memaddr, struct disassemble_info *info)
  582. {
  583. int is_decoded; /* Nonzero means instruction has a match. */
  584. /* Initialize global variables. */
  585. cst4flag = 0;
  586. size_changed = 0;
  587. /* Retrieve the encoding from current memory location. */
  588. get_words_at_PC (memaddr, info);
  589. /* Find a matching opcode in table. */
  590. is_decoded = match_opcode ();
  591. /* If found, print the instruction's mnemonic and arguments. */
  592. if (is_decoded > 0 && (words[0] != 0 || words[1] != 0))
  593. {
  594. info->fprintf_func (info->stream, "%s", instruction->mnemonic);
  595. if ((currInsn.nargs = get_number_of_operands ()) != 0)
  596. info->fprintf_func (info->stream, "\t");
  597. make_instruction ();
  598. print_arguments (&currInsn, memaddr, info);
  599. return currInsn.size;
  600. }
  601. /* No match found. */
  602. info->fprintf_func (info->stream,"%s ",ILLEGAL);
  603. return 2;
  604. }