00ee342bbf29c8c579c96cb46333ef8866b0b282
[binutils-gdb.git] / opcodes / nios2-dis.c
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.
5
6 This file is part of the GNU opcodes library.
7
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
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.
17
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. */
22
23 #include "sysdep.h"
24 #include "dis-asm.h"
25 #include "opcode/nios2.h"
26 #include "libiberty.h"
27 #include <string.h>
28 #include <assert.h>
29
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
34 #include "elf-bfd.h"
35 #include "elf/nios2.h"
36 #endif
37
38 /* Default length of Nios II instruction in bytes. */
39 #define INSNLEN 4
40
41 /* Data structures used by the opcode hash table. */
42 typedef struct _nios2_opcode_hash
43 {
44 const struct nios2_opcode *opcode;
45 struct _nios2_opcode_hash *next;
46 } nios2_opcode_hash;
47
48 /* Hash table size. */
49 #define OPCODE_HASH_SIZE (IW_R1_OP_UNSHIFTED_MASK + 1)
50
51 /* Extract the opcode from an instruction word. */
52 static unsigned int
53 nios2_r1_extract_opcode (unsigned int x)
54 {
55 return GET_IW_R1_OP (x);
56 }
57
58 /* Pseudo-ops are stored in a different table than regular instructions. */
59
60 typedef struct _nios2_disassembler_state
61 {
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;
68 bfd_boolean init;
69 } nios2_disassembler_state;
70
71 static nios2_disassembler_state
72 nios2_r1_disassembler_state = {
73 nios2_r1_opcodes,
74 &nios2_num_r1_opcodes,
75 nios2_r1_extract_opcode,
76 {},
77 {},
78 NULL,
79 0
80 };
81
82 /* Function to initialize the opcode hash table. */
83 static void
84 nios2_init_opcode_hash (nios2_disassembler_state *state)
85 {
86 unsigned int i;
87 register const struct nios2_opcode *op;
88
89 for (i = 0; i < OPCODE_HASH_SIZE; i++)
90 for (op = state->opcodes; op < &state->opcodes[*(state->num_opcodes)]; op++)
91 {
92 nios2_opcode_hash *new_hash;
93 nios2_opcode_hash **bucket = NULL;
94
95 if ((op->pinfo & NIOS2_INSN_MACRO) == NIOS2_INSN_MACRO)
96 {
97 if (i == state->extract_opcode (op->match)
98 && (op->pinfo & (NIOS2_INSN_MACRO_MOV | NIOS2_INSN_MACRO_MOVI)
99 & 0x7fffffff))
100 {
101 bucket = &(state->ps_hash[i]);
102 if (strcmp (op->name, "nop") == 0)
103 state->nop = op;
104 }
105 }
106 else if (i == state->extract_opcode (op->match))
107 bucket = &(state->hash[i]);
108
109 if (bucket)
110 {
111 new_hash =
112 (nios2_opcode_hash *) malloc (sizeof (nios2_opcode_hash));
113 if (new_hash == NULL)
114 {
115 fprintf (stderr,
116 "error allocating memory...broken disassembler\n");
117 abort ();
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
128 #ifdef DEBUG_HASHTABLE
129 for (i = 0; i < OPCODE_HASH_SIZE; ++i)
130 {
131 nios2_opcode_hash *tmp_hash = state->hash[i];
132 printf ("index: 0x%02X ops: ", i);
133 while (tmp_hash != NULL)
134 {
135 printf ("%s ", tmp_hash->opcode->name);
136 tmp_hash = tmp_hash->next;
137 }
138 printf ("\n");
139 }
140
141 for (i = 0; i < OPCODE_HASH_SIZE; ++i)
142 {
143 nios2_opcode_hash *tmp_hash = state->ps_hash[i];
144 printf ("index: 0x%02X ops: ", i);
145 while (tmp_hash != NULL)
146 {
147 printf ("%s ", tmp_hash->opcode->name);
148 tmp_hash = tmp_hash->next;
149 }
150 printf ("\n");
151 }
152 #endif /* DEBUG_HASHTABLE */
153 }
154
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)
159 {
160 nios2_opcode_hash *entry;
161 nios2_disassembler_state *state;
162
163 state = &nios2_r1_disassembler_state;
164
165 /* Build a hash table to shorten the search time. */
166 if (!state->init)
167 nios2_init_opcode_hash (state);
168
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))
172 return state->nop;
173
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;
179
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;
185
186 return NULL;
187 }
188
189 /* There are 32 regular registers, 32 coprocessor registers,
190 and 32 control registers. */
191 #define NUMREGNAMES 32
192
193 /* Return a pointer to the base of the coprocessor register name array. */
194 static struct nios2_reg *
195 nios2_coprocessor_regs (void)
196 {
197 static struct nios2_reg *cached = NULL;
198
199 if (!cached)
200 {
201 int i;
202 for (i = NUMREGNAMES; i < nios2_num_regs; i++)
203 if (!strcmp (nios2_regs[i].name, "c0"))
204 {
205 cached = nios2_regs + i;
206 break;
207 }
208 assert (cached);
209 }
210 return cached;
211 }
212
213 /* Return a pointer to the base of the control register name array. */
214 static struct nios2_reg *
215 nios2_control_regs (void)
216 {
217 static struct nios2_reg *cached = NULL;
218
219 if (!cached)
220 {
221 int i;
222 for (i = NUMREGNAMES; i < nios2_num_regs; i++)
223 if (!strcmp (nios2_regs[i].name, "status"))
224 {
225 cached = nios2_regs + i;
226 break;
227 }
228 assert (cached);
229 }
230 return cached;
231 }
232
233 /* Helper routine to report internal errors. */
234 static void
235 bad_opcode (const struct nios2_opcode *op)
236 {
237 fprintf (stderr, "Internal error: broken opcode descriptor for `%s %s'\n",
238 op->name, op->args);
239 abort ();
240 }
241
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. */
245 static int
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)
250 {
251 unsigned long i = 0;
252 struct nios2_reg *reg_base;
253
254 switch (*argptr)
255 {
256 case ',':
257 case '(':
258 case ')':
259 (*info->fprintf_func) (info->stream, "%c", *argptr);
260 break;
261
262 case 'd':
263 switch (op->format)
264 {
265 case iw_r_type:
266 i = GET_IW_R_C (opcode);
267 reg_base = nios2_regs;
268 break;
269 case iw_custom_type:
270 i = GET_IW_CUSTOM_C (opcode);
271 if (GET_IW_CUSTOM_READC (opcode) == 0)
272 reg_base = nios2_coprocessor_regs ();
273 else
274 reg_base = nios2_regs;
275 break;
276 default:
277 bad_opcode (op);
278 }
279 if (i < NUMREGNAMES)
280 (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
281 else
282 (*info->fprintf_func) (info->stream, "unknown");
283 break;
284
285 case 's':
286 switch (op->format)
287 {
288 case iw_r_type:
289 i = GET_IW_R_A (opcode);
290 reg_base = nios2_regs;
291 break;
292 case iw_i_type:
293 i = GET_IW_I_A (opcode);
294 reg_base = nios2_regs;
295 break;
296 case iw_custom_type:
297 i = GET_IW_CUSTOM_A (opcode);
298 if (GET_IW_CUSTOM_READA (opcode) == 0)
299 reg_base = nios2_coprocessor_regs ();
300 else
301 reg_base = nios2_regs;
302 break;
303 default:
304 bad_opcode (op);
305 }
306 if (i < NUMREGNAMES)
307 (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
308 else
309 (*info->fprintf_func) (info->stream, "unknown");
310 break;
311
312 case 't':
313 switch (op->format)
314 {
315 case iw_r_type:
316 i = GET_IW_R_B (opcode);
317 reg_base = nios2_regs;
318 break;
319 case iw_i_type:
320 i = GET_IW_I_B (opcode);
321 reg_base = nios2_regs;
322 break;
323 case iw_custom_type:
324 i = GET_IW_CUSTOM_B (opcode);
325 if (GET_IW_CUSTOM_READB (opcode) == 0)
326 reg_base = nios2_coprocessor_regs ();
327 else
328 reg_base = nios2_regs;
329 break;
330 default:
331 bad_opcode (op);
332 }
333 if (i < NUMREGNAMES)
334 (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
335 else
336 (*info->fprintf_func) (info->stream, "unknown");
337 break;
338
339 case 'i':
340 /* 16-bit signed immediate. */
341 switch (op->format)
342 {
343 case iw_i_type:
344 i = (signed) (GET_IW_I_IMM16 (opcode) << 16) >> 16;
345 break;
346 default:
347 bad_opcode (op);
348 }
349 (*info->fprintf_func) (info->stream, "%ld", i);
350 break;
351
352 case 'u':
353 /* 16-bit unsigned immediate. */
354 switch (op->format)
355 {
356 case iw_i_type:
357 i = GET_IW_I_IMM16 (opcode);
358 break;
359 default:
360 bad_opcode (op);
361 }
362 (*info->fprintf_func) (info->stream, "%ld", i);
363 break;
364
365 case 'o':
366 /* 16-bit signed immediate address offset. */
367 switch (op->format)
368 {
369 case iw_i_type:
370 i = (signed) (GET_IW_I_IMM16 (opcode) << 16) >> 16;
371 break;
372 default:
373 bad_opcode (op);
374 }
375 address = address + 4 + i;
376 (*info->print_address_func) (address, info);
377 break;
378
379 case 'j':
380 /* 5-bit unsigned immediate. */
381 switch (op->format)
382 {
383 case iw_r_type:
384 i = GET_IW_R_IMM5 (opcode);
385 break;
386 default:
387 bad_opcode (op);
388 }
389 (*info->fprintf_func) (info->stream, "%ld", i);
390 break;
391
392 case 'l':
393 /* 8-bit unsigned immediate. */
394 switch (op->format)
395 {
396 case iw_custom_type:
397 i = GET_IW_CUSTOM_N (opcode);
398 break;
399 default:
400 bad_opcode (op);
401 }
402 (*info->fprintf_func) (info->stream, "%lu", i);
403 break;
404
405 case 'm':
406 /* 26-bit unsigned immediate. */
407 switch (op->format)
408 {
409 case iw_j_type:
410 i = GET_IW_J_IMM26 (opcode);
411 break;
412 default:
413 bad_opcode (op);
414 }
415 /* This translates to an address because it's only used in call
416 instructions. */
417 address = (address & 0xf0000000) | (i << 2);
418 (*info->print_address_func) (address, info);
419 break;
420
421 case 'c':
422 /* Control register index. */
423 switch (op->format)
424 {
425 case iw_r_type:
426 i = GET_IW_R_IMM5 (opcode);
427 break;
428 default:
429 bad_opcode (op);
430 }
431 reg_base = nios2_control_regs ();
432 (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
433 break;
434
435 default:
436 (*info->fprintf_func) (info->stream, "unknown");
437 break;
438 }
439 return 0;
440 }
441
442 /* nios2_disassemble does all the work of disassembling a Nios II
443 instruction opcode. */
444 static int
445 nios2_disassemble (bfd_vma address, unsigned long opcode,
446 disassemble_info *info)
447 {
448 const struct nios2_opcode *op;
449
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;
455 info->data_size = 0;
456 info->insn_type = dis_nonbranch;
457 info->target = 0;
458 info->target2 = 0;
459
460 /* Find the major opcode and use this to disassemble
461 the instruction and its arguments. */
462 op = nios2_find_opcode_hash (opcode);
463
464 if (op != NULL)
465 {
466 const char *argstr = op->args;
467 (*info->fprintf_func) (info->stream, "%s", op->name);
468 if (argstr != NULL && *argstr != '\0')
469 {
470 (*info->fprintf_func) (info->stream, "\t");
471 while (*argstr != '\0')
472 {
473 nios2_print_insn_arg (argstr, opcode, address, info, op);
474 ++argstr;
475 }
476 }
477 /* Tell the caller how far to advance the program counter. */
478 info->bytes_per_chunk = op->size;
479 return op->size;
480 }
481 else
482 {
483 /* Handle undefined instructions. */
484 info->insn_type = dis_noninsn;
485 (*info->fprintf_func) (info->stream, "0x%lx", opcode);
486 return INSNLEN;
487 }
488 }
489
490
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. */
498
499 static int
500 print_insn_nios2 (bfd_vma address, disassemble_info *info,
501 enum bfd_endian endianness)
502 {
503 bfd_byte buffer[INSNLEN];
504 int status;
505
506 status = (*info->read_memory_func) (address, buffer, INSNLEN, info);
507 if (status == 0)
508 {
509 unsigned long insn;
510 if (endianness == BFD_ENDIAN_BIG)
511 insn = (unsigned long) bfd_getb32 (buffer);
512 else
513 insn = (unsigned long) bfd_getl32 (buffer);
514 status = nios2_disassemble (address, insn, info);
515 }
516 else
517 {
518 (*info->memory_error_func) (status, address, info);
519 status = -1;
520 }
521 return status;
522 }
523
524 /* These two functions are the main entry points, accessed from
525 disassemble.c. */
526 int
527 print_insn_big_nios2 (bfd_vma address, disassemble_info *info)
528 {
529 return print_insn_nios2 (address, info, BFD_ENDIAN_BIG);
530 }
531
532 int
533 print_insn_little_nios2 (bfd_vma address, disassemble_info *info)
534 {
535 return print_insn_nios2 (address, info, BFD_ENDIAN_LITTLE);
536 }