lm32-dis.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* Disassembler interface for targets using CGEN. -*- C -*-
  2. CGEN: Cpu tools GENerator
  3. THIS FILE IS MACHINE GENERATED WITH CGEN.
  4. - the resultant file is machine generated, cgen-dis.in isn't
  5. Copyright (C) 1996-2017 Free Software Foundation, Inc.
  6. This file is part of libopcodes.
  7. This library 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, or (at your option)
  10. any later version.
  11. It is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  14. 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 Foundation, Inc.,
  17. 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  18. /* ??? Eventually more and more of this stuff can go to cpu-independent files.
  19. Keep that in mind. */
  20. #include "sysdep.h"
  21. #include <stdio.h>
  22. #include "ansidecl.h"
  23. #include "disassemble.h"
  24. #include "bfd.h"
  25. #include "symcat.h"
  26. #include "libiberty.h"
  27. #include "lm32-desc.h"
  28. #include "lm32-opc.h"
  29. #include "opintl.h"
  30. /* Default text to print if an instruction isn't recognized. */
  31. #define UNKNOWN_INSN_MSG _("*unknown*")
  32. static void print_normal
  33. (CGEN_CPU_DESC, void *, long, unsigned int, bfd_vma, int);
  34. static void print_address
  35. (CGEN_CPU_DESC, void *, bfd_vma, unsigned int, bfd_vma, int) ATTRIBUTE_UNUSED;
  36. static void print_keyword
  37. (CGEN_CPU_DESC, void *, CGEN_KEYWORD *, long, unsigned int) ATTRIBUTE_UNUSED;
  38. static void print_insn_normal
  39. (CGEN_CPU_DESC, void *, const CGEN_INSN *, CGEN_FIELDS *, bfd_vma, int);
  40. static int print_insn
  41. (CGEN_CPU_DESC, bfd_vma, disassemble_info *, bfd_byte *, unsigned);
  42. static int default_print_insn
  43. (CGEN_CPU_DESC, bfd_vma, disassemble_info *) ATTRIBUTE_UNUSED;
  44. static int read_insn
  45. (CGEN_CPU_DESC, bfd_vma, disassemble_info *, bfd_byte *, int, CGEN_EXTRACT_INFO *,
  46. unsigned long *);
  47. /* -- disassembler routines inserted here. */
  48. void lm32_cgen_print_operand
  49. (CGEN_CPU_DESC, int, PTR, CGEN_FIELDS *, void const *, bfd_vma, int);
  50. /* Main entry point for printing operands.
  51. XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
  52. of dis-asm.h on cgen.h.
  53. This function is basically just a big switch statement. Earlier versions
  54. used tables to look up the function to use, but
  55. - if the table contains both assembler and disassembler functions then
  56. the disassembler contains much of the assembler and vice-versa,
  57. - there's a lot of inlining possibilities as things grow,
  58. - using a switch statement avoids the function call overhead.
  59. This function could be moved into `print_insn_normal', but keeping it
  60. separate makes clear the interface between `print_insn_normal' and each of
  61. the handlers. */
  62. void
  63. lm32_cgen_print_operand (CGEN_CPU_DESC cd,
  64. int opindex,
  65. void * xinfo,
  66. CGEN_FIELDS *fields,
  67. void const *attrs ATTRIBUTE_UNUSED,
  68. bfd_vma pc,
  69. int length)
  70. {
  71. disassemble_info *info = (disassemble_info *) xinfo;
  72. switch (opindex)
  73. {
  74. case LM32_OPERAND_BRANCH :
  75. print_address (cd, info, fields->f_branch, 0|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
  76. break;
  77. case LM32_OPERAND_CALL :
  78. print_address (cd, info, fields->f_call, 0|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
  79. break;
  80. case LM32_OPERAND_CSR :
  81. print_keyword (cd, info, & lm32_cgen_opval_h_csr, fields->f_csr, 0);
  82. break;
  83. case LM32_OPERAND_EXCEPTION :
  84. print_normal (cd, info, fields->f_exception, 0, pc, length);
  85. break;
  86. case LM32_OPERAND_GOT16 :
  87. print_normal (cd, info, fields->f_imm, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  88. break;
  89. case LM32_OPERAND_GOTOFFHI16 :
  90. print_normal (cd, info, fields->f_imm, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  91. break;
  92. case LM32_OPERAND_GOTOFFLO16 :
  93. print_normal (cd, info, fields->f_imm, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  94. break;
  95. case LM32_OPERAND_GP16 :
  96. print_normal (cd, info, fields->f_imm, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  97. break;
  98. case LM32_OPERAND_HI16 :
  99. print_normal (cd, info, fields->f_uimm, 0, pc, length);
  100. break;
  101. case LM32_OPERAND_IMM :
  102. print_normal (cd, info, fields->f_imm, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  103. break;
  104. case LM32_OPERAND_LO16 :
  105. print_normal (cd, info, fields->f_uimm, 0, pc, length);
  106. break;
  107. case LM32_OPERAND_R0 :
  108. print_keyword (cd, info, & lm32_cgen_opval_h_gr, fields->f_r0, 0);
  109. break;
  110. case LM32_OPERAND_R1 :
  111. print_keyword (cd, info, & lm32_cgen_opval_h_gr, fields->f_r1, 0);
  112. break;
  113. case LM32_OPERAND_R2 :
  114. print_keyword (cd, info, & lm32_cgen_opval_h_gr, fields->f_r2, 0);
  115. break;
  116. case LM32_OPERAND_SHIFT :
  117. print_normal (cd, info, fields->f_shift, 0, pc, length);
  118. break;
  119. case LM32_OPERAND_UIMM :
  120. print_normal (cd, info, fields->f_uimm, 0, pc, length);
  121. break;
  122. case LM32_OPERAND_USER :
  123. print_normal (cd, info, fields->f_user, 0, pc, length);
  124. break;
  125. default :
  126. /* xgettext:c-format */
  127. fprintf (stderr, _("Unrecognized field %d while printing insn.\n"),
  128. opindex);
  129. abort ();
  130. }
  131. }
  132. cgen_print_fn * const lm32_cgen_print_handlers[] =
  133. {
  134. print_insn_normal,
  135. };
  136. void
  137. lm32_cgen_init_dis (CGEN_CPU_DESC cd)
  138. {
  139. lm32_cgen_init_opcode_table (cd);
  140. lm32_cgen_init_ibld_table (cd);
  141. cd->print_handlers = & lm32_cgen_print_handlers[0];
  142. cd->print_operand = lm32_cgen_print_operand;
  143. }
  144. /* Default print handler. */
  145. static void
  146. print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  147. void *dis_info,
  148. long value,
  149. unsigned int attrs,
  150. bfd_vma pc ATTRIBUTE_UNUSED,
  151. int length ATTRIBUTE_UNUSED)
  152. {
  153. disassemble_info *info = (disassemble_info *) dis_info;
  154. /* Print the operand as directed by the attributes. */
  155. if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
  156. ; /* nothing to do */
  157. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
  158. (*info->fprintf_func) (info->stream, "%ld", value);
  159. else
  160. (*info->fprintf_func) (info->stream, "0x%lx", value);
  161. }
  162. /* Default address handler. */
  163. static void
  164. print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  165. void *dis_info,
  166. bfd_vma value,
  167. unsigned int attrs,
  168. bfd_vma pc ATTRIBUTE_UNUSED,
  169. int length ATTRIBUTE_UNUSED)
  170. {
  171. disassemble_info *info = (disassemble_info *) dis_info;
  172. /* Print the operand as directed by the attributes. */
  173. if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
  174. ; /* Nothing to do. */
  175. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR))
  176. (*info->print_address_func) (value, info);
  177. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR))
  178. (*info->print_address_func) (value, info);
  179. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
  180. (*info->fprintf_func) (info->stream, "%ld", (long) value);
  181. else
  182. (*info->fprintf_func) (info->stream, "0x%lx", (long) value);
  183. }
  184. /* Keyword print handler. */
  185. static void
  186. print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  187. void *dis_info,
  188. CGEN_KEYWORD *keyword_table,
  189. long value,
  190. unsigned int attrs ATTRIBUTE_UNUSED)
  191. {
  192. disassemble_info *info = (disassemble_info *) dis_info;
  193. const CGEN_KEYWORD_ENTRY *ke;
  194. ke = cgen_keyword_lookup_value (keyword_table, value);
  195. if (ke != NULL)
  196. (*info->fprintf_func) (info->stream, "%s", ke->name);
  197. else
  198. (*info->fprintf_func) (info->stream, "???");
  199. }
  200. /* Default insn printer.
  201. DIS_INFO is defined as `void *' so the disassembler needn't know anything
  202. about disassemble_info. */
  203. static void
  204. print_insn_normal (CGEN_CPU_DESC cd,
  205. void *dis_info,
  206. const CGEN_INSN *insn,
  207. CGEN_FIELDS *fields,
  208. bfd_vma pc,
  209. int length)
  210. {
  211. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  212. disassemble_info *info = (disassemble_info *) dis_info;
  213. const CGEN_SYNTAX_CHAR_TYPE *syn;
  214. CGEN_INIT_PRINT (cd);
  215. for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
  216. {
  217. if (CGEN_SYNTAX_MNEMONIC_P (*syn))
  218. {
  219. (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn));
  220. continue;
  221. }
  222. if (CGEN_SYNTAX_CHAR_P (*syn))
  223. {
  224. (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn));
  225. continue;
  226. }
  227. /* We have an operand. */
  228. lm32_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info,
  229. fields, CGEN_INSN_ATTRS (insn), pc, length);
  230. }
  231. }
  232. /* Subroutine of print_insn. Reads an insn into the given buffers and updates
  233. the extract info.
  234. Returns 0 if all is well, non-zero otherwise. */
  235. static int
  236. read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  237. bfd_vma pc,
  238. disassemble_info *info,
  239. bfd_byte *buf,
  240. int buflen,
  241. CGEN_EXTRACT_INFO *ex_info,
  242. unsigned long *insn_value)
  243. {
  244. int status = (*info->read_memory_func) (pc, buf, buflen, info);
  245. if (status != 0)
  246. {
  247. (*info->memory_error_func) (status, pc, info);
  248. return -1;
  249. }
  250. ex_info->dis_info = info;
  251. ex_info->valid = (1 << buflen) - 1;
  252. ex_info->insn_bytes = buf;
  253. *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG);
  254. return 0;
  255. }
  256. /* Utility to print an insn.
  257. BUF is the base part of the insn, target byte order, BUFLEN bytes long.
  258. The result is the size of the insn in bytes or zero for an unknown insn
  259. or -1 if an error occurs fetching data (memory_error_func will have
  260. been called). */
  261. static int
  262. print_insn (CGEN_CPU_DESC cd,
  263. bfd_vma pc,
  264. disassemble_info *info,
  265. bfd_byte *buf,
  266. unsigned int buflen)
  267. {
  268. CGEN_INSN_INT insn_value;
  269. const CGEN_INSN_LIST *insn_list;
  270. CGEN_EXTRACT_INFO ex_info;
  271. int basesize;
  272. /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
  273. basesize = cd->base_insn_bitsize < buflen * 8 ?
  274. cd->base_insn_bitsize : buflen * 8;
  275. insn_value = cgen_get_insn_value (cd, buf, basesize);
  276. /* Fill in ex_info fields like read_insn would. Don't actually call
  277. read_insn, since the incoming buffer is already read (and possibly
  278. modified a la m32r). */
  279. ex_info.valid = (1 << buflen) - 1;
  280. ex_info.dis_info = info;
  281. ex_info.insn_bytes = buf;
  282. /* The instructions are stored in hash lists.
  283. Pick the first one and keep trying until we find the right one. */
  284. insn_list = CGEN_DIS_LOOKUP_INSN (cd, (char *) buf, insn_value);
  285. while (insn_list != NULL)
  286. {
  287. const CGEN_INSN *insn = insn_list->insn;
  288. CGEN_FIELDS fields;
  289. int length;
  290. unsigned long insn_value_cropped;
  291. #ifdef CGEN_VALIDATE_INSN_SUPPORTED
  292. /* Not needed as insn shouldn't be in hash lists if not supported. */
  293. /* Supported by this cpu? */
  294. if (! lm32_cgen_insn_supported (cd, insn))
  295. {
  296. insn_list = CGEN_DIS_NEXT_INSN (insn_list);
  297. continue;
  298. }
  299. #endif
  300. /* Basic bit mask must be correct. */
  301. /* ??? May wish to allow target to defer this check until the extract
  302. handler. */
  303. /* Base size may exceed this instruction's size. Extract the
  304. relevant part from the buffer. */
  305. if ((unsigned) (CGEN_INSN_BITSIZE (insn) / 8) < buflen &&
  306. (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
  307. insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn),
  308. info->endian == BFD_ENDIAN_BIG);
  309. else
  310. insn_value_cropped = insn_value;
  311. if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn))
  312. == CGEN_INSN_BASE_VALUE (insn))
  313. {
  314. /* Printing is handled in two passes. The first pass parses the
  315. machine insn and extracts the fields. The second pass prints
  316. them. */
  317. /* Make sure the entire insn is loaded into insn_value, if it
  318. can fit. */
  319. if (((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize) &&
  320. (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
  321. {
  322. unsigned long full_insn_value;
  323. int rc = read_insn (cd, pc, info, buf,
  324. CGEN_INSN_BITSIZE (insn) / 8,
  325. & ex_info, & full_insn_value);
  326. if (rc != 0)
  327. return rc;
  328. length = CGEN_EXTRACT_FN (cd, insn)
  329. (cd, insn, &ex_info, full_insn_value, &fields, pc);
  330. }
  331. else
  332. length = CGEN_EXTRACT_FN (cd, insn)
  333. (cd, insn, &ex_info, insn_value_cropped, &fields, pc);
  334. /* Length < 0 -> error. */
  335. if (length < 0)
  336. return length;
  337. if (length > 0)
  338. {
  339. CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length);
  340. /* Length is in bits, result is in bytes. */
  341. return length / 8;
  342. }
  343. }
  344. insn_list = CGEN_DIS_NEXT_INSN (insn_list);
  345. }
  346. return 0;
  347. }
  348. /* Default value for CGEN_PRINT_INSN.
  349. The result is the size of the insn in bytes or zero for an unknown insn
  350. or -1 if an error occured fetching bytes. */
  351. #ifndef CGEN_PRINT_INSN
  352. #define CGEN_PRINT_INSN default_print_insn
  353. #endif
  354. static int
  355. default_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info)
  356. {
  357. bfd_byte buf[CGEN_MAX_INSN_SIZE];
  358. int buflen;
  359. int status;
  360. /* Attempt to read the base part of the insn. */
  361. buflen = cd->base_insn_bitsize / 8;
  362. status = (*info->read_memory_func) (pc, buf, buflen, info);
  363. /* Try again with the minimum part, if min < base. */
  364. if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
  365. {
  366. buflen = cd->min_insn_bitsize / 8;
  367. status = (*info->read_memory_func) (pc, buf, buflen, info);
  368. }
  369. if (status != 0)
  370. {
  371. (*info->memory_error_func) (status, pc, info);
  372. return -1;
  373. }
  374. return print_insn (cd, pc, info, buf, buflen);
  375. }
  376. /* Main entry point.
  377. Print one instruction from PC on INFO->STREAM.
  378. Return the size of the instruction (in bytes). */
  379. typedef struct cpu_desc_list
  380. {
  381. struct cpu_desc_list *next;
  382. CGEN_BITSET *isa;
  383. int mach;
  384. int endian;
  385. CGEN_CPU_DESC cd;
  386. } cpu_desc_list;
  387. int
  388. print_insn_lm32 (bfd_vma pc, disassemble_info *info)
  389. {
  390. static cpu_desc_list *cd_list = 0;
  391. cpu_desc_list *cl = 0;
  392. static CGEN_CPU_DESC cd = 0;
  393. static CGEN_BITSET *prev_isa;
  394. static int prev_mach;
  395. static int prev_endian;
  396. int length;
  397. CGEN_BITSET *isa;
  398. int mach;
  399. int endian = (info->endian == BFD_ENDIAN_BIG
  400. ? CGEN_ENDIAN_BIG
  401. : CGEN_ENDIAN_LITTLE);
  402. enum bfd_architecture arch;
  403. /* ??? gdb will set mach but leave the architecture as "unknown" */
  404. #ifndef CGEN_BFD_ARCH
  405. #define CGEN_BFD_ARCH bfd_arch_lm32
  406. #endif
  407. arch = info->arch;
  408. if (arch == bfd_arch_unknown)
  409. arch = CGEN_BFD_ARCH;
  410. /* There's no standard way to compute the machine or isa number
  411. so we leave it to the target. */
  412. #ifdef CGEN_COMPUTE_MACH
  413. mach = CGEN_COMPUTE_MACH (info);
  414. #else
  415. mach = info->mach;
  416. #endif
  417. #ifdef CGEN_COMPUTE_ISA
  418. {
  419. static CGEN_BITSET *permanent_isa;
  420. if (!permanent_isa)
  421. permanent_isa = cgen_bitset_create (MAX_ISAS);
  422. isa = permanent_isa;
  423. cgen_bitset_clear (isa);
  424. cgen_bitset_add (isa, CGEN_COMPUTE_ISA (info));
  425. }
  426. #else
  427. isa = info->insn_sets;
  428. #endif
  429. /* If we've switched cpu's, try to find a handle we've used before */
  430. if (cd
  431. && (cgen_bitset_compare (isa, prev_isa) != 0
  432. || mach != prev_mach
  433. || endian != prev_endian))
  434. {
  435. cd = 0;
  436. for (cl = cd_list; cl; cl = cl->next)
  437. {
  438. if (cgen_bitset_compare (cl->isa, isa) == 0 &&
  439. cl->mach == mach &&
  440. cl->endian == endian)
  441. {
  442. cd = cl->cd;
  443. prev_isa = cd->isas;
  444. break;
  445. }
  446. }
  447. }
  448. /* If we haven't initialized yet, initialize the opcode table. */
  449. if (! cd)
  450. {
  451. const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach);
  452. const char *mach_name;
  453. if (!arch_type)
  454. abort ();
  455. mach_name = arch_type->printable_name;
  456. prev_isa = cgen_bitset_copy (isa);
  457. prev_mach = mach;
  458. prev_endian = endian;
  459. cd = lm32_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa,
  460. CGEN_CPU_OPEN_BFDMACH, mach_name,
  461. CGEN_CPU_OPEN_ENDIAN, prev_endian,
  462. CGEN_CPU_OPEN_END);
  463. if (!cd)
  464. abort ();
  465. /* Save this away for future reference. */
  466. cl = xmalloc (sizeof (struct cpu_desc_list));
  467. cl->cd = cd;
  468. cl->isa = prev_isa;
  469. cl->mach = mach;
  470. cl->endian = endian;
  471. cl->next = cd_list;
  472. cd_list = cl;
  473. lm32_cgen_init_dis (cd);
  474. }
  475. /* We try to have as much common code as possible.
  476. But at this point some targets need to take over. */
  477. /* ??? Some targets may need a hook elsewhere. Try to avoid this,
  478. but if not possible try to move this hook elsewhere rather than
  479. have two hooks. */
  480. length = CGEN_PRINT_INSN (cd, pc, info);
  481. if (length > 0)
  482. return length;
  483. if (length < 0)
  484. return -1;
  485. (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
  486. return cd->default_insn_bitsize / 8;
  487. }