1 /* Disassembly routines for TMS320C30 architecture
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
3 Contributed by Steven Haworth (steve@pm.cse.rmit.edu.au)
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. */
25 #include "disassemble.h"
26 #include "opcode/tic30.h"
29 #define PARALLEL_INSN 2
31 /* Gets the type of instruction based on the top 2 or 3 bits of the
33 #define GET_TYPE(insn) (insn & 0x80000000 ? insn & 0xC0000000 : insn & 0xE0000000)
35 /* Instruction types. */
36 #define TWO_OPERAND_1 0x00000000
37 #define TWO_OPERAND_2 0x40000000
38 #define THREE_OPERAND 0x20000000
39 #define PAR_STORE 0xC0000000
40 #define MUL_ADDS 0x80000000
41 #define BRANCHES 0x60000000
43 /* Specific instruction id bits. */
44 #define NORMAL_IDEN 0x1F800000
45 #define PAR_STORE_IDEN 0x3E000000
46 #define MUL_ADD_IDEN 0x2C000000
47 #define BR_IMM_IDEN 0x1F000000
48 #define BR_COND_IDEN 0x1C3F0000
50 /* Addressing modes. */
51 #define AM_REGISTER 0x00000000
52 #define AM_DIRECT 0x00200000
53 #define AM_INDIRECT 0x00400000
54 #define AM_IMM 0x00600000
56 #define P_FIELD 0x03000000
59 #define LDP_INSN 0x08700000
61 /* TMS320C30 program counter for current instruction. */
62 static unsigned int _pc
;
72 get_tic30_instruction (unsigned long insn_word
, struct instruction
*insn
)
74 switch (GET_TYPE (insn_word
))
79 insn
->type
= NORMAL_INSN
;
81 insn_template
*current_optab
= (insn_template
*) tic30_optab
;
83 for (; current_optab
< tic30_optab_end
; current_optab
++)
85 if (GET_TYPE (current_optab
->base_opcode
) == GET_TYPE (insn_word
))
87 if (current_optab
->operands
== 0)
89 if (current_optab
->base_opcode
== insn_word
)
91 insn
->tm
= current_optab
;
95 else if ((current_optab
->base_opcode
& NORMAL_IDEN
) == (insn_word
& NORMAL_IDEN
))
97 insn
->tm
= current_optab
;
106 insn
->type
= PARALLEL_INSN
;
108 partemplate
*current_optab
= (partemplate
*) tic30_paroptab
;
110 for (; current_optab
< tic30_paroptab_end
; current_optab
++)
112 if (GET_TYPE (current_optab
->base_opcode
) == GET_TYPE (insn_word
))
114 if ((current_optab
->base_opcode
& PAR_STORE_IDEN
)
115 == (insn_word
& PAR_STORE_IDEN
))
117 insn
->ptm
= current_optab
;
126 insn
->type
= PARALLEL_INSN
;
128 partemplate
*current_optab
= (partemplate
*) tic30_paroptab
;
130 for (; current_optab
< tic30_paroptab_end
; current_optab
++)
132 if (GET_TYPE (current_optab
->base_opcode
) == GET_TYPE (insn_word
))
134 if ((current_optab
->base_opcode
& MUL_ADD_IDEN
)
135 == (insn_word
& MUL_ADD_IDEN
))
137 insn
->ptm
= current_optab
;
146 insn
->type
= NORMAL_INSN
;
148 insn_template
*current_optab
= (insn_template
*) tic30_optab
;
150 for (; current_optab
< tic30_optab_end
; current_optab
++)
152 if (GET_TYPE (current_optab
->base_opcode
) == GET_TYPE (insn_word
))
154 if (current_optab
->operand_types
[0] & Imm24
)
156 if ((current_optab
->base_opcode
& BR_IMM_IDEN
)
157 == (insn_word
& BR_IMM_IDEN
))
159 insn
->tm
= current_optab
;
163 else if (current_optab
->operands
> 0)
165 if ((current_optab
->base_opcode
& BR_COND_IDEN
)
166 == (insn_word
& BR_COND_IDEN
))
168 insn
->tm
= current_optab
;
174 if ((current_optab
->base_opcode
& (BR_COND_IDEN
| 0x00800000))
175 == (insn_word
& (BR_COND_IDEN
| 0x00800000)))
177 insn
->tm
= current_optab
;
191 #define OPERAND_BUFFER_LEN 15
194 get_register_operand (unsigned char fragment
, char *buffer
)
196 const reg
*current_reg
= tic30_regtab
;
200 for (; current_reg
< tic30_regtab_end
; current_reg
++)
202 if ((fragment
& 0x1F) == current_reg
->opcode
)
204 strncpy (buffer
, current_reg
->name
, OPERAND_BUFFER_LEN
- 1);
205 buffer
[OPERAND_BUFFER_LEN
- 1] = 0;
213 get_indirect_operand (unsigned short fragment
,
223 /* Determine which bits identify the sections of the indirect
224 operand based on the size in bytes. */
228 mod
= (fragment
& 0x00F8) >> 3;
229 arnum
= (fragment
& 0x0007);
233 mod
= (fragment
& 0xF800) >> 11;
234 arnum
= (fragment
& 0x0700) >> 8;
235 disp
= (fragment
& 0x00FF);
241 const ind_addr_type
*current_ind
= tic30_indaddr_tab
;
243 for (; current_ind
< tic30_indaddrtab_end
; current_ind
++)
245 if (current_ind
->modfield
== mod
)
247 if (current_ind
->displacement
== IMPLIED_DISP
&& size
== 2)
255 len
= strlen (current_ind
->syntax
);
257 for (i
= 0, bufcnt
= 0; i
< len
; i
++, bufcnt
++)
259 buffer
[bufcnt
] = current_ind
->syntax
[i
];
262 && bufcnt
< OPERAND_BUFFER_LEN
- 1
263 && buffer
[bufcnt
- 1] == 'a'
264 && buffer
[bufcnt
] == 'r')
265 buffer
[++bufcnt
] = arnum
+ '0';
267 if (bufcnt
< OPERAND_BUFFER_LEN
- 1
268 && buffer
[bufcnt
] == '('
269 && current_ind
->displacement
== DISP_REQUIRED
)
271 snprintf (buffer
+ (bufcnt
+ 1),
272 OPERAND_BUFFER_LEN
- (bufcnt
+ 1),
274 bufcnt
+= strlen (buffer
+ (bufcnt
+ 1));
277 buffer
[bufcnt
+ 1] = '\0';
287 cnvt_tmsfloat_ieee (unsigned long tmsfloat
, int size
, float *ieeefloat
)
289 unsigned long exponent
, sign
, mant
;
298 if ((tmsfloat
& 0x0000F000) == 0x00008000)
299 tmsfloat
= 0x80000000;
303 tmsfloat
= (long) tmsfloat
>> 4;
306 exponent
= tmsfloat
& 0xFF000000;
307 if (exponent
== 0x80000000)
312 exponent
+= 0x7F000000;
313 sign
= (tmsfloat
& 0x00800000) << 8;
314 mant
= tmsfloat
& 0x007FFFFF;
315 if (exponent
== 0xFF000000)
321 *ieeefloat
= HUGE_VALF
;
323 *ieeefloat
= -HUGE_VALF
;
326 *ieeefloat
= 1.0 / 0.0;
328 *ieeefloat
= -1.0 / 0.0;
335 mant
= (~mant
) & 0x007FFFFF;
337 exponent
+= mant
& 0x00800000;
338 exponent
&= 0x7F800000;
341 if (tmsfloat
== 0x80000000)
342 sign
= mant
= exponent
= 0;
343 tmsfloat
= sign
| exponent
| mant
;
350 print_two_operand (disassemble_info
*info
,
351 unsigned long insn_word
,
352 struct instruction
*insn
)
355 char operand
[2][OPERAND_BUFFER_LEN
] =
362 if (insn
->tm
== NULL
)
364 strcpy (name
, insn
->tm
->name
);
365 if (insn
->tm
->opcode_modifier
== AddressMode
)
368 /* Determine whether instruction is a store or a normal instruction. */
369 if ((insn
->tm
->operand_types
[1] & (Direct
| Indirect
))
370 == (Direct
| Indirect
))
380 /* Get the destination register. */
381 if (insn
->tm
->operands
== 2)
382 get_register_operand ((insn_word
& 0x001F0000) >> 16, operand
[dest_op
]);
383 /* Get the source operand based on addressing mode. */
384 switch (insn_word
& AddressMode
)
387 /* Check for the NOP instruction before getting the operand. */
388 if ((insn
->tm
->operand_types
[0] & NotReq
) == 0)
389 get_register_operand ((insn_word
& 0x0000001F), operand
[src_op
]);
392 sprintf (operand
[src_op
], "@0x%lX", (insn_word
& 0x0000FFFF));
395 get_indirect_operand ((insn_word
& 0x0000FFFF), 2, operand
[src_op
]);
398 /* Get the value of the immediate operand based on variable type. */
399 switch (insn
->tm
->imm_arg_type
)
402 cnvt_tmsfloat_ieee ((insn_word
& 0x0000FFFF), 2, &f_number
);
403 sprintf (operand
[src_op
], "%2.2f", f_number
);
406 sprintf (operand
[src_op
], "%d", (short) (insn_word
& 0x0000FFFF));
409 sprintf (operand
[src_op
], "%lu", (insn_word
& 0x0000FFFF));
414 /* Handle special case for LDP instruction. */
415 if ((insn_word
& 0xFFFFFF00) == LDP_INSN
)
417 strcpy (name
, "ldp");
418 sprintf (operand
[0], "0x%06lX", (insn_word
& 0x000000FF) << 16);
419 operand
[1][0] = '\0';
423 /* Handle case for stack and rotate instructions. */
424 else if (insn
->tm
->operands
== 1)
426 if (insn
->tm
->opcode_modifier
== StackOp
)
427 get_register_operand ((insn_word
& 0x001F0000) >> 16, operand
[0]);
429 /* Output instruction to stream. */
430 info
->fprintf_func (info
->stream
, " %s %s%c%s", name
,
431 operand
[0][0] ? operand
[0] : "",
432 operand
[1][0] ? ',' : ' ',
433 operand
[1][0] ? operand
[1] : "");
438 print_three_operand (disassemble_info
*info
,
439 unsigned long insn_word
,
440 struct instruction
*insn
)
442 char operand
[3][OPERAND_BUFFER_LEN
] =
449 if (insn
->tm
== NULL
)
451 switch (insn_word
& AddressMode
)
454 get_register_operand ((insn_word
& 0x000000FF), operand
[0]);
455 get_register_operand ((insn_word
& 0x0000FF00) >> 8, operand
[1]);
458 get_register_operand ((insn_word
& 0x000000FF), operand
[0]);
459 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1]);
462 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0]);
463 get_register_operand ((insn_word
& 0x0000FF00) >> 8, operand
[1]);
466 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0]);
467 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1]);
472 if (insn
->tm
->operands
== 3)
473 get_register_operand ((insn_word
& 0x001F0000) >> 16, operand
[2]);
474 info
->fprintf_func (info
->stream
, " %s %s,%s%c%s", insn
->tm
->name
,
475 operand
[0], operand
[1],
476 operand
[2][0] ? ',' : ' ',
477 operand
[2][0] ? operand
[2] : "");
482 print_par_insn (disassemble_info
*info
,
483 unsigned long insn_word
,
484 struct instruction
*insn
)
488 char operand
[2][3][OPERAND_BUFFER_LEN
] =
502 if (insn
->ptm
== NULL
)
504 /* Parse out the names of each of the parallel instructions from the
505 q_insn1_insn2 format. */
506 name1
= (char *) strdup (insn
->ptm
->name
+ 2);
508 len
= strlen (name1
);
509 for (i
= 0; i
< len
; i
++)
513 name2
= &name1
[i
+ 1];
518 /* Get the operands of the instruction based on the operand order. */
519 switch (insn
->ptm
->oporder
)
522 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][0]);
523 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][1]);
524 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
525 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][1]);
528 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][0]);
529 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][0]);
530 get_register_operand ((insn_word
>> 19) & 0x07, operand
[1][1]);
531 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][1]);
534 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][1]);
535 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][1]);
536 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
537 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][0]);
540 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][0]);
541 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][1]);
542 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
543 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][1]);
544 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][2]);
547 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][1]);
548 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][1]);
549 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
550 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][0]);
551 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][2]);
554 if (insn_word
& 0x00800000)
555 get_register_operand (0x01, operand
[0][2]);
557 get_register_operand (0x00, operand
[0][2]);
558 if (insn_word
& 0x00400000)
559 get_register_operand (0x03, operand
[1][2]);
561 get_register_operand (0x02, operand
[1][2]);
562 switch (insn_word
& P_FIELD
)
565 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][1]);
566 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[0][0]);
567 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][1]);
568 get_register_operand ((insn_word
>> 19) & 0x07, operand
[1][0]);
571 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[1][0]);
572 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[0][0]);
573 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][1]);
574 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][1]);
577 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[1][1]);
578 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][0]);
579 get_register_operand ((insn_word
>> 16) & 0x07, operand
[0][1]);
580 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][0]);
583 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[1][1]);
584 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[0][0]);
585 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
586 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][1]);
593 info
->fprintf_func (info
->stream
, " %s %s,%s%c%s", name1
,
594 operand
[0][0], operand
[0][1],
595 operand
[0][2][0] ? ',' : ' ',
596 operand
[0][2][0] ? operand
[0][2] : "");
597 info
->fprintf_func (info
->stream
, "\n\t\t\t|| %s %s,%s%c%s", name2
,
598 operand
[1][0], operand
[1][1],
599 operand
[1][2][0] ? ',' : ' ',
600 operand
[1][2][0] ? operand
[1][2] : "");
606 print_branch (disassemble_info
*info
,
607 unsigned long insn_word
,
608 struct instruction
*insn
)
610 char operand
[2][OPERAND_BUFFER_LEN
] =
615 unsigned long address
;
618 if (insn
->tm
== NULL
)
620 /* Get the operands for 24-bit immediate jumps. */
621 if (insn
->tm
->operand_types
[0] & Imm24
)
623 address
= insn_word
& 0x00FFFFFF;
624 sprintf (operand
[0], "0x%lX", address
);
627 /* Get the operand for the trap instruction. */
628 else if (insn
->tm
->operand_types
[0] & IVector
)
630 address
= insn_word
& 0x0000001F;
631 sprintf (operand
[0], "0x%lX", address
);
635 address
= insn_word
& 0x0000FFFF;
636 /* Get the operands for the DB instructions. */
637 if (insn
->tm
->operands
== 2)
639 get_register_operand (((insn_word
& 0x01C00000) >> 22) + REG_AR0
, operand
[0]);
640 if (insn_word
& PCRel
)
642 sprintf (operand
[1], "%d", (short) address
);
646 get_register_operand (insn_word
& 0x0000001F, operand
[1]);
648 /* Get the operands for the standard branches. */
649 else if (insn
->tm
->operands
== 1)
651 if (insn_word
& PCRel
)
653 address
= (short) address
;
654 sprintf (operand
[0], "%ld", address
);
658 get_register_operand (insn_word
& 0x0000001F, operand
[0]);
661 info
->fprintf_func (info
->stream
, " %s %s%c%s", insn
->tm
->name
,
662 operand
[0][0] ? operand
[0] : "",
663 operand
[1][0] ? ',' : ' ',
664 operand
[1][0] ? operand
[1] : "");
665 /* Print destination of branch in relation to current symbol. */
666 if (print_label
&& info
->symbols
)
668 asymbol
*sym
= *info
->symbols
;
670 if ((insn
->tm
->opcode_modifier
== PCRel
) && (insn_word
& PCRel
))
672 address
= (_pc
+ 1 + (short) address
) - ((sym
->section
->vma
+ sym
->value
) / 4);
673 /* Check for delayed instruction, if so adjust destination. */
674 if (insn_word
& 0x00200000)
679 address
-= ((sym
->section
->vma
+ sym
->value
) / 4);
682 info
->fprintf_func (info
->stream
, " <%s>", sym
->name
);
684 info
->fprintf_func (info
->stream
, " <%s %c %lu>", sym
->name
,
685 ((short) address
< 0) ? '-' : '+',
692 print_insn_tic30 (bfd_vma pc
, disassemble_info
*info
)
694 unsigned long insn_word
;
695 struct instruction insn
= { 0, NULL
, NULL
};
696 bfd_vma bufaddr
= pc
- info
->buffer_vma
;
698 if (bufaddr
+ 3 >= info
->buffer_length
)
701 /* Obtain the current instruction word from the buffer. */
702 insn_word
= (((unsigned) *(info
->buffer
+ bufaddr
) << 24)
703 | (*(info
->buffer
+ bufaddr
+ 1) << 16)
704 | (*(info
->buffer
+ bufaddr
+ 2) << 8)
705 | *(info
->buffer
+ bufaddr
+ 3));
707 /* Get the instruction referred to by the current instruction word
708 and print it out based on its type. */
709 if (!get_tic30_instruction (insn_word
, &insn
))
711 switch (GET_TYPE (insn_word
))
715 if (!print_two_operand (info
, insn_word
, &insn
))
719 if (!print_three_operand (info
, insn_word
, &insn
))
724 if (!print_par_insn (info
, insn_word
, &insn
))
728 if (!print_branch (info
, insn_word
, &insn
))