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