mt-dis.c 22 KB

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