or1k.opc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* OpenRISC 1000 opcode support. -*- C -*-
  2. Copyright 2000-2014 Free Software Foundation, Inc.
  3. Originally ontributed for OR32 by Red Hat Inc;
  4. This file is part of the GNU Binutils.
  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, see <http://www.gnu.org/licenses/>. */
  15. /* This file is an addendum to or1k.cpu. Heavy use of C code isn't
  16. appropriate in .cpu files, so it resides here. This especially applies
  17. to assembly/disassembly where parsing/printing can be quite involved.
  18. Such things aren't really part of the specification of the cpu, per se,
  19. so .cpu files provide the general framework and .opc files handle the
  20. nitty-gritty details as necessary.
  21. Each section is delimited with start and end markers.
  22. <arch>-opc.h additions use: "-- opc.h"
  23. <arch>-opc.c additions use: "-- opc.c"
  24. <arch>-asm.c additions use: "-- asm.c"
  25. <arch>-dis.c additions use: "-- dis.c"
  26. <arch>-ibd.h additions use: "-- ibd.h" */
  27. /* -- opc.h */
  28. #undef CGEN_DIS_HASH_SIZE
  29. #define CGEN_DIS_HASH_SIZE 256
  30. #undef CGEN_DIS_HASH
  31. #define CGEN_DIS_HASH(buffer, value) (((unsigned char *) (buffer))[0] >> 2)
  32. /* Check applicability of instructions against machines. */
  33. #define CGEN_VALIDATE_INSN_SUPPORTED
  34. extern int or1k_cgen_insn_supported (CGEN_CPU_DESC, const CGEN_INSN *);
  35. /* -- */
  36. /* -- opc.c */
  37. /* Special check to ensure that instruction exists for given machine. */
  38. int
  39. or1k_cgen_insn_supported (CGEN_CPU_DESC cd, const CGEN_INSN *insn)
  40. {
  41. int machs = CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_MACH);
  42. /* No mach attribute? Assume it's supported for all machs. */
  43. if (machs == 0)
  44. return 1;
  45. return ((machs & cd->machs) != 0);
  46. }
  47. /* -- */
  48. /* -- asm.c */
  49. static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
  50. static const char * INVALID_STORE_RELOC = N_("relocation invalid for store");
  51. static const char * INVALID_RELOC_TYPE = N_("internal relocation type invalid");
  52. #define CGEN_VERBOSE_ASSEMBLER_ERRORS
  53. static const char *
  54. parse_disp26 (CGEN_CPU_DESC cd,
  55. const char ** strp,
  56. int opindex,
  57. int opinfo ATTRIBUTE_UNUSED,
  58. enum cgen_parse_operand_result * resultp,
  59. bfd_vma * valuep)
  60. {
  61. const char *str = *strp;
  62. const char *errmsg = NULL;
  63. bfd_reloc_code_real_type reloc = BFD_RELOC_OR1K_REL_26;
  64. if (strncasecmp (str, "plta(", 5) == 0)
  65. {
  66. *strp = str + 5;
  67. reloc = BFD_RELOC_OR1K_PLTA26;
  68. }
  69. else if (strncasecmp (str, "plt(", 4) == 0)
  70. {
  71. *strp = str + 4;
  72. reloc = BFD_RELOC_OR1K_PLT26;
  73. }
  74. errmsg = cgen_parse_address (cd, strp, opindex, reloc, resultp, valuep);
  75. if (reloc != BFD_RELOC_OR1K_REL_26)
  76. {
  77. if (**strp != ')')
  78. errmsg = MISSING_CLOSING_PARENTHESIS;
  79. else
  80. ++*strp;
  81. }
  82. return errmsg;
  83. }
  84. static const char *
  85. parse_disp21 (CGEN_CPU_DESC cd,
  86. const char ** strp,
  87. int opindex,
  88. int opinfo ATTRIBUTE_UNUSED,
  89. enum cgen_parse_operand_result * resultp,
  90. bfd_vma * valuep)
  91. {
  92. const char *str = *strp;
  93. const char *errmsg = NULL;
  94. bfd_reloc_code_real_type reloc = BFD_RELOC_OR1K_PCREL_PG21;
  95. if (strncasecmp (str, "got(", 4) == 0)
  96. {
  97. *strp = str + 4;
  98. reloc = BFD_RELOC_OR1K_GOT_PG21;
  99. }
  100. else if (strncasecmp (str, "tlsgd(", 6) == 0)
  101. {
  102. *strp = str + 6;
  103. reloc = BFD_RELOC_OR1K_TLS_GD_PG21;
  104. }
  105. else if (strncasecmp (str, "tlsldm(", 7) == 0)
  106. {
  107. *strp = str + 7;
  108. reloc = BFD_RELOC_OR1K_TLS_LDM_PG21;
  109. }
  110. else if (strncasecmp (str, "gottp(", 6) == 0)
  111. {
  112. *strp = str + 6;
  113. reloc = BFD_RELOC_OR1K_TLS_IE_PG21;
  114. }
  115. errmsg = cgen_parse_address (cd, strp, opindex, reloc, resultp, valuep);
  116. if (reloc != BFD_RELOC_OR1K_PCREL_PG21)
  117. {
  118. if (**strp != ')')
  119. errmsg = MISSING_CLOSING_PARENTHESIS;
  120. else
  121. ++*strp;
  122. }
  123. return errmsg;
  124. }
  125. enum or1k_rclass
  126. {
  127. RCLASS_DIRECT = 0,
  128. RCLASS_GOT = 1,
  129. RCLASS_GOTPC = 2,
  130. RCLASS_GOTOFF = 3,
  131. RCLASS_TLSGD = 4,
  132. RCLASS_TLSLDM = 5,
  133. RCLASS_DTPOFF = 6,
  134. RCLASS_GOTTPOFF = 7,
  135. RCLASS_TPOFF = 8,
  136. };
  137. enum or1k_rtype
  138. {
  139. RTYPE_LO = 0,
  140. RTYPE_SLO = 1,
  141. RTYPE_PO = 2,
  142. RTYPE_SPO = 3,
  143. RTYPE_HI = 4,
  144. RTYPE_AHI = 5,
  145. };
  146. #define RCLASS_SHIFT 3
  147. #define RTYPE_MASK 7
  148. static const bfd_reloc_code_real_type or1k_imm16_relocs[][6] = {
  149. { BFD_RELOC_LO16,
  150. BFD_RELOC_OR1K_SLO16,
  151. BFD_RELOC_OR1K_LO13,
  152. BFD_RELOC_OR1K_SLO13,
  153. BFD_RELOC_HI16,
  154. BFD_RELOC_HI16_S, },
  155. { BFD_RELOC_OR1K_GOT16,
  156. BFD_RELOC_UNUSED,
  157. BFD_RELOC_OR1K_GOT_LO13,
  158. BFD_RELOC_UNUSED,
  159. BFD_RELOC_UNUSED,
  160. BFD_RELOC_OR1K_GOT_AHI16 },
  161. { BFD_RELOC_OR1K_GOTPC_LO16,
  162. BFD_RELOC_UNUSED,
  163. BFD_RELOC_UNUSED,
  164. BFD_RELOC_UNUSED,
  165. BFD_RELOC_OR1K_GOTPC_HI16,
  166. BFD_RELOC_UNUSED },
  167. { BFD_RELOC_LO16_GOTOFF,
  168. BFD_RELOC_OR1K_GOTOFF_SLO16,
  169. BFD_RELOC_UNUSED,
  170. BFD_RELOC_UNUSED,
  171. BFD_RELOC_HI16_GOTOFF,
  172. BFD_RELOC_HI16_S_GOTOFF },
  173. { BFD_RELOC_OR1K_TLS_GD_LO16,
  174. BFD_RELOC_UNUSED,
  175. BFD_RELOC_OR1K_TLS_GD_LO13,
  176. BFD_RELOC_UNUSED,
  177. BFD_RELOC_OR1K_TLS_GD_HI16,
  178. BFD_RELOC_UNUSED },
  179. { BFD_RELOC_OR1K_TLS_LDM_LO16,
  180. BFD_RELOC_UNUSED,
  181. BFD_RELOC_OR1K_TLS_LDM_LO13,
  182. BFD_RELOC_UNUSED,
  183. BFD_RELOC_OR1K_TLS_LDM_HI16,
  184. BFD_RELOC_UNUSED },
  185. { BFD_RELOC_OR1K_TLS_LDO_LO16,
  186. BFD_RELOC_UNUSED,
  187. BFD_RELOC_UNUSED,
  188. BFD_RELOC_UNUSED,
  189. BFD_RELOC_OR1K_TLS_LDO_HI16,
  190. BFD_RELOC_UNUSED },
  191. { BFD_RELOC_OR1K_TLS_IE_LO16,
  192. BFD_RELOC_UNUSED,
  193. BFD_RELOC_OR1K_TLS_IE_LO13,
  194. BFD_RELOC_UNUSED,
  195. BFD_RELOC_OR1K_TLS_IE_HI16,
  196. BFD_RELOC_OR1K_TLS_IE_AHI16 },
  197. { BFD_RELOC_OR1K_TLS_LE_LO16,
  198. BFD_RELOC_OR1K_TLS_LE_SLO16,
  199. BFD_RELOC_UNUSED,
  200. BFD_RELOC_UNUSED,
  201. BFD_RELOC_OR1K_TLS_LE_HI16,
  202. BFD_RELOC_OR1K_TLS_LE_AHI16 },
  203. };
  204. static int
  205. parse_reloc (const char **strp)
  206. {
  207. const char *str = *strp;
  208. enum or1k_rclass cls = RCLASS_DIRECT;
  209. enum or1k_rtype typ;
  210. if (strncasecmp (str, "got(", 4) == 0)
  211. {
  212. *strp = str + 4;
  213. return (RCLASS_GOT << RCLASS_SHIFT) | RTYPE_LO;
  214. }
  215. if (strncasecmp (str, "gotpo(", 6) == 0)
  216. {
  217. *strp = str + 6;
  218. return (RCLASS_GOT << RCLASS_SHIFT) | RTYPE_PO;
  219. }
  220. if (strncasecmp (str, "gottppo(", 8) == 0)
  221. {
  222. *strp = str + 8;
  223. return (RCLASS_GOTTPOFF << RCLASS_SHIFT) | RTYPE_PO;
  224. }
  225. if (strncasecmp (str, "gotpc", 5) == 0)
  226. {
  227. str += 5;
  228. cls = RCLASS_GOTPC;
  229. }
  230. else if (strncasecmp (str, "gotoff", 6) == 0)
  231. {
  232. str += 6;
  233. cls = RCLASS_GOTOFF;
  234. }
  235. else if (strncasecmp (str, "tlsgd", 5) == 0)
  236. {
  237. str += 5;
  238. cls = RCLASS_TLSGD;
  239. }
  240. else if (strncasecmp (str, "tlsldm", 6) == 0)
  241. {
  242. str += 6;
  243. cls = RCLASS_TLSLDM;
  244. }
  245. else if (strncasecmp (str, "dtpoff", 6) == 0)
  246. {
  247. str += 6;
  248. cls = RCLASS_DTPOFF;
  249. }
  250. else if (strncasecmp (str, "gottpoff", 8) == 0)
  251. {
  252. str += 8;
  253. cls = RCLASS_GOTTPOFF;
  254. }
  255. else if (strncasecmp (str, "tpoff", 5) == 0)
  256. {
  257. str += 5;
  258. cls = RCLASS_TPOFF;
  259. }
  260. else if (strncasecmp (str, "got", 3) == 0)
  261. {
  262. str += 3;
  263. cls = RCLASS_GOT;
  264. }
  265. if (strncasecmp (str, "hi(", 3) == 0)
  266. {
  267. str += 3;
  268. typ = RTYPE_HI;
  269. }
  270. else if (strncasecmp (str, "lo(", 3) == 0)
  271. {
  272. str += 3;
  273. typ = RTYPE_LO;
  274. }
  275. else if (strncasecmp (str, "ha(", 3) == 0)
  276. {
  277. str += 3;
  278. typ = RTYPE_AHI;
  279. }
  280. else if (strncasecmp (str, "po(", 3) == 0 && cls != RCLASS_GOTTPOFF)
  281. {
  282. str += 3;
  283. typ = RTYPE_PO;
  284. }
  285. else
  286. return -1;
  287. *strp = str;
  288. return (cls << RCLASS_SHIFT) | typ;
  289. }
  290. static const char *
  291. parse_imm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
  292. long *valuep, int splitp)
  293. {
  294. const char *errmsg;
  295. enum cgen_parse_operand_result result_type;
  296. bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
  297. enum or1k_rtype reloc_type;
  298. int reloc_code;
  299. bfd_vma ret;
  300. if (**strp == '#')
  301. ++*strp;
  302. reloc_code = parse_reloc (strp);
  303. reloc_type = reloc_code & RTYPE_MASK;
  304. if (reloc_code >= 0)
  305. {
  306. enum or1k_rclass reloc_class = reloc_code >> RCLASS_SHIFT;
  307. if (splitp)
  308. {
  309. if ((reloc_type == RTYPE_LO || reloc_type == RTYPE_PO)
  310. && reloc_class != RCLASS_GOT)
  311. /* If split we or up the type to RTYPE_SLO or RTYPE_SPO. */
  312. reloc_type |= 1;
  313. else
  314. return INVALID_STORE_RELOC;
  315. }
  316. reloc = or1k_imm16_relocs[reloc_class][reloc_type];
  317. }
  318. if (reloc != BFD_RELOC_UNUSED)
  319. {
  320. bfd_vma value;
  321. errmsg = cgen_parse_address (cd, strp, opindex, reloc,
  322. &result_type, &value);
  323. if (**strp != ')')
  324. errmsg = MISSING_CLOSING_PARENTHESIS;
  325. ++*strp;
  326. ret = value;
  327. if (errmsg == NULL && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
  328. switch (reloc_type)
  329. {
  330. case RTYPE_AHI:
  331. ret += 0x8000;
  332. /* FALLTHRU */
  333. case RTYPE_HI:
  334. ret >>= 16;
  335. /* FALLTHRU */
  336. case RTYPE_LO:
  337. case RTYPE_SLO:
  338. ret &= 0xffff;
  339. ret = (ret ^ 0x8000) - 0x8000;
  340. break;
  341. case RTYPE_PO:
  342. case RTYPE_SPO:
  343. ret &= 0x1fff;
  344. break;
  345. default:
  346. errmsg = INVALID_RELOC_TYPE;
  347. }
  348. }
  349. else
  350. {
  351. long value;
  352. errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
  353. ret = value;
  354. }
  355. if (errmsg == NULL)
  356. *valuep = ret;
  357. return errmsg;
  358. }
  359. static const char *
  360. parse_simm16 (CGEN_CPU_DESC cd, const char **strp, int opindex, long *valuep)
  361. {
  362. return parse_imm16(cd, strp, opindex, (long *) valuep, 0);
  363. }
  364. static const char *
  365. parse_simm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
  366. long *valuep)
  367. {
  368. return parse_imm16(cd, strp, opindex, (long *) valuep, 1);
  369. }
  370. static const char *
  371. parse_uimm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
  372. unsigned long *valuep)
  373. {
  374. const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 0);
  375. if (errmsg == NULL)
  376. *valuep &= 0xffff;
  377. return errmsg;
  378. }
  379. static const char *
  380. parse_uimm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
  381. unsigned long *valuep)
  382. {
  383. const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 1);
  384. if (errmsg == NULL)
  385. *valuep &= 0xffff;
  386. return errmsg;
  387. }
  388. /* Parse register pairs with syntax rA,rB to a flag + rA value. */
  389. static const char *
  390. parse_regpair (CGEN_CPU_DESC cd, const char **strp,
  391. int opindex ATTRIBUTE_UNUSED, unsigned long *valuep)
  392. {
  393. long reg1_index;
  394. long reg2_index;
  395. const char *errmsg;
  396. /* The first part should just be a register. */
  397. errmsg = cgen_parse_keyword (cd, strp, &or1k_cgen_opval_h_gpr,
  398. &reg1_index);
  399. /* If that worked skip the comma separator. */
  400. if (errmsg == NULL)
  401. {
  402. if (**strp == ',')
  403. ++*strp;
  404. else
  405. errmsg = "Unexpected character, expected ','";
  406. }
  407. /* If that worked the next part is just another register. */
  408. if (errmsg == NULL)
  409. errmsg = cgen_parse_keyword (cd, strp, &or1k_cgen_opval_h_gpr,
  410. &reg2_index);
  411. /* Validate the register pair is valid and create the output value. */
  412. if (errmsg == NULL)
  413. {
  414. int regoffset = reg2_index - reg1_index;
  415. if (regoffset == 1 || regoffset == 2)
  416. {
  417. unsigned short offsetmask;
  418. unsigned short value;
  419. offsetmask = ((regoffset == 2 ? 1 : 0) << 5);
  420. value = offsetmask | reg1_index;
  421. *valuep = value;
  422. }
  423. else
  424. errmsg = "Invalid register pair, offset not 1 or 2.";
  425. }
  426. return errmsg;
  427. }
  428. /* -- */
  429. /* -- dis.c */
  430. static void
  431. print_regpair (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  432. void * dis_info,
  433. long value,
  434. unsigned int attrs ATTRIBUTE_UNUSED,
  435. bfd_vma pc ATTRIBUTE_UNUSED,
  436. int length ATTRIBUTE_UNUSED)
  437. {
  438. disassemble_info *info = dis_info;
  439. char reg1_index;
  440. char reg2_index;
  441. reg1_index = value & 0x1f;
  442. reg2_index = reg1_index + ((value & (1 << 5)) ? 2 : 1);
  443. (*info->fprintf_func) (info->stream, "r%d,r%d", reg1_index, reg2_index);
  444. }
  445. /* -- */
  446. /* -- ibd.h */
  447. /* -- */