fr30-dis.c 22 KB

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