cgen-dis.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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-2022 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 "@prefix@-desc.h"
  28. #include "@prefix@-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. /* Default print handler. */
  49. static void
  50. print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  51. void *dis_info,
  52. long value,
  53. unsigned int attrs,
  54. bfd_vma pc ATTRIBUTE_UNUSED,
  55. int length ATTRIBUTE_UNUSED)
  56. {
  57. disassemble_info *info = (disassemble_info *) dis_info;
  58. /* Print the operand as directed by the attributes. */
  59. if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
  60. ; /* nothing to do */
  61. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
  62. (*info->fprintf_func) (info->stream, "%ld", value);
  63. else
  64. (*info->fprintf_func) (info->stream, "0x%lx", value);
  65. }
  66. /* Default address handler. */
  67. static void
  68. print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  69. void *dis_info,
  70. bfd_vma value,
  71. unsigned int attrs,
  72. bfd_vma pc ATTRIBUTE_UNUSED,
  73. int length ATTRIBUTE_UNUSED)
  74. {
  75. disassemble_info *info = (disassemble_info *) dis_info;
  76. /* Print the operand as directed by the attributes. */
  77. if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
  78. ; /* Nothing to do. */
  79. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR))
  80. (*info->print_address_func) (value, info);
  81. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR))
  82. (*info->print_address_func) (value, info);
  83. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
  84. (*info->fprintf_func) (info->stream, "%ld", (long) value);
  85. else
  86. (*info->fprintf_func) (info->stream, "0x%lx", (long) value);
  87. }
  88. /* Keyword print handler. */
  89. static void
  90. print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  91. void *dis_info,
  92. CGEN_KEYWORD *keyword_table,
  93. long value,
  94. unsigned int attrs ATTRIBUTE_UNUSED)
  95. {
  96. disassemble_info *info = (disassemble_info *) dis_info;
  97. const CGEN_KEYWORD_ENTRY *ke;
  98. ke = cgen_keyword_lookup_value (keyword_table, value);
  99. if (ke != NULL)
  100. (*info->fprintf_func) (info->stream, "%s", ke->name);
  101. else
  102. (*info->fprintf_func) (info->stream, "???");
  103. }
  104. /* Default insn printer.
  105. DIS_INFO is defined as `void *' so the disassembler needn't know anything
  106. about disassemble_info. */
  107. static void
  108. print_insn_normal (CGEN_CPU_DESC cd,
  109. void *dis_info,
  110. const CGEN_INSN *insn,
  111. CGEN_FIELDS *fields,
  112. bfd_vma pc,
  113. int length)
  114. {
  115. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  116. disassemble_info *info = (disassemble_info *) dis_info;
  117. const CGEN_SYNTAX_CHAR_TYPE *syn;
  118. CGEN_INIT_PRINT (cd);
  119. for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
  120. {
  121. if (CGEN_SYNTAX_MNEMONIC_P (*syn))
  122. {
  123. (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn));
  124. continue;
  125. }
  126. if (CGEN_SYNTAX_CHAR_P (*syn))
  127. {
  128. (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn));
  129. continue;
  130. }
  131. /* We have an operand. */
  132. @arch@_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info,
  133. fields, CGEN_INSN_ATTRS (insn), pc, length);
  134. }
  135. }
  136. /* Subroutine of print_insn. Reads an insn into the given buffers and updates
  137. the extract info.
  138. Returns 0 if all is well, non-zero otherwise. */
  139. static int
  140. read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  141. bfd_vma pc,
  142. disassemble_info *info,
  143. bfd_byte *buf,
  144. int buflen,
  145. CGEN_EXTRACT_INFO *ex_info,
  146. unsigned long *insn_value)
  147. {
  148. int status = (*info->read_memory_func) (pc, buf, buflen, info);
  149. if (status != 0)
  150. {
  151. (*info->memory_error_func) (status, pc, info);
  152. return -1;
  153. }
  154. ex_info->dis_info = info;
  155. ex_info->valid = (1 << buflen) - 1;
  156. ex_info->insn_bytes = buf;
  157. *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG);
  158. return 0;
  159. }
  160. /* Utility to print an insn.
  161. BUF is the base part of the insn, target byte order, BUFLEN bytes long.
  162. The result is the size of the insn in bytes or zero for an unknown insn
  163. or -1 if an error occurs fetching data (memory_error_func will have
  164. been called). */
  165. static int
  166. print_insn (CGEN_CPU_DESC cd,
  167. bfd_vma pc,
  168. disassemble_info *info,
  169. bfd_byte *buf,
  170. unsigned int buflen)
  171. {
  172. CGEN_INSN_INT insn_value;
  173. const CGEN_INSN_LIST *insn_list;
  174. CGEN_EXTRACT_INFO ex_info;
  175. int basesize;
  176. /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
  177. basesize = cd->base_insn_bitsize < buflen * 8 ?
  178. cd->base_insn_bitsize : buflen * 8;
  179. insn_value = cgen_get_insn_value (cd, buf, basesize, cd->insn_endian);
  180. /* Fill in ex_info fields like read_insn would. Don't actually call
  181. read_insn, since the incoming buffer is already read (and possibly
  182. modified a la m32r). */
  183. ex_info.valid = (1 << buflen) - 1;
  184. ex_info.dis_info = info;
  185. ex_info.insn_bytes = buf;
  186. /* The instructions are stored in hash lists.
  187. Pick the first one and keep trying until we find the right one. */
  188. insn_list = CGEN_DIS_LOOKUP_INSN (cd, (char *) buf, insn_value);
  189. while (insn_list != NULL)
  190. {
  191. const CGEN_INSN *insn = insn_list->insn;
  192. CGEN_FIELDS fields;
  193. int length;
  194. unsigned long insn_value_cropped;
  195. #ifdef CGEN_VALIDATE_INSN_SUPPORTED
  196. /* Not needed as insn shouldn't be in hash lists if not supported. */
  197. /* Supported by this cpu? */
  198. if (! @arch@_cgen_insn_supported (cd, insn))
  199. {
  200. insn_list = CGEN_DIS_NEXT_INSN (insn_list);
  201. continue;
  202. }
  203. #endif
  204. /* Basic bit mask must be correct. */
  205. /* ??? May wish to allow target to defer this check until the extract
  206. handler. */
  207. /* Base size may exceed this instruction's size. Extract the
  208. relevant part from the buffer. */
  209. if ((unsigned) (CGEN_INSN_BITSIZE (insn) / 8) < buflen &&
  210. (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
  211. insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn),
  212. info->endian == BFD_ENDIAN_BIG);
  213. else
  214. insn_value_cropped = insn_value;
  215. if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn))
  216. == CGEN_INSN_BASE_VALUE (insn))
  217. {
  218. /* Printing is handled in two passes. The first pass parses the
  219. machine insn and extracts the fields. The second pass prints
  220. them. */
  221. /* Make sure the entire insn is loaded into insn_value, if it
  222. can fit. */
  223. if (((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize) &&
  224. (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
  225. {
  226. unsigned long full_insn_value;
  227. int rc = read_insn (cd, pc, info, buf,
  228. CGEN_INSN_BITSIZE (insn) / 8,
  229. & ex_info, & full_insn_value);
  230. if (rc != 0)
  231. return rc;
  232. length = CGEN_EXTRACT_FN (cd, insn)
  233. (cd, insn, &ex_info, full_insn_value, &fields, pc);
  234. }
  235. else
  236. length = CGEN_EXTRACT_FN (cd, insn)
  237. (cd, insn, &ex_info, insn_value_cropped, &fields, pc);
  238. /* Length < 0 -> error. */
  239. if (length < 0)
  240. return length;
  241. if (length > 0)
  242. {
  243. CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length);
  244. /* Length is in bits, result is in bytes. */
  245. return length / 8;
  246. }
  247. }
  248. insn_list = CGEN_DIS_NEXT_INSN (insn_list);
  249. }
  250. return 0;
  251. }
  252. /* Default value for CGEN_PRINT_INSN.
  253. The result is the size of the insn in bytes or zero for an unknown insn
  254. or -1 if an error occured fetching bytes. */
  255. #ifndef CGEN_PRINT_INSN
  256. #define CGEN_PRINT_INSN default_print_insn
  257. #endif
  258. static int
  259. default_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info)
  260. {
  261. bfd_byte buf[CGEN_MAX_INSN_SIZE];
  262. int buflen;
  263. int status;
  264. /* Attempt to read the base part of the insn. */
  265. buflen = cd->base_insn_bitsize / 8;
  266. status = (*info->read_memory_func) (pc, buf, buflen, info);
  267. /* Try again with the minimum part, if min < base. */
  268. if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
  269. {
  270. buflen = cd->min_insn_bitsize / 8;
  271. status = (*info->read_memory_func) (pc, buf, buflen, info);
  272. }
  273. if (status != 0)
  274. {
  275. (*info->memory_error_func) (status, pc, info);
  276. return -1;
  277. }
  278. return print_insn (cd, pc, info, buf, buflen);
  279. }
  280. /* Main entry point.
  281. Print one instruction from PC on INFO->STREAM.
  282. Return the size of the instruction (in bytes). */
  283. typedef struct cpu_desc_list
  284. {
  285. struct cpu_desc_list *next;
  286. CGEN_BITSET *isa;
  287. int mach;
  288. int endian;
  289. int insn_endian;
  290. CGEN_CPU_DESC cd;
  291. } cpu_desc_list;
  292. int
  293. print_insn_@arch@ (bfd_vma pc, disassemble_info *info)
  294. {
  295. static cpu_desc_list *cd_list = 0;
  296. cpu_desc_list *cl = 0;
  297. static CGEN_CPU_DESC cd = 0;
  298. static CGEN_BITSET *prev_isa;
  299. static int prev_mach;
  300. static int prev_endian;
  301. static int prev_insn_endian;
  302. int length;
  303. CGEN_BITSET *isa;
  304. int mach;
  305. int endian = (info->endian == BFD_ENDIAN_BIG
  306. ? CGEN_ENDIAN_BIG
  307. : CGEN_ENDIAN_LITTLE);
  308. int insn_endian = (info->endian_code == BFD_ENDIAN_BIG
  309. ? CGEN_ENDIAN_BIG
  310. : CGEN_ENDIAN_LITTLE);
  311. enum bfd_architecture arch;
  312. /* ??? gdb will set mach but leave the architecture as "unknown" */
  313. #ifndef CGEN_BFD_ARCH
  314. #define CGEN_BFD_ARCH bfd_arch_@arch@
  315. #endif
  316. arch = info->arch;
  317. if (arch == bfd_arch_unknown)
  318. arch = CGEN_BFD_ARCH;
  319. /* There's no standard way to compute the machine or isa number
  320. so we leave it to the target. */
  321. #ifdef CGEN_COMPUTE_MACH
  322. mach = CGEN_COMPUTE_MACH (info);
  323. #else
  324. mach = info->mach;
  325. #endif
  326. #ifdef CGEN_COMPUTE_ISA
  327. {
  328. static CGEN_BITSET *permanent_isa;
  329. if (!permanent_isa)
  330. permanent_isa = cgen_bitset_create (MAX_ISAS);
  331. isa = permanent_isa;
  332. cgen_bitset_clear (isa);
  333. cgen_bitset_add (isa, CGEN_COMPUTE_ISA (info));
  334. }
  335. #else
  336. isa = info->private_data;
  337. #endif
  338. /* If we've switched cpu's, try to find a handle we've used before */
  339. if (cd
  340. && (cgen_bitset_compare (isa, prev_isa) != 0
  341. || mach != prev_mach
  342. || endian != prev_endian))
  343. {
  344. cd = 0;
  345. for (cl = cd_list; cl; cl = cl->next)
  346. {
  347. if (cgen_bitset_compare (cl->isa, isa) == 0 &&
  348. cl->mach == mach &&
  349. cl->endian == endian)
  350. {
  351. cd = cl->cd;
  352. prev_isa = cd->isas;
  353. break;
  354. }
  355. }
  356. }
  357. /* If we haven't initialized yet, initialize the opcode table. */
  358. if (! cd)
  359. {
  360. const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach);
  361. const char *mach_name;
  362. if (!arch_type)
  363. abort ();
  364. mach_name = arch_type->printable_name;
  365. prev_isa = cgen_bitset_copy (isa);
  366. prev_mach = mach;
  367. prev_endian = endian;
  368. prev_insn_endian = insn_endian;
  369. cd = @arch@_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa,
  370. CGEN_CPU_OPEN_BFDMACH, mach_name,
  371. CGEN_CPU_OPEN_ENDIAN, prev_endian,
  372. CGEN_CPU_OPEN_INSN_ENDIAN, prev_insn_endian,
  373. CGEN_CPU_OPEN_END);
  374. if (!cd)
  375. abort ();
  376. /* Save this away for future reference. */
  377. cl = xmalloc (sizeof (struct cpu_desc_list));
  378. cl->cd = cd;
  379. cl->isa = prev_isa;
  380. cl->mach = mach;
  381. cl->endian = endian;
  382. cl->next = cd_list;
  383. cd_list = cl;
  384. @arch@_cgen_init_dis (cd);
  385. }
  386. /* We try to have as much common code as possible.
  387. But at this point some targets need to take over. */
  388. /* ??? Some targets may need a hook elsewhere. Try to avoid this,
  389. but if not possible try to move this hook elsewhere rather than
  390. have two hooks. */
  391. length = CGEN_PRINT_INSN (cd, pc, info);
  392. if (length > 0)
  393. return length;
  394. if (length < 0)
  395. return -1;
  396. (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
  397. return cd->default_insn_bitsize / 8;
  398. }