epiphany-dis.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 "epiphany-desc.h"
  28. #include "epiphany-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. /* -- dis.c */
  49. #define CGEN_PRINT_INSN epiphany_print_insn
  50. static int
  51. epiphany_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info)
  52. {
  53. bfd_byte buf[CGEN_MAX_INSN_SIZE];
  54. int buflen;
  55. int status;
  56. info->bytes_per_chunk = 2;
  57. info->bytes_per_line = 4;
  58. /* Attempt to read the base part of the insn. */
  59. buflen = cd->base_insn_bitsize / 8;
  60. status = (*info->read_memory_func) (pc, buf, buflen, info);
  61. /* Try again with the minimum part, if min < base. */
  62. if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
  63. {
  64. buflen = cd->min_insn_bitsize / 8;
  65. status = (*info->read_memory_func) (pc, buf, buflen, info);
  66. }
  67. if (status != 0)
  68. {
  69. (*info->memory_error_func) (status, pc, info);
  70. return -1;
  71. }
  72. return print_insn (cd, pc, info, buf, buflen);
  73. }
  74. static void
  75. print_postindex (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  76. void * dis_info,
  77. long value,
  78. unsigned int attrs ATTRIBUTE_UNUSED,
  79. bfd_vma pc ATTRIBUTE_UNUSED,
  80. int length ATTRIBUTE_UNUSED)
  81. {
  82. disassemble_info *info = (disassemble_info *) dis_info;
  83. (*info->fprintf_func) (info->stream, value ? "-" : "+");
  84. }
  85. static void
  86. print_simm_not_reg (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  87. void * dis_info,
  88. long value,
  89. unsigned int attrs ATTRIBUTE_UNUSED,
  90. bfd_vma pc ATTRIBUTE_UNUSED,
  91. int length ATTRIBUTE_UNUSED)
  92. {
  93. print_address (cd, dis_info, value, attrs, pc, length);
  94. }
  95. static void
  96. print_uimm_not_reg (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  97. void * dis_info,
  98. unsigned long value,
  99. unsigned int attrs ATTRIBUTE_UNUSED,
  100. bfd_vma pc ATTRIBUTE_UNUSED,
  101. int length ATTRIBUTE_UNUSED)
  102. {
  103. disassemble_info *info = (disassemble_info *)dis_info;
  104. if (value & 0x800)
  105. (*info->fprintf_func) (info->stream, "-");
  106. value &= 0x7ff;
  107. print_address (cd, dis_info, value, attrs, pc, length);
  108. }
  109. /* -- */
  110. void epiphany_cgen_print_operand
  111. (CGEN_CPU_DESC, int, PTR, CGEN_FIELDS *, void const *, bfd_vma, int);
  112. /* Main entry point for printing operands.
  113. XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
  114. of dis-asm.h on cgen.h.
  115. This function is basically just a big switch statement. Earlier versions
  116. used tables to look up the function to use, but
  117. - if the table contains both assembler and disassembler functions then
  118. the disassembler contains much of the assembler and vice-versa,
  119. - there's a lot of inlining possibilities as things grow,
  120. - using a switch statement avoids the function call overhead.
  121. This function could be moved into `print_insn_normal', but keeping it
  122. separate makes clear the interface between `print_insn_normal' and each of
  123. the handlers. */
  124. void
  125. epiphany_cgen_print_operand (CGEN_CPU_DESC cd,
  126. int opindex,
  127. void * xinfo,
  128. CGEN_FIELDS *fields,
  129. void const *attrs ATTRIBUTE_UNUSED,
  130. bfd_vma pc,
  131. int length)
  132. {
  133. disassemble_info *info = (disassemble_info *) xinfo;
  134. switch (opindex)
  135. {
  136. case EPIPHANY_OPERAND_DIRECTION :
  137. print_postindex (cd, info, fields->f_addsubx, 0, pc, length);
  138. break;
  139. case EPIPHANY_OPERAND_DISP11 :
  140. print_uimm_not_reg (cd, info, fields->f_disp11, 0|(1<<CGEN_OPERAND_VIRTUAL), pc, length);
  141. break;
  142. case EPIPHANY_OPERAND_DISP3 :
  143. print_normal (cd, info, fields->f_disp3, 0, pc, length);
  144. break;
  145. case EPIPHANY_OPERAND_DPMI :
  146. print_postindex (cd, info, fields->f_subd, 0, pc, length);
  147. break;
  148. case EPIPHANY_OPERAND_FRD :
  149. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rd, 0);
  150. break;
  151. case EPIPHANY_OPERAND_FRD6 :
  152. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rd6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  153. break;
  154. case EPIPHANY_OPERAND_FRM :
  155. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rm, 0);
  156. break;
  157. case EPIPHANY_OPERAND_FRM6 :
  158. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rm6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  159. break;
  160. case EPIPHANY_OPERAND_FRN :
  161. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rn, 0);
  162. break;
  163. case EPIPHANY_OPERAND_FRN6 :
  164. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rn6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  165. break;
  166. case EPIPHANY_OPERAND_IMM16 :
  167. print_address (cd, info, fields->f_imm16, 0|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_VIRTUAL), pc, length);
  168. break;
  169. case EPIPHANY_OPERAND_IMM8 :
  170. print_address (cd, info, fields->f_imm8, 0|(1<<CGEN_OPERAND_RELAX), pc, length);
  171. break;
  172. case EPIPHANY_OPERAND_RD :
  173. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rd, 0);
  174. break;
  175. case EPIPHANY_OPERAND_RD6 :
  176. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rd6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  177. break;
  178. case EPIPHANY_OPERAND_RM :
  179. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rm, 0);
  180. break;
  181. case EPIPHANY_OPERAND_RM6 :
  182. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rm6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  183. break;
  184. case EPIPHANY_OPERAND_RN :
  185. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rn, 0);
  186. break;
  187. case EPIPHANY_OPERAND_RN6 :
  188. print_keyword (cd, info, & epiphany_cgen_opval_gr_names, fields->f_rn6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  189. break;
  190. case EPIPHANY_OPERAND_SD :
  191. print_keyword (cd, info, & epiphany_cgen_opval_cr_names, fields->f_sd, 0);
  192. break;
  193. case EPIPHANY_OPERAND_SD6 :
  194. print_keyword (cd, info, & epiphany_cgen_opval_cr_names, fields->f_sd6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  195. break;
  196. case EPIPHANY_OPERAND_SDDMA :
  197. print_keyword (cd, info, & epiphany_cgen_opval_crdma_names, fields->f_sd6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  198. break;
  199. case EPIPHANY_OPERAND_SDMEM :
  200. print_keyword (cd, info, & epiphany_cgen_opval_crmem_names, fields->f_sd6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  201. break;
  202. case EPIPHANY_OPERAND_SDMESH :
  203. print_keyword (cd, info, & epiphany_cgen_opval_crmesh_names, fields->f_sd6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  204. break;
  205. case EPIPHANY_OPERAND_SHIFT :
  206. print_normal (cd, info, fields->f_shift, 0, pc, length);
  207. break;
  208. case EPIPHANY_OPERAND_SIMM11 :
  209. print_simm_not_reg (cd, info, fields->f_sdisp11, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_VIRTUAL), pc, length);
  210. break;
  211. case EPIPHANY_OPERAND_SIMM24 :
  212. print_address (cd, info, fields->f_simm24, 0|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
  213. break;
  214. case EPIPHANY_OPERAND_SIMM3 :
  215. print_simm_not_reg (cd, info, fields->f_sdisp3, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_RELAX), pc, length);
  216. break;
  217. case EPIPHANY_OPERAND_SIMM8 :
  218. print_address (cd, info, fields->f_simm8, 0|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
  219. break;
  220. case EPIPHANY_OPERAND_SN :
  221. print_keyword (cd, info, & epiphany_cgen_opval_cr_names, fields->f_sn, 0);
  222. break;
  223. case EPIPHANY_OPERAND_SN6 :
  224. print_keyword (cd, info, & epiphany_cgen_opval_cr_names, fields->f_sn6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  225. break;
  226. case EPIPHANY_OPERAND_SNDMA :
  227. print_keyword (cd, info, & epiphany_cgen_opval_crdma_names, fields->f_sn6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  228. break;
  229. case EPIPHANY_OPERAND_SNMEM :
  230. print_keyword (cd, info, & epiphany_cgen_opval_crmem_names, fields->f_sn6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  231. break;
  232. case EPIPHANY_OPERAND_SNMESH :
  233. print_keyword (cd, info, & epiphany_cgen_opval_crmesh_names, fields->f_sn6, 0|(1<<CGEN_OPERAND_VIRTUAL));
  234. break;
  235. case EPIPHANY_OPERAND_SWI_NUM :
  236. print_uimm_not_reg (cd, info, fields->f_trap_num, 0, pc, length);
  237. break;
  238. case EPIPHANY_OPERAND_TRAPNUM6 :
  239. print_normal (cd, info, fields->f_trap_num, 0, pc, length);
  240. break;
  241. default :
  242. /* xgettext:c-format */
  243. fprintf (stderr, _("Unrecognized field %d while printing insn.\n"),
  244. opindex);
  245. abort ();
  246. }
  247. }
  248. cgen_print_fn * const epiphany_cgen_print_handlers[] =
  249. {
  250. print_insn_normal,
  251. };
  252. void
  253. epiphany_cgen_init_dis (CGEN_CPU_DESC cd)
  254. {
  255. epiphany_cgen_init_opcode_table (cd);
  256. epiphany_cgen_init_ibld_table (cd);
  257. cd->print_handlers = & epiphany_cgen_print_handlers[0];
  258. cd->print_operand = epiphany_cgen_print_operand;
  259. }
  260. /* Default print handler. */
  261. static void
  262. print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  263. void *dis_info,
  264. long value,
  265. unsigned int attrs,
  266. bfd_vma pc ATTRIBUTE_UNUSED,
  267. int length ATTRIBUTE_UNUSED)
  268. {
  269. disassemble_info *info = (disassemble_info *) dis_info;
  270. /* Print the operand as directed by the attributes. */
  271. if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
  272. ; /* nothing to do */
  273. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
  274. (*info->fprintf_func) (info->stream, "%ld", value);
  275. else
  276. (*info->fprintf_func) (info->stream, "0x%lx", value);
  277. }
  278. /* Default address handler. */
  279. static void
  280. print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  281. void *dis_info,
  282. bfd_vma value,
  283. unsigned int attrs,
  284. bfd_vma pc ATTRIBUTE_UNUSED,
  285. int length ATTRIBUTE_UNUSED)
  286. {
  287. disassemble_info *info = (disassemble_info *) dis_info;
  288. /* Print the operand as directed by the attributes. */
  289. if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
  290. ; /* Nothing to do. */
  291. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR))
  292. (*info->print_address_func) (value, info);
  293. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR))
  294. (*info->print_address_func) (value, info);
  295. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
  296. (*info->fprintf_func) (info->stream, "%ld", (long) value);
  297. else
  298. (*info->fprintf_func) (info->stream, "0x%lx", (long) value);
  299. }
  300. /* Keyword print handler. */
  301. static void
  302. print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  303. void *dis_info,
  304. CGEN_KEYWORD *keyword_table,
  305. long value,
  306. unsigned int attrs ATTRIBUTE_UNUSED)
  307. {
  308. disassemble_info *info = (disassemble_info *) dis_info;
  309. const CGEN_KEYWORD_ENTRY *ke;
  310. ke = cgen_keyword_lookup_value (keyword_table, value);
  311. if (ke != NULL)
  312. (*info->fprintf_func) (info->stream, "%s", ke->name);
  313. else
  314. (*info->fprintf_func) (info->stream, "???");
  315. }
  316. /* Default insn printer.
  317. DIS_INFO is defined as `void *' so the disassembler needn't know anything
  318. about disassemble_info. */
  319. static void
  320. print_insn_normal (CGEN_CPU_DESC cd,
  321. void *dis_info,
  322. const CGEN_INSN *insn,
  323. CGEN_FIELDS *fields,
  324. bfd_vma pc,
  325. int length)
  326. {
  327. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  328. disassemble_info *info = (disassemble_info *) dis_info;
  329. const CGEN_SYNTAX_CHAR_TYPE *syn;
  330. CGEN_INIT_PRINT (cd);
  331. for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
  332. {
  333. if (CGEN_SYNTAX_MNEMONIC_P (*syn))
  334. {
  335. (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn));
  336. continue;
  337. }
  338. if (CGEN_SYNTAX_CHAR_P (*syn))
  339. {
  340. (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn));
  341. continue;
  342. }
  343. /* We have an operand. */
  344. epiphany_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info,
  345. fields, CGEN_INSN_ATTRS (insn), pc, length);
  346. }
  347. }
  348. /* Subroutine of print_insn. Reads an insn into the given buffers and updates
  349. the extract info.
  350. Returns 0 if all is well, non-zero otherwise. */
  351. static int
  352. read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  353. bfd_vma pc,
  354. disassemble_info *info,
  355. bfd_byte *buf,
  356. int buflen,
  357. CGEN_EXTRACT_INFO *ex_info,
  358. unsigned long *insn_value)
  359. {
  360. int status = (*info->read_memory_func) (pc, buf, buflen, info);
  361. if (status != 0)
  362. {
  363. (*info->memory_error_func) (status, pc, info);
  364. return -1;
  365. }
  366. ex_info->dis_info = info;
  367. ex_info->valid = (1 << buflen) - 1;
  368. ex_info->insn_bytes = buf;
  369. *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG);
  370. return 0;
  371. }
  372. /* Utility to print an insn.
  373. BUF is the base part of the insn, target byte order, BUFLEN bytes long.
  374. The result is the size of the insn in bytes or zero for an unknown insn
  375. or -1 if an error occurs fetching data (memory_error_func will have
  376. been called). */
  377. static int
  378. print_insn (CGEN_CPU_DESC cd,
  379. bfd_vma pc,
  380. disassemble_info *info,
  381. bfd_byte *buf,
  382. unsigned int buflen)
  383. {
  384. CGEN_INSN_INT insn_value;
  385. const CGEN_INSN_LIST *insn_list;
  386. CGEN_EXTRACT_INFO ex_info;
  387. int basesize;
  388. /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
  389. basesize = cd->base_insn_bitsize < buflen * 8 ?
  390. cd->base_insn_bitsize : buflen * 8;
  391. insn_value = cgen_get_insn_value (cd, buf, basesize);
  392. /* Fill in ex_info fields like read_insn would. Don't actually call
  393. read_insn, since the incoming buffer is already read (and possibly
  394. modified a la m32r). */
  395. ex_info.valid = (1 << buflen) - 1;
  396. ex_info.dis_info = info;
  397. ex_info.insn_bytes = buf;
  398. /* The instructions are stored in hash lists.
  399. Pick the first one and keep trying until we find the right one. */
  400. insn_list = CGEN_DIS_LOOKUP_INSN (cd, (char *) buf, insn_value);
  401. while (insn_list != NULL)
  402. {
  403. const CGEN_INSN *insn = insn_list->insn;
  404. CGEN_FIELDS fields;
  405. int length;
  406. unsigned long insn_value_cropped;
  407. #ifdef CGEN_VALIDATE_INSN_SUPPORTED
  408. /* Not needed as insn shouldn't be in hash lists if not supported. */
  409. /* Supported by this cpu? */
  410. if (! epiphany_cgen_insn_supported (cd, insn))
  411. {
  412. insn_list = CGEN_DIS_NEXT_INSN (insn_list);
  413. continue;
  414. }
  415. #endif
  416. /* Basic bit mask must be correct. */
  417. /* ??? May wish to allow target to defer this check until the extract
  418. handler. */
  419. /* Base size may exceed this instruction's size. Extract the
  420. relevant part from the buffer. */
  421. if ((unsigned) (CGEN_INSN_BITSIZE (insn) / 8) < buflen &&
  422. (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
  423. insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn),
  424. info->endian == BFD_ENDIAN_BIG);
  425. else
  426. insn_value_cropped = insn_value;
  427. if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn))
  428. == CGEN_INSN_BASE_VALUE (insn))
  429. {
  430. /* Printing is handled in two passes. The first pass parses the
  431. machine insn and extracts the fields. The second pass prints
  432. them. */
  433. /* Make sure the entire insn is loaded into insn_value, if it
  434. can fit. */
  435. if (((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize) &&
  436. (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
  437. {
  438. unsigned long full_insn_value;
  439. int rc = read_insn (cd, pc, info, buf,
  440. CGEN_INSN_BITSIZE (insn) / 8,
  441. & ex_info, & full_insn_value);
  442. if (rc != 0)
  443. return rc;
  444. length = CGEN_EXTRACT_FN (cd, insn)
  445. (cd, insn, &ex_info, full_insn_value, &fields, pc);
  446. }
  447. else
  448. length = CGEN_EXTRACT_FN (cd, insn)
  449. (cd, insn, &ex_info, insn_value_cropped, &fields, pc);
  450. /* Length < 0 -> error. */
  451. if (length < 0)
  452. return length;
  453. if (length > 0)
  454. {
  455. CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length);
  456. /* Length is in bits, result is in bytes. */
  457. return length / 8;
  458. }
  459. }
  460. insn_list = CGEN_DIS_NEXT_INSN (insn_list);
  461. }
  462. return 0;
  463. }
  464. /* Default value for CGEN_PRINT_INSN.
  465. The result is the size of the insn in bytes or zero for an unknown insn
  466. or -1 if an error occured fetching bytes. */
  467. #ifndef CGEN_PRINT_INSN
  468. #define CGEN_PRINT_INSN default_print_insn
  469. #endif
  470. static int
  471. default_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info)
  472. {
  473. bfd_byte buf[CGEN_MAX_INSN_SIZE];
  474. int buflen;
  475. int status;
  476. /* Attempt to read the base part of the insn. */
  477. buflen = cd->base_insn_bitsize / 8;
  478. status = (*info->read_memory_func) (pc, buf, buflen, info);
  479. /* Try again with the minimum part, if min < base. */
  480. if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
  481. {
  482. buflen = cd->min_insn_bitsize / 8;
  483. status = (*info->read_memory_func) (pc, buf, buflen, info);
  484. }
  485. if (status != 0)
  486. {
  487. (*info->memory_error_func) (status, pc, info);
  488. return -1;
  489. }
  490. return print_insn (cd, pc, info, buf, buflen);
  491. }
  492. /* Main entry point.
  493. Print one instruction from PC on INFO->STREAM.
  494. Return the size of the instruction (in bytes). */
  495. typedef struct cpu_desc_list
  496. {
  497. struct cpu_desc_list *next;
  498. CGEN_BITSET *isa;
  499. int mach;
  500. int endian;
  501. CGEN_CPU_DESC cd;
  502. } cpu_desc_list;
  503. int
  504. print_insn_epiphany (bfd_vma pc, disassemble_info *info)
  505. {
  506. static cpu_desc_list *cd_list = 0;
  507. cpu_desc_list *cl = 0;
  508. static CGEN_CPU_DESC cd = 0;
  509. static CGEN_BITSET *prev_isa;
  510. static int prev_mach;
  511. static int prev_endian;
  512. int length;
  513. CGEN_BITSET *isa;
  514. int mach;
  515. int endian = (info->endian == BFD_ENDIAN_BIG
  516. ? CGEN_ENDIAN_BIG
  517. : CGEN_ENDIAN_LITTLE);
  518. enum bfd_architecture arch;
  519. /* ??? gdb will set mach but leave the architecture as "unknown" */
  520. #ifndef CGEN_BFD_ARCH
  521. #define CGEN_BFD_ARCH bfd_arch_epiphany
  522. #endif
  523. arch = info->arch;
  524. if (arch == bfd_arch_unknown)
  525. arch = CGEN_BFD_ARCH;
  526. /* There's no standard way to compute the machine or isa number
  527. so we leave it to the target. */
  528. #ifdef CGEN_COMPUTE_MACH
  529. mach = CGEN_COMPUTE_MACH (info);
  530. #else
  531. mach = info->mach;
  532. #endif
  533. #ifdef CGEN_COMPUTE_ISA
  534. {
  535. static CGEN_BITSET *permanent_isa;
  536. if (!permanent_isa)
  537. permanent_isa = cgen_bitset_create (MAX_ISAS);
  538. isa = permanent_isa;
  539. cgen_bitset_clear (isa);
  540. cgen_bitset_add (isa, CGEN_COMPUTE_ISA (info));
  541. }
  542. #else
  543. isa = info->insn_sets;
  544. #endif
  545. /* If we've switched cpu's, try to find a handle we've used before */
  546. if (cd
  547. && (cgen_bitset_compare (isa, prev_isa) != 0
  548. || mach != prev_mach
  549. || endian != prev_endian))
  550. {
  551. cd = 0;
  552. for (cl = cd_list; cl; cl = cl->next)
  553. {
  554. if (cgen_bitset_compare (cl->isa, isa) == 0 &&
  555. cl->mach == mach &&
  556. cl->endian == endian)
  557. {
  558. cd = cl->cd;
  559. prev_isa = cd->isas;
  560. break;
  561. }
  562. }
  563. }
  564. /* If we haven't initialized yet, initialize the opcode table. */
  565. if (! cd)
  566. {
  567. const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach);
  568. const char *mach_name;
  569. if (!arch_type)
  570. abort ();
  571. mach_name = arch_type->printable_name;
  572. prev_isa = cgen_bitset_copy (isa);
  573. prev_mach = mach;
  574. prev_endian = endian;
  575. cd = epiphany_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa,
  576. CGEN_CPU_OPEN_BFDMACH, mach_name,
  577. CGEN_CPU_OPEN_ENDIAN, prev_endian,
  578. CGEN_CPU_OPEN_END);
  579. if (!cd)
  580. abort ();
  581. /* Save this away for future reference. */
  582. cl = xmalloc (sizeof (struct cpu_desc_list));
  583. cl->cd = cd;
  584. cl->isa = prev_isa;
  585. cl->mach = mach;
  586. cl->endian = endian;
  587. cl->next = cd_list;
  588. cd_list = cl;
  589. epiphany_cgen_init_dis (cd);
  590. }
  591. /* We try to have as much common code as possible.
  592. But at this point some targets need to take over. */
  593. /* ??? Some targets may need a hook elsewhere. Try to avoid this,
  594. but if not possible try to move this hook elsewhere rather than
  595. have two hooks. */
  596. length = CGEN_PRINT_INSN (cd, pc, info);
  597. if (length > 0)
  598. return length;
  599. if (length < 0)
  600. return -1;
  601. (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
  602. return cd->default_insn_bitsize / 8;
  603. }