d10v-dis.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Disassemble D10V instructions.
  2. Copyright (C) 1996-2022 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/d10v.h"
  19. #include "disassemble.h"
  20. /* The PC wraps at 18 bits, except for the segment number,
  21. so use this mask to keep the parts we want. */
  22. #define PC_MASK 0x0303FFFF
  23. static void
  24. print_operand (struct d10v_operand *oper,
  25. unsigned long insn,
  26. struct d10v_opcode *op,
  27. bfd_vma memaddr,
  28. struct disassemble_info *info)
  29. {
  30. int num, shift;
  31. if (oper->flags == OPERAND_ATMINUS)
  32. {
  33. (*info->fprintf_func) (info->stream, "@-");
  34. return;
  35. }
  36. if (oper->flags == OPERAND_MINUS)
  37. {
  38. (*info->fprintf_func) (info->stream, "-");
  39. return;
  40. }
  41. if (oper->flags == OPERAND_PLUS)
  42. {
  43. (*info->fprintf_func) (info->stream, "+");
  44. return;
  45. }
  46. if (oper->flags == OPERAND_ATSIGN)
  47. {
  48. (*info->fprintf_func) (info->stream, "@");
  49. return;
  50. }
  51. if (oper->flags == OPERAND_ATPAR)
  52. {
  53. (*info->fprintf_func) (info->stream, "@(");
  54. return;
  55. }
  56. shift = oper->shift;
  57. /* The LONG_L format shifts registers over by 15. */
  58. if (op->format == LONG_L && (oper->flags & OPERAND_REG))
  59. shift += 15;
  60. num = (insn >> shift) & (0x7FFFFFFF >> (31 - oper->bits));
  61. if (oper->flags & OPERAND_REG)
  62. {
  63. int i;
  64. int match = 0;
  65. num += (oper->flags
  66. & (OPERAND_GPR | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL));
  67. if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
  68. num += num ? OPERAND_ACC1 : OPERAND_ACC0;
  69. for (i = 0; i < d10v_reg_name_cnt (); i++)
  70. {
  71. if (num == (d10v_predefined_registers[i].value & ~ OPERAND_SP))
  72. {
  73. if (d10v_predefined_registers[i].pname)
  74. (*info->fprintf_func) (info->stream, "%s",
  75. d10v_predefined_registers[i].pname);
  76. else
  77. (*info->fprintf_func) (info->stream, "%s",
  78. d10v_predefined_registers[i].name);
  79. match = 1;
  80. break;
  81. }
  82. }
  83. if (match == 0)
  84. {
  85. /* This would only get executed if a register was not in the
  86. register table. */
  87. if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
  88. (*info->fprintf_func) (info->stream, "a");
  89. else if (oper->flags & OPERAND_CONTROL)
  90. (*info->fprintf_func) (info->stream, "cr");
  91. else if (oper->flags & OPERAND_REG)
  92. (*info->fprintf_func) (info->stream, "r");
  93. (*info->fprintf_func) (info->stream, "%d", num & REGISTER_MASK);
  94. }
  95. }
  96. else
  97. {
  98. /* Addresses are right-shifted by 2. */
  99. if (oper->flags & OPERAND_ADDR)
  100. {
  101. long max;
  102. int neg = 0;
  103. max = (1 << (oper->bits - 1));
  104. if (num & max)
  105. {
  106. num = -num & ((1 << oper->bits) - 1);
  107. neg = 1;
  108. }
  109. num = num << 2;
  110. if (info->flags & INSN_HAS_RELOC)
  111. (*info->print_address_func) (num & PC_MASK, info);
  112. else
  113. {
  114. if (neg)
  115. (*info->print_address_func) ((memaddr - num) & PC_MASK, info);
  116. else
  117. (*info->print_address_func) ((memaddr + num) & PC_MASK, info);
  118. }
  119. }
  120. else
  121. {
  122. if (oper->flags & OPERAND_SIGNED)
  123. {
  124. int max = (1 << (oper->bits - 1));
  125. if (num & max)
  126. {
  127. num = -num & ((1 << oper->bits) - 1);
  128. (*info->fprintf_func) (info->stream, "-");
  129. }
  130. }
  131. (*info->fprintf_func) (info->stream, "0x%x", num);
  132. }
  133. }
  134. }
  135. static void
  136. dis_long (unsigned long insn,
  137. bfd_vma memaddr,
  138. struct disassemble_info *info)
  139. {
  140. int i;
  141. struct d10v_opcode *op = (struct d10v_opcode *) d10v_opcodes;
  142. struct d10v_operand *oper;
  143. int need_paren = 0;
  144. int match = 0;
  145. while (op->name)
  146. {
  147. if ((op->format & LONG_OPCODE)
  148. && ((op->mask & insn) == (unsigned long) op->opcode))
  149. {
  150. match = 1;
  151. (*info->fprintf_func) (info->stream, "%s\t", op->name);
  152. for (i = 0; op->operands[i]; i++)
  153. {
  154. oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
  155. if (oper->flags == OPERAND_ATPAR)
  156. need_paren = 1;
  157. print_operand (oper, insn, op, memaddr, info);
  158. if (op->operands[i + 1] && oper->bits
  159. && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
  160. && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
  161. (*info->fprintf_func) (info->stream, ", ");
  162. }
  163. break;
  164. }
  165. op++;
  166. }
  167. if (!match)
  168. (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
  169. if (need_paren)
  170. (*info->fprintf_func) (info->stream, ")");
  171. }
  172. static void
  173. dis_2_short (unsigned long insn,
  174. bfd_vma memaddr,
  175. struct disassemble_info *info,
  176. int order)
  177. {
  178. int i, j;
  179. unsigned int ins[2];
  180. struct d10v_opcode *op;
  181. int match, num_match = 0;
  182. struct d10v_operand *oper;
  183. int need_paren = 0;
  184. ins[0] = (insn & 0x3FFFFFFF) >> 15;
  185. ins[1] = insn & 0x00007FFF;
  186. for (j = 0; j < 2; j++)
  187. {
  188. op = (struct d10v_opcode *) d10v_opcodes;
  189. match = 0;
  190. while (op->name)
  191. {
  192. if ((op->format & SHORT_OPCODE)
  193. && ((((unsigned int) op->mask) & ins[j])
  194. == (unsigned int) op->opcode))
  195. {
  196. (*info->fprintf_func) (info->stream, "%s\t", op->name);
  197. for (i = 0; op->operands[i]; i++)
  198. {
  199. oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
  200. if (oper->flags == OPERAND_ATPAR)
  201. need_paren = 1;
  202. print_operand (oper, ins[j], op, memaddr, info);
  203. if (op->operands[i + 1] && oper->bits
  204. && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
  205. && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
  206. (*info->fprintf_func) (info->stream, ", ");
  207. }
  208. match = 1;
  209. num_match++;
  210. break;
  211. }
  212. op++;
  213. }
  214. if (!match)
  215. (*info->fprintf_func) (info->stream, "unknown");
  216. switch (order)
  217. {
  218. case 0:
  219. (*info->fprintf_func) (info->stream, "\t->\t");
  220. order = -1;
  221. break;
  222. case 1:
  223. (*info->fprintf_func) (info->stream, "\t<-\t");
  224. order = -1;
  225. break;
  226. case 2:
  227. (*info->fprintf_func) (info->stream, "\t||\t");
  228. order = -1;
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. if (num_match == 0)
  235. (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
  236. if (need_paren)
  237. (*info->fprintf_func) (info->stream, ")");
  238. }
  239. int
  240. print_insn_d10v (bfd_vma memaddr, struct disassemble_info *info)
  241. {
  242. int status;
  243. bfd_byte buffer[4];
  244. unsigned long insn;
  245. status = (*info->read_memory_func) (memaddr, buffer, 4, info);
  246. if (status != 0)
  247. {
  248. (*info->memory_error_func) (status, memaddr, info);
  249. return -1;
  250. }
  251. insn = bfd_getb32 (buffer);
  252. status = insn & FM11;
  253. switch (status)
  254. {
  255. case 0:
  256. dis_2_short (insn, memaddr, info, 2);
  257. break;
  258. case FM01:
  259. dis_2_short (insn, memaddr, info, 0);
  260. break;
  261. case FM10:
  262. dis_2_short (insn, memaddr, info, 1);
  263. break;
  264. case FM11:
  265. dis_long (insn, memaddr, info);
  266. break;
  267. }
  268. return 4;
  269. }