m32r.opc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* M32R opcode support. -*- C -*-
  2. Copyright 1998, 1999, 2000, 2001, 2004, 2005, 2007, 2009
  3. Free Software Foundation, Inc.
  4. Contributed by Red Hat Inc; developed under contract from
  5. Mitsubishi Electric Corporation.
  6. This file is part of the GNU Binutils.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  18. MA 02110-1301, USA. */
  19. /* This file is an addendum to m32r.cpu. Heavy use of C code isn't
  20. appropriate in .cpu files, so it resides here. This especially applies
  21. to assembly/disassembly where parsing/printing can be quite involved.
  22. Such things aren't really part of the specification of the cpu, per se,
  23. so .cpu files provide the general framework and .opc files handle the
  24. nitty-gritty details as necessary.
  25. Each section is delimited with start and end markers.
  26. <arch>-opc.h additions use: "-- opc.h"
  27. <arch>-opc.c additions use: "-- opc.c"
  28. <arch>-asm.c additions use: "-- asm.c"
  29. <arch>-dis.c additions use: "-- dis.c"
  30. <arch>-ibd.h additions use: "-- ibd.h" */
  31. /* -- opc.h */
  32. #undef CGEN_DIS_HASH_SIZE
  33. #define CGEN_DIS_HASH_SIZE 256
  34. #undef CGEN_DIS_HASH
  35. #if 0
  36. #define X(b) (((unsigned char *) (b))[0] & 0xf0)
  37. #define CGEN_DIS_HASH(buffer, value) \
  38. (X (buffer) | \
  39. (X (buffer) == 0x40 || X (buffer) == 0xe0 || X (buffer) == 0x60 || X (buffer) == 0x50 ? 0 \
  40. : X (buffer) == 0x70 || X (buffer) == 0xf0 ? (((unsigned char *) (buffer))[0] & 0xf) \
  41. : X (buffer) == 0x30 ? ((((unsigned char *) (buffer))[1] & 0x70) >> 4) \
  42. : ((((unsigned char *) (buffer))[1] & 0xf0) >> 4)))
  43. #else
  44. #define CGEN_DIS_HASH(buffer, value) m32r_cgen_dis_hash (buffer, value)
  45. extern unsigned int m32r_cgen_dis_hash (const char *, CGEN_INSN_INT);
  46. #endif
  47. /* -- */
  48. /* -- opc.c */
  49. unsigned int
  50. m32r_cgen_dis_hash (const char * buf ATTRIBUTE_UNUSED, CGEN_INSN_INT value)
  51. {
  52. unsigned int x;
  53. if (value & 0xffff0000) /* 32bit instructions. */
  54. value = (value >> 16) & 0xffff;
  55. x = (value >> 8) & 0xf0;
  56. if (x == 0x40 || x == 0xe0 || x == 0x60 || x == 0x50)
  57. return x;
  58. if (x == 0x70 || x == 0xf0)
  59. return x | ((value >> 8) & 0x0f);
  60. if (x == 0x30)
  61. return x | ((value & 0x70) >> 4);
  62. else
  63. return x | ((value & 0xf0) >> 4);
  64. }
  65. /* -- */
  66. /* -- asm.c */
  67. static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
  68. /* Handle '#' prefixes (i.e. skip over them). */
  69. static const char *
  70. parse_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  71. const char **strp,
  72. int opindex ATTRIBUTE_UNUSED,
  73. long *valuep ATTRIBUTE_UNUSED)
  74. {
  75. if (**strp == '#')
  76. ++*strp;
  77. return NULL;
  78. }
  79. /* Handle shigh(), high(). */
  80. static const char *
  81. parse_hi16 (CGEN_CPU_DESC cd,
  82. const char **strp,
  83. int opindex,
  84. unsigned long *valuep)
  85. {
  86. const char *errmsg;
  87. enum cgen_parse_operand_result result_type;
  88. bfd_vma value;
  89. if (**strp == '#')
  90. ++*strp;
  91. if (strncasecmp (*strp, "high(", 5) == 0)
  92. {
  93. *strp += 5;
  94. errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_ULO,
  95. & result_type, & value);
  96. if (**strp != ')')
  97. return MISSING_CLOSING_PARENTHESIS;
  98. ++*strp;
  99. if (errmsg == NULL
  100. && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
  101. {
  102. value >>= 16;
  103. value &= 0xffff;
  104. }
  105. *valuep = value;
  106. return errmsg;
  107. }
  108. else if (strncasecmp (*strp, "shigh(", 6) == 0)
  109. {
  110. *strp += 6;
  111. errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_SLO,
  112. & result_type, & value);
  113. if (**strp != ')')
  114. return MISSING_CLOSING_PARENTHESIS;
  115. ++*strp;
  116. if (errmsg == NULL
  117. && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
  118. {
  119. value += 0x8000;
  120. value >>= 16;
  121. value &= 0xffff;
  122. }
  123. *valuep = value;
  124. return errmsg;
  125. }
  126. return cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
  127. }
  128. /* Handle low() in a signed context. Also handle sda().
  129. The signedness of the value doesn't matter to low(), but this also
  130. handles the case where low() isn't present. */
  131. static const char *
  132. parse_slo16 (CGEN_CPU_DESC cd,
  133. const char ** strp,
  134. int opindex,
  135. long * valuep)
  136. {
  137. const char *errmsg;
  138. enum cgen_parse_operand_result result_type;
  139. bfd_vma value;
  140. if (**strp == '#')
  141. ++*strp;
  142. if (strncasecmp (*strp, "low(", 4) == 0)
  143. {
  144. *strp += 4;
  145. errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16,
  146. & result_type, & value);
  147. if (**strp != ')')
  148. return MISSING_CLOSING_PARENTHESIS;
  149. ++*strp;
  150. if (errmsg == NULL
  151. && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
  152. value = ((value & 0xffff) ^ 0x8000) - 0x8000;
  153. *valuep = value;
  154. return errmsg;
  155. }
  156. if (strncasecmp (*strp, "sda(", 4) == 0)
  157. {
  158. *strp += 4;
  159. errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_SDA16,
  160. NULL, & value);
  161. if (**strp != ')')
  162. return MISSING_CLOSING_PARENTHESIS;
  163. ++*strp;
  164. *valuep = value;
  165. return errmsg;
  166. }
  167. return cgen_parse_signed_integer (cd, strp, opindex, valuep);
  168. }
  169. /* Handle low() in an unsigned context.
  170. The signedness of the value doesn't matter to low(), but this also
  171. handles the case where low() isn't present. */
  172. static const char *
  173. parse_ulo16 (CGEN_CPU_DESC cd,
  174. const char **strp,
  175. int opindex,
  176. unsigned long *valuep)
  177. {
  178. const char *errmsg;
  179. enum cgen_parse_operand_result result_type;
  180. bfd_vma value;
  181. if (**strp == '#')
  182. ++*strp;
  183. if (strncasecmp (*strp, "low(", 4) == 0)
  184. {
  185. *strp += 4;
  186. errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16,
  187. & result_type, & value);
  188. if (**strp != ')')
  189. return MISSING_CLOSING_PARENTHESIS;
  190. ++*strp;
  191. if (errmsg == NULL
  192. && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
  193. value &= 0xffff;
  194. *valuep = value;
  195. return errmsg;
  196. }
  197. return cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
  198. }
  199. /* -- */
  200. /* -- dis.c */
  201. /* Print signed operands with '#' prefixes. */
  202. static void
  203. print_signed_with_hash_prefix (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  204. void * dis_info,
  205. long value,
  206. unsigned int attrs ATTRIBUTE_UNUSED,
  207. bfd_vma pc ATTRIBUTE_UNUSED,
  208. int length ATTRIBUTE_UNUSED)
  209. {
  210. disassemble_info *info = (disassemble_info *) dis_info;
  211. (*info->fprintf_func) (info->stream, "#");
  212. (*info->fprintf_func) (info->stream, "%ld", value);
  213. }
  214. /* Print unsigned operands with '#' prefixes. */
  215. static void
  216. print_unsigned_with_hash_prefix (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  217. void * dis_info,
  218. long value,
  219. unsigned int attrs ATTRIBUTE_UNUSED,
  220. bfd_vma pc ATTRIBUTE_UNUSED,
  221. int length ATTRIBUTE_UNUSED)
  222. {
  223. disassemble_info *info = (disassemble_info *) dis_info;
  224. (*info->fprintf_func) (info->stream, "#");
  225. (*info->fprintf_func) (info->stream, "0x%lx", value);
  226. }
  227. /* Handle '#' prefixes as operands. */
  228. static void
  229. print_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  230. void * dis_info,
  231. long value ATTRIBUTE_UNUSED,
  232. unsigned int attrs ATTRIBUTE_UNUSED,
  233. bfd_vma pc ATTRIBUTE_UNUSED,
  234. int length ATTRIBUTE_UNUSED)
  235. {
  236. disassemble_info *info = (disassemble_info *) dis_info;
  237. (*info->fprintf_func) (info->stream, "#");
  238. }
  239. #undef CGEN_PRINT_INSN
  240. #define CGEN_PRINT_INSN my_print_insn
  241. static int
  242. my_print_insn (CGEN_CPU_DESC cd,
  243. bfd_vma pc,
  244. disassemble_info *info)
  245. {
  246. bfd_byte buffer[CGEN_MAX_INSN_SIZE];
  247. bfd_byte *buf = buffer;
  248. int status;
  249. int buflen = (pc & 3) == 0 ? 4 : 2;
  250. int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG;
  251. bfd_byte *x;
  252. /* Read the base part of the insn. */
  253. status = (*info->read_memory_func) (pc - ((!big_p && (pc & 3) != 0) ? 2 : 0),
  254. buf, buflen, info);
  255. if (status != 0)
  256. {
  257. (*info->memory_error_func) (status, pc, info);
  258. return -1;
  259. }
  260. /* 32 bit insn? */
  261. x = (big_p ? &buf[0] : &buf[3]);
  262. if ((pc & 3) == 0 && (*x & 0x80) != 0)
  263. return print_insn (cd, pc, info, buf, buflen);
  264. /* Print the first insn. */
  265. if ((pc & 3) == 0)
  266. {
  267. buf += (big_p ? 0 : 2);
  268. if (print_insn (cd, pc, info, buf, 2) == 0)
  269. (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
  270. buf += (big_p ? 2 : -2);
  271. }
  272. x = (big_p ? &buf[0] : &buf[1]);
  273. if (*x & 0x80)
  274. {
  275. /* Parallel. */
  276. (*info->fprintf_func) (info->stream, " || ");
  277. *x &= 0x7f;
  278. }
  279. else
  280. (*info->fprintf_func) (info->stream, " -> ");
  281. /* The "& 3" is to pass a consistent address.
  282. Parallel insns arguably both begin on the word boundary.
  283. Also, branch insns are calculated relative to the word boundary. */
  284. if (print_insn (cd, pc & ~ (bfd_vma) 3, info, buf, 2) == 0)
  285. (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
  286. return (pc & 3) ? 2 : 4;
  287. }
  288. /* -- */