1 /* Print instructions for the Texas TMS320C[34]X, for GDB and GNU Binutils.
3 Copyright 2002, 2003 Free Software Foundation, Inc.
5 Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz)
7 This program 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 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "libiberty.h"
24 #include "opcode/tic4x.h"
28 #define TIC4X_HASH_SIZE 11 /* 11 (bits) and above should give unique entries. */
29 #define TIC4X_SPESOP_SIZE 8 /* Max 8. ops for special instructions */
50 static int tic4x_version
= 0;
51 static int tic4x_dp
= 0;
53 static int tic4x_pc_offset
54 PARAMS ((unsigned int));
55 static int tic4x_print_char
56 PARAMS ((struct disassemble_info
*, char));
57 static int tic4x_print_str
58 PARAMS ((struct disassemble_info
*, char *));
59 static int tic4x_print_register
60 PARAMS ((struct disassemble_info
*, unsigned long));
61 static int tic4x_print_addr
62 PARAMS ((struct disassemble_info
*, unsigned long));
63 static int tic4x_print_relative
64 PARAMS ((struct disassemble_info
*, unsigned long, long, unsigned long));
66 PARAMS ((unsigned int, FILE *, fprintf_ftype
));
67 static int tic4x_print_direct
68 PARAMS ((struct disassemble_info
*, unsigned long));
69 static int tic4x_print_immed
70 PARAMS ((struct disassemble_info
*, immed_t
, unsigned long));
71 static int tic4x_print_cond
72 PARAMS ((struct disassemble_info
*, unsigned int));
73 static int tic4x_print_indirect
74 PARAMS ((struct disassemble_info
*, indirect_t
, unsigned long));
75 static int tic4x_print_op
76 PARAMS ((struct disassemble_info
*, unsigned long, tic4x_inst_t
*, unsigned long));
77 static void tic4x_hash_opcode_special
78 PARAMS ((tic4x_inst_t
**, const tic4x_inst_t
*));
79 static void tic4x_hash_opcode
80 PARAMS ((tic4x_inst_t
**, tic4x_inst_t
**, const tic4x_inst_t
*, unsigned long));
81 static int tic4x_disassemble
82 PARAMS ((unsigned long, unsigned long, struct disassemble_info
*));
84 PARAMS ((bfd_vma
, struct disassemble_info
*));
91 /* Determine the PC offset for a C[34]x instruction.
92 This could be simplified using some boolean algebra
93 but at the expense of readability. */
97 case 0x62: /* call (C4x) */
98 case 0x64: /* rptb (C4x) */
102 case 0x65: /* rptbd (C4x) */
111 switch ((op
& 0xffe00000) >> 20)
114 case 0x720: /* callB */
115 case 0x740: /* trapB */
118 case 0x6a2: /* bBd */
119 case 0x6a6: /* bBat */
120 case 0x6aa: /* bBaf */
121 case 0x722: /* lajB */
122 case 0x748: /* latB */
123 case 0x798: /* rptbd */
130 switch ((op
& 0xfe200000) >> 20)
132 case 0x6e0: /* dbB */
135 case 0x6e2: /* dbBd */
146 tic4x_print_char (info
, ch
)
147 struct disassemble_info
* info
;
151 (*info
->fprintf_func
) (info
->stream
, "%c", ch
);
156 tic4x_print_str (info
, str
)
157 struct disassemble_info
*info
;
161 (*info
->fprintf_func
) (info
->stream
, "%s", str
);
166 tic4x_print_register (info
, regno
)
167 struct disassemble_info
*info
;
170 static tic4x_register_t
**registertable
= NULL
;
173 if (registertable
== NULL
)
175 registertable
= (tic4x_register_t
**)
176 xmalloc (sizeof (tic4x_register_t
*) * REG_TABLE_SIZE
);
177 for (i
= 0; i
< tic3x_num_registers
; i
++)
178 registertable
[tic3x_registers
[i
].regno
] = (void *)&tic3x_registers
[i
];
179 if (IS_CPU_TIC4X (tic4x_version
))
181 /* Add C4x additional registers, overwriting
182 any C3x registers if necessary. */
183 for (i
= 0; i
< tic4x_num_registers
; i
++)
184 registertable
[tic4x_registers
[i
].regno
] = (void *)&tic4x_registers
[i
];
187 if ((int) regno
> (IS_CPU_TIC4X (tic4x_version
) ? TIC4X_REG_MAX
: TIC3X_REG_MAX
))
190 (*info
->fprintf_func
) (info
->stream
, "%s", registertable
[regno
]->name
);
195 tic4x_print_addr (info
, addr
)
196 struct disassemble_info
*info
;
200 (*info
->print_address_func
)(addr
, info
);
205 tic4x_print_relative (info
, pc
, offset
, opcode
)
206 struct disassemble_info
*info
;
209 unsigned long opcode
;
211 return tic4x_print_addr (info
, pc
+ offset
+ tic4x_pc_offset (opcode
));
215 tic4x_print_direct (info
, arg
)
216 struct disassemble_info
*info
;
221 (*info
->fprintf_func
) (info
->stream
, "@");
222 tic4x_print_addr (info
, arg
+ (tic4x_dp
<< 16));
227 /* FIXME: make the floating point stuff not rely on host
228 floating point arithmetic. */
230 tic4x_print_ftoa (val
, stream
, pfunc
)
240 e
= EXTRS (val
, 31, 24); /* exponent */
243 s
= EXTRU (val
, 23, 23); /* sign bit */
244 f
= EXTRU (val
, 22, 0); /* mantissa */
249 num
= f
/ (double)(1 << 23);
250 num
= ldexp (num
, e
);
252 (*pfunc
)(stream
, "%.9g", num
);
256 tic4x_print_immed (info
, type
, arg
)
257 struct disassemble_info
*info
;
272 (*info
->fprintf_func
) (info
->stream
, "%d", (long)arg
);
277 (*info
->fprintf_func
) (info
->stream
, "%u", arg
);
281 e
= EXTRS (arg
, 15, 12);
284 s
= EXTRU (arg
, 11, 11);
285 f
= EXTRU (arg
, 10, 0);
290 num
= f
/ (double)(1 << 11);
291 num
= ldexp (num
, e
);
293 (*info
->fprintf_func
) (info
->stream
, "%f", num
);
296 e
= EXTRS (arg
, 31, 24);
299 s
= EXTRU (arg
, 23, 23);
300 f
= EXTRU (arg
, 22, 0);
305 num
= f
/ (double)(1 << 23);
306 num
= ldexp (num
, e
);
308 (*info
->fprintf_func
) (info
->stream
, "%f", num
);
315 tic4x_print_cond (info
, cond
)
316 struct disassemble_info
*info
;
319 static tic4x_cond_t
**condtable
= NULL
;
322 if (condtable
== NULL
)
324 condtable
= (tic4x_cond_t
**)xmalloc (sizeof (tic4x_cond_t
*) * 32);
325 for (i
= 0; i
< tic4x_num_conds
; i
++)
326 condtable
[tic4x_conds
[i
].cond
] = (void *)&tic4x_conds
[i
];
328 if (cond
> 31 || condtable
[cond
] == NULL
)
331 (*info
->fprintf_func
) (info
->stream
, "%s", condtable
[cond
]->name
);
336 tic4x_print_indirect (info
, type
, arg
)
337 struct disassemble_info
*info
;
351 case INDIRECT_TIC4X
: /* *+ARn(disp) */
352 disp
= EXTRU (arg
, 7, 3);
353 aregno
= EXTRU (arg
, 2, 0) + REG_AR0
;
358 aregno
= EXTRU (arg
, 2, 0) + REG_AR0
;
359 modn
= EXTRU (arg
, 7, 3);
362 disp
= EXTRU (arg
, 7, 0);
363 aregno
= EXTRU (arg
, 10, 8) + REG_AR0
;
364 modn
= EXTRU (arg
, 15, 11);
365 if (modn
> 7 && disp
!= 0)
369 (*info
->fprintf_func
)(info
->stream
, "# internal error: Unknown indirect type %d", type
);
372 if (modn
> TIC3X_MODN_MAX
)
374 a
= tic4x_indirects
[modn
].name
;
380 tic4x_print_register (info
, aregno
);
383 tic4x_print_immed (info
, IMMED_UINT
, disp
);
386 tic4x_print_str (info
, "ir0");
389 tic4x_print_str (info
, "ir1");
392 tic4x_print_char (info
, *a
);
401 tic4x_print_op (info
, instruction
, p
, pc
)
402 struct disassemble_info
*info
;
403 unsigned long instruction
;
409 char *parallel
= NULL
;
411 /* Print instruction name. */
413 while (*s
&& parallel
== NULL
)
418 if (! tic4x_print_cond (info
, EXTRU (instruction
, 20, 16)))
422 if (! tic4x_print_cond (info
, EXTRU (instruction
, 27, 23)))
426 parallel
= s
+ 1; /* Skip past `_' in name */
429 tic4x_print_char (info
, *s
);
435 /* Print arguments. */
438 tic4x_print_char (info
, ' ');
444 case '*': /* indirect 0--15 */
445 if (! tic4x_print_indirect (info
, INDIRECT_LONG
,
446 EXTRU (instruction
, 15, 0)))
450 case '#': /* only used for ldp, ldpk */
451 tic4x_print_immed (info
, IMMED_UINT
, EXTRU (instruction
, 15, 0));
454 case '@': /* direct 0--15 */
455 tic4x_print_direct (info
, EXTRU (instruction
, 15, 0));
458 case 'A': /* address register 24--22 */
459 if (! tic4x_print_register (info
, EXTRU (instruction
, 24, 22) +
464 case 'B': /* 24-bit unsigned int immediate br(d)/call/rptb
466 if (IS_CPU_TIC4X (tic4x_version
))
467 tic4x_print_relative (info
, pc
, EXTRS (instruction
, 23, 0),
470 tic4x_print_addr (info
, EXTRU (instruction
, 23, 0));
473 case 'C': /* indirect (short C4x) 0--7 */
474 if (! IS_CPU_TIC4X (tic4x_version
))
476 if (! tic4x_print_indirect (info
, INDIRECT_TIC4X
,
477 EXTRU (instruction
, 7, 0)))
482 /* Cockup if get here... */
485 case 'E': /* register 0--7 */
487 if (! tic4x_print_register (info
, EXTRU (instruction
, 7, 0)))
491 case 'F': /* 16-bit float immediate 0--15 */
492 tic4x_print_immed (info
, IMMED_SFLOAT
,
493 EXTRU (instruction
, 15, 0));
496 case 'i': /* Extended indirect 0--7 */
497 if ( EXTRU (instruction
, 7, 5) == 7 )
499 if( !tic4x_print_register (info
, EXTRU (instruction
, 4, 0)) )
505 case 'I': /* indirect (short) 0--7 */
506 if (! tic4x_print_indirect (info
, INDIRECT_SHORT
,
507 EXTRU (instruction
, 7, 0)))
511 case 'j': /* Extended indirect 8--15 */
512 if ( EXTRU (instruction
, 15, 13) == 7 )
514 if( !tic4x_print_register (info
, EXTRU (instruction
, 12, 8)) )
519 case 'J': /* indirect (short) 8--15 */
520 if (! tic4x_print_indirect (info
, INDIRECT_SHORT
,
521 EXTRU (instruction
, 15, 8)))
525 case 'G': /* register 8--15 */
527 if (! tic4x_print_register (info
, EXTRU (instruction
, 15, 8)))
531 case 'H': /* register 16--18 */
532 if (! tic4x_print_register (info
, EXTRU (instruction
, 18, 16)))
536 case 'K': /* register 19--21 */
537 if (! tic4x_print_register (info
, EXTRU (instruction
, 21, 19)))
541 case 'L': /* register 22--24 */
542 if (! tic4x_print_register (info
, EXTRU (instruction
, 24, 22)))
546 case 'M': /* register 22--22 */
547 tic4x_print_register (info
, EXTRU (instruction
, 22, 22) + REG_R2
);
550 case 'N': /* register 23--23 */
551 tic4x_print_register (info
, EXTRU (instruction
, 23, 23) + REG_R0
);
554 case 'O': /* indirect (short C4x) 8--15 */
555 if (! IS_CPU_TIC4X (tic4x_version
))
557 if (! tic4x_print_indirect (info
, INDIRECT_TIC4X
,
558 EXTRU (instruction
, 15, 8)))
562 case 'P': /* displacement 0--15 (used by Bcond and BcondD) */
563 tic4x_print_relative (info
, pc
, EXTRS (instruction
, 15, 0),
567 case 'Q': /* register 0--15 */
569 if (! tic4x_print_register (info
, EXTRU (instruction
, 15, 0)))
573 case 'R': /* register 16--20 */
575 if (! tic4x_print_register (info
, EXTRU (instruction
, 20, 16)))
579 case 'S': /* 16-bit signed immediate 0--15 */
580 tic4x_print_immed (info
, IMMED_SINT
,
581 EXTRS (instruction
, 15, 0));
584 case 'T': /* 5-bit signed immediate 16--20 (C4x stik) */
585 if (! IS_CPU_TIC4X (tic4x_version
))
587 if (! tic4x_print_immed (info
, IMMED_SUINT
,
588 EXTRU (instruction
, 20, 16)))
592 case 'U': /* 16-bit unsigned int immediate 0--15 */
593 tic4x_print_immed (info
, IMMED_SUINT
, EXTRU (instruction
, 15, 0));
596 case 'V': /* 5/9-bit unsigned vector 0--4/8 */
597 tic4x_print_immed (info
, IMMED_SUINT
,
598 IS_CPU_TIC4X (tic4x_version
) ?
599 EXTRU (instruction
, 8, 0) :
600 EXTRU (instruction
, 4, 0) & ~0x20);
603 case 'W': /* 8-bit signed immediate 0--7 */
604 if (! IS_CPU_TIC4X (tic4x_version
))
606 tic4x_print_immed (info
, IMMED_SINT
, EXTRS (instruction
, 7, 0));
609 case 'X': /* expansion register 4--0 */
610 val
= EXTRU (instruction
, 4, 0) + REG_IVTP
;
611 if (val
< REG_IVTP
|| val
> REG_TVTP
)
613 if (! tic4x_print_register (info
, val
))
617 case 'Y': /* address register 16--20 */
618 val
= EXTRU (instruction
, 20, 16);
619 if (val
< REG_AR0
|| val
> REG_SP
)
621 if (! tic4x_print_register (info
, val
))
625 case 'Z': /* expansion register 16--20 */
626 val
= EXTRU (instruction
, 20, 16) + REG_IVTP
;
627 if (val
< REG_IVTP
|| val
> REG_TVTP
)
629 if (! tic4x_print_register (info
, val
))
633 case '|': /* Parallel instruction */
634 tic4x_print_str (info
, " || ");
635 tic4x_print_str (info
, parallel
);
636 tic4x_print_char (info
, ' ');
640 tic4x_print_char (info
, ',');
644 tic4x_print_char (info
, *s
);
653 tic4x_hash_opcode_special (optable_special
, inst
)
654 tic4x_inst_t
**optable_special
;
655 const tic4x_inst_t
*inst
;
659 for( i
=0; i
<TIC4X_SPESOP_SIZE
; i
++ )
660 if( optable_special
[i
] != NULL
661 && optable_special
[i
]->opcode
== inst
->opcode
)
663 /* Collision (we have it already) - overwrite */
664 optable_special
[i
] = (void *)inst
;
668 for( i
=0; i
<TIC4X_SPESOP_SIZE
; i
++ )
669 if( optable_special
[i
] == NULL
)
671 /* Add the new opcode */
672 optable_special
[i
] = (void *)inst
;
676 /* This should never occur. This happens if the number of special
677 instructions exceeds TIC4X_SPESOP_SIZE. Please increase the variable
680 printf("optable_special[] is full, please increase TIC4X_SPESOP_SIZE!\n");
685 tic4x_hash_opcode (optable
, optable_special
, inst
, tic4x_oplevel
)
686 tic4x_inst_t
**optable
;
687 tic4x_inst_t
**optable_special
;
688 const tic4x_inst_t
*inst
;
689 const unsigned long tic4x_oplevel
;
692 int opcode
= inst
->opcode
>> (32 - TIC4X_HASH_SIZE
);
693 int opmask
= inst
->opmask
>> (32 - TIC4X_HASH_SIZE
);
695 /* Use a TIC4X_HASH_SIZE bit index as a hash index. We should
696 have unique entries so there's no point having a linked list
698 for (j
= opcode
; j
< opmask
; j
++)
699 if ( (j
& opmask
) == opcode
700 && inst
->oplevel
& tic4x_oplevel
)
703 /* We should only have collisions for synonyms like
705 if (optable
[j
] != NULL
)
706 printf("Collision at index %d, %s and %s\n",
707 j
, optable
[j
]->name
, inst
->name
);
709 /* Catch those ops that collide with others already inside the
710 hash, and have a opmask greater than the one we use in the
711 hash. Store them in a special-list, that will handle full
712 32-bit INSN, not only the first 11-bit (or so). */
713 if ( optable
[j
] != NULL
714 && inst
->opmask
& ~(opmask
<< (32 - TIC4X_HASH_SIZE
)) )
716 /* Add the instruction already on the list */
717 tic4x_hash_opcode_special(optable_special
, optable
[j
]);
719 /* Add the new instruction */
720 tic4x_hash_opcode_special(optable_special
, inst
);
723 optable
[j
] = (void *)inst
;
727 /* Disassemble the instruction in 'instruction'.
728 'pc' should be the address of this instruction, it will
729 be used to print the target address if this is a relative jump or call
730 the disassembled instruction is written to 'info'.
731 The function returns the length of this instruction in words. */
734 tic4x_disassemble (pc
, instruction
, info
)
736 unsigned long instruction
;
737 struct disassemble_info
*info
;
739 static tic4x_inst_t
**optable
= NULL
;
740 static tic4x_inst_t
**optable_special
= NULL
;
743 unsigned long tic4x_oplevel
;
745 tic4x_version
= info
->mach
;
747 tic4x_oplevel
= (IS_CPU_TIC4X (tic4x_version
)) ? OP_C4X
: 0;
748 tic4x_oplevel
|= OP_C3X
|OP_LPWR
|OP_IDLE2
|OP_ENH
;
752 optable
= (tic4x_inst_t
**)
753 xcalloc (sizeof (tic4x_inst_t
*), (1 << TIC4X_HASH_SIZE
));
755 optable_special
= (tic4x_inst_t
**)
756 xcalloc (sizeof (tic4x_inst_t
*), TIC4X_SPESOP_SIZE
);
758 /* Install opcodes in reverse order so that preferred
759 forms overwrite synonyms. */
760 for (i
= tic4x_num_insts
- 1; i
>= 0; i
--)
761 tic4x_hash_opcode (optable
, optable_special
, &tic4x_insts
[i
], tic4x_oplevel
);
763 /* We now need to remove the insn that are special from the
764 "normal" optable, to make the disasm search this extra list
767 for (i
=0; i
<TIC4X_SPESOP_SIZE
; i
++)
768 if ( optable_special
[i
] != NULL
)
769 optable
[optable_special
[i
]->opcode
>> (32 - TIC4X_HASH_SIZE
)] = NULL
;
772 /* See if we can pick up any loading of the DP register... */
773 if ((instruction
>> 16) == 0x5070 || (instruction
>> 16) == 0x1f70)
774 tic4x_dp
= EXTRU (instruction
, 15, 0);
776 p
= optable
[instruction
>> (32 - TIC4X_HASH_SIZE
)];
779 if ( ((instruction
& p
->opmask
) == p
->opcode
)
780 && tic4x_print_op (NULL
, instruction
, p
, pc
) )
781 tic4x_print_op (info
, instruction
, p
, pc
);
783 (*info
->fprintf_func
) (info
->stream
, "%08x", instruction
);
787 for (i
= 0; i
<TIC4X_SPESOP_SIZE
; i
++)
788 if (optable_special
[i
] != NULL
789 && optable_special
[i
]->opcode
== instruction
)
791 (*info
->fprintf_func
)(info
->stream
, "%s", optable_special
[i
]->name
);
794 if (i
==TIC4X_SPESOP_SIZE
)
795 (*info
->fprintf_func
) (info
->stream
, "%08x", instruction
);
798 /* Return size of insn in words. */
802 /* The entry point from objdump and gdb. */
804 print_insn_tic4x (memaddr
, info
)
806 struct disassemble_info
*info
;
813 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 4, info
);
816 (*info
->memory_error_func
) (status
, memaddr
, info
);
821 op
= bfd_getl32 (buffer
);
822 info
->bytes_per_line
= 4;
823 info
->bytes_per_chunk
= 4;
824 info
->octets_per_byte
= 4;
825 info
->display_endian
= BFD_ENDIAN_LITTLE
;
826 return tic4x_disassemble (pc
, op
, info
) * 4;