nios2-dis.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /* Altera Nios II disassemble routines
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. Contributed by Nigel Gray (ngray@altera.com).
  4. Contributed by Mentor Graphics, Inc.
  5. This file is part of the GNU opcodes library.
  6. This library is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. It is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  13. License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this file; see the file COPYING. If not, write to the
  16. Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "sysdep.h"
  19. #include "disassemble.h"
  20. #include "opintl.h"
  21. #include "opcode/nios2.h"
  22. #include "libiberty.h"
  23. #include <string.h>
  24. #include <assert.h>
  25. /* No symbol table is available when this code runs out in an embedded
  26. system as when it is used for disassembler support in a monitor. */
  27. #if !defined(EMBEDDED_ENV)
  28. #define SYMTAB_AVAILABLE 1
  29. #include "elf-bfd.h"
  30. #include "elf/nios2.h"
  31. #endif
  32. /* Default length of Nios II instruction in bytes. */
  33. #define INSNLEN 4
  34. /* Data structures used by the opcode hash table. */
  35. typedef struct _nios2_opcode_hash
  36. {
  37. const struct nios2_opcode *opcode;
  38. struct _nios2_opcode_hash *next;
  39. } nios2_opcode_hash;
  40. /* Hash table size. */
  41. #define OPCODE_HASH_SIZE (IW_R1_OP_UNSHIFTED_MASK + 1)
  42. /* Extract the opcode from an instruction word. */
  43. static unsigned int
  44. nios2_r1_extract_opcode (unsigned int x)
  45. {
  46. return GET_IW_R1_OP (x);
  47. }
  48. static unsigned int
  49. nios2_r2_extract_opcode (unsigned int x)
  50. {
  51. return GET_IW_R2_OP (x);
  52. }
  53. /* We maintain separate hash tables for R1 and R2 opcodes, and pseudo-ops
  54. are stored in a different table than regular instructions. */
  55. typedef struct _nios2_disassembler_state
  56. {
  57. const struct nios2_opcode *opcodes;
  58. const int *num_opcodes;
  59. unsigned int (*extract_opcode) (unsigned int);
  60. nios2_opcode_hash *hash[OPCODE_HASH_SIZE];
  61. nios2_opcode_hash *ps_hash[OPCODE_HASH_SIZE];
  62. const struct nios2_opcode *nop;
  63. bool init;
  64. } nios2_disassembler_state;
  65. static nios2_disassembler_state
  66. nios2_r1_disassembler_state = {
  67. nios2_r1_opcodes,
  68. &nios2_num_r1_opcodes,
  69. nios2_r1_extract_opcode,
  70. {},
  71. {},
  72. NULL,
  73. 0
  74. };
  75. static nios2_disassembler_state
  76. nios2_r2_disassembler_state = {
  77. nios2_r2_opcodes,
  78. &nios2_num_r2_opcodes,
  79. nios2_r2_extract_opcode,
  80. {},
  81. {},
  82. NULL,
  83. 0
  84. };
  85. /* Function to initialize the opcode hash table. */
  86. static void
  87. nios2_init_opcode_hash (nios2_disassembler_state *state)
  88. {
  89. unsigned int i;
  90. register const struct nios2_opcode *op;
  91. for (i = 0; i < OPCODE_HASH_SIZE; i++)
  92. for (op = state->opcodes; op < &state->opcodes[*(state->num_opcodes)]; op++)
  93. {
  94. nios2_opcode_hash *new_hash;
  95. nios2_opcode_hash **bucket = NULL;
  96. if ((op->pinfo & NIOS2_INSN_MACRO) == NIOS2_INSN_MACRO)
  97. {
  98. if (i == state->extract_opcode (op->match)
  99. && (op->pinfo & (NIOS2_INSN_MACRO_MOV | NIOS2_INSN_MACRO_MOVI)
  100. & 0x7fffffff))
  101. {
  102. bucket = &(state->ps_hash[i]);
  103. if (strcmp (op->name, "nop") == 0)
  104. state->nop = op;
  105. }
  106. }
  107. else if (i == state->extract_opcode (op->match))
  108. bucket = &(state->hash[i]);
  109. if (bucket)
  110. {
  111. new_hash =
  112. (nios2_opcode_hash *) malloc (sizeof (nios2_opcode_hash));
  113. if (new_hash == NULL)
  114. {
  115. /* xgettext:c-format */
  116. opcodes_error_handler (_("out of memory"));
  117. exit (1);
  118. }
  119. new_hash->opcode = op;
  120. new_hash->next = NULL;
  121. while (*bucket)
  122. bucket = &((*bucket)->next);
  123. *bucket = new_hash;
  124. }
  125. }
  126. state->init = 1;
  127. #ifdef DEBUG_HASHTABLE
  128. for (i = 0; i < OPCODE_HASH_SIZE; ++i)
  129. {
  130. nios2_opcode_hash *tmp_hash = state->hash[i];
  131. printf ("index: 0x%02X ops: ", i);
  132. while (tmp_hash != NULL)
  133. {
  134. printf ("%s ", tmp_hash->opcode->name);
  135. tmp_hash = tmp_hash->next;
  136. }
  137. printf ("\n");
  138. }
  139. for (i = 0; i < OPCODE_HASH_SIZE; ++i)
  140. {
  141. nios2_opcode_hash *tmp_hash = state->ps_hash[i];
  142. printf ("index: 0x%02X ops: ", i);
  143. while (tmp_hash != NULL)
  144. {
  145. printf ("%s ", tmp_hash->opcode->name);
  146. tmp_hash = tmp_hash->next;
  147. }
  148. printf ("\n");
  149. }
  150. #endif /* DEBUG_HASHTABLE */
  151. }
  152. /* Return a pointer to an nios2_opcode struct for a given instruction
  153. word OPCODE for bfd machine MACH, or NULL if there is an error. */
  154. const struct nios2_opcode *
  155. nios2_find_opcode_hash (unsigned long opcode, unsigned long mach)
  156. {
  157. nios2_opcode_hash *entry;
  158. nios2_disassembler_state *state;
  159. /* Select the right instruction set, hash tables, and opcode accessor
  160. for the mach variant. */
  161. if (mach == bfd_mach_nios2r2)
  162. state = &nios2_r2_disassembler_state;
  163. else
  164. state = &nios2_r1_disassembler_state;
  165. /* Build a hash table to shorten the search time. */
  166. if (!state->init)
  167. nios2_init_opcode_hash (state);
  168. /* Check for NOP first. Both NOP and MOV are macros that expand into
  169. an ADD instruction, and we always want to give priority to NOP. */
  170. if (state->nop->match == (opcode & state->nop->mask))
  171. return state->nop;
  172. /* First look in the pseudo-op hashtable. */
  173. for (entry = state->ps_hash[state->extract_opcode (opcode)];
  174. entry; entry = entry->next)
  175. if (entry->opcode->match == (opcode & entry->opcode->mask))
  176. return entry->opcode;
  177. /* Otherwise look in the main hashtable. */
  178. for (entry = state->hash[state->extract_opcode (opcode)];
  179. entry; entry = entry->next)
  180. if (entry->opcode->match == (opcode & entry->opcode->mask))
  181. return entry->opcode;
  182. return NULL;
  183. }
  184. /* There are 32 regular registers, 32 coprocessor registers,
  185. and 32 control registers. */
  186. #define NUMREGNAMES 32
  187. /* Return a pointer to the base of the coprocessor register name array. */
  188. static struct nios2_reg *
  189. nios2_coprocessor_regs (void)
  190. {
  191. static struct nios2_reg *cached = NULL;
  192. if (!cached)
  193. {
  194. int i;
  195. for (i = NUMREGNAMES; i < nios2_num_regs; i++)
  196. if (!strcmp (nios2_regs[i].name, "c0"))
  197. {
  198. cached = nios2_regs + i;
  199. break;
  200. }
  201. assert (cached);
  202. }
  203. return cached;
  204. }
  205. /* Return a pointer to the base of the control register name array. */
  206. static struct nios2_reg *
  207. nios2_control_regs (void)
  208. {
  209. static struct nios2_reg *cached = NULL;
  210. if (!cached)
  211. {
  212. int i;
  213. for (i = NUMREGNAMES; i < nios2_num_regs; i++)
  214. if (!strcmp (nios2_regs[i].name, "status"))
  215. {
  216. cached = nios2_regs + i;
  217. break;
  218. }
  219. assert (cached);
  220. }
  221. return cached;
  222. }
  223. /* Helper routine to report internal errors. */
  224. static void
  225. bad_opcode (const struct nios2_opcode *op)
  226. {
  227. opcodes_error_handler
  228. /* xgettext:c-format */
  229. (_("internal error: broken opcode descriptor for `%s %s'"),
  230. op->name, op->args);
  231. abort ();
  232. }
  233. /* The function nios2_print_insn_arg uses the character pointed
  234. to by ARGPTR to determine how it print the next token or separator
  235. character in the arguments to an instruction. */
  236. static int
  237. nios2_print_insn_arg (const char *argptr,
  238. unsigned long opcode, bfd_vma address,
  239. disassemble_info *info,
  240. const struct nios2_opcode *op)
  241. {
  242. unsigned long i = 0;
  243. long s = 0;
  244. int32_t o = 0;
  245. struct nios2_reg *reg_base;
  246. switch (*argptr)
  247. {
  248. case ',':
  249. case '(':
  250. case ')':
  251. (*info->fprintf_func) (info->stream, "%c", *argptr);
  252. break;
  253. case 'c':
  254. /* Control register index. */
  255. switch (op->format)
  256. {
  257. case iw_r_type:
  258. i = GET_IW_R_IMM5 (opcode);
  259. break;
  260. case iw_F3X6L5_type:
  261. i = GET_IW_F3X6L5_IMM5 (opcode);
  262. break;
  263. default:
  264. bad_opcode (op);
  265. }
  266. reg_base = nios2_control_regs ();
  267. (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
  268. break;
  269. case 'd':
  270. reg_base = nios2_regs;
  271. switch (op->format)
  272. {
  273. case iw_r_type:
  274. i = GET_IW_R_C (opcode);
  275. break;
  276. case iw_custom_type:
  277. i = GET_IW_CUSTOM_C (opcode);
  278. if (GET_IW_CUSTOM_READC (opcode) == 0)
  279. reg_base = nios2_coprocessor_regs ();
  280. break;
  281. case iw_F3X6L5_type:
  282. case iw_F3X6_type:
  283. i = GET_IW_F3X6L5_C (opcode);
  284. break;
  285. case iw_F3X8_type:
  286. i = GET_IW_F3X8_C (opcode);
  287. if (GET_IW_F3X8_READC (opcode) == 0)
  288. reg_base = nios2_coprocessor_regs ();
  289. break;
  290. case iw_F2_type:
  291. i = GET_IW_F2_B (opcode);
  292. break;
  293. default:
  294. bad_opcode (op);
  295. }
  296. if (i < NUMREGNAMES)
  297. (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
  298. else
  299. (*info->fprintf_func) (info->stream, "unknown");
  300. break;
  301. case 's':
  302. reg_base = nios2_regs;
  303. switch (op->format)
  304. {
  305. case iw_r_type:
  306. i = GET_IW_R_A (opcode);
  307. break;
  308. case iw_i_type:
  309. i = GET_IW_I_A (opcode);
  310. break;
  311. case iw_custom_type:
  312. i = GET_IW_CUSTOM_A (opcode);
  313. if (GET_IW_CUSTOM_READA (opcode) == 0)
  314. reg_base = nios2_coprocessor_regs ();
  315. break;
  316. case iw_F2I16_type:
  317. i = GET_IW_F2I16_A (opcode);
  318. break;
  319. case iw_F2X4I12_type:
  320. i = GET_IW_F2X4I12_A (opcode);
  321. break;
  322. case iw_F1X4I12_type:
  323. i = GET_IW_F1X4I12_A (opcode);
  324. break;
  325. case iw_F1X4L17_type:
  326. i = GET_IW_F1X4L17_A (opcode);
  327. break;
  328. case iw_F3X6L5_type:
  329. case iw_F3X6_type:
  330. i = GET_IW_F3X6L5_A (opcode);
  331. break;
  332. case iw_F2X6L10_type:
  333. i = GET_IW_F2X6L10_A (opcode);
  334. break;
  335. case iw_F3X8_type:
  336. i = GET_IW_F3X8_A (opcode);
  337. if (GET_IW_F3X8_READA (opcode) == 0)
  338. reg_base = nios2_coprocessor_regs ();
  339. break;
  340. case iw_F1X1_type:
  341. i = GET_IW_F1X1_A (opcode);
  342. break;
  343. case iw_F1I5_type:
  344. i = 27; /* Implicit stack pointer reference. */
  345. break;
  346. case iw_F2_type:
  347. i = GET_IW_F2_A (opcode);
  348. break;
  349. default:
  350. bad_opcode (op);
  351. }
  352. if (i < NUMREGNAMES)
  353. (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
  354. else
  355. (*info->fprintf_func) (info->stream, "unknown");
  356. break;
  357. case 't':
  358. reg_base = nios2_regs;
  359. switch (op->format)
  360. {
  361. case iw_r_type:
  362. i = GET_IW_R_B (opcode);
  363. break;
  364. case iw_i_type:
  365. i = GET_IW_I_B (opcode);
  366. break;
  367. case iw_custom_type:
  368. i = GET_IW_CUSTOM_B (opcode);
  369. if (GET_IW_CUSTOM_READB (opcode) == 0)
  370. reg_base = nios2_coprocessor_regs ();
  371. break;
  372. case iw_F2I16_type:
  373. i = GET_IW_F2I16_B (opcode);
  374. break;
  375. case iw_F2X4I12_type:
  376. i = GET_IW_F2X4I12_B (opcode);
  377. break;
  378. case iw_F3X6L5_type:
  379. case iw_F3X6_type:
  380. i = GET_IW_F3X6L5_B (opcode);
  381. break;
  382. case iw_F2X6L10_type:
  383. i = GET_IW_F2X6L10_B (opcode);
  384. break;
  385. case iw_F3X8_type:
  386. i = GET_IW_F3X8_B (opcode);
  387. if (GET_IW_F3X8_READB (opcode) == 0)
  388. reg_base = nios2_coprocessor_regs ();
  389. break;
  390. case iw_F1I5_type:
  391. i = GET_IW_F1I5_B (opcode);
  392. break;
  393. case iw_F2_type:
  394. i = GET_IW_F2_B (opcode);
  395. break;
  396. case iw_T1X1I6_type:
  397. i = 0;
  398. break;
  399. default:
  400. bad_opcode (op);
  401. }
  402. if (i < NUMREGNAMES)
  403. (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
  404. else
  405. (*info->fprintf_func) (info->stream, "unknown");
  406. break;
  407. case 'D':
  408. switch (op->format)
  409. {
  410. case iw_T1I7_type:
  411. i = GET_IW_T1I7_A3 (opcode);
  412. break;
  413. case iw_T2X1L3_type:
  414. i = GET_IW_T2X1L3_B3 (opcode);
  415. break;
  416. case iw_T2X1I3_type:
  417. i = GET_IW_T2X1I3_B3 (opcode);
  418. break;
  419. case iw_T3X1_type:
  420. i = GET_IW_T3X1_C3 (opcode);
  421. break;
  422. case iw_T2X3_type:
  423. if (op->num_args == 3)
  424. i = GET_IW_T2X3_A3 (opcode);
  425. else
  426. i = GET_IW_T2X3_B3 (opcode);
  427. break;
  428. default:
  429. bad_opcode (op);
  430. }
  431. i = nios2_r2_reg3_mappings[i];
  432. (*info->fprintf_func) (info->stream, "%s", nios2_regs[i].name);
  433. break;
  434. case 'M':
  435. /* 6-bit unsigned immediate with no shift. */
  436. switch (op->format)
  437. {
  438. case iw_T1X1I6_type:
  439. i = GET_IW_T1X1I6_IMM6 (opcode);
  440. break;
  441. default:
  442. bad_opcode (op);
  443. }
  444. (*info->fprintf_func) (info->stream, "%ld", i);
  445. break;
  446. case 'N':
  447. /* 6-bit unsigned immediate with 2-bit shift. */
  448. switch (op->format)
  449. {
  450. case iw_T1X1I6_type:
  451. i = GET_IW_T1X1I6_IMM6 (opcode) << 2;
  452. break;
  453. default:
  454. bad_opcode (op);
  455. }
  456. (*info->fprintf_func) (info->stream, "%ld", i);
  457. break;
  458. case 'S':
  459. switch (op->format)
  460. {
  461. case iw_T1I7_type:
  462. i = GET_IW_T1I7_A3 (opcode);
  463. break;
  464. case iw_T2I4_type:
  465. i = GET_IW_T2I4_A3 (opcode);
  466. break;
  467. case iw_T2X1L3_type:
  468. i = GET_IW_T2X1L3_A3 (opcode);
  469. break;
  470. case iw_T2X1I3_type:
  471. i = GET_IW_T2X1I3_A3 (opcode);
  472. break;
  473. case iw_T3X1_type:
  474. i = GET_IW_T3X1_A3 (opcode);
  475. break;
  476. case iw_T2X3_type:
  477. i = GET_IW_T2X3_A3 (opcode);
  478. break;
  479. case iw_T1X1I6_type:
  480. i = GET_IW_T1X1I6_A3 (opcode);
  481. break;
  482. default:
  483. bad_opcode (op);
  484. }
  485. i = nios2_r2_reg3_mappings[i];
  486. (*info->fprintf_func) (info->stream, "%s", nios2_regs[i].name);
  487. break;
  488. case 'T':
  489. switch (op->format)
  490. {
  491. case iw_T2I4_type:
  492. i = GET_IW_T2I4_B3 (opcode);
  493. break;
  494. case iw_T3X1_type:
  495. i = GET_IW_T3X1_B3 (opcode);
  496. break;
  497. case iw_T2X3_type:
  498. i = GET_IW_T2X3_B3 (opcode);
  499. break;
  500. default:
  501. bad_opcode (op);
  502. }
  503. i = nios2_r2_reg3_mappings[i];
  504. (*info->fprintf_func) (info->stream, "%s", nios2_regs[i].name);
  505. break;
  506. case 'i':
  507. /* 16-bit signed immediate. */
  508. switch (op->format)
  509. {
  510. case iw_i_type:
  511. s = ((int32_t) ((GET_IW_I_IMM16 (opcode) & 0xffff) ^ 0x8000)
  512. - 0x8000);
  513. break;
  514. case iw_F2I16_type:
  515. s = ((int32_t) ((GET_IW_F2I16_IMM16 (opcode) & 0xffff) ^ 0x8000)
  516. - 0x8000);
  517. break;
  518. default:
  519. bad_opcode (op);
  520. }
  521. (*info->fprintf_func) (info->stream, "%ld", s);
  522. break;
  523. case 'I':
  524. /* 12-bit signed immediate. */
  525. switch (op->format)
  526. {
  527. case iw_F2X4I12_type:
  528. s = ((int32_t) ((GET_IW_F2X4I12_IMM12 (opcode) & 0xfff) ^ 0x800)
  529. - 0x800);
  530. break;
  531. case iw_F1X4I12_type:
  532. s = ((int32_t) ((GET_IW_F1X4I12_IMM12 (opcode) & 0xfff) ^ 0x800)
  533. - 0x800);
  534. break;
  535. default:
  536. bad_opcode (op);
  537. }
  538. (*info->fprintf_func) (info->stream, "%ld", s);
  539. break;
  540. case 'u':
  541. /* 16-bit unsigned immediate. */
  542. switch (op->format)
  543. {
  544. case iw_i_type:
  545. i = GET_IW_I_IMM16 (opcode);
  546. break;
  547. case iw_F2I16_type:
  548. i = GET_IW_F2I16_IMM16 (opcode);
  549. break;
  550. default:
  551. bad_opcode (op);
  552. }
  553. (*info->fprintf_func) (info->stream, "%ld", i);
  554. break;
  555. case 'U':
  556. /* 7-bit unsigned immediate with 2-bit shift. */
  557. switch (op->format)
  558. {
  559. case iw_T1I7_type:
  560. i = GET_IW_T1I7_IMM7 (opcode) << 2;
  561. break;
  562. case iw_X1I7_type:
  563. i = GET_IW_X1I7_IMM7 (opcode) << 2;
  564. break;
  565. default:
  566. bad_opcode (op);
  567. }
  568. (*info->fprintf_func) (info->stream, "%ld", i);
  569. break;
  570. case 'V':
  571. /* 5-bit unsigned immediate with 2-bit shift. */
  572. switch (op->format)
  573. {
  574. case iw_F1I5_type:
  575. i = GET_IW_F1I5_IMM5 (opcode) << 2;
  576. break;
  577. default:
  578. bad_opcode (op);
  579. }
  580. (*info->fprintf_func) (info->stream, "%ld", i);
  581. break;
  582. case 'W':
  583. /* 4-bit unsigned immediate with 2-bit shift. */
  584. switch (op->format)
  585. {
  586. case iw_T2I4_type:
  587. i = GET_IW_T2I4_IMM4 (opcode) << 2;
  588. break;
  589. case iw_L5I4X1_type:
  590. i = GET_IW_L5I4X1_IMM4 (opcode) << 2;
  591. break;
  592. default:
  593. bad_opcode (op);
  594. }
  595. (*info->fprintf_func) (info->stream, "%ld", i);
  596. break;
  597. case 'X':
  598. /* 4-bit unsigned immediate with 1-bit shift. */
  599. switch (op->format)
  600. {
  601. case iw_T2I4_type:
  602. i = GET_IW_T2I4_IMM4 (opcode) << 1;
  603. break;
  604. default:
  605. bad_opcode (op);
  606. }
  607. (*info->fprintf_func) (info->stream, "%ld", i);
  608. break;
  609. case 'Y':
  610. /* 4-bit unsigned immediate without shift. */
  611. switch (op->format)
  612. {
  613. case iw_T2I4_type:
  614. i = GET_IW_T2I4_IMM4 (opcode);
  615. break;
  616. default:
  617. bad_opcode (op);
  618. }
  619. (*info->fprintf_func) (info->stream, "%ld", i);
  620. break;
  621. case 'o':
  622. /* 16-bit signed immediate address offset. */
  623. switch (op->format)
  624. {
  625. case iw_i_type:
  626. o = ((GET_IW_I_IMM16 (opcode) & 0xffff) ^ 0x8000) - 0x8000;
  627. break;
  628. case iw_F2I16_type:
  629. o = ((GET_IW_F2I16_IMM16 (opcode) & 0xffff) ^ 0x8000) - 0x8000;
  630. break;
  631. default:
  632. bad_opcode (op);
  633. }
  634. address = address + 4 + o;
  635. (*info->print_address_func) (address, info);
  636. break;
  637. case 'O':
  638. /* 10-bit signed address offset with 1-bit shift. */
  639. switch (op->format)
  640. {
  641. case iw_I10_type:
  642. o = (((GET_IW_I10_IMM10 (opcode) & 0x3ff) ^ 0x200) - 0x200) * 2;
  643. break;
  644. default:
  645. bad_opcode (op);
  646. }
  647. address = address + 2 + o;
  648. (*info->print_address_func) (address, info);
  649. break;
  650. case 'P':
  651. /* 7-bit signed address offset with 1-bit shift. */
  652. switch (op->format)
  653. {
  654. case iw_T1I7_type:
  655. o = (((GET_IW_T1I7_IMM7 (opcode) & 0x7f) ^ 0x40) - 0x40) * 2;
  656. break;
  657. default:
  658. bad_opcode (op);
  659. }
  660. address = address + 2 + o;
  661. (*info->print_address_func) (address, info);
  662. break;
  663. case 'j':
  664. /* 5-bit unsigned immediate. */
  665. switch (op->format)
  666. {
  667. case iw_r_type:
  668. i = GET_IW_R_IMM5 (opcode);
  669. break;
  670. case iw_F3X6L5_type:
  671. i = GET_IW_F3X6L5_IMM5 (opcode);
  672. break;
  673. case iw_F2X6L10_type:
  674. i = GET_IW_F2X6L10_MSB (opcode);
  675. break;
  676. case iw_X2L5_type:
  677. i = GET_IW_X2L5_IMM5 (opcode);
  678. break;
  679. default:
  680. bad_opcode (op);
  681. }
  682. (*info->fprintf_func) (info->stream, "%ld", i);
  683. break;
  684. case 'k':
  685. /* Second 5-bit unsigned immediate field. */
  686. switch (op->format)
  687. {
  688. case iw_F2X6L10_type:
  689. i = GET_IW_F2X6L10_LSB (opcode);
  690. break;
  691. default:
  692. bad_opcode (op);
  693. }
  694. (*info->fprintf_func) (info->stream, "%ld", i);
  695. break;
  696. case 'l':
  697. /* 8-bit unsigned immediate. */
  698. switch (op->format)
  699. {
  700. case iw_custom_type:
  701. i = GET_IW_CUSTOM_N (opcode);
  702. break;
  703. case iw_F3X8_type:
  704. i = GET_IW_F3X8_N (opcode);
  705. break;
  706. default:
  707. bad_opcode (op);
  708. }
  709. (*info->fprintf_func) (info->stream, "%lu", i);
  710. break;
  711. case 'm':
  712. /* 26-bit unsigned immediate. */
  713. switch (op->format)
  714. {
  715. case iw_j_type:
  716. i = GET_IW_J_IMM26 (opcode);
  717. break;
  718. case iw_L26_type:
  719. i = GET_IW_L26_IMM26 (opcode);
  720. break;
  721. default:
  722. bad_opcode (op);
  723. }
  724. /* This translates to an address because it's only used in call
  725. instructions. */
  726. address = (address & 0xf0000000) | (i << 2);
  727. (*info->print_address_func) (address, info);
  728. break;
  729. case 'e':
  730. /* Encoded enumeration for addi.n/subi.n. */
  731. switch (op->format)
  732. {
  733. case iw_T2X1I3_type:
  734. i = nios2_r2_asi_n_mappings[GET_IW_T2X1I3_IMM3 (opcode)];
  735. break;
  736. default:
  737. bad_opcode (op);
  738. }
  739. (*info->fprintf_func) (info->stream, "%lu", i);
  740. break;
  741. case 'f':
  742. /* Encoded enumeration for slli.n/srli.n. */
  743. switch (op->format)
  744. {
  745. case iw_T2X1L3_type:
  746. i = nios2_r2_shi_n_mappings[GET_IW_T2X1I3_IMM3 (opcode)];
  747. break;
  748. default:
  749. bad_opcode (op);
  750. }
  751. (*info->fprintf_func) (info->stream, "%lu", i);
  752. break;
  753. case 'g':
  754. /* Encoded enumeration for andi.n. */
  755. switch (op->format)
  756. {
  757. case iw_T2I4_type:
  758. i = nios2_r2_andi_n_mappings[GET_IW_T2I4_IMM4 (opcode)];
  759. break;
  760. default:
  761. bad_opcode (op);
  762. }
  763. (*info->fprintf_func) (info->stream, "%lu", i);
  764. break;
  765. case 'h':
  766. /* Encoded enumeration for movi.n. */
  767. switch (op->format)
  768. {
  769. case iw_T1I7_type:
  770. i = GET_IW_T1I7_IMM7 (opcode);
  771. if (i == 125)
  772. i = 0xff;
  773. else if (i == 126)
  774. i = -2;
  775. else if (i == 127)
  776. i = -1;
  777. break;
  778. default:
  779. bad_opcode (op);
  780. }
  781. (*info->fprintf_func) (info->stream, "%ld", i);
  782. break;
  783. case 'R':
  784. {
  785. unsigned long reglist = 0;
  786. int dir = 1;
  787. int k, t;
  788. switch (op->format)
  789. {
  790. case iw_F1X4L17_type:
  791. /* Encoding for ldwm/stwm. */
  792. i = GET_IW_F1X4L17_REGMASK (opcode);
  793. if (GET_IW_F1X4L17_RS (opcode))
  794. {
  795. reglist = ((i << 14) & 0x00ffc000);
  796. if (i & (1 << 10))
  797. reglist |= (1 << 28);
  798. if (i & (1 << 11))
  799. reglist |= (1u << 31);
  800. }
  801. else
  802. reglist = i << 2;
  803. dir = GET_IW_F1X4L17_REGMASK (opcode) ? 1 : -1;
  804. break;
  805. case iw_L5I4X1_type:
  806. /* Encoding for push.n/pop.n. */
  807. reglist |= (1u << 31);
  808. if (GET_IW_L5I4X1_FP (opcode))
  809. reglist |= (1 << 28);
  810. if (GET_IW_L5I4X1_CS (opcode))
  811. {
  812. int val = GET_IW_L5I4X1_REGRANGE (opcode);
  813. reglist |= nios2_r2_reg_range_mappings[val];
  814. }
  815. dir = (op->match == MATCH_R2_POP_N ? 1 : -1);
  816. break;
  817. default:
  818. bad_opcode (op);
  819. }
  820. t = 0;
  821. (*info->fprintf_func) (info->stream, "{");
  822. for (k = (dir == 1 ? 0 : 31);
  823. (dir == 1 && k < 32) || (dir == -1 && k >= 0);
  824. k += dir)
  825. if (reglist & (1u << k))
  826. {
  827. if (t)
  828. (*info->fprintf_func) (info->stream, ",");
  829. else
  830. t++;
  831. (*info->fprintf_func) (info->stream, "%s", nios2_regs[k].name);
  832. }
  833. (*info->fprintf_func) (info->stream, "}");
  834. break;
  835. }
  836. case 'B':
  837. /* Base register and options for ldwm/stwm. */
  838. switch (op->format)
  839. {
  840. case iw_F1X4L17_type:
  841. if (GET_IW_F1X4L17_ID (opcode) == 0)
  842. (*info->fprintf_func) (info->stream, "--");
  843. i = GET_IW_F1X4I12_A (opcode);
  844. (*info->fprintf_func) (info->stream, "(%s)",
  845. nios2_builtin_regs[i].name);
  846. if (GET_IW_F1X4L17_ID (opcode))
  847. (*info->fprintf_func) (info->stream, "++");
  848. if (GET_IW_F1X4L17_WB (opcode))
  849. (*info->fprintf_func) (info->stream, ",writeback");
  850. if (GET_IW_F1X4L17_PC (opcode))
  851. (*info->fprintf_func) (info->stream, ",ret");
  852. break;
  853. default:
  854. bad_opcode (op);
  855. }
  856. break;
  857. default:
  858. (*info->fprintf_func) (info->stream, "unknown");
  859. break;
  860. }
  861. return 0;
  862. }
  863. /* nios2_disassemble does all the work of disassembling a Nios II
  864. instruction opcode. */
  865. static int
  866. nios2_disassemble (bfd_vma address, unsigned long opcode,
  867. disassemble_info *info)
  868. {
  869. const struct nios2_opcode *op;
  870. info->bytes_per_line = INSNLEN;
  871. info->bytes_per_chunk = INSNLEN;
  872. info->display_endian = info->endian;
  873. info->insn_info_valid = 1;
  874. info->branch_delay_insns = 0;
  875. info->data_size = 0;
  876. info->insn_type = dis_nonbranch;
  877. info->target = 0;
  878. info->target2 = 0;
  879. /* Find the major opcode and use this to disassemble
  880. the instruction and its arguments. */
  881. op = nios2_find_opcode_hash (opcode, info->mach);
  882. if (op != NULL)
  883. {
  884. const char *argstr = op->args;
  885. (*info->fprintf_func) (info->stream, "%s", op->name);
  886. if (argstr != NULL && *argstr != '\0')
  887. {
  888. (*info->fprintf_func) (info->stream, "\t");
  889. while (*argstr != '\0')
  890. {
  891. nios2_print_insn_arg (argstr, opcode, address, info, op);
  892. ++argstr;
  893. }
  894. }
  895. /* Tell the caller how far to advance the program counter. */
  896. info->bytes_per_chunk = op->size;
  897. return op->size;
  898. }
  899. else
  900. {
  901. /* Handle undefined instructions. */
  902. info->insn_type = dis_noninsn;
  903. (*info->fprintf_func) (info->stream, "0x%lx", opcode);
  904. return INSNLEN;
  905. }
  906. }
  907. /* print_insn_nios2 is the main disassemble function for Nios II.
  908. The function diassembler(abfd) (source in disassemble.c) returns a
  909. pointer to this either print_insn_big_nios2 or
  910. print_insn_little_nios2, which in turn call this function when the
  911. bfd machine type is Nios II. print_insn_nios2 reads the
  912. instruction word at the address given, and prints the disassembled
  913. instruction on the stream info->stream using info->fprintf_func. */
  914. static int
  915. print_insn_nios2 (bfd_vma address, disassemble_info *info,
  916. enum bfd_endian endianness)
  917. {
  918. bfd_byte buffer[INSNLEN];
  919. int status;
  920. status = (*info->read_memory_func) (address, buffer, INSNLEN, info);
  921. if (status == 0)
  922. {
  923. unsigned long insn;
  924. if (endianness == BFD_ENDIAN_BIG)
  925. insn = (unsigned long) bfd_getb32 (buffer);
  926. else
  927. insn = (unsigned long) bfd_getl32 (buffer);
  928. return nios2_disassemble (address, insn, info);
  929. }
  930. /* We might have a 16-bit R2 instruction at the end of memory. Try that. */
  931. if (info->mach == bfd_mach_nios2r2)
  932. {
  933. status = (*info->read_memory_func) (address, buffer, 2, info);
  934. if (status == 0)
  935. {
  936. unsigned long insn;
  937. if (endianness == BFD_ENDIAN_BIG)
  938. insn = (unsigned long) bfd_getb16 (buffer);
  939. else
  940. insn = (unsigned long) bfd_getl16 (buffer);
  941. return nios2_disassemble (address, insn, info);
  942. }
  943. }
  944. /* If we got here, we couldn't read anything. */
  945. (*info->memory_error_func) (status, address, info);
  946. return -1;
  947. }
  948. /* These two functions are the main entry points, accessed from
  949. disassemble.c. */
  950. int
  951. print_insn_big_nios2 (bfd_vma address, disassemble_info *info)
  952. {
  953. return print_insn_nios2 (address, info, BFD_ENDIAN_BIG);
  954. }
  955. int
  956. print_insn_little_nios2 (bfd_vma address, disassemble_info *info)
  957. {
  958. return print_insn_nios2 (address, info, BFD_ENDIAN_LITTLE);
  959. }