mt-dis.c 21 KB

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