lm32-dis.c 17 KB

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