ip2k-dis.c 20 KB

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