1 /* Print instructions for the Texas TMS320C[34]X, for GDB and GNU Binutils.
3 Copyright (C) 2002-2019 Free Software Foundation, Inc.
5 Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz)
7 This file is part of the GNU opcodes library.
9 This library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 It is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17 License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
26 #include "libiberty.h"
27 #include "disassemble.h"
28 #include "opcode/tic4x.h"
32 #define TIC4X_HASH_SIZE 11 /* 11 (bits) and above should give unique entries. */
33 #define TIC4X_SPESOP_SIZE 8 /* Max 8. ops for special instructions. */
54 static int tic4x_version
= 0;
55 static int tic4x_dp
= 0;
58 tic4x_pc_offset (unsigned int op
)
60 /* Determine the PC offset for a C[34]x instruction.
61 This could be simplified using some boolean algebra
62 but at the expense of readability. */
66 case 0x62: /* call (C4x) */
67 case 0x64: /* rptb (C4x) */
71 case 0x65: /* rptbd (C4x) */
80 switch ((op
& 0xffe00000) >> 20)
83 case 0x720: /* callB */
84 case 0x740: /* trapB */
88 case 0x6a6: /* bBat */
89 case 0x6aa: /* bBaf */
90 case 0x722: /* lajB */
91 case 0x748: /* latB */
92 case 0x798: /* rptbd */
99 switch ((op
& 0xfe200000) >> 20)
101 case 0x6e0: /* dbB */
104 case 0x6e2: /* dbBd */
115 tic4x_print_char (struct disassemble_info
* info
, char ch
)
118 (*info
->fprintf_func
) (info
->stream
, "%c", ch
);
123 tic4x_print_str (struct disassemble_info
*info
, const char *str
)
126 (*info
->fprintf_func
) (info
->stream
, "%s", str
);
131 tic4x_print_register (struct disassemble_info
*info
, unsigned long regno
)
133 static tic4x_register_t
** registertable
= NULL
;
136 if (registertable
== NULL
)
138 registertable
= xmalloc (sizeof (tic4x_register_t
*) * REG_TABLE_SIZE
);
139 for (i
= 0; i
< tic3x_num_registers
; i
++)
140 registertable
[tic3x_registers
[i
].regno
]
141 = (tic4x_register_t
*) (tic3x_registers
+ i
);
142 if (IS_CPU_TIC4X (tic4x_version
))
144 /* Add C4x additional registers, overwriting
145 any C3x registers if necessary. */
146 for (i
= 0; i
< tic4x_num_registers
; i
++)
147 registertable
[tic4x_registers
[i
].regno
]
148 = (tic4x_register_t
*)(tic4x_registers
+ i
);
151 if (regno
> (IS_CPU_TIC4X (tic4x_version
) ? TIC4X_REG_MAX
: TIC3X_REG_MAX
)
152 || registertable
[regno
] == NULL
)
155 (*info
->fprintf_func
) (info
->stream
, "%s", registertable
[regno
]->name
);
160 tic4x_print_addr (struct disassemble_info
*info
, unsigned long addr
)
163 (*info
->print_address_func
)(addr
, info
);
168 tic4x_print_relative (struct disassemble_info
*info
,
171 unsigned long opcode
)
173 return tic4x_print_addr (info
, pc
+ offset
+ tic4x_pc_offset (opcode
));
177 tic4x_print_direct (struct disassemble_info
*info
, unsigned long arg
)
181 (*info
->fprintf_func
) (info
->stream
, "@");
182 tic4x_print_addr (info
, arg
+ (tic4x_dp
<< 16));
187 /* FIXME: make the floating point stuff not rely on host
188 floating point arithmetic. */
191 tic4x_print_ftoa (unsigned int val
, FILE *stream
, fprintf_ftype pfunc
)
198 e
= EXTRS (val
, 31, 24); /* Exponent. */
201 s
= EXTRU (val
, 23, 23); /* Sign bit. */
202 f
= EXTRU (val
, 22, 0); /* Mantissa. */
207 num
= f
/ (double)(1 << 23);
208 num
= ldexp (num
, e
);
210 (*pfunc
)(stream
, "%.9g", num
);
215 tic4x_print_immed (struct disassemble_info
*info
,
230 (*info
->fprintf_func
) (info
->stream
, "%ld", (long) arg
);
235 (*info
->fprintf_func
) (info
->stream
, "%lu", arg
);
239 e
= EXTRS (arg
, 15, 12);
242 s
= EXTRU (arg
, 11, 11);
243 f
= EXTRU (arg
, 10, 0);
248 num
= f
/ (double)(1 << 11);
249 num
= ldexp (num
, e
);
251 (*info
->fprintf_func
) (info
->stream
, "%f", num
);
254 e
= EXTRS (arg
, 31, 24);
257 s
= EXTRU (arg
, 23, 23);
258 f
= EXTRU (arg
, 22, 0);
263 num
= f
/ (double)(1 << 23);
264 num
= ldexp (num
, e
);
266 (*info
->fprintf_func
) (info
->stream
, "%f", num
);
273 tic4x_print_cond (struct disassemble_info
*info
, unsigned int cond
)
275 static tic4x_cond_t
**condtable
= NULL
;
278 if (condtable
== NULL
)
280 condtable
= xmalloc (sizeof (tic4x_cond_t
*) * 32);
281 for (i
= 0; i
< tic4x_num_conds
; i
++)
282 condtable
[tic4x_conds
[i
].cond
] = (tic4x_cond_t
*)(tic4x_conds
+ i
);
284 if (cond
> 31 || condtable
[cond
] == NULL
)
287 (*info
->fprintf_func
) (info
->stream
, "%s", condtable
[cond
]->name
);
292 tic4x_print_indirect (struct disassemble_info
*info
,
306 case INDIRECT_TIC4X
: /* *+ARn(disp) */
307 disp
= EXTRU (arg
, 7, 3);
308 aregno
= EXTRU (arg
, 2, 0) + REG_AR0
;
313 aregno
= EXTRU (arg
, 2, 0) + REG_AR0
;
314 modn
= EXTRU (arg
, 7, 3);
317 disp
= EXTRU (arg
, 7, 0);
318 aregno
= EXTRU (arg
, 10, 8) + REG_AR0
;
319 modn
= EXTRU (arg
, 15, 11);
320 if (modn
> 7 && disp
!= 0)
324 (*info
->fprintf_func
)(info
->stream
, "# internal error: Unknown indirect type %d", type
);
327 if (modn
> TIC3X_MODN_MAX
)
329 a
= tic4x_indirects
[modn
].name
;
335 tic4x_print_register (info
, aregno
);
338 tic4x_print_immed (info
, IMMED_UINT
, disp
);
341 tic4x_print_str (info
, "ir0");
344 tic4x_print_str (info
, "ir1");
347 tic4x_print_char (info
, *a
);
356 tic4x_print_op (struct disassemble_info
*info
,
357 unsigned long instruction
,
363 const char *parallel
= NULL
;
365 /* Print instruction name. */
367 while (*s
&& parallel
== NULL
)
372 if (! tic4x_print_cond (info
, EXTRU (instruction
, 20, 16)))
376 if (! tic4x_print_cond (info
, EXTRU (instruction
, 27, 23)))
380 parallel
= s
+ 1; /* Skip past `_' in name. */
383 tic4x_print_char (info
, *s
);
389 /* Print arguments. */
392 tic4x_print_char (info
, ' ');
398 case '*': /* Indirect 0--15. */
399 if (! tic4x_print_indirect (info
, INDIRECT_LONG
,
400 EXTRU (instruction
, 15, 0)))
404 case '#': /* Only used for ldp, ldpk. */
405 tic4x_print_immed (info
, IMMED_UINT
, EXTRU (instruction
, 15, 0));
408 case '@': /* Direct 0--15. */
409 tic4x_print_direct (info
, EXTRU (instruction
, 15, 0));
412 case 'A': /* Address register 24--22. */
413 if (! tic4x_print_register (info
, EXTRU (instruction
, 24, 22) +
418 case 'B': /* 24-bit unsigned int immediate br(d)/call/rptb
420 if (IS_CPU_TIC4X (tic4x_version
))
421 tic4x_print_relative (info
, pc
, EXTRS (instruction
, 23, 0),
424 tic4x_print_addr (info
, EXTRU (instruction
, 23, 0));
427 case 'C': /* Indirect (short C4x) 0--7. */
428 if (! IS_CPU_TIC4X (tic4x_version
))
430 if (! tic4x_print_indirect (info
, INDIRECT_TIC4X
,
431 EXTRU (instruction
, 7, 0)))
436 /* Cockup if get here... */
439 case 'E': /* Register 0--7. */
441 if (! tic4x_print_register (info
, EXTRU (instruction
, 7, 0)))
445 case 'F': /* 16-bit float immediate 0--15. */
446 tic4x_print_immed (info
, IMMED_SFLOAT
,
447 EXTRU (instruction
, 15, 0));
450 case 'i': /* Extended indirect 0--7. */
451 if (EXTRU (instruction
, 7, 5) == 7)
453 if (!tic4x_print_register (info
, EXTRU (instruction
, 4, 0)))
459 case 'I': /* Indirect (short) 0--7. */
460 if (! tic4x_print_indirect (info
, INDIRECT_SHORT
,
461 EXTRU (instruction
, 7, 0)))
465 case 'j': /* Extended indirect 8--15 */
466 if (EXTRU (instruction
, 15, 13) == 7)
468 if (! tic4x_print_register (info
, EXTRU (instruction
, 12, 8)))
474 case 'J': /* Indirect (short) 8--15. */
475 if (! tic4x_print_indirect (info
, INDIRECT_SHORT
,
476 EXTRU (instruction
, 15, 8)))
480 case 'G': /* Register 8--15. */
482 if (! tic4x_print_register (info
, EXTRU (instruction
, 15, 8)))
486 case 'H': /* Register 16--18. */
487 if (! tic4x_print_register (info
, EXTRU (instruction
, 18, 16)))
491 case 'K': /* Register 19--21. */
492 if (! tic4x_print_register (info
, EXTRU (instruction
, 21, 19)))
496 case 'L': /* Register 22--24. */
497 if (! tic4x_print_register (info
, EXTRU (instruction
, 24, 22)))
501 case 'M': /* Register 22--22. */
502 tic4x_print_register (info
, EXTRU (instruction
, 22, 22) + REG_R2
);
505 case 'N': /* Register 23--23. */
506 tic4x_print_register (info
, EXTRU (instruction
, 23, 23) + REG_R0
);
509 case 'O': /* Indirect (short C4x) 8--15. */
510 if (! IS_CPU_TIC4X (tic4x_version
))
512 if (! tic4x_print_indirect (info
, INDIRECT_TIC4X
,
513 EXTRU (instruction
, 15, 8)))
517 case 'P': /* Displacement 0--15 (used by Bcond and BcondD). */
518 tic4x_print_relative (info
, pc
, EXTRS (instruction
, 15, 0),
522 case 'Q': /* Register 0--15. */
524 if (! tic4x_print_register (info
, EXTRU (instruction
, 15, 0)))
528 case 'R': /* Register 16--20. */
530 if (! tic4x_print_register (info
, EXTRU (instruction
, 20, 16)))
534 case 'S': /* 16-bit signed immediate 0--15. */
535 tic4x_print_immed (info
, IMMED_SINT
,
536 EXTRS (instruction
, 15, 0));
539 case 'T': /* 5-bit signed immediate 16--20 (C4x stik). */
540 if (! IS_CPU_TIC4X (tic4x_version
))
542 if (! tic4x_print_immed (info
, IMMED_SUINT
,
543 EXTRU (instruction
, 20, 16)))
547 case 'U': /* 16-bit unsigned int immediate 0--15. */
548 tic4x_print_immed (info
, IMMED_SUINT
, EXTRU (instruction
, 15, 0));
551 case 'V': /* 5/9-bit unsigned vector 0--4/8. */
552 tic4x_print_immed (info
, IMMED_SUINT
,
553 IS_CPU_TIC4X (tic4x_version
) ?
554 EXTRU (instruction
, 8, 0) :
555 EXTRU (instruction
, 4, 0) & ~0x20);
558 case 'W': /* 8-bit signed immediate 0--7. */
559 if (! IS_CPU_TIC4X (tic4x_version
))
561 tic4x_print_immed (info
, IMMED_SINT
, EXTRS (instruction
, 7, 0));
564 case 'X': /* Expansion register 4--0. */
565 val
= EXTRU (instruction
, 4, 0) + REG_IVTP
;
566 if (val
< REG_IVTP
|| val
> REG_TVTP
)
568 if (! tic4x_print_register (info
, val
))
572 case 'Y': /* Address register 16--20. */
573 val
= EXTRU (instruction
, 20, 16);
574 if (val
< REG_AR0
|| val
> REG_SP
)
576 if (! tic4x_print_register (info
, val
))
580 case 'Z': /* Expansion register 16--20. */
581 val
= EXTRU (instruction
, 20, 16) + REG_IVTP
;
582 if (val
< REG_IVTP
|| val
> REG_TVTP
)
584 if (! tic4x_print_register (info
, val
))
588 case '|': /* Parallel instruction. */
589 tic4x_print_str (info
, " || ");
590 tic4x_print_str (info
, parallel
);
591 tic4x_print_char (info
, ' ');
595 tic4x_print_char (info
, ',');
599 tic4x_print_char (info
, *s
);
608 tic4x_hash_opcode_special (tic4x_inst_t
**optable_special
,
609 const tic4x_inst_t
*inst
)
613 for (i
= 0;i
< TIC4X_SPESOP_SIZE
; i
++)
614 if (optable_special
[i
] != NULL
615 && optable_special
[i
]->opcode
== inst
->opcode
)
617 /* Collision (we have it already) - overwrite. */
618 optable_special
[i
] = (tic4x_inst_t
*) inst
;
622 for (i
= 0; i
< TIC4X_SPESOP_SIZE
; i
++)
623 if (optable_special
[i
] == NULL
)
625 /* Add the new opcode. */
626 optable_special
[i
] = (tic4x_inst_t
*) inst
;
630 /* This should never occur. This happens if the number of special
631 instructions exceeds TIC4X_SPESOP_SIZE. Please increase the variable
634 printf ("optable_special[] is full, please increase TIC4X_SPESOP_SIZE!\n");
639 tic4x_hash_opcode (tic4x_inst_t
**optable
,
640 tic4x_inst_t
**optable_special
,
641 const tic4x_inst_t
*inst
,
642 const unsigned long tic4x_oplevel
)
645 unsigned int opcode
= inst
->opcode
>> (32 - TIC4X_HASH_SIZE
);
646 unsigned int opmask
= inst
->opmask
>> (32 - TIC4X_HASH_SIZE
);
648 /* Use a TIC4X_HASH_SIZE bit index as a hash index. We should
649 have unique entries so there's no point having a linked list
651 for (j
= opcode
; j
< opmask
; j
++)
652 if ((j
& opmask
) == opcode
653 && inst
->oplevel
& tic4x_oplevel
)
656 /* We should only have collisions for synonyms like
658 if (optable
[j
] != NULL
)
659 printf ("Collision at index %d, %s and %s\n",
660 j
, optable
[j
]->name
, inst
->name
);
662 /* Catch those ops that collide with others already inside the
663 hash, and have a opmask greater than the one we use in the
664 hash. Store them in a special-list, that will handle full
665 32-bit INSN, not only the first 11-bit (or so). */
666 if (optable
[j
] != NULL
667 && inst
->opmask
& ~(opmask
<< (32 - TIC4X_HASH_SIZE
)))
669 /* Add the instruction already on the list. */
670 tic4x_hash_opcode_special (optable_special
, optable
[j
]);
672 /* Add the new instruction. */
673 tic4x_hash_opcode_special (optable_special
, inst
);
676 optable
[j
] = (tic4x_inst_t
*) inst
;
680 /* Disassemble the instruction in 'instruction'.
681 'pc' should be the address of this instruction, it will
682 be used to print the target address if this is a relative jump or call
683 the disassembled instruction is written to 'info'.
684 The function returns the length of this instruction in words. */
687 tic4x_disassemble (unsigned long pc
,
688 unsigned long instruction
,
689 struct disassemble_info
*info
)
691 static tic4x_inst_t
**optable
= NULL
;
692 static tic4x_inst_t
**optable_special
= NULL
;
695 unsigned long tic4x_oplevel
;
697 tic4x_version
= info
->mach
;
699 tic4x_oplevel
= (IS_CPU_TIC4X (tic4x_version
)) ? OP_C4X
: 0;
700 tic4x_oplevel
|= OP_C3X
| OP_LPWR
| OP_IDLE2
| OP_ENH
;
704 optable
= xcalloc (sizeof (tic4x_inst_t
*), (1 << TIC4X_HASH_SIZE
));
706 optable_special
= xcalloc (sizeof (tic4x_inst_t
*), TIC4X_SPESOP_SIZE
);
708 /* Install opcodes in reverse order so that preferred
709 forms overwrite synonyms. */
710 for (i
= tic4x_num_insts
- 1; i
>= 0; i
--)
711 tic4x_hash_opcode (optable
, optable_special
, &tic4x_insts
[i
],
714 /* We now need to remove the insn that are special from the
715 "normal" optable, to make the disasm search this extra list
717 for (i
= 0; i
< TIC4X_SPESOP_SIZE
; i
++)
718 if (optable_special
[i
] != NULL
)
719 optable
[optable_special
[i
]->opcode
>> (32 - TIC4X_HASH_SIZE
)] = NULL
;
722 /* See if we can pick up any loading of the DP register... */
723 if ((instruction
>> 16) == 0x5070 || (instruction
>> 16) == 0x1f70)
724 tic4x_dp
= EXTRU (instruction
, 15, 0);
726 p
= optable
[instruction
>> (32 - TIC4X_HASH_SIZE
)];
729 if (((instruction
& p
->opmask
) == p
->opcode
)
730 && tic4x_print_op (NULL
, instruction
, p
, pc
))
731 tic4x_print_op (info
, instruction
, p
, pc
);
733 (*info
->fprintf_func
) (info
->stream
, "%08lx", instruction
);
737 for (i
= 0; i
<TIC4X_SPESOP_SIZE
; i
++)
738 if (optable_special
[i
] != NULL
739 && optable_special
[i
]->opcode
== instruction
)
741 (*info
->fprintf_func
)(info
->stream
, "%s", optable_special
[i
]->name
);
744 if (i
== TIC4X_SPESOP_SIZE
)
745 (*info
->fprintf_func
) (info
->stream
, "%08lx", instruction
);
748 /* Return size of insn in words. */
752 /* The entry point from objdump and gdb. */
754 print_insn_tic4x (bfd_vma memaddr
, struct disassemble_info
*info
)
761 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 4, info
);
764 (*info
->memory_error_func
) (status
, memaddr
, info
);
769 op
= bfd_getl32 (buffer
);
770 info
->bytes_per_line
= 4;
771 info
->bytes_per_chunk
= 4;
772 info
->octets_per_byte
= 4;
773 info
->display_endian
= BFD_ENDIAN_LITTLE
;
774 return tic4x_disassemble (pc
, op
, info
) * 4;
This page took 0.046405 seconds and 4 git commands to generate.