xc16x-dis.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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 "xc16x-desc.h"
  29. #include "xc16x-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. /* Print an operand with a "." prefix.
  51. NOTE: This prints the operand in hex.
  52. ??? This exists to maintain disassembler compatibility with previous
  53. versions. Ideally we'd print the "." in print_dot. */
  54. static void
  55. print_with_dot_prefix (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  56. void * dis_info,
  57. long value,
  58. unsigned attrs ATTRIBUTE_UNUSED,
  59. bfd_vma pc ATTRIBUTE_UNUSED,
  60. int length ATTRIBUTE_UNUSED)
  61. {
  62. disassemble_info *info = (disassemble_info *) dis_info;
  63. info->fprintf_func (info->stream, ".");
  64. info->fprintf_func (info->stream, "0x%lx", value);
  65. }
  66. /* Print an operand with a "#pof:" prefix.
  67. NOTE: This prints the operand as an address.
  68. ??? This exists to maintain disassembler compatibility with previous
  69. versions. Ideally we'd print "#pof:" in print_pof. */
  70. static void
  71. print_with_pof_prefix (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  72. void * dis_info,
  73. bfd_vma value,
  74. unsigned attrs ATTRIBUTE_UNUSED,
  75. bfd_vma pc ATTRIBUTE_UNUSED,
  76. int length ATTRIBUTE_UNUSED)
  77. {
  78. disassemble_info *info = (disassemble_info *) dis_info;
  79. info->fprintf_func (info->stream, "#pof:");
  80. info->fprintf_func (info->stream, "0x%lx", (long) value);
  81. }
  82. /* Print an operand with a "#pag:" prefix.
  83. NOTE: This prints the operand in hex.
  84. ??? This exists to maintain disassembler compatibility with previous
  85. versions. Ideally we'd print "#pag:" in print_pag. */
  86. static void
  87. print_with_pag_prefix (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  88. void * dis_info,
  89. long value,
  90. unsigned attrs ATTRIBUTE_UNUSED,
  91. bfd_vma pc ATTRIBUTE_UNUSED,
  92. int length ATTRIBUTE_UNUSED)
  93. {
  94. disassemble_info *info = (disassemble_info *) dis_info;
  95. info->fprintf_func (info->stream, "#pag:");
  96. info->fprintf_func (info->stream, "0x%lx", value);
  97. }
  98. /* Print a 'pof:' prefix to an operand. */
  99. static void
  100. print_pof (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  101. void * dis_info ATTRIBUTE_UNUSED,
  102. long value ATTRIBUTE_UNUSED,
  103. unsigned int attrs ATTRIBUTE_UNUSED,
  104. bfd_vma pc ATTRIBUTE_UNUSED,
  105. int length ATTRIBUTE_UNUSED)
  106. {
  107. }
  108. /* Print a 'pag:' prefix to an operand. */
  109. static void
  110. print_pag (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  111. void * dis_info ATTRIBUTE_UNUSED,
  112. long value ATTRIBUTE_UNUSED,
  113. unsigned int attrs ATTRIBUTE_UNUSED,
  114. bfd_vma pc ATTRIBUTE_UNUSED,
  115. int length ATTRIBUTE_UNUSED)
  116. {
  117. }
  118. /* Print a 'sof:' prefix to an operand. */
  119. static void
  120. print_sof (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  121. void * dis_info,
  122. long value ATTRIBUTE_UNUSED,
  123. unsigned int attrs ATTRIBUTE_UNUSED,
  124. bfd_vma pc ATTRIBUTE_UNUSED,
  125. int length ATTRIBUTE_UNUSED)
  126. {
  127. disassemble_info *info = (disassemble_info *) dis_info;
  128. info->fprintf_func (info->stream, "sof:");
  129. }
  130. /* Print a 'seg:' prefix to an operand. */
  131. static void
  132. print_seg (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  133. void * dis_info,
  134. long value ATTRIBUTE_UNUSED,
  135. unsigned int attrs ATTRIBUTE_UNUSED,
  136. bfd_vma pc ATTRIBUTE_UNUSED,
  137. int length ATTRIBUTE_UNUSED)
  138. {
  139. disassemble_info *info = (disassemble_info *) dis_info;
  140. info->fprintf_func (info->stream, "seg:");
  141. }
  142. /* Print a '#' prefix to an operand. */
  143. static void
  144. print_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  145. void * dis_info,
  146. long value ATTRIBUTE_UNUSED,
  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. info->fprintf_func (info->stream, "#");
  153. }
  154. /* Print a '.' prefix to an operand. */
  155. static void
  156. print_dot (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  157. void * dis_info ATTRIBUTE_UNUSED,
  158. long value ATTRIBUTE_UNUSED,
  159. unsigned int attrs ATTRIBUTE_UNUSED,
  160. bfd_vma pc ATTRIBUTE_UNUSED,
  161. int length ATTRIBUTE_UNUSED)
  162. {
  163. }
  164. /* -- */
  165. void xc16x_cgen_print_operand
  166. (CGEN_CPU_DESC, int, PTR, CGEN_FIELDS *, void const *, bfd_vma, int);
  167. /* Main entry point for printing operands.
  168. XINFO is a `void *' and not a `disassemble_info *' to not put a requirement
  169. of dis-asm.h on cgen.h.
  170. This function is basically just a big switch statement. Earlier versions
  171. used tables to look up the function to use, but
  172. - if the table contains both assembler and disassembler functions then
  173. the disassembler contains much of the assembler and vice-versa,
  174. - there's a lot of inlining possibilities as things grow,
  175. - using a switch statement avoids the function call overhead.
  176. This function could be moved into `print_insn_normal', but keeping it
  177. separate makes clear the interface between `print_insn_normal' and each of
  178. the handlers. */
  179. void
  180. xc16x_cgen_print_operand (CGEN_CPU_DESC cd,
  181. int opindex,
  182. void * xinfo,
  183. CGEN_FIELDS *fields,
  184. void const *attrs ATTRIBUTE_UNUSED,
  185. bfd_vma pc,
  186. int length)
  187. {
  188. disassemble_info *info = (disassemble_info *) xinfo;
  189. switch (opindex)
  190. {
  191. case XC16X_OPERAND_REGNAM :
  192. print_keyword (cd, info, & xc16x_cgen_opval_psw_names, fields->f_reg8, 0);
  193. break;
  194. case XC16X_OPERAND_BIT01 :
  195. print_normal (cd, info, fields->f_op_1bit, 0, pc, length);
  196. break;
  197. case XC16X_OPERAND_BIT1 :
  198. print_normal (cd, info, fields->f_op_bit1, 0, pc, length);
  199. break;
  200. case XC16X_OPERAND_BIT2 :
  201. print_normal (cd, info, fields->f_op_bit2, 0, pc, length);
  202. break;
  203. case XC16X_OPERAND_BIT4 :
  204. print_normal (cd, info, fields->f_op_bit4, 0, pc, length);
  205. break;
  206. case XC16X_OPERAND_BIT8 :
  207. print_normal (cd, info, fields->f_op_bit8, 0, pc, length);
  208. break;
  209. case XC16X_OPERAND_BITONE :
  210. print_normal (cd, info, fields->f_op_onebit, 0, pc, length);
  211. break;
  212. case XC16X_OPERAND_CADDR :
  213. print_address (cd, info, fields->f_offset16, 0|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_ABS_ADDR), pc, length);
  214. break;
  215. case XC16X_OPERAND_COND :
  216. print_keyword (cd, info, & xc16x_cgen_opval_conditioncode_names, fields->f_condcode, 0);
  217. break;
  218. case XC16X_OPERAND_DATA8 :
  219. print_normal (cd, info, fields->f_data8, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
  220. break;
  221. case XC16X_OPERAND_DATAHI8 :
  222. print_normal (cd, info, fields->f_datahi8, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
  223. break;
  224. case XC16X_OPERAND_DOT :
  225. print_dot (cd, info, 0, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  226. break;
  227. case XC16X_OPERAND_DR :
  228. print_keyword (cd, info, & xc16x_cgen_opval_gr_names, fields->f_r1, 0);
  229. break;
  230. case XC16X_OPERAND_DRB :
  231. print_keyword (cd, info, & xc16x_cgen_opval_grb_names, fields->f_r1, 0);
  232. break;
  233. case XC16X_OPERAND_DRI :
  234. print_keyword (cd, info, & xc16x_cgen_opval_gr_names, fields->f_r4, 0);
  235. break;
  236. case XC16X_OPERAND_EXTCOND :
  237. print_keyword (cd, info, & xc16x_cgen_opval_extconditioncode_names, fields->f_extccode, 0);
  238. break;
  239. case XC16X_OPERAND_GENREG :
  240. print_keyword (cd, info, & xc16x_cgen_opval_r8_names, fields->f_regb8, 0);
  241. break;
  242. case XC16X_OPERAND_HASH :
  243. print_hash (cd, info, 0, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  244. break;
  245. case XC16X_OPERAND_ICOND :
  246. print_keyword (cd, info, & xc16x_cgen_opval_conditioncode_names, fields->f_icondcode, 0);
  247. break;
  248. case XC16X_OPERAND_LBIT2 :
  249. print_normal (cd, info, fields->f_op_lbit2, 0, pc, length);
  250. break;
  251. case XC16X_OPERAND_LBIT4 :
  252. print_normal (cd, info, fields->f_op_lbit4, 0, pc, length);
  253. break;
  254. case XC16X_OPERAND_MASK8 :
  255. print_normal (cd, info, fields->f_mask8, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
  256. break;
  257. case XC16X_OPERAND_MASKLO8 :
  258. print_normal (cd, info, fields->f_datahi8, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
  259. break;
  260. case XC16X_OPERAND_MEMGR8 :
  261. print_keyword (cd, info, & xc16x_cgen_opval_memgr8_names, fields->f_memgr8, 0);
  262. break;
  263. case XC16X_OPERAND_MEMORY :
  264. print_address (cd, info, fields->f_memory, 0, pc, length);
  265. break;
  266. case XC16X_OPERAND_PAG :
  267. print_pag (cd, info, 0, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  268. break;
  269. case XC16X_OPERAND_PAGENUM :
  270. print_normal (cd, info, fields->f_pagenum, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
  271. break;
  272. case XC16X_OPERAND_POF :
  273. print_pof (cd, info, 0, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  274. break;
  275. case XC16X_OPERAND_QBIT :
  276. print_with_dot_prefix (cd, info, fields->f_qbit, 0|(1<<CGEN_OPERAND_DOT_PREFIX), pc, length);
  277. break;
  278. case XC16X_OPERAND_QHIBIT :
  279. print_with_dot_prefix (cd, info, fields->f_qhibit, 0|(1<<CGEN_OPERAND_DOT_PREFIX), pc, length);
  280. break;
  281. case XC16X_OPERAND_QLOBIT :
  282. print_with_dot_prefix (cd, info, fields->f_qlobit, 0|(1<<CGEN_OPERAND_DOT_PREFIX), pc, length);
  283. break;
  284. case XC16X_OPERAND_REG8 :
  285. print_keyword (cd, info, & xc16x_cgen_opval_r8_names, fields->f_reg8, 0);
  286. break;
  287. case XC16X_OPERAND_REGB8 :
  288. print_keyword (cd, info, & xc16x_cgen_opval_grb8_names, fields->f_regb8, 0);
  289. break;
  290. case XC16X_OPERAND_REGBMEM8 :
  291. print_keyword (cd, info, & xc16x_cgen_opval_regbmem8_names, fields->f_regmem8, 0);
  292. break;
  293. case XC16X_OPERAND_REGHI8 :
  294. print_keyword (cd, info, & xc16x_cgen_opval_r8_names, fields->f_reghi8, 0);
  295. break;
  296. case XC16X_OPERAND_REGMEM8 :
  297. print_keyword (cd, info, & xc16x_cgen_opval_regmem8_names, fields->f_regmem8, 0);
  298. break;
  299. case XC16X_OPERAND_REGOFF8 :
  300. print_keyword (cd, info, & xc16x_cgen_opval_r8_names, fields->f_regoff8, 0);
  301. break;
  302. case XC16X_OPERAND_REL :
  303. print_normal (cd, info, fields->f_rel8, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
  304. break;
  305. case XC16X_OPERAND_RELHI :
  306. print_normal (cd, info, fields->f_relhi8, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
  307. break;
  308. case XC16X_OPERAND_SEG :
  309. print_normal (cd, info, fields->f_seg8, 0, pc, length);
  310. break;
  311. case XC16X_OPERAND_SEGHI8 :
  312. print_normal (cd, info, fields->f_segnum8, 0, pc, length);
  313. break;
  314. case XC16X_OPERAND_SEGM :
  315. print_seg (cd, info, 0, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  316. break;
  317. case XC16X_OPERAND_SOF :
  318. print_sof (cd, info, 0, 0|(1<<CGEN_OPERAND_SIGNED), pc, length);
  319. break;
  320. case XC16X_OPERAND_SR :
  321. print_keyword (cd, info, & xc16x_cgen_opval_gr_names, fields->f_r2, 0);
  322. break;
  323. case XC16X_OPERAND_SR2 :
  324. print_keyword (cd, info, & xc16x_cgen_opval_gr_names, fields->f_r0, 0);
  325. break;
  326. case XC16X_OPERAND_SRB :
  327. print_keyword (cd, info, & xc16x_cgen_opval_grb_names, fields->f_r2, 0);
  328. break;
  329. case XC16X_OPERAND_SRC1 :
  330. print_keyword (cd, info, & xc16x_cgen_opval_gr_names, fields->f_r1, 0);
  331. break;
  332. case XC16X_OPERAND_SRC2 :
  333. print_keyword (cd, info, & xc16x_cgen_opval_gr_names, fields->f_r2, 0);
  334. break;
  335. case XC16X_OPERAND_SRDIV :
  336. print_keyword (cd, info, & xc16x_cgen_opval_regdiv8_names, fields->f_reg8, 0);
  337. break;
  338. case XC16X_OPERAND_U4 :
  339. print_keyword (cd, info, & xc16x_cgen_opval_reg0_name, fields->f_uimm4, 0);
  340. break;
  341. case XC16X_OPERAND_UIMM16 :
  342. print_normal (cd, info, fields->f_uimm16, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
  343. break;
  344. case XC16X_OPERAND_UIMM2 :
  345. print_keyword (cd, info, & xc16x_cgen_opval_ext_names, fields->f_uimm2, 0|(1<<CGEN_OPERAND_HASH_PREFIX));
  346. break;
  347. case XC16X_OPERAND_UIMM3 :
  348. print_keyword (cd, info, & xc16x_cgen_opval_reg0_name1, fields->f_uimm3, 0|(1<<CGEN_OPERAND_HASH_PREFIX));
  349. break;
  350. case XC16X_OPERAND_UIMM4 :
  351. print_normal (cd, info, fields->f_uimm4, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
  352. break;
  353. case XC16X_OPERAND_UIMM7 :
  354. print_normal (cd, info, fields->f_uimm7, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length);
  355. break;
  356. case XC16X_OPERAND_UIMM8 :
  357. print_normal (cd, info, fields->f_uimm8, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length);
  358. break;
  359. case XC16X_OPERAND_UPAG16 :
  360. print_with_pag_prefix (cd, info, fields->f_uimm16, 0|(1<<CGEN_OPERAND_PAG_PREFIX), pc, length);
  361. break;
  362. case XC16X_OPERAND_UPOF16 :
  363. print_with_pof_prefix (cd, info, fields->f_memory, 0|(1<<CGEN_OPERAND_POF_PREFIX), pc, length);
  364. break;
  365. case XC16X_OPERAND_USEG16 :
  366. print_normal (cd, info, fields->f_offset16, 0|(1<<CGEN_OPERAND_SEG_PREFIX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_ABS_ADDR), pc, length);
  367. break;
  368. case XC16X_OPERAND_USEG8 :
  369. print_normal (cd, info, fields->f_seg8, 0|(1<<CGEN_OPERAND_SEG_PREFIX), pc, length);
  370. break;
  371. case XC16X_OPERAND_USOF16 :
  372. print_normal (cd, info, fields->f_offset16, 0|(1<<CGEN_OPERAND_SOF_PREFIX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_ABS_ADDR), pc, length);
  373. break;
  374. default :
  375. /* xgettext:c-format */
  376. opcodes_error_handler
  377. (_("internal error: unrecognized field %d while printing insn"),
  378. opindex);
  379. abort ();
  380. }
  381. }
  382. cgen_print_fn * const xc16x_cgen_print_handlers[] =
  383. {
  384. print_insn_normal,
  385. };
  386. void
  387. xc16x_cgen_init_dis (CGEN_CPU_DESC cd)
  388. {
  389. xc16x_cgen_init_opcode_table (cd);
  390. xc16x_cgen_init_ibld_table (cd);
  391. cd->print_handlers = & xc16x_cgen_print_handlers[0];
  392. cd->print_operand = xc16x_cgen_print_operand;
  393. }
  394. /* Default print handler. */
  395. static void
  396. print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  397. void *dis_info,
  398. long value,
  399. unsigned int attrs,
  400. bfd_vma pc ATTRIBUTE_UNUSED,
  401. int length ATTRIBUTE_UNUSED)
  402. {
  403. disassemble_info *info = (disassemble_info *) dis_info;
  404. /* Print the operand as directed by the attributes. */
  405. if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
  406. ; /* nothing to do */
  407. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
  408. (*info->fprintf_func) (info->stream, "%ld", value);
  409. else
  410. (*info->fprintf_func) (info->stream, "0x%lx", value);
  411. }
  412. /* Default address handler. */
  413. static void
  414. print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  415. void *dis_info,
  416. bfd_vma value,
  417. unsigned int attrs,
  418. bfd_vma pc ATTRIBUTE_UNUSED,
  419. int length ATTRIBUTE_UNUSED)
  420. {
  421. disassemble_info *info = (disassemble_info *) dis_info;
  422. /* Print the operand as directed by the attributes. */
  423. if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY))
  424. ; /* Nothing to do. */
  425. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR))
  426. (*info->print_address_func) (value, info);
  427. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR))
  428. (*info->print_address_func) (value, info);
  429. else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED))
  430. (*info->fprintf_func) (info->stream, "%ld", (long) value);
  431. else
  432. (*info->fprintf_func) (info->stream, "0x%lx", (long) value);
  433. }
  434. /* Keyword print handler. */
  435. static void
  436. print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  437. void *dis_info,
  438. CGEN_KEYWORD *keyword_table,
  439. long value,
  440. unsigned int attrs ATTRIBUTE_UNUSED)
  441. {
  442. disassemble_info *info = (disassemble_info *) dis_info;
  443. const CGEN_KEYWORD_ENTRY *ke;
  444. ke = cgen_keyword_lookup_value (keyword_table, value);
  445. if (ke != NULL)
  446. (*info->fprintf_func) (info->stream, "%s", ke->name);
  447. else
  448. (*info->fprintf_func) (info->stream, "???");
  449. }
  450. /* Default insn printer.
  451. DIS_INFO is defined as `void *' so the disassembler needn't know anything
  452. about disassemble_info. */
  453. static void
  454. print_insn_normal (CGEN_CPU_DESC cd,
  455. void *dis_info,
  456. const CGEN_INSN *insn,
  457. CGEN_FIELDS *fields,
  458. bfd_vma pc,
  459. int length)
  460. {
  461. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  462. disassemble_info *info = (disassemble_info *) dis_info;
  463. const CGEN_SYNTAX_CHAR_TYPE *syn;
  464. CGEN_INIT_PRINT (cd);
  465. for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
  466. {
  467. if (CGEN_SYNTAX_MNEMONIC_P (*syn))
  468. {
  469. (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn));
  470. continue;
  471. }
  472. if (CGEN_SYNTAX_CHAR_P (*syn))
  473. {
  474. (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn));
  475. continue;
  476. }
  477. /* We have an operand. */
  478. xc16x_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info,
  479. fields, CGEN_INSN_ATTRS (insn), pc, length);
  480. }
  481. }
  482. /* Subroutine of print_insn. Reads an insn into the given buffers and updates
  483. the extract info.
  484. Returns 0 if all is well, non-zero otherwise. */
  485. static int
  486. read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  487. bfd_vma pc,
  488. disassemble_info *info,
  489. bfd_byte *buf,
  490. int buflen,
  491. CGEN_EXTRACT_INFO *ex_info,
  492. unsigned long *insn_value)
  493. {
  494. int status = (*info->read_memory_func) (pc, buf, buflen, info);
  495. if (status != 0)
  496. {
  497. (*info->memory_error_func) (status, pc, info);
  498. return -1;
  499. }
  500. ex_info->dis_info = info;
  501. ex_info->valid = (1 << buflen) - 1;
  502. ex_info->insn_bytes = buf;
  503. *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG);
  504. return 0;
  505. }
  506. /* Utility to print an insn.
  507. BUF is the base part of the insn, target byte order, BUFLEN bytes long.
  508. The result is the size of the insn in bytes or zero for an unknown insn
  509. or -1 if an error occurs fetching data (memory_error_func will have
  510. been called). */
  511. static int
  512. print_insn (CGEN_CPU_DESC cd,
  513. bfd_vma pc,
  514. disassemble_info *info,
  515. bfd_byte *buf,
  516. unsigned int buflen)
  517. {
  518. CGEN_INSN_INT insn_value;
  519. const CGEN_INSN_LIST *insn_list;
  520. CGEN_EXTRACT_INFO ex_info;
  521. int basesize;
  522. /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */
  523. basesize = cd->base_insn_bitsize < buflen * 8 ?
  524. cd->base_insn_bitsize : buflen * 8;
  525. insn_value = cgen_get_insn_value (cd, buf, basesize, cd->insn_endian);
  526. /* Fill in ex_info fields like read_insn would. Don't actually call
  527. read_insn, since the incoming buffer is already read (and possibly
  528. modified a la m32r). */
  529. ex_info.valid = (1 << buflen) - 1;
  530. ex_info.dis_info = info;
  531. ex_info.insn_bytes = buf;
  532. /* The instructions are stored in hash lists.
  533. Pick the first one and keep trying until we find the right one. */
  534. insn_list = CGEN_DIS_LOOKUP_INSN (cd, (char *) buf, insn_value);
  535. while (insn_list != NULL)
  536. {
  537. const CGEN_INSN *insn = insn_list->insn;
  538. CGEN_FIELDS fields;
  539. int length;
  540. unsigned long insn_value_cropped;
  541. #ifdef CGEN_VALIDATE_INSN_SUPPORTED
  542. /* Not needed as insn shouldn't be in hash lists if not supported. */
  543. /* Supported by this cpu? */
  544. if (! xc16x_cgen_insn_supported (cd, insn))
  545. {
  546. insn_list = CGEN_DIS_NEXT_INSN (insn_list);
  547. continue;
  548. }
  549. #endif
  550. /* Basic bit mask must be correct. */
  551. /* ??? May wish to allow target to defer this check until the extract
  552. handler. */
  553. /* Base size may exceed this instruction's size. Extract the
  554. relevant part from the buffer. */
  555. if ((unsigned) (CGEN_INSN_BITSIZE (insn) / 8) < buflen &&
  556. (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
  557. insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn),
  558. info->endian == BFD_ENDIAN_BIG);
  559. else
  560. insn_value_cropped = insn_value;
  561. if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn))
  562. == CGEN_INSN_BASE_VALUE (insn))
  563. {
  564. /* Printing is handled in two passes. The first pass parses the
  565. machine insn and extracts the fields. The second pass prints
  566. them. */
  567. /* Make sure the entire insn is loaded into insn_value, if it
  568. can fit. */
  569. if (((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize) &&
  570. (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long))
  571. {
  572. unsigned long full_insn_value;
  573. int rc = read_insn (cd, pc, info, buf,
  574. CGEN_INSN_BITSIZE (insn) / 8,
  575. & ex_info, & full_insn_value);
  576. if (rc != 0)
  577. return rc;
  578. length = CGEN_EXTRACT_FN (cd, insn)
  579. (cd, insn, &ex_info, full_insn_value, &fields, pc);
  580. }
  581. else
  582. length = CGEN_EXTRACT_FN (cd, insn)
  583. (cd, insn, &ex_info, insn_value_cropped, &fields, pc);
  584. /* Length < 0 -> error. */
  585. if (length < 0)
  586. return length;
  587. if (length > 0)
  588. {
  589. CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length);
  590. /* Length is in bits, result is in bytes. */
  591. return length / 8;
  592. }
  593. }
  594. insn_list = CGEN_DIS_NEXT_INSN (insn_list);
  595. }
  596. return 0;
  597. }
  598. /* Default value for CGEN_PRINT_INSN.
  599. The result is the size of the insn in bytes or zero for an unknown insn
  600. or -1 if an error occured fetching bytes. */
  601. #ifndef CGEN_PRINT_INSN
  602. #define CGEN_PRINT_INSN default_print_insn
  603. #endif
  604. static int
  605. default_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info)
  606. {
  607. bfd_byte buf[CGEN_MAX_INSN_SIZE];
  608. int buflen;
  609. int status;
  610. /* Attempt to read the base part of the insn. */
  611. buflen = cd->base_insn_bitsize / 8;
  612. status = (*info->read_memory_func) (pc, buf, buflen, info);
  613. /* Try again with the minimum part, if min < base. */
  614. if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize))
  615. {
  616. buflen = cd->min_insn_bitsize / 8;
  617. status = (*info->read_memory_func) (pc, buf, buflen, info);
  618. }
  619. if (status != 0)
  620. {
  621. (*info->memory_error_func) (status, pc, info);
  622. return -1;
  623. }
  624. return print_insn (cd, pc, info, buf, buflen);
  625. }
  626. /* Main entry point.
  627. Print one instruction from PC on INFO->STREAM.
  628. Return the size of the instruction (in bytes). */
  629. typedef struct cpu_desc_list
  630. {
  631. struct cpu_desc_list *next;
  632. CGEN_BITSET *isa;
  633. int mach;
  634. int endian;
  635. int insn_endian;
  636. CGEN_CPU_DESC cd;
  637. } cpu_desc_list;
  638. int
  639. print_insn_xc16x (bfd_vma pc, disassemble_info *info)
  640. {
  641. static cpu_desc_list *cd_list = 0;
  642. cpu_desc_list *cl = 0;
  643. static CGEN_CPU_DESC cd = 0;
  644. static CGEN_BITSET *prev_isa;
  645. static int prev_mach;
  646. static int prev_endian;
  647. static int prev_insn_endian;
  648. int length;
  649. CGEN_BITSET *isa;
  650. int mach;
  651. int endian = (info->endian == BFD_ENDIAN_BIG
  652. ? CGEN_ENDIAN_BIG
  653. : CGEN_ENDIAN_LITTLE);
  654. int insn_endian = (info->endian_code == BFD_ENDIAN_BIG
  655. ? CGEN_ENDIAN_BIG
  656. : CGEN_ENDIAN_LITTLE);
  657. enum bfd_architecture arch;
  658. /* ??? gdb will set mach but leave the architecture as "unknown" */
  659. #ifndef CGEN_BFD_ARCH
  660. #define CGEN_BFD_ARCH bfd_arch_xc16x
  661. #endif
  662. arch = info->arch;
  663. if (arch == bfd_arch_unknown)
  664. arch = CGEN_BFD_ARCH;
  665. /* There's no standard way to compute the machine or isa number
  666. so we leave it to the target. */
  667. #ifdef CGEN_COMPUTE_MACH
  668. mach = CGEN_COMPUTE_MACH (info);
  669. #else
  670. mach = info->mach;
  671. #endif
  672. #ifdef CGEN_COMPUTE_ISA
  673. {
  674. static CGEN_BITSET *permanent_isa;
  675. if (!permanent_isa)
  676. permanent_isa = cgen_bitset_create (MAX_ISAS);
  677. isa = permanent_isa;
  678. cgen_bitset_clear (isa);
  679. cgen_bitset_add (isa, CGEN_COMPUTE_ISA (info));
  680. }
  681. #else
  682. isa = info->private_data;
  683. #endif
  684. /* If we've switched cpu's, try to find a handle we've used before */
  685. if (cd
  686. && (cgen_bitset_compare (isa, prev_isa) != 0
  687. || mach != prev_mach
  688. || endian != prev_endian))
  689. {
  690. cd = 0;
  691. for (cl = cd_list; cl; cl = cl->next)
  692. {
  693. if (cgen_bitset_compare (cl->isa, isa) == 0 &&
  694. cl->mach == mach &&
  695. cl->endian == endian)
  696. {
  697. cd = cl->cd;
  698. prev_isa = cd->isas;
  699. break;
  700. }
  701. }
  702. }
  703. /* If we haven't initialized yet, initialize the opcode table. */
  704. if (! cd)
  705. {
  706. const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach);
  707. const char *mach_name;
  708. if (!arch_type)
  709. abort ();
  710. mach_name = arch_type->printable_name;
  711. prev_isa = cgen_bitset_copy (isa);
  712. prev_mach = mach;
  713. prev_endian = endian;
  714. prev_insn_endian = insn_endian;
  715. cd = xc16x_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa,
  716. CGEN_CPU_OPEN_BFDMACH, mach_name,
  717. CGEN_CPU_OPEN_ENDIAN, prev_endian,
  718. CGEN_CPU_OPEN_INSN_ENDIAN, prev_insn_endian,
  719. CGEN_CPU_OPEN_END);
  720. if (!cd)
  721. abort ();
  722. /* Save this away for future reference. */
  723. cl = xmalloc (sizeof (struct cpu_desc_list));
  724. cl->cd = cd;
  725. cl->isa = prev_isa;
  726. cl->mach = mach;
  727. cl->endian = endian;
  728. cl->next = cd_list;
  729. cd_list = cl;
  730. xc16x_cgen_init_dis (cd);
  731. }
  732. /* We try to have as much common code as possible.
  733. But at this point some targets need to take over. */
  734. /* ??? Some targets may need a hook elsewhere. Try to avoid this,
  735. but if not possible try to move this hook elsewhere rather than
  736. have two hooks. */
  737. length = CGEN_PRINT_INSN (cd, pc, info);
  738. if (length > 0)
  739. return length;
  740. if (length < 0)
  741. return -1;
  742. (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
  743. return cd->default_insn_bitsize / 8;
  744. }