tic80-dis.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Print TI TMS320C80 (MVP) instructions
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU opcodes library.
  4. This library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. It is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include <stdio.h>
  18. #include "opcode/tic80.h"
  19. #include "dis-asm.h"
  20. static int length;
  21. /* Print an integer operand. Try to be somewhat smart about the
  22. format by assuming that small positive or negative integers are
  23. probably loop increment values, structure offsets, or similar
  24. values that are more meaningful printed as signed decimal values.
  25. Larger numbers are probably better printed as hex values. */
  26. static void
  27. print_operand_integer (struct disassemble_info *info, long value)
  28. {
  29. if ((value > 9999 || value < -9999))
  30. (*info->fprintf_func) (info->stream, "%#lx", value);
  31. else
  32. (*info->fprintf_func) (info->stream, "%ld", value);
  33. }
  34. /* FIXME: depends upon sizeof (long) == sizeof (float) and
  35. also upon host floating point format matching target
  36. floating point format. */
  37. static void
  38. print_operand_float (struct disassemble_info *info, long value)
  39. {
  40. union { float f; long l; } fval;
  41. fval.l = value;
  42. (*info->fprintf_func) (info->stream, "%g", fval.f);
  43. }
  44. static void
  45. print_operand_control_register (struct disassemble_info *info, long value)
  46. {
  47. const char *tmp;
  48. tmp = tic80_value_to_symbol (value, TIC80_OPERAND_CR);
  49. if (tmp != NULL)
  50. (*info->fprintf_func) (info->stream, "%s", tmp);
  51. else
  52. (*info->fprintf_func) (info->stream, "%#lx", value);
  53. }
  54. static void
  55. print_operand_condition_code (struct disassemble_info *info, long value)
  56. {
  57. const char *tmp;
  58. tmp = tic80_value_to_symbol (value, TIC80_OPERAND_CC);
  59. if (tmp != NULL)
  60. (*info->fprintf_func) (info->stream, "%s", tmp);
  61. else
  62. (*info->fprintf_func) (info->stream, "%ld", value);
  63. }
  64. static void
  65. print_operand_bitnum (struct disassemble_info *info, long value)
  66. {
  67. int bitnum;
  68. const char *tmp;
  69. bitnum = ~value & 0x1F;
  70. tmp = tic80_value_to_symbol (bitnum, TIC80_OPERAND_BITNUM);
  71. if (tmp != NULL)
  72. (*info->fprintf_func) (info->stream, "%s", tmp);
  73. else
  74. (*info->fprintf_func) (info->stream, "%d", bitnum);
  75. }
  76. /* Print the operand as directed by the flags. */
  77. #define M_SI(insn,op) ((((op)->flags & TIC80_OPERAND_M_SI) != 0) && ((insn) & (1 << 17)))
  78. #define M_LI(insn,op) ((((op)->flags & TIC80_OPERAND_M_LI) != 0) && ((insn) & (1 << 15)))
  79. #define R_SCALED(insn,op) ((((op)->flags & TIC80_OPERAND_SCALED) != 0) && ((insn) & (1 << 11)))
  80. static void
  81. print_operand (struct disassemble_info *info,
  82. long value,
  83. unsigned long insn,
  84. const struct tic80_operand *operand,
  85. bfd_vma memaddr)
  86. {
  87. if ((operand->flags & TIC80_OPERAND_GPR) != 0)
  88. {
  89. (*info->fprintf_func) (info->stream, "r%ld", value);
  90. if (M_SI (insn, operand) || M_LI (insn, operand))
  91. {
  92. (*info->fprintf_func) (info->stream, ":m");
  93. }
  94. }
  95. else if ((operand->flags & TIC80_OPERAND_FPA) != 0)
  96. (*info->fprintf_func) (info->stream, "a%ld", value);
  97. else if ((operand->flags & TIC80_OPERAND_PCREL) != 0)
  98. (*info->print_address_func) (memaddr + 4 * value, info);
  99. else if ((operand->flags & TIC80_OPERAND_BASEREL) != 0)
  100. (*info->print_address_func) (value, info);
  101. else if ((operand->flags & TIC80_OPERAND_BITNUM) != 0)
  102. print_operand_bitnum (info, value);
  103. else if ((operand->flags & TIC80_OPERAND_CC) != 0)
  104. print_operand_condition_code (info, value);
  105. else if ((operand->flags & TIC80_OPERAND_CR) != 0)
  106. print_operand_control_register (info, value);
  107. else if ((operand->flags & TIC80_OPERAND_FLOAT) != 0)
  108. print_operand_float (info, value);
  109. else if ((operand->flags & TIC80_OPERAND_BITFIELD))
  110. (*info->fprintf_func) (info->stream, "%#lx", value);
  111. else
  112. print_operand_integer (info, value);
  113. /* If this is a scaled operand, then print the modifier. */
  114. if (R_SCALED (insn, operand))
  115. (*info->fprintf_func) (info->stream, ":s");
  116. }
  117. /* Get the next 32 bit word from the instruction stream and convert it
  118. into internal format in the unsigned long INSN, for which we are
  119. passed the address. Return 0 on success, -1 on error. */
  120. static int
  121. fill_instruction (struct disassemble_info *info,
  122. bfd_vma memaddr,
  123. unsigned long *insnp)
  124. {
  125. bfd_byte buffer[4];
  126. int status;
  127. /* Get the bits for the next 32 bit word and put in buffer. */
  128. status = (*info->read_memory_func) (memaddr + length, buffer, 4, info);
  129. if (status != 0)
  130. {
  131. (*info->memory_error_func) (status, memaddr, info);
  132. return -1;
  133. }
  134. /* Read was successful, so increment count of bytes read and convert
  135. the bits into internal format. */
  136. length += 4;
  137. if (info->endian == BFD_ENDIAN_LITTLE)
  138. *insnp = bfd_getl32 (buffer);
  139. else if (info->endian == BFD_ENDIAN_BIG)
  140. *insnp = bfd_getb32 (buffer);
  141. else
  142. /* FIXME: Should probably just default to one or the other. */
  143. abort ();
  144. return 0;
  145. }
  146. /* We have chosen an opcode table entry. */
  147. static int
  148. print_one_instruction (struct disassemble_info *info,
  149. bfd_vma memaddr,
  150. unsigned long insn,
  151. const struct tic80_opcode *opcode)
  152. {
  153. const struct tic80_operand *operand;
  154. long value;
  155. int status;
  156. const unsigned char *opindex;
  157. int close_paren;
  158. (*info->fprintf_func) (info->stream, "%-10s", opcode->name);
  159. for (opindex = opcode->operands; *opindex != 0; opindex++)
  160. {
  161. operand = tic80_operands + *opindex;
  162. /* Extract the value from the instruction. */
  163. if (operand->extract)
  164. value = (*operand->extract) (insn, NULL);
  165. else if (operand->bits == 32)
  166. {
  167. status = fill_instruction (info, memaddr, (unsigned long *) &value);
  168. if (status == -1)
  169. return status;
  170. }
  171. else
  172. {
  173. value = (insn >> operand->shift) & ((1 << operand->bits) - 1);
  174. if ((operand->flags & TIC80_OPERAND_SIGNED) != 0
  175. && (value & (1 << (operand->bits - 1))) != 0)
  176. value -= 1 << operand->bits;
  177. }
  178. /* If this operand is enclosed in parenthesis, then print
  179. the open paren, otherwise just print the regular comma
  180. separator, except for the first operand. */
  181. if ((operand->flags & TIC80_OPERAND_PARENS) == 0)
  182. {
  183. close_paren = 0;
  184. if (opindex != opcode->operands)
  185. (*info->fprintf_func) (info->stream, ",");
  186. }
  187. else
  188. {
  189. close_paren = 1;
  190. (*info->fprintf_func) (info->stream, "(");
  191. }
  192. print_operand (info, value, insn, operand, memaddr);
  193. /* If we printed an open paren before printing this operand, close
  194. it now. The flag gets reset on each loop. */
  195. if (close_paren)
  196. (*info->fprintf_func) (info->stream, ")");
  197. }
  198. return length;
  199. }
  200. /* There are no specific bits that tell us for certain whether a vector
  201. instruction opcode contains one or two instructions. However since
  202. a destination register of r0 is illegal, we can check for nonzero
  203. values in both destination register fields. Only opcodes that have
  204. two valid instructions will have non-zero in both. */
  205. #define TWO_INSN(insn) ((((insn) & (0x1F << 27)) != 0) && (((insn) & (0x1F << 22)) != 0))
  206. static int
  207. print_instruction (struct disassemble_info *info,
  208. bfd_vma memaddr,
  209. unsigned long insn,
  210. const struct tic80_opcode *vec_opcode)
  211. {
  212. const struct tic80_opcode *opcode;
  213. const struct tic80_opcode *opcode_end;
  214. /* Find the first opcode match in the opcodes table. For vector
  215. opcodes (vec_opcode != NULL) find the first match that is not the
  216. previously found match. FIXME: there should be faster ways to
  217. search (hash table or binary search), but don't worry too much
  218. about it until other TIc80 support is finished. */
  219. opcode_end = tic80_opcodes + tic80_num_opcodes;
  220. for (opcode = tic80_opcodes; opcode < opcode_end; opcode++)
  221. {
  222. if ((insn & opcode->mask) == opcode->opcode &&
  223. opcode != vec_opcode)
  224. break;
  225. }
  226. if (opcode == opcode_end)
  227. {
  228. /* No match found, just print the bits as a .word directive. */
  229. (*info->fprintf_func) (info->stream, ".word %#08lx", insn);
  230. }
  231. else
  232. {
  233. /* Match found, decode the instruction. */
  234. length = print_one_instruction (info, memaddr, insn, opcode);
  235. if (opcode->flags & TIC80_VECTOR && vec_opcode == NULL && TWO_INSN (insn))
  236. {
  237. /* There is another instruction to print from the same opcode.
  238. Print the separator and then find and print the other
  239. instruction. */
  240. (*info->fprintf_func) (info->stream, " || ");
  241. length = print_instruction (info, memaddr, insn, opcode);
  242. }
  243. }
  244. return length;
  245. }
  246. int
  247. print_insn_tic80 (bfd_vma memaddr, struct disassemble_info *info)
  248. {
  249. unsigned long insn;
  250. int status;
  251. length = 0;
  252. info->bytes_per_line = 8;
  253. status = fill_instruction (info, memaddr, &insn);
  254. if (status != -1)
  255. status = print_instruction (info, memaddr, insn, NULL);
  256. return status;
  257. }