loongarch-dis.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* LoongArch opcode support.
  2. Copyright (C) 2021-2022 Free Software Foundation, Inc.
  3. Contributed by Loongson Ltd.
  4. This file is part of the GNU opcodes library.
  5. This library 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, or (at your option)
  8. any later version.
  9. It is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; see the file COPYING3. If not,
  15. see <http://www.gnu.org/licenses/>. */
  16. #include "sysdep.h"
  17. #include "disassemble.h"
  18. #include "opintl.h"
  19. #include "opcode/loongarch.h"
  20. #include "libiberty.h"
  21. #include <stdlib.h>
  22. static const struct loongarch_opcode *
  23. get_loongarch_opcode_by_binfmt (insn_t insn)
  24. {
  25. const struct loongarch_opcode *it;
  26. struct loongarch_ase *ase;
  27. size_t i;
  28. for (ase = loongarch_ASEs; ase->enabled; ase++)
  29. {
  30. if (!*ase->enabled || (ase->include && !*ase->include)
  31. || (ase->exclude && *ase->exclude))
  32. continue;
  33. if (!ase->opc_htab_inited)
  34. {
  35. for (it = ase->opcodes; it->mask; it++)
  36. if (!ase->opc_htab[LARCH_INSN_OPC (it->match)]
  37. && it->macro == NULL)
  38. ase->opc_htab[LARCH_INSN_OPC (it->match)] = it;
  39. for (i = 0; i < 16; i++)
  40. if (!ase->opc_htab[i])
  41. ase->opc_htab[i] = it;
  42. ase->opc_htab_inited = 1;
  43. }
  44. it = ase->opc_htab[LARCH_INSN_OPC (insn)];
  45. for (; it->name; it++)
  46. if ((insn & it->mask) == it->match && it->mask
  47. && !(it->include && !*it->include)
  48. && !(it->exclude && *it->exclude))
  49. return it;
  50. }
  51. return NULL;
  52. }
  53. static const char *const *loongarch_r_disname = NULL;
  54. static const char *const *loongarch_f_disname = NULL;
  55. static const char *const *loongarch_c_disname = NULL;
  56. static const char *const *loongarch_cr_disname = NULL;
  57. static const char *const *loongarch_v_disname = NULL;
  58. static const char *const *loongarch_x_disname = NULL;
  59. static void
  60. set_default_loongarch_dis_options (void)
  61. {
  62. LARCH_opts.ase_ilp32 = 1;
  63. LARCH_opts.ase_lp64 = 1;
  64. LARCH_opts.ase_sf = 1;
  65. LARCH_opts.ase_df = 1;
  66. LARCH_opts.ase_lsx = 1;
  67. LARCH_opts.ase_lasx = 1;
  68. loongarch_r_disname = loongarch_r_lp64_name;
  69. loongarch_f_disname = loongarch_f_lp64_name;
  70. loongarch_c_disname = loongarch_c_normal_name;
  71. loongarch_cr_disname = loongarch_cr_normal_name;
  72. loongarch_v_disname = loongarch_v_normal_name;
  73. loongarch_x_disname = loongarch_x_normal_name;
  74. }
  75. static int
  76. parse_loongarch_dis_option (const char *option)
  77. {
  78. if (strcmp (option, "numeric") == 0)
  79. {
  80. loongarch_r_disname = loongarch_r_normal_name;
  81. loongarch_f_disname = loongarch_f_normal_name;
  82. }
  83. return -1;
  84. }
  85. static int
  86. parse_loongarch_dis_options (const char *opts_in)
  87. {
  88. set_default_loongarch_dis_options ();
  89. if (opts_in == NULL)
  90. return 0;
  91. char *opts, *opt, *opt_end;
  92. opts = xmalloc (strlen (opts_in) + 1);
  93. strcpy (opts, opts_in);
  94. for (opt = opt_end = opts; opt_end != NULL; opt = opt_end + 1)
  95. {
  96. if ((opt_end = strchr (opt, ',')) != NULL)
  97. *opt_end = 0;
  98. if (parse_loongarch_dis_option (opt) != 0)
  99. return -1;
  100. }
  101. free (opts);
  102. return 0;
  103. }
  104. static int32_t
  105. dis_one_arg (char esc1, char esc2, const char *bit_field,
  106. const char *arg ATTRIBUTE_UNUSED, void *context)
  107. {
  108. static int need_comma = 0;
  109. struct disassemble_info *info = context;
  110. insn_t insn = *(insn_t *) info->private_data;
  111. int32_t imm, u_imm;
  112. if (esc1)
  113. {
  114. if (need_comma)
  115. info->fprintf_func (info->stream, ", ");
  116. need_comma = 1;
  117. imm = loongarch_decode_imm (bit_field, insn, 1);
  118. u_imm = loongarch_decode_imm (bit_field, insn, 0);
  119. }
  120. switch (esc1)
  121. {
  122. case 'r':
  123. info->fprintf_func (info->stream, "%s", loongarch_r_disname[u_imm]);
  124. break;
  125. case 'f':
  126. info->fprintf_func (info->stream, "%s", loongarch_f_disname[u_imm]);
  127. break;
  128. case 'c':
  129. switch (esc2)
  130. {
  131. case 'r':
  132. info->fprintf_func (info->stream, "%s", loongarch_cr_disname[u_imm]);
  133. break;
  134. default:
  135. info->fprintf_func (info->stream, "%s", loongarch_c_disname[u_imm]);
  136. }
  137. break;
  138. case 'v':
  139. info->fprintf_func (info->stream, "%s", loongarch_v_disname[u_imm]);
  140. break;
  141. case 'x':
  142. info->fprintf_func (info->stream, "%s", loongarch_x_disname[u_imm]);
  143. break;
  144. case 'u':
  145. info->fprintf_func (info->stream, "0x%x", u_imm);
  146. break;
  147. case 's':
  148. if (imm == 0)
  149. info->fprintf_func (info->stream, "%d", imm);
  150. else
  151. info->fprintf_func (info->stream, "%d(0x%x)", imm, u_imm);
  152. switch (esc2)
  153. {
  154. case 'b':
  155. info->insn_type = dis_branch;
  156. info->target += imm;
  157. }
  158. break;
  159. case '\0':
  160. need_comma = 0;
  161. }
  162. return 0;
  163. }
  164. static void
  165. disassemble_one (insn_t insn, struct disassemble_info *info)
  166. {
  167. const struct loongarch_opcode *opc = get_loongarch_opcode_by_binfmt (insn);
  168. #ifdef LOONGARCH_DEBUG
  169. char have_space[32] = { 0 };
  170. insn_t t;
  171. int i;
  172. const char *t_f = opc ? opc->format : NULL;
  173. if (t_f)
  174. while (*t_f)
  175. {
  176. while (('a' <= t_f[0] && t_f[0] <= 'z')
  177. || ('A' <= t_f[0] && t_f[0] <= 'Z')
  178. || t_f[0] == ',')
  179. t_f++;
  180. while (1)
  181. {
  182. i = strtol (t_f, &t_f, 10);
  183. have_space[i] = 1;
  184. t_f++; /* ':' */
  185. i += strtol (t_f, &t_f, 10);
  186. have_space[i] = 1;
  187. if (t_f[0] == '|')
  188. t_f++;
  189. else
  190. break;
  191. }
  192. if (t_f[0] == '<')
  193. t_f += 2; /* '<' '<' */
  194. strtol (t_f, &t_f, 10);
  195. }
  196. have_space[28] = 1;
  197. have_space[0] = 0;
  198. t = ~((insn_t) -1 >> 1);
  199. for (i = 31; 0 <= i; i--)
  200. {
  201. if (t & insn)
  202. info->fprintf_func (info->stream, "1");
  203. else
  204. info->fprintf_func (info->stream, "0");
  205. if (have_space[i])
  206. info->fprintf_func (info->stream, " ");
  207. t = t >> 1;
  208. }
  209. info->fprintf_func (info->stream, "\t");
  210. #endif
  211. if (!opc)
  212. {
  213. info->insn_type = dis_noninsn;
  214. info->fprintf_func (info->stream, "0x%08x", insn);
  215. return;
  216. }
  217. info->insn_type = dis_nonbranch;
  218. info->fprintf_func (info->stream, "%-12s", opc->name);
  219. {
  220. char *fake_args = xmalloc (strlen (opc->format) + 1);
  221. const char *fake_arg_strs[MAX_ARG_NUM_PLUS_2];
  222. strcpy (fake_args, opc->format);
  223. if (0 < loongarch_split_args_by_comma (fake_args, fake_arg_strs))
  224. info->fprintf_func (info->stream, "\t");
  225. info->private_data = &insn;
  226. loongarch_foreach_args (opc->format, fake_arg_strs, dis_one_arg, info);
  227. free (fake_args);
  228. }
  229. if (info->insn_type == dis_branch || info->insn_type == dis_condbranch
  230. /* Someother if we have extra info to print. */)
  231. info->fprintf_func (info->stream, "\t#");
  232. if (info->insn_type == dis_branch || info->insn_type == dis_condbranch)
  233. {
  234. info->fprintf_func (info->stream, " ");
  235. info->print_address_func (info->target, info);
  236. }
  237. }
  238. int
  239. print_insn_loongarch (bfd_vma memaddr, struct disassemble_info *info)
  240. {
  241. insn_t insn;
  242. int status;
  243. static int not_init_yet = 1;
  244. if (not_init_yet)
  245. {
  246. parse_loongarch_dis_options (info->disassembler_options);
  247. not_init_yet = 0;
  248. }
  249. info->bytes_per_chunk = 4;
  250. info->bytes_per_line = 4;
  251. info->display_endian = BFD_ENDIAN_LITTLE;
  252. info->insn_info_valid = 1;
  253. info->target = memaddr;
  254. if ((status = info->read_memory_func (memaddr, (bfd_byte *) &insn,
  255. sizeof (insn), info)) != 0)
  256. {
  257. info->memory_error_func (status, memaddr, info);
  258. return -1; /* loongarch_insn_length (0); */
  259. }
  260. disassemble_one (insn, info);
  261. return loongarch_insn_length (insn);
  262. }
  263. void
  264. print_loongarch_disassembler_options (FILE *stream)
  265. {
  266. fprintf (stream, _("\n\
  267. The following LoongArch disassembler options are supported for use\n\
  268. with the -M switch (multiple options should be separated by commas):\n"));
  269. fprintf (stream, _("\n\
  270. numeric Print numeric register names, rather than ABI names.\n"));
  271. fprintf (stream, _("\n"));
  272. }
  273. int
  274. loongarch_parse_dis_options (const char *opts_in)
  275. {
  276. return parse_loongarch_dis_options (opts_in);
  277. }
  278. static void
  279. my_print_address_func (bfd_vma addr, struct disassemble_info *dinfo)
  280. {
  281. dinfo->fprintf_func (dinfo->stream, "0x%llx", (long long) addr);
  282. }
  283. void
  284. loongarch_disassemble_one (int64_t pc, insn_t insn,
  285. int (*fprintf_func) (void *stream,
  286. const char *format, ...),
  287. void *stream)
  288. {
  289. static struct disassemble_info my_disinfo =
  290. {
  291. .print_address_func = my_print_address_func,
  292. };
  293. static int not_init_yet = 1;
  294. if (not_init_yet)
  295. {
  296. loongarch_parse_dis_options (NULL);
  297. not_init_yet = 0;
  298. }
  299. my_disinfo.fprintf_func = fprintf_func;
  300. my_disinfo.stream = stream;
  301. my_disinfo.target = pc;
  302. disassemble_one (insn, &my_disinfo);
  303. }