epiphany.opc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* Adapteva epiphany opcode support. -*- C -*-
  2. Copyright 2009, 2011 Free Software Foundation, Inc.
  3. Contributed by Embecosm on behalf of Adapteva, Inc.
  4. This file is part of the GNU Binutils and of GDB.
  5. This program 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 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /*
  18. Each section is delimited with start and end markers.
  19. <arch>-opc.h additions use: "-- opc.h"
  20. <arch>-opc.c additions use: "-- opc.c"
  21. <arch>-asm.c additions use: "-- asm.c"
  22. <arch>-dis.c additions use: "-- dis.c"
  23. <arch>-ibd.h additions use: "-- ibd.h". */
  24. /* -- opc.h */
  25. /* enumerate relaxation types for gas. */
  26. typedef enum epiphany_relax_types
  27. {
  28. EPIPHANY_RELAX_NONE=0,
  29. EPIPHANY_RELAX_NEED_RELAXING,
  30. EPIPHANY_RELAX_BRANCH_SHORT, /* Fits into +127..-128 */
  31. EPIPHANY_RELAX_BRANCH_LONG, /* b/bl/b<cond> +-2*16 */
  32. EPIPHANY_RELAX_ARITH_SIMM3, /* add/sub -7..3 */
  33. EPIPHANY_RELAX_ARITH_SIMM11, /* add/sub -2**11-1 .. 2**10-1 */
  34. EPIPHANY_RELAX_MOV_IMM8, /* mov r,imm8 */
  35. EPIPHANY_RELAX_MOV_IMM16, /* mov r,imm16 */
  36. EPIPHANY_RELAX_LDST_IMM3, /* (ldr|str)* r,[r,disp3] */
  37. EPIPHANY_RELAX_LDST_IMM11 /* (ldr|str)* r,[r,disp11] */
  38. } EPIPHANY_RELAX_TYPES;
  39. /* Override disassembly hashing... */
  40. /* Can only depend on instruction having 4 decode bits which gets us to the
  41. major groups of 16/32 instructions. */
  42. #undef CGEN_DIS_HASH_SIZE
  43. #if 1
  44. /* hash code on the 4 LSBs */
  45. #define CGEN_DIS_HASH_SIZE 16
  46. #define CGEN_DIS_HASH(buf, value) ((*buf) & 0xf)
  47. #else
  48. #define CGEN_DIS_HASH_SIZE 1
  49. #define CGEN_DIS_HASH(buf, value) 0
  50. #endif
  51. extern const char * parse_shortregs (CGEN_CPU_DESC cd,
  52. const char ** strp,
  53. CGEN_KEYWORD * keywords,
  54. long * valuep);
  55. extern const char * parse_branch_addr (CGEN_CPU_DESC cd,
  56. const char ** strp,
  57. int opindex,
  58. int opinfo,
  59. enum cgen_parse_operand_result * resultp,
  60. bfd_vma *valuep);
  61. /* Allows reason codes to be output when assembler errors occur. */
  62. #define CGEN_VERBOSE_ASSEMBLER_ERRORS
  63. /* -- opc.c */
  64. /* -- asm.c */
  65. const char *
  66. parse_shortregs (CGEN_CPU_DESC cd,
  67. const char ** strp,
  68. CGEN_KEYWORD * keywords,
  69. long * regno)
  70. {
  71. const char * errmsg;
  72. /* Parse register. */
  73. errmsg = cgen_parse_keyword (cd, strp, keywords, regno);
  74. if (errmsg)
  75. return errmsg;
  76. if (*regno > 7)
  77. errmsg = _("register unavailable for short instructions");
  78. return errmsg;
  79. }
  80. static const char * parse_simm_not_reg (CGEN_CPU_DESC, const char **, int,
  81. long *);
  82. static const char *
  83. parse_uimm_not_reg (CGEN_CPU_DESC cd,
  84. const char ** strp,
  85. int opindex,
  86. unsigned long * valuep)
  87. {
  88. long * svalp = (void *) valuep;
  89. return parse_simm_not_reg (cd, strp, opindex, svalp);
  90. }
  91. /* Handle simm3/simm11/imm3/imm12. */
  92. static const char *
  93. parse_simm_not_reg (CGEN_CPU_DESC cd,
  94. const char ** strp,
  95. int opindex,
  96. long * valuep)
  97. {
  98. const char * errmsg;
  99. int sign = 0;
  100. int bits = 0;
  101. switch (opindex)
  102. {
  103. case EPIPHANY_OPERAND_SIMM3:
  104. sign = 1; bits = 3; break;
  105. case EPIPHANY_OPERAND_SIMM11:
  106. sign = 1; bits = 11; break;
  107. case EPIPHANY_OPERAND_DISP3:
  108. sign = 0; bits = 3; break;
  109. case EPIPHANY_OPERAND_DISP11:
  110. /* Load/store displacement is a sign-magnitude 12 bit value. */
  111. sign = 0; bits = 11; break;
  112. }
  113. /* First try to parse as a register name and reject the operand. */
  114. errmsg = cgen_parse_keyword (cd, strp, & epiphany_cgen_opval_gr_names,valuep);
  115. if (!errmsg)
  116. return _("register name used as immediate value");
  117. errmsg = (sign ? cgen_parse_signed_integer (cd, strp, opindex, valuep)
  118. : cgen_parse_unsigned_integer (cd, strp, opindex,
  119. (unsigned long *) valuep));
  120. if (errmsg)
  121. return errmsg;
  122. if (sign)
  123. errmsg = cgen_validate_signed_integer (*valuep,
  124. -((1L << bits) - 1), (1 << (bits - 1)) - 1);
  125. else
  126. errmsg = cgen_validate_unsigned_integer (*valuep, 0, (1L << bits) - 1);
  127. return errmsg;
  128. }
  129. static const char *
  130. parse_postindex (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  131. const char ** strp,
  132. int opindex ATTRIBUTE_UNUSED,
  133. unsigned long *valuep)
  134. {
  135. if (**strp == '#')
  136. ++*strp; /* Skip leading hashes. */
  137. if (**strp == '-')
  138. {
  139. *valuep = 1;
  140. ++*strp;
  141. }
  142. else if (**strp == '+')
  143. {
  144. *valuep = 0;
  145. ++*strp;
  146. }
  147. else
  148. *valuep = 0;
  149. return NULL;
  150. }
  151. static const char *
  152. parse_imm8 (CGEN_CPU_DESC cd,
  153. const char ** strp,
  154. int opindex,
  155. bfd_reloc_code_real_type code,
  156. enum cgen_parse_operand_result * result_type,
  157. bfd_vma * valuep)
  158. {
  159. const char * errmsg;
  160. enum cgen_parse_operand_result rt;
  161. long dummyval;
  162. if (!result_type)
  163. result_type = &rt;
  164. code = BFD_RELOC_NONE;
  165. if (!cgen_parse_keyword (cd, strp, &epiphany_cgen_opval_gr_names, &dummyval)
  166. || !cgen_parse_keyword (cd, strp, &epiphany_cgen_opval_cr_names,
  167. &dummyval))
  168. /* Don't treat "mov ip,ip" as a move-immediate. */
  169. return _("register source in immediate move");
  170. errmsg = cgen_parse_address (cd, strp, opindex, code, result_type, valuep);
  171. if (errmsg)
  172. return errmsg;
  173. if (*result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
  174. errmsg = cgen_validate_unsigned_integer (*valuep, 0, 0xff);
  175. else
  176. errmsg = _("byte relocation unsupported");
  177. *valuep &= 0xff;
  178. return errmsg;
  179. }
  180. static const char * MISSING_CLOSE_PARENTHESIS = N_("missing `)'");
  181. static const char *
  182. parse_imm16 (CGEN_CPU_DESC cd,
  183. const char ** strp,
  184. int opindex,
  185. bfd_reloc_code_real_type code ATTRIBUTE_UNUSED,
  186. enum cgen_parse_operand_result * result_type,
  187. bfd_vma * valuep)
  188. {
  189. const char * errmsg;
  190. enum cgen_parse_operand_result rt;
  191. long dummyval;
  192. if (!result_type)
  193. result_type = &rt;
  194. if (strncasecmp (*strp, "%high(", 6) == 0)
  195. {
  196. *strp += 6;
  197. errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_EPIPHANY_HIGH,
  198. result_type, valuep);
  199. if (**strp != ')')
  200. return MISSING_CLOSE_PARENTHESIS;
  201. ++*strp;
  202. *valuep >>= 16;
  203. }
  204. else if (strncasecmp (*strp, "%low(", 5) == 0)
  205. {
  206. *strp += 5;
  207. errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_EPIPHANY_LOW,
  208. result_type, valuep);
  209. if (**strp != ')')
  210. return MISSING_CLOSE_PARENTHESIS;
  211. ++*strp;
  212. }
  213. else if (!cgen_parse_keyword (cd, strp, &epiphany_cgen_opval_gr_names,
  214. &dummyval)
  215. || !cgen_parse_keyword (cd, strp, &epiphany_cgen_opval_cr_names,
  216. &dummyval))
  217. /* Don't treat "mov ip,ip" as a move-immediate. */
  218. return _("register source in immediate move");
  219. else
  220. errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_16,
  221. result_type, valuep);
  222. if (!errmsg && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
  223. errmsg = cgen_validate_unsigned_integer (*valuep, 0, 0xffff);
  224. *valuep &= 0xffff;
  225. return errmsg;
  226. }
  227. const char *
  228. parse_branch_addr (CGEN_CPU_DESC cd,
  229. const char ** strp,
  230. int opindex,
  231. int opinfo ATTRIBUTE_UNUSED,
  232. enum cgen_parse_operand_result * resultp ATTRIBUTE_UNUSED,
  233. bfd_vma *valuep ATTRIBUTE_UNUSED)
  234. {
  235. const char * errmsg;
  236. enum cgen_parse_operand_result result_type;
  237. bfd_reloc_code_real_type code = BFD_RELOC_NONE;
  238. bfd_vma value;
  239. switch (opindex)
  240. {
  241. case EPIPHANY_OPERAND_SIMM24:
  242. code = BFD_RELOC_EPIPHANY_SIMM24;
  243. break;
  244. case EPIPHANY_OPERAND_SIMM8:
  245. code = BFD_RELOC_EPIPHANY_SIMM8;
  246. break;
  247. default:
  248. errmsg = _("ABORT: unknown operand");
  249. return errmsg;
  250. }
  251. errmsg = cgen_parse_address (cd, strp, opindex, code,
  252. &result_type, &value);
  253. if (errmsg == NULL)
  254. {
  255. if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
  256. {
  257. /* Act as if we had done a PC-relative branch, ala .+num. */
  258. char buf[20];
  259. const char * bufp = (const char *) buf;
  260. sprintf (buf, ".+%ld", (long) value);
  261. errmsg = cgen_parse_address (cd, &bufp, opindex, code, &result_type,
  262. &value);
  263. }
  264. if (result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED)
  265. {
  266. /* This will happen for things like (s2-s1) where s2 and s1
  267. are labels. */
  268. /* Nothing further to be done. */
  269. }
  270. else
  271. errmsg = _("Not a pc-relative address.");
  272. }
  273. return errmsg;
  274. }
  275. /* -- dis.c */
  276. #define CGEN_PRINT_INSN epiphany_print_insn
  277. static int
  278. epiphany_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info)
  279. {
  280. bfd_byte buf[CGEN_MAX_INSN_SIZE];
  281. int buflen;
  282. int status;
  283. info->bytes_per_chunk = 2;
  284. /* Attempt to read the base part of the insn. */
  285. info->bytes_per_line = buflen = cd->base_insn_bitsize / 8;
  286. status = (*info->read_memory_func) (pc, buf, buflen, info);
  287. /* Try again with the minimum part, if min < base. */
  288. if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
  289. {
  290. info->bytes_per_line = buflen = cd->min_insn_bitsize / 8;
  291. status = (*info->read_memory_func) (pc, buf, buflen, info);
  292. }
  293. if (status != 0)
  294. {
  295. (*info->memory_error_func) (status, pc, info);
  296. return -1;
  297. }
  298. return print_insn (cd, pc, info, buf, buflen);
  299. }
  300. static void
  301. print_postindex (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  302. void * dis_info,
  303. long value,
  304. unsigned int attrs ATTRIBUTE_UNUSED,
  305. bfd_vma pc ATTRIBUTE_UNUSED,
  306. int length ATTRIBUTE_UNUSED)
  307. {
  308. disassemble_info *info = (disassemble_info *) dis_info;
  309. (*info->fprintf_func) (info->stream, value ? "-" : "+");
  310. }
  311. static void
  312. print_simm_not_reg (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  313. void * dis_info,
  314. long value,
  315. unsigned int attrs ATTRIBUTE_UNUSED,
  316. bfd_vma pc ATTRIBUTE_UNUSED,
  317. int length ATTRIBUTE_UNUSED)
  318. {
  319. print_address (cd, dis_info, value, attrs, pc, length);
  320. }
  321. static void
  322. print_uimm_not_reg (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  323. void * dis_info,
  324. unsigned long value,
  325. unsigned int attrs ATTRIBUTE_UNUSED,
  326. bfd_vma pc ATTRIBUTE_UNUSED,
  327. int length ATTRIBUTE_UNUSED)
  328. {
  329. disassemble_info *info = (disassemble_info *)dis_info;
  330. if (value & 0x800)
  331. (*info->fprintf_func) (info->stream, "-");
  332. value &= 0x7ff;
  333. print_address (cd, dis_info, value, attrs, pc, length);
  334. }
  335. /* -- */