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 opcode, or NULL if there is an error. */
157 const struct nios2_opcode
*
158 nios2_find_opcode_hash (unsigned long opcode
)
160 nios2_opcode_hash
*entry
;
161 nios2_disassembler_state
*state
;
163 state
= &nios2_r1_disassembler_state
;
165 /* Build a hash table to shorten the search time. */
167 nios2_init_opcode_hash (state
);
169 /* Check for NOP first. Both NOP and MOV are macros that expand into
170 an ADD instruction, and we always want to give priority to NOP. */
171 if (state
->nop
->match
== (opcode
& state
->nop
->mask
))
174 /* First look in the pseudo-op hashtable. */
175 for (entry
= state
->ps_hash
[state
->extract_opcode (opcode
)];
176 entry
; entry
= entry
->next
)
177 if (entry
->opcode
->match
== (opcode
& entry
->opcode
->mask
))
178 return entry
->opcode
;
180 /* Otherwise look in the main hashtable. */
181 for (entry
= state
->hash
[state
->extract_opcode (opcode
)];
182 entry
; entry
= entry
->next
)
183 if (entry
->opcode
->match
== (opcode
& entry
->opcode
->mask
))
184 return entry
->opcode
;
189 /* There are 32 regular registers, 32 coprocessor registers,
190 and 32 control registers. */
191 #define NUMREGNAMES 32
193 /* Return a pointer to the base of the coprocessor register name array. */
194 static struct nios2_reg
*
195 nios2_coprocessor_regs (void)
197 static struct nios2_reg
*cached
= NULL
;
202 for (i
= NUMREGNAMES
; i
< nios2_num_regs
; i
++)
203 if (!strcmp (nios2_regs
[i
].name
, "c0"))
205 cached
= nios2_regs
+ i
;
213 /* Return a pointer to the base of the control register name array. */
214 static struct nios2_reg
*
215 nios2_control_regs (void)
217 static struct nios2_reg
*cached
= NULL
;
222 for (i
= NUMREGNAMES
; i
< nios2_num_regs
; i
++)
223 if (!strcmp (nios2_regs
[i
].name
, "status"))
225 cached
= nios2_regs
+ i
;
233 /* Helper routine to report internal errors. */
235 bad_opcode (const struct nios2_opcode
*op
)
237 fprintf (stderr
, "Internal error: broken opcode descriptor for `%s %s'\n",
242 /* The function nios2_print_insn_arg uses the character pointed
243 to by ARGPTR to determine how it print the next token or separator
244 character in the arguments to an instruction. */
246 nios2_print_insn_arg (const char *argptr
,
247 unsigned long opcode
, bfd_vma address
,
248 disassemble_info
*info
,
249 const struct nios2_opcode
*op
)
252 struct nios2_reg
*reg_base
;
259 (*info
->fprintf_func
) (info
->stream
, "%c", *argptr
);
266 i
= GET_IW_R_C (opcode
);
267 reg_base
= nios2_regs
;
270 i
= GET_IW_CUSTOM_C (opcode
);
271 if (GET_IW_CUSTOM_READC (opcode
) == 0)
272 reg_base
= nios2_coprocessor_regs ();
274 reg_base
= nios2_regs
;
280 (*info
->fprintf_func
) (info
->stream
, "%s", reg_base
[i
].name
);
282 (*info
->fprintf_func
) (info
->stream
, "unknown");
289 i
= GET_IW_R_A (opcode
);
290 reg_base
= nios2_regs
;
293 i
= GET_IW_I_A (opcode
);
294 reg_base
= nios2_regs
;
297 i
= GET_IW_CUSTOM_A (opcode
);
298 if (GET_IW_CUSTOM_READA (opcode
) == 0)
299 reg_base
= nios2_coprocessor_regs ();
301 reg_base
= nios2_regs
;
307 (*info
->fprintf_func
) (info
->stream
, "%s", reg_base
[i
].name
);
309 (*info
->fprintf_func
) (info
->stream
, "unknown");
316 i
= GET_IW_R_B (opcode
);
317 reg_base
= nios2_regs
;
320 i
= GET_IW_I_B (opcode
);
321 reg_base
= nios2_regs
;
324 i
= GET_IW_CUSTOM_B (opcode
);
325 if (GET_IW_CUSTOM_READB (opcode
) == 0)
326 reg_base
= nios2_coprocessor_regs ();
328 reg_base
= nios2_regs
;
334 (*info
->fprintf_func
) (info
->stream
, "%s", reg_base
[i
].name
);
336 (*info
->fprintf_func
) (info
->stream
, "unknown");
340 /* 16-bit signed immediate. */
344 i
= (signed) (GET_IW_I_IMM16 (opcode
) << 16) >> 16;
349 (*info
->fprintf_func
) (info
->stream
, "%ld", i
);
353 /* 16-bit unsigned immediate. */
357 i
= GET_IW_I_IMM16 (opcode
);
362 (*info
->fprintf_func
) (info
->stream
, "%ld", i
);
366 /* 16-bit signed immediate address offset. */
370 i
= (signed) (GET_IW_I_IMM16 (opcode
) << 16) >> 16;
375 address
= address
+ 4 + i
;
376 (*info
->print_address_func
) (address
, info
);
380 /* 5-bit unsigned immediate. */
384 i
= GET_IW_R_IMM5 (opcode
);
389 (*info
->fprintf_func
) (info
->stream
, "%ld", i
);
393 /* 8-bit unsigned immediate. */
397 i
= GET_IW_CUSTOM_N (opcode
);
402 (*info
->fprintf_func
) (info
->stream
, "%lu", i
);
406 /* 26-bit unsigned immediate. */
410 i
= GET_IW_J_IMM26 (opcode
);
415 /* This translates to an address because it's only used in call
417 address
= (address
& 0xf0000000) | (i
<< 2);
418 (*info
->print_address_func
) (address
, info
);
422 /* Control register index. */
426 i
= GET_IW_R_IMM5 (opcode
);
431 reg_base
= nios2_control_regs ();
432 (*info
->fprintf_func
) (info
->stream
, "%s", reg_base
[i
].name
);
436 (*info
->fprintf_func
) (info
->stream
, "unknown");
442 /* nios2_disassemble does all the work of disassembling a Nios II
443 instruction opcode. */
445 nios2_disassemble (bfd_vma address
, unsigned long opcode
,
446 disassemble_info
*info
)
448 const struct nios2_opcode
*op
;
450 info
->bytes_per_line
= INSNLEN
;
451 info
->bytes_per_chunk
= INSNLEN
;
452 info
->display_endian
= info
->endian
;
453 info
->insn_info_valid
= 1;
454 info
->branch_delay_insns
= 0;
456 info
->insn_type
= dis_nonbranch
;
460 /* Find the major opcode and use this to disassemble
461 the instruction and its arguments. */
462 op
= nios2_find_opcode_hash (opcode
);
466 const char *argstr
= op
->args
;
467 (*info
->fprintf_func
) (info
->stream
, "%s", op
->name
);
468 if (argstr
!= NULL
&& *argstr
!= '\0')
470 (*info
->fprintf_func
) (info
->stream
, "\t");
471 while (*argstr
!= '\0')
473 nios2_print_insn_arg (argstr
, opcode
, address
, info
, op
);
477 /* Tell the caller how far to advance the program counter. */
478 info
->bytes_per_chunk
= op
->size
;
483 /* Handle undefined instructions. */
484 info
->insn_type
= dis_noninsn
;
485 (*info
->fprintf_func
) (info
->stream
, "0x%lx", opcode
);
491 /* print_insn_nios2 is the main disassemble function for Nios II.
492 The function diassembler(abfd) (source in disassemble.c) returns a
493 pointer to this either print_insn_big_nios2 or
494 print_insn_little_nios2, which in turn call this function when the
495 bfd machine type is Nios II. print_insn_nios2 reads the
496 instruction word at the address given, and prints the disassembled
497 instruction on the stream info->stream using info->fprintf_func. */
500 print_insn_nios2 (bfd_vma address
, disassemble_info
*info
,
501 enum bfd_endian endianness
)
503 bfd_byte buffer
[INSNLEN
];
506 status
= (*info
->read_memory_func
) (address
, buffer
, INSNLEN
, info
);
510 if (endianness
== BFD_ENDIAN_BIG
)
511 insn
= (unsigned long) bfd_getb32 (buffer
);
513 insn
= (unsigned long) bfd_getl32 (buffer
);
514 status
= nios2_disassemble (address
, insn
, info
);
518 (*info
->memory_error_func
) (status
, address
, info
);
524 /* These two functions are the main entry points, accessed from
527 print_insn_big_nios2 (bfd_vma address
, disassemble_info
*info
)
529 return print_insn_nios2 (address
, info
, BFD_ENDIAN_BIG
);
533 print_insn_little_nios2 (bfd_vma address
, disassemble_info
*info
)
535 return print_insn_nios2 (address
, info
, BFD_ENDIAN_LITTLE
);