1 /* Altera Nios II disassemble routines
2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
6 This file is part of the GNU opcodes library.
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)
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this file; see the file COPYING. If not, write to the
20 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
25 #include "opcode/nios2.h"
26 #include "libiberty.h"
30 /* No symbol table is available when this code runs out in an embedded
31 system as when it is used for disassembler support in a monitor. */
32 #if !defined(EMBEDDED_ENV)
33 #define SYMTAB_AVAILABLE 1
35 #include "elf/nios2.h"
38 /* Default length of Nios II instruction in bytes. */
41 /* Data structures used by the opcode hash table. */
42 typedef struct _nios2_opcode_hash
44 const struct nios2_opcode *opcode;
45 struct _nios2_opcode_hash *next;
48 /* Hash table size. */
49 #define OPCODE_HASH_SIZE (IW_R1_OP_UNSHIFTED_MASK + 1)
51 /* Extract the opcode from an instruction word. */
53 nios2_r1_extract_opcode (unsigned int x)
55 return GET_IW_R1_OP (x);
58 /* Pseudo-ops are stored in a different table than regular instructions. */
60 typedef struct _nios2_disassembler_state
62 const struct nios2_opcode *opcodes;
63 const int *num_opcodes;
64 unsigned int (*extract_opcode) (unsigned int);
65 nios2_opcode_hash *hash[OPCODE_HASH_SIZE];
66 nios2_opcode_hash *ps_hash[OPCODE_HASH_SIZE];
67 const struct nios2_opcode *nop;
69 } nios2_disassembler_state;
71 static nios2_disassembler_state
72 nios2_r1_disassembler_state = {
74 &nios2_num_r1_opcodes,
75 nios2_r1_extract_opcode,
82 /* Function to initialize the opcode hash table. */
84 nios2_init_opcode_hash (nios2_disassembler_state *state)
87 register const struct nios2_opcode *op;
89 for (i = 0; i < OPCODE_HASH_SIZE; i++)
90 for (op = state->opcodes; op < &state->opcodes[*(state->num_opcodes)]; op++)
92 nios2_opcode_hash *new_hash;
93 nios2_opcode_hash **bucket = NULL;
95 if ((op->pinfo & NIOS2_INSN_MACRO) == NIOS2_INSN_MACRO)
97 if (i == state->extract_opcode (op->match)
98 && (op->pinfo & (NIOS2_INSN_MACRO_MOV | NIOS2_INSN_MACRO_MOVI)
101 bucket = &(state->ps_hash[i]);
102 if (strcmp (op->name, "nop") == 0)
106 else if (i == state->extract_opcode (op->match))
107 bucket = &(state->hash[i]);
112 (nios2_opcode_hash *) malloc (sizeof (nios2_opcode_hash));
113 if (new_hash == NULL)
116 "error allocating memory...broken disassembler\n");
119 new_hash->opcode = op;
120 new_hash->next = NULL;
122 bucket = &((*bucket)->next);
128 #ifdef DEBUG_HASHTABLE
129 for (i = 0; i < OPCODE_HASH_SIZE; ++i)
131 nios2_opcode_hash *tmp_hash = state->hash[i];
132 printf ("index: 0x%02X ops: ", i);
133 while (tmp_hash != NULL)
135 printf ("%s ", tmp_hash->opcode->name);
136 tmp_hash = tmp_hash->next;
141 for (i = 0; i < OPCODE_HASH_SIZE; ++i)
143 nios2_opcode_hash *tmp_hash = state->ps_hash[i];
144 printf ("index: 0x%02X ops: ", i);
145 while (tmp_hash != NULL)
147 printf ("%s ", tmp_hash->opcode->name);
148 tmp_hash = tmp_hash->next;
152 #endif /* DEBUG_HASHTABLE */
155 /* Return a pointer to an nios2_opcode struct for a given instruction
156 word OPCODE for bfd machine MACH, or NULL if there is an error. */
157 const struct nios2_opcode *
158 nios2_find_opcode_hash (unsigned long opcode,
159 unsigned long mach ATTRIBUTE_UNUSED)
161 nios2_opcode_hash *entry;
162 nios2_disassembler_state *state;
164 state = &nios2_r1_disassembler_state;
166 /* Build a hash table to shorten the search time. */
168 nios2_init_opcode_hash (state);
170 /* Check for NOP first. Both NOP and MOV are macros that expand into
171 an ADD instruction, and we always want to give priority to NOP. */
172 if (state->nop->match == (opcode & state->nop->mask))
175 /* First look in the pseudo-op hashtable. */
176 for (entry = state->ps_hash[state->extract_opcode (opcode)];
177 entry; entry = entry->next)
178 if (entry->opcode->match == (opcode & entry->opcode->mask))
179 return entry->opcode;
181 /* Otherwise look in the main hashtable. */
182 for (entry = state->hash[state->extract_opcode (opcode)];
183 entry; entry = entry->next)
184 if (entry->opcode->match == (opcode & entry->opcode->mask))
185 return entry->opcode;
190 /* There are 32 regular registers, 32 coprocessor registers,
191 and 32 control registers. */
192 #define NUMREGNAMES 32
194 /* Return a pointer to the base of the coprocessor register name array. */
195 static struct nios2_reg *
196 nios2_coprocessor_regs (void)
198 static struct nios2_reg *cached = NULL;
203 for (i = NUMREGNAMES; i < nios2_num_regs; i++)
204 if (!strcmp (nios2_regs[i].name, "c0"))
206 cached = nios2_regs + i;
214 /* Return a pointer to the base of the control register name array. */
215 static struct nios2_reg *
216 nios2_control_regs (void)
218 static struct nios2_reg *cached = NULL;
223 for (i = NUMREGNAMES; i < nios2_num_regs; i++)
224 if (!strcmp (nios2_regs[i].name, "status"))
226 cached = nios2_regs + i;
234 /* Helper routine to report internal errors. */
236 bad_opcode (const struct nios2_opcode *op)
238 fprintf (stderr, "Internal error: broken opcode descriptor for `%s %s'\n",
243 /* The function nios2_print_insn_arg uses the character pointed
244 to by ARGPTR to determine how it print the next token or separator
245 character in the arguments to an instruction. */
247 nios2_print_insn_arg (const char *argptr,
248 unsigned long opcode, bfd_vma address,
249 disassemble_info *info,
250 const struct nios2_opcode *op)
253 struct nios2_reg *reg_base;
260 (*info->fprintf_func) (info->stream, "%c", *argptr);
267 i = GET_IW_R_C (opcode);
268 reg_base = nios2_regs;
271 i = GET_IW_CUSTOM_C (opcode);
272 if (GET_IW_CUSTOM_READC (opcode) == 0)
273 reg_base = nios2_coprocessor_regs ();
275 reg_base = nios2_regs;
281 (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
283 (*info->fprintf_func) (info->stream, "unknown");
290 i = GET_IW_R_A (opcode);
291 reg_base = nios2_regs;
294 i = GET_IW_I_A (opcode);
295 reg_base = nios2_regs;
298 i = GET_IW_CUSTOM_A (opcode);
299 if (GET_IW_CUSTOM_READA (opcode) == 0)
300 reg_base = nios2_coprocessor_regs ();
302 reg_base = nios2_regs;
308 (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
310 (*info->fprintf_func) (info->stream, "unknown");
317 i = GET_IW_R_B (opcode);
318 reg_base = nios2_regs;
321 i = GET_IW_I_B (opcode);
322 reg_base = nios2_regs;
325 i = GET_IW_CUSTOM_B (opcode);
326 if (GET_IW_CUSTOM_READB (opcode) == 0)
327 reg_base = nios2_coprocessor_regs ();
329 reg_base = nios2_regs;
335 (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
337 (*info->fprintf_func) (info->stream, "unknown");
341 /* 16-bit signed immediate. */
345 i = (signed) (GET_IW_I_IMM16 (opcode) << 16) >> 16;
350 (*info->fprintf_func) (info->stream, "%ld", i);
354 /* 16-bit unsigned immediate. */
358 i = GET_IW_I_IMM16 (opcode);
363 (*info->fprintf_func) (info->stream, "%ld", i);
367 /* 16-bit signed immediate address offset. */
371 i = (signed) (GET_IW_I_IMM16 (opcode) << 16) >> 16;
376 address = address + 4 + i;
377 (*info->print_address_func) (address, info);
381 /* 5-bit unsigned immediate. */
385 i = GET_IW_R_IMM5 (opcode);
390 (*info->fprintf_func) (info->stream, "%ld", i);
394 /* 8-bit unsigned immediate. */
398 i = GET_IW_CUSTOM_N (opcode);
403 (*info->fprintf_func) (info->stream, "%lu", i);
407 /* 26-bit unsigned immediate. */
411 i = GET_IW_J_IMM26 (opcode);
416 /* This translates to an address because it's only used in call
418 address = (address & 0xf0000000) | (i << 2);
419 (*info->print_address_func) (address, info);
423 /* Control register index. */
427 i = GET_IW_R_IMM5 (opcode);
432 reg_base = nios2_control_regs ();
433 (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
437 (*info->fprintf_func) (info->stream, "unknown");
443 /* nios2_disassemble does all the work of disassembling a Nios II
444 instruction opcode. */
446 nios2_disassemble (bfd_vma address, unsigned long opcode,
447 disassemble_info *info)
449 const struct nios2_opcode *op;
451 info->bytes_per_line = INSNLEN;
452 info->bytes_per_chunk = INSNLEN;
453 info->display_endian = info->endian;
454 info->insn_info_valid = 1;
455 info->branch_delay_insns = 0;
457 info->insn_type = dis_nonbranch;
461 /* Find the major opcode and use this to disassemble
462 the instruction and its arguments. */
463 op = nios2_find_opcode_hash (opcode, info->mach);
467 const char *argstr = op->args;
468 (*info->fprintf_func) (info->stream, "%s", op->name);
469 if (argstr != NULL && *argstr != '\0')
471 (*info->fprintf_func) (info->stream, "\t");
472 while (*argstr != '\0')
474 nios2_print_insn_arg (argstr, opcode, address, info, op);
478 /* Tell the caller how far to advance the program counter. */
479 info->bytes_per_chunk = op->size;
484 /* Handle undefined instructions. */
485 info->insn_type = dis_noninsn;
486 (*info->fprintf_func) (info->stream, "0x%lx", opcode);
492 /* print_insn_nios2 is the main disassemble function for Nios II.
493 The function diassembler(abfd) (source in disassemble.c) returns a
494 pointer to this either print_insn_big_nios2 or
495 print_insn_little_nios2, which in turn call this function when the
496 bfd machine type is Nios II. print_insn_nios2 reads the
497 instruction word at the address given, and prints the disassembled
498 instruction on the stream info->stream using info->fprintf_func. */
501 print_insn_nios2 (bfd_vma address, disassemble_info *info,
502 enum bfd_endian endianness)
504 bfd_byte buffer[INSNLEN];
507 status = (*info->read_memory_func) (address, buffer, INSNLEN, info);
511 if (endianness == BFD_ENDIAN_BIG)
512 insn = (unsigned long) bfd_getb32 (buffer);
514 insn = (unsigned long) bfd_getl32 (buffer);
515 status = nios2_disassemble (address, insn, info);
519 (*info->memory_error_func) (status, address, info);
525 /* These two functions are the main entry points, accessed from
528 print_insn_big_nios2 (bfd_vma address, disassemble_info *info)
530 return print_insn_nios2 (address, info, BFD_ENDIAN_BIG);
534 print_insn_little_nios2 (bfd_vma address, disassemble_info *info)
536 return print_insn_nios2 (address, info, BFD_ENDIAN_LITTLE);