1 /* Disassemble Xilinx microblaze instructions.
3 Copyright (C) 2009-2019 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. */
27 #include "disassemble.h"
29 #include "microblaze-opc.h"
30 #include "microblaze-dis.h"
32 #define get_field_rd(buf, instr) get_field (buf, instr, RD_MASK, RD_LOW)
33 #define get_field_r1(buf, instr) get_field (buf, instr, RA_MASK, RA_LOW)
34 #define get_field_r2(buf, instr) get_field (buf, instr, RB_MASK, RB_LOW)
35 #define get_int_field_imm(instr) ((instr & IMM_MASK) >> IMM_LOW)
36 #define get_int_field_r1(instr) ((instr & RA_MASK) >> RA_LOW)
39 #define STRBUF_SIZE 25
44 char str
[NUM_STRBUFS
][STRBUF_SIZE
];
48 strbuf (struct string_buf
*buf
)
50 #ifdef ENABLE_CHECKING
51 if (buf
->which
>= NUM_STRBUFS
)
54 return buf
->str
[buf
->which
++];
58 get_field (struct string_buf
*buf
, long instr
, long mask
, unsigned short low
)
60 char *p
= strbuf (buf
);
62 sprintf (p
, "%s%d", register_prefix
, (int)((instr
& mask
) >> low
));
67 get_field_imm (struct string_buf
*buf
, long instr
)
69 char *p
= strbuf (buf
);
71 sprintf (p
, "%d", (short)((instr
& IMM_MASK
) >> IMM_LOW
));
76 get_field_imm5 (struct string_buf
*buf
, long instr
)
78 char *p
= strbuf (buf
);
80 sprintf (p
, "%d", (short)((instr
& IMM5_MASK
) >> IMM_LOW
));
85 get_field_imm5_mbar (struct string_buf
*buf
, long instr
)
87 char *p
= strbuf (buf
);
89 sprintf (p
, "%d", (short)((instr
& IMM5_MBAR_MASK
) >> IMM_MBAR
));
94 get_field_rfsl (struct string_buf
*buf
, long instr
)
96 char *p
= strbuf (buf
);
98 sprintf (p
, "%s%d", fsl_register_prefix
,
99 (short)((instr
& RFSL_MASK
) >> IMM_LOW
));
104 get_field_imm15 (struct string_buf
*buf
, long instr
)
106 char *p
= strbuf (buf
);
108 sprintf (p
, "%d", (short)((instr
& IMM15_MASK
) >> IMM_LOW
));
113 get_field_special (struct string_buf
*buf
, long instr
,
114 struct op_code_struct
*op
)
116 char *p
= strbuf (buf
);
119 switch ((((instr
& IMM_MASK
) >> IMM_LOW
) ^ op
->immval_mask
))
151 case REG_TLBLO_MASK
:
154 case REG_TLBHI_MASK
:
157 case REG_TLBSX_MASK
:
167 if (((((instr
& IMM_MASK
) >> IMM_LOW
) ^ op
->immval_mask
) & 0xE000)
170 sprintf (p
, "%spvr%d", register_prefix
,
171 (unsigned short)(((instr
& IMM_MASK
) >> IMM_LOW
)
172 ^ op
->immval_mask
) ^ REG_PVR_MASK
);
180 sprintf (p
, "%s%s", register_prefix
, spr
);
185 read_insn_microblaze (bfd_vma memaddr
,
186 struct disassemble_info
*info
,
187 struct op_code_struct
**opr
)
189 unsigned char ibytes
[4];
191 struct op_code_struct
* op
;
194 status
= info
->read_memory_func (memaddr
, ibytes
, 4, info
);
198 info
->memory_error_func (status
, memaddr
, info
);
202 if (info
->endian
== BFD_ENDIAN_BIG
)
203 inst
= (ibytes
[0] << 24) | (ibytes
[1] << 16) | (ibytes
[2] << 8) | ibytes
[3];
204 else if (info
->endian
== BFD_ENDIAN_LITTLE
)
205 inst
= (ibytes
[3] << 24) | (ibytes
[2] << 16) | (ibytes
[1] << 8) | ibytes
[0];
209 /* Just a linear search of the table. */
210 for (op
= opcodes
; op
->name
!= 0; op
++)
211 if (op
->bit_sequence
== (inst
& op
->opcode_mask
))
220 print_insn_microblaze (bfd_vma memaddr
, struct disassemble_info
* info
)
222 fprintf_ftype print_func
= info
->fprintf_func
;
223 void * stream
= info
->stream
;
224 unsigned long inst
, prev_inst
;
225 struct op_code_struct
* op
, *pop
;
227 bfd_boolean immfound
= FALSE
;
228 static bfd_vma prev_insn_addr
= -1; /* Init the prev insn addr. */
229 static int prev_insn_vma
= -1; /* Init the prev insn vma. */
230 int curr_insn_vma
= info
->buffer_vma
;
231 struct string_buf buf
;
234 info
->bytes_per_chunk
= 4;
236 inst
= read_insn_microblaze (memaddr
, info
, &op
);
240 if (prev_insn_vma
== curr_insn_vma
)
242 if (memaddr
-(info
->bytes_per_chunk
) == prev_insn_addr
)
244 prev_inst
= read_insn_microblaze (prev_insn_addr
, info
, &pop
);
247 if (pop
->instr
== imm
)
249 immval
= (get_int_field_imm (prev_inst
) << 16) & 0xffff0000;
260 /* Make curr insn as prev insn. */
261 prev_insn_addr
= memaddr
;
262 prev_insn_vma
= curr_insn_vma
;
264 if (op
->name
== NULL
)
265 print_func (stream
, ".short 0x%04x", (unsigned int) inst
);
268 print_func (stream
, "%s", op
->name
);
270 switch (op
->inst_type
)
272 case INST_TYPE_RD_R1_R2
:
273 print_func (stream
, "\t%s, %s, %s", get_field_rd (&buf
, inst
),
274 get_field_r1 (&buf
, inst
), get_field_r2 (&buf
, inst
));
276 case INST_TYPE_RD_R1_IMM
:
277 print_func (stream
, "\t%s, %s, %s", get_field_rd (&buf
, inst
),
278 get_field_r1 (&buf
, inst
), get_field_imm (&buf
, inst
));
279 if (info
->print_address_func
&& get_int_field_r1 (inst
) == 0
280 && info
->symbol_at_address_func
)
283 immval
|= (get_int_field_imm (inst
) & 0x0000ffff);
286 immval
= get_int_field_imm (inst
);
288 immval
|= 0xFFFF0000;
290 if (immval
> 0 && info
->symbol_at_address_func (immval
, info
))
292 print_func (stream
, "\t// ");
293 info
->print_address_func (immval
, info
);
297 case INST_TYPE_RD_R1_IMM5
:
298 print_func (stream
, "\t%s, %s, %s", get_field_rd (&buf
, inst
),
299 get_field_r1 (&buf
, inst
), get_field_imm5 (&buf
, inst
));
301 case INST_TYPE_RD_RFSL
:
302 print_func (stream
, "\t%s, %s", get_field_rd (&buf
, inst
),
303 get_field_rfsl (&buf
, inst
));
305 case INST_TYPE_R1_RFSL
:
306 print_func (stream
, "\t%s, %s", get_field_r1 (&buf
, inst
),
307 get_field_rfsl (&buf
, inst
));
309 case INST_TYPE_RD_SPECIAL
:
310 print_func (stream
, "\t%s, %s", get_field_rd (&buf
, inst
),
311 get_field_special (&buf
, inst
, op
));
313 case INST_TYPE_SPECIAL_R1
:
314 print_func (stream
, "\t%s, %s", get_field_special (&buf
, inst
, op
),
315 get_field_r1 (&buf
, inst
));
317 case INST_TYPE_RD_R1
:
318 print_func (stream
, "\t%s, %s", get_field_rd (&buf
, inst
),
319 get_field_r1 (&buf
, inst
));
321 case INST_TYPE_R1_R2
:
322 print_func (stream
, "\t%s, %s", get_field_r1 (&buf
, inst
),
323 get_field_r2 (&buf
, inst
));
325 case INST_TYPE_R1_IMM
:
326 print_func (stream
, "\t%s, %s", get_field_r1 (&buf
, inst
),
327 get_field_imm (&buf
, inst
));
328 /* The non-pc relative instructions are returns, which shouldn't
329 have a label printed. */
330 if (info
->print_address_func
&& op
->inst_offset_type
== INST_PC_OFFSET
331 && info
->symbol_at_address_func
)
334 immval
|= (get_int_field_imm (inst
) & 0x0000ffff);
337 immval
= get_int_field_imm (inst
);
339 immval
|= 0xFFFF0000;
342 if (immval
> 0 && info
->symbol_at_address_func (immval
, info
))
344 print_func (stream
, "\t// ");
345 info
->print_address_func (immval
, info
);
349 print_func (stream
, "\t\t// ");
350 print_func (stream
, "%x", immval
);
354 case INST_TYPE_RD_IMM
:
355 print_func (stream
, "\t%s, %s", get_field_rd (&buf
, inst
),
356 get_field_imm (&buf
, inst
));
357 if (info
->print_address_func
&& info
->symbol_at_address_func
)
360 immval
|= (get_int_field_imm (inst
) & 0x0000ffff);
363 immval
= get_int_field_imm (inst
);
365 immval
|= 0xFFFF0000;
367 if (op
->inst_offset_type
== INST_PC_OFFSET
)
368 immval
+= (int) memaddr
;
369 if (info
->symbol_at_address_func (immval
, info
))
371 print_func (stream
, "\t// ");
372 info
->print_address_func (immval
, info
);
377 print_func (stream
, "\t%s", get_field_imm (&buf
, inst
));
378 if (info
->print_address_func
&& info
->symbol_at_address_func
382 immval
|= (get_int_field_imm (inst
) & 0x0000ffff);
385 immval
= get_int_field_imm (inst
);
387 immval
|= 0xFFFF0000;
389 if (op
->inst_offset_type
== INST_PC_OFFSET
)
390 immval
+= (int) memaddr
;
391 if (immval
> 0 && info
->symbol_at_address_func (immval
, info
))
393 print_func (stream
, "\t// ");
394 info
->print_address_func (immval
, info
);
396 else if (op
->inst_offset_type
== INST_PC_OFFSET
)
398 print_func (stream
, "\t\t// ");
399 print_func (stream
, "%x", immval
);
403 case INST_TYPE_RD_R2
:
404 print_func (stream
, "\t%s, %s", get_field_rd (&buf
, inst
),
405 get_field_r2 (&buf
, inst
));
408 print_func (stream
, "\t%s", get_field_r2 (&buf
, inst
));
411 print_func (stream
, "\t%s", get_field_r1 (&buf
, inst
));
413 case INST_TYPE_R1_R2_SPECIAL
:
414 print_func (stream
, "\t%s, %s", get_field_r1 (&buf
, inst
),
415 get_field_r2 (&buf
, inst
));
417 case INST_TYPE_RD_IMM15
:
418 print_func (stream
, "\t%s, %s", get_field_rd (&buf
, inst
),
419 get_field_imm15 (&buf
, inst
));
423 print_func (stream
, "\t%s", get_field_imm5_mbar (&buf
, inst
));
425 /* For mbar 16 or sleep insn. */
428 /* For tuqula instruction */
430 print_func (stream
, "\t%s", get_field_rd (&buf
, inst
));
433 print_func (stream
, "\t%s", get_field_rfsl (&buf
, inst
));
436 /* If the disassembler lags the instruction set. */
437 print_func (stream
, "\tundecoded operands, inst is 0x%04x",
438 (unsigned int) inst
);
443 /* Say how many bytes we consumed. */
447 enum microblaze_instr
448 get_insn_microblaze (long inst
,
449 bfd_boolean
*isunsignedimm
,
450 enum microblaze_instr_type
*insn_type
,
453 struct op_code_struct
* op
;
454 *isunsignedimm
= FALSE
;
456 /* Just a linear search of the table. */
457 for (op
= opcodes
; op
->name
!= 0; op
++)
458 if (op
->bit_sequence
== (inst
& op
->opcode_mask
))
465 *isunsignedimm
= (op
->inst_type
== INST_TYPE_RD_R1_UNSIGNED_IMM
);
466 *insn_type
= op
->instr_type
;
467 *delay_slots
= op
->delay_slots
;
472 enum microblaze_instr
473 microblaze_decode_insn (long insn
, int *rd
, int *ra
, int *rb
, int *immed
)
475 enum microblaze_instr op
;
477 enum microblaze_instr_type t2
;
480 op
= get_insn_microblaze (insn
, &t1
, &t2
, &t3
);
481 *rd
= (insn
& RD_MASK
) >> RD_LOW
;
482 *ra
= (insn
& RA_MASK
) >> RA_LOW
;
483 *rb
= (insn
& RB_MASK
) >> RB_LOW
;
484 t3
= (insn
& IMM_MASK
) >> IMM_LOW
;
490 microblaze_get_target_address (long inst
, bfd_boolean immfound
, int immval
,
491 long pcval
, long r1val
, long r2val
,
492 bfd_boolean
*targetvalid
,
493 bfd_boolean
*unconditionalbranch
)
495 struct op_code_struct
* op
;
498 *unconditionalbranch
= FALSE
;
499 /* Just a linear search of the table. */
500 for (op
= opcodes
; op
->name
!= 0; op
++)
501 if (op
->bit_sequence
== (inst
& op
->opcode_mask
))
506 *targetvalid
= FALSE
;
508 else if (op
->instr_type
== branch_inst
)
510 switch (op
->inst_type
)
513 *unconditionalbranch
= TRUE
;
515 case INST_TYPE_RD_R2
:
516 case INST_TYPE_R1_R2
:
519 if (op
->inst_offset_type
== INST_PC_OFFSET
)
523 *unconditionalbranch
= TRUE
;
525 case INST_TYPE_RD_IMM
:
526 case INST_TYPE_R1_IMM
:
529 targetaddr
= (immval
<< 16) & 0xffff0000;
530 targetaddr
|= (get_int_field_imm (inst
) & 0x0000ffff);
534 targetaddr
= get_int_field_imm (inst
);
535 if (targetaddr
& 0x8000)
536 targetaddr
|= 0xFFFF0000;
538 if (op
->inst_offset_type
== INST_PC_OFFSET
)
543 *targetvalid
= FALSE
;
547 else if (op
->instr_type
== return_inst
)
551 targetaddr
= (immval
<< 16) & 0xffff0000;
552 targetaddr
|= (get_int_field_imm (inst
) & 0x0000ffff);
556 targetaddr
= get_int_field_imm (inst
);
557 if (targetaddr
& 0x8000)
558 targetaddr
|= 0xFFFF0000;
564 *targetvalid
= FALSE
;