1 /* Disassemble Xilinx microblaze instructions.
3 Copyright 2009, 2012 Free Software Foundation, Inc.
5 This file is part of the GNU opcodes library.
7 This library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 It is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this file; see the file COPYING. If not, write to the
19 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
29 #include "microblaze-opc.h"
30 #include "microblaze-dis.h"
32 #define get_field_rd(instr) get_field (instr, RD_MASK, RD_LOW)
33 #define get_field_r1(instr) get_field (instr, RA_MASK, RA_LOW)
34 #define get_field_r2(instr) get_field (instr, RB_MASK, RB_LOW)
35 #define get_int_field_imm(instr) ((instr & IMM_MASK) >> IMM_LOW)
36 #define get_int_field_imm5(instr) ((instr & IMM5_MASK) >> IMM_LOW)
37 #define get_int_field_r1(instr) ((instr & RA_MASK) >> RA_LOW)
42 get_field (long instr, long mask, unsigned short low)
46 sprintf (tmpstr, "%s%d", register_prefix, (int)((instr & mask) >> low));
47 return (strdup (tmpstr));
51 get_field_imm (long instr)
55 sprintf (tmpstr, "%d", (short)((instr & IMM_MASK) >> IMM_LOW));
56 return (strdup (tmpstr));
60 get_field_imm5 (long instr)
64 sprintf (tmpstr, "%d", (short)((instr & IMM5_MASK) >> IMM_LOW));
65 return (strdup (tmpstr));
71 get_field_rfsl (long instr)
75 sprintf (tmpstr, "%s%d", fsl_register_prefix,
76 (short)((instr & RFSL_MASK) >> IMM_LOW));
77 return (strdup (tmpstr));
81 get_field_imm15 (long instr)
85 sprintf (tmpstr, "%d", (short)((instr & IMM15_MASK) >> IMM_LOW));
86 return (strdup (tmpstr));
92 get_field_special (long instr, struct op_code_struct * op)
97 switch ((((instr & IMM_MASK) >> IMM_LOW) ^ op->immval_mask))
128 strcpy (spr, "tlbx");
130 case REG_TLBLO_MASK :
131 strcpy (spr, "tlblo");
133 case REG_TLBHI_MASK :
134 strcpy (spr, "tlbhi");
136 case REG_TLBSX_MASK :
137 strcpy (spr, "tlbsx");
140 if (((((instr & IMM_MASK) >> IMM_LOW) ^ op->immval_mask) & 0xE000)
143 sprintf (tmpstr, "%spvr%d", register_prefix,
144 (unsigned short)(((instr & IMM_MASK) >> IMM_LOW)
145 ^ op->immval_mask) ^ REG_PVR_MASK);
146 return (strdup (tmpstr));
158 sprintf (tmpstr, "%s%s", register_prefix, spr);
159 return (strdup (tmpstr));
163 read_insn_microblaze (bfd_vma memaddr,
164 struct disassemble_info *info,
165 struct op_code_struct **opr)
167 unsigned char ibytes[4];
169 struct op_code_struct * op;
172 status = info->read_memory_func (memaddr, ibytes, 4, info);
176 info->memory_error_func (status, memaddr, info);
180 if (info->endian == BFD_ENDIAN_BIG)
181 inst = (ibytes[0] << 24) | (ibytes[1] << 16) | (ibytes[2] << 8) | ibytes[3];
182 else if (info->endian == BFD_ENDIAN_LITTLE)
183 inst = (ibytes[3] << 24) | (ibytes[2] << 16) | (ibytes[1] << 8) | ibytes[0];
187 /* Just a linear search of the table. */
188 for (op = opcodes; op->name != 0; op ++)
189 if (op->bit_sequence == (inst & op->opcode_mask))
198 print_insn_microblaze (bfd_vma memaddr, struct disassemble_info * info)
200 fprintf_ftype print_func = info->fprintf_func;
201 void * stream = info->stream;
202 unsigned long inst, prev_inst;
203 struct op_code_struct * op, *pop;
205 bfd_boolean immfound = FALSE;
206 static bfd_vma prev_insn_addr = -1; /* Init the prev insn addr. */
207 static int prev_insn_vma = -1; /* Init the prev insn vma. */
208 int curr_insn_vma = info->buffer_vma;
210 info->bytes_per_chunk = 4;
212 inst = read_insn_microblaze (memaddr, info, &op);
216 if (prev_insn_vma == curr_insn_vma)
218 if (memaddr-(info->bytes_per_chunk) == prev_insn_addr)
220 prev_inst = read_insn_microblaze (prev_insn_addr, info, &pop);
223 if (pop->instr == imm)
225 immval = (get_int_field_imm (prev_inst) << 16) & 0xffff0000;
236 /* Make curr insn as prev insn. */
237 prev_insn_addr = memaddr;
238 prev_insn_vma = curr_insn_vma;
240 if (op->name == NULL)
241 print_func (stream, ".short 0x%04x", (unsigned int) inst);
244 print_func (stream, "%s", op->name);
246 switch (op->inst_type)
248 case INST_TYPE_RD_R1_R2:
249 print_func (stream, "\t%s, %s, %s", get_field_rd (inst),
250 get_field_r1(inst), get_field_r2 (inst));
252 case INST_TYPE_RD_R1_IMM:
253 print_func (stream, "\t%s, %s, %s", get_field_rd (inst),
254 get_field_r1(inst), get_field_imm (inst));
255 if (info->print_address_func && get_int_field_r1 (inst) == 0
256 && info->symbol_at_address_func)
259 immval |= (get_int_field_imm (inst) & 0x0000ffff);
262 immval = get_int_field_imm (inst);
264 immval |= 0xFFFF0000;
266 if (immval > 0 && info->symbol_at_address_func (immval, info))
268 print_func (stream, "\t// ");
269 info->print_address_func (immval, info);
273 case INST_TYPE_RD_R1_IMM5:
274 print_func (stream, "\t%s, %s, %s", get_field_rd (inst),
275 get_field_r1(inst), get_field_imm5 (inst));
278 case INST_TYPE_RD_RFSL:
279 print_func (stream, "\t%s, %s", get_field_rd (inst), get_field_rfsl (inst));
281 case INST_TYPE_R1_RFSL:
282 print_func (stream, "\t%s, %s", get_field_r1 (inst), get_field_rfsl (inst));
285 case INST_TYPE_RD_SPECIAL:
286 print_func (stream, "\t%s, %s", get_field_rd (inst),
287 get_field_special (inst, op));
289 case INST_TYPE_SPECIAL_R1:
290 print_func (stream, "\t%s, %s", get_field_special (inst, op),
293 case INST_TYPE_RD_R1:
294 print_func (stream, "\t%s, %s", get_field_rd (inst), get_field_r1 (inst));
296 case INST_TYPE_R1_R2:
297 print_func (stream, "\t%s, %s", get_field_r1 (inst), get_field_r2 (inst));
299 case INST_TYPE_R1_IMM:
300 print_func (stream, "\t%s, %s", get_field_r1 (inst), get_field_imm (inst));
301 /* The non-pc relative instructions are returns, which shouldn't
302 have a label printed. */
303 if (info->print_address_func && op->inst_offset_type == INST_PC_OFFSET
304 && info->symbol_at_address_func)
307 immval |= (get_int_field_imm (inst) & 0x0000ffff);
310 immval = get_int_field_imm (inst);
312 immval |= 0xFFFF0000;
315 if (immval > 0 && info->symbol_at_address_func (immval, info))
317 print_func (stream, "\t// ");
318 info->print_address_func (immval, info);
322 print_func (stream, "\t\t// ");
323 print_func (stream, "%x", immval);
327 case INST_TYPE_RD_IMM:
328 print_func (stream, "\t%s, %s", get_field_rd (inst), get_field_imm (inst));
329 if (info->print_address_func && info->symbol_at_address_func)
332 immval |= (get_int_field_imm (inst) & 0x0000ffff);
335 immval = get_int_field_imm (inst);
337 immval |= 0xFFFF0000;
339 if (op->inst_offset_type == INST_PC_OFFSET)
340 immval += (int) memaddr;
341 if (info->symbol_at_address_func (immval, info))
343 print_func (stream, "\t// ");
344 info->print_address_func (immval, info);
350 print_func (stream, "\t%s", get_field_imm (inst));
351 if (info->print_address_func && info->symbol_at_address_func
354 if ((immfound) && (op->inst_type != INST_TYPE_IMM5))
355 immval |= (get_int_field_imm (inst) & 0x0000ffff);
358 if (op->inst_type == INST_TYPE_IMM5)
359 immval = get_int_field_imm5 (inst);
362 immval = get_int_field_imm (inst);
364 immval |= 0xFFFF0000;
367 if (op->inst_offset_type == INST_PC_OFFSET)
368 immval += (int) memaddr;
369 if (immval > 0 && info->symbol_at_address_func (immval, info))
371 print_func (stream, "\t// ");
372 info->print_address_func (immval, info);
374 else if (op->inst_offset_type == INST_PC_OFFSET)
376 print_func (stream, "\t\t// ");
377 print_func (stream, "%x", immval);
381 case INST_TYPE_RD_R2:
382 print_func (stream, "\t%s, %s", get_field_rd (inst), get_field_r2 (inst));
385 print_func (stream, "\t%s", get_field_r2 (inst));
388 print_func (stream, "\t%s", get_field_r1 (inst));
390 case INST_TYPE_RD_R1_SPECIAL:
391 print_func (stream, "\t%s, %s", get_field_rd (inst), get_field_r2 (inst));
394 case INST_TYPE_RD_IMM15:
395 print_func (stream, "\t%s, %s", get_field_rd (inst), get_field_imm15 (inst));
397 /* For tuqula instruction */
399 print_func (stream, "\t%s", get_field_rd (inst));
402 print_func (stream, "\t%s", get_field_rfsl (inst));
406 /* If the disassembler lags the instruction set. */
407 print_func (stream, "\tundecoded operands, inst is 0x%04x", (unsigned int) inst);
412 /* Say how many bytes we consumed. */
416 enum microblaze_instr
417 get_insn_microblaze (long inst,
418 bfd_boolean *isunsignedimm,
419 enum microblaze_instr_type *insn_type,
422 struct op_code_struct * op;
423 *isunsignedimm = FALSE;
425 /* Just a linear search of the table. */
426 for (op = opcodes; op->name != 0; op ++)
427 if (op->bit_sequence == (inst & op->opcode_mask))
434 *isunsignedimm = (op->inst_type == INST_TYPE_RD_R1_UNSIGNED_IMM);
435 *insn_type = op->instr_type;
436 *delay_slots = op->delay_slots;
441 enum microblaze_instr
442 microblaze_decode_insn (long insn, int *rd, int *ra, int *rb, int *immed)
444 enum microblaze_instr op;
446 enum microblaze_instr_type t2;
449 op = get_insn_microblaze (insn, &t1, &t2, &t3);
450 *rd = (insn & RD_MASK) >> RD_LOW;
451 *ra = (insn & RA_MASK) >> RA_LOW;
452 *rb = (insn & RB_MASK) >> RB_LOW;
453 t3 = (insn & IMM_MASK) >> IMM_LOW;
459 microblaze_get_target_address (long inst, bfd_boolean immfound, int immval,
460 long pcval, long r1val, long r2val,
461 bfd_boolean *targetvalid,
462 bfd_boolean *unconditionalbranch)
464 struct op_code_struct * op;
467 *unconditionalbranch = FALSE;
468 /* Just a linear search of the table. */
469 for (op = opcodes; op->name != 0; op ++)
470 if (op->bit_sequence == (inst & op->opcode_mask))
475 *targetvalid = FALSE;
477 else if (op->instr_type == branch_inst)
479 switch (op->inst_type)
482 *unconditionalbranch = TRUE;
484 case INST_TYPE_RD_R2:
485 case INST_TYPE_R1_R2:
488 if (op->inst_offset_type == INST_PC_OFFSET)
492 *unconditionalbranch = TRUE;
494 case INST_TYPE_RD_IMM:
495 case INST_TYPE_R1_IMM:
498 targetaddr = (immval << 16) & 0xffff0000;
499 targetaddr |= (get_int_field_imm (inst) & 0x0000ffff);
503 targetaddr = get_int_field_imm (inst);
504 if (targetaddr & 0x8000)
505 targetaddr |= 0xFFFF0000;
507 if (op->inst_offset_type == INST_PC_OFFSET)
512 *targetvalid = FALSE;
516 else if (op->instr_type == return_inst)
520 targetaddr = (immval << 16) & 0xffff0000;
521 targetaddr |= (get_int_field_imm (inst) & 0x0000ffff);
525 targetaddr = get_int_field_imm (inst);
526 if (targetaddr & 0x8000)
527 targetaddr |= 0xFFFF0000;
533 *targetvalid = FALSE;