1 /* Disassembly routines for TMS320C30 architecture
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Steven Haworth (steve@pm.cse.rmit.edu.au)
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #include "opcode/tic30.h"
27 #define PARALLEL_INSN 2
29 /* Gets the type of instruction based on the top 2 or 3 bits of the
31 #define GET_TYPE(insn) (insn & 0x80000000 ? insn & 0xC0000000 : insn & 0xE0000000)
33 /* Instruction types. */
34 #define TWO_OPERAND_1 0x00000000
35 #define TWO_OPERAND_2 0x40000000
36 #define THREE_OPERAND 0x20000000
37 #define PAR_STORE 0xC0000000
38 #define MUL_ADDS 0x80000000
39 #define BRANCHES 0x60000000
41 /* Specific instruction id bits. */
42 #define NORMAL_IDEN 0x1F800000
43 #define PAR_STORE_IDEN 0x3E000000
44 #define MUL_ADD_IDEN 0x2C000000
45 #define BR_IMM_IDEN 0x1F000000
46 #define BR_COND_IDEN 0x1C3F0000
48 /* Addressing modes. */
49 #define AM_REGISTER 0x00000000
50 #define AM_DIRECT 0x00200000
51 #define AM_INDIRECT 0x00400000
52 #define AM_IMM 0x00600000
54 #define P_FIELD 0x03000000
57 #define LDP_INSN 0x08700000
59 /* TMS320C30 program counter for current instruction. */
60 static unsigned int _pc
;
69 int get_tic30_instruction
PARAMS ((unsigned long, struct instruction
*));
71 PARAMS ((disassemble_info
*, unsigned long, struct instruction
*));
72 int print_three_operand
73 PARAMS ((disassemble_info
*, unsigned long, struct instruction
*));
75 PARAMS ((disassemble_info
*, unsigned long, struct instruction
*));
77 PARAMS ((disassemble_info
*, unsigned long, struct instruction
*));
78 int get_indirect_operand
PARAMS ((unsigned short, int, char *));
79 int get_register_operand
PARAMS ((unsigned char, char *));
80 int cnvt_tmsfloat_ieee
PARAMS ((unsigned long, int, float *));
83 print_insn_tic30 (pc
, info
)
85 disassemble_info
*info
;
87 unsigned long insn_word
;
88 struct instruction insn
=
90 bfd_vma bufaddr
= pc
- info
->buffer_vma
;
91 /* Obtain the current instruction word from the buffer. */
92 insn_word
= (*(info
->buffer
+ bufaddr
) << 24) | (*(info
->buffer
+ bufaddr
+ 1) << 16) |
93 (*(info
->buffer
+ bufaddr
+ 2) << 8) | *(info
->buffer
+ bufaddr
+ 3);
95 /* Get the instruction refered to by the current instruction word
96 and print it out based on its type. */
97 if (!get_tic30_instruction (insn_word
, &insn
))
99 switch (GET_TYPE (insn_word
))
103 if (!print_two_operand (info
, insn_word
, &insn
))
107 if (!print_three_operand (info
, insn_word
, &insn
))
112 if (!print_par_insn (info
, insn_word
, &insn
))
116 if (!print_branch (info
, insn_word
, &insn
))
124 get_tic30_instruction (insn_word
, insn
)
125 unsigned long insn_word
;
126 struct instruction
*insn
;
128 switch (GET_TYPE (insn_word
))
133 insn
->type
= NORMAL_INSN
;
135 template *current_optab
= (template *) tic30_optab
;
136 for (; current_optab
< tic30_optab_end
; current_optab
++)
138 if (GET_TYPE (current_optab
->base_opcode
) == GET_TYPE (insn_word
))
140 if (current_optab
->operands
== 0)
142 if (current_optab
->base_opcode
== insn_word
)
144 insn
->tm
= current_optab
;
148 else if ((current_optab
->base_opcode
& NORMAL_IDEN
) == (insn_word
& NORMAL_IDEN
))
150 insn
->tm
= current_optab
;
158 insn
->type
= PARALLEL_INSN
;
160 partemplate
*current_optab
= (partemplate
*) tic30_paroptab
;
161 for (; current_optab
< tic30_paroptab_end
; current_optab
++)
163 if (GET_TYPE (current_optab
->base_opcode
) == GET_TYPE (insn_word
))
165 if ((current_optab
->base_opcode
& PAR_STORE_IDEN
) == (insn_word
& PAR_STORE_IDEN
))
167 insn
->ptm
= current_optab
;
175 insn
->type
= PARALLEL_INSN
;
177 partemplate
*current_optab
= (partemplate
*) tic30_paroptab
;
178 for (; current_optab
< tic30_paroptab_end
; current_optab
++)
180 if (GET_TYPE (current_optab
->base_opcode
) == GET_TYPE (insn_word
))
182 if ((current_optab
->base_opcode
& MUL_ADD_IDEN
) == (insn_word
& MUL_ADD_IDEN
))
184 insn
->ptm
= current_optab
;
192 insn
->type
= NORMAL_INSN
;
194 template *current_optab
= (template *) tic30_optab
;
195 for (; current_optab
< tic30_optab_end
; current_optab
++)
197 if (GET_TYPE (current_optab
->base_opcode
) == GET_TYPE (insn_word
))
199 if (current_optab
->operand_types
[0] & Imm24
)
201 if ((current_optab
->base_opcode
& BR_IMM_IDEN
) == (insn_word
& BR_IMM_IDEN
))
203 insn
->tm
= current_optab
;
207 else if (current_optab
->operands
> 0)
209 if ((current_optab
->base_opcode
& BR_COND_IDEN
) == (insn_word
& BR_COND_IDEN
))
211 insn
->tm
= current_optab
;
217 if ((current_optab
->base_opcode
& (BR_COND_IDEN
| 0x00800000)) == (insn_word
& (BR_COND_IDEN
| 0x00800000)))
219 insn
->tm
= current_optab
;
234 print_two_operand (info
, insn_word
, insn
)
235 disassemble_info
*info
;
236 unsigned long insn_word
;
237 struct instruction
*insn
;
240 char operand
[2][13] =
246 if (insn
->tm
== NULL
)
248 strcpy (name
, insn
->tm
->name
);
249 if (insn
->tm
->opcode_modifier
== AddressMode
)
252 /* Determine whether instruction is a store or a normal instruction. */
253 if ((insn
->tm
->operand_types
[1] & (Direct
| Indirect
)) == (Direct
| Indirect
))
263 /* Get the destination register. */
264 if (insn
->tm
->operands
== 2)
265 get_register_operand ((insn_word
& 0x001F0000) >> 16, operand
[dest_op
]);
266 /* Get the source operand based on addressing mode. */
267 switch (insn_word
& AddressMode
)
270 /* Check for the NOP instruction before getting the operand. */
271 if ((insn
->tm
->operand_types
[0] & NotReq
) == 0)
272 get_register_operand ((insn_word
& 0x0000001F), operand
[src_op
]);
275 sprintf (operand
[src_op
], "@0x%lX", (insn_word
& 0x0000FFFF));
278 get_indirect_operand ((insn_word
& 0x0000FFFF), 2, operand
[src_op
]);
281 /* Get the value of the immediate operand based on variable type. */
282 switch (insn
->tm
->imm_arg_type
)
285 cnvt_tmsfloat_ieee ((insn_word
& 0x0000FFFF), 2, &f_number
);
286 sprintf (operand
[src_op
], "%2.2f", f_number
);
289 sprintf (operand
[src_op
], "%d", (short) (insn_word
& 0x0000FFFF));
292 sprintf (operand
[src_op
], "%lu", (insn_word
& 0x0000FFFF));
297 /* Handle special case for LDP instruction. */
298 if ((insn_word
& 0xFFFFFF00) == LDP_INSN
)
300 strcpy (name
, "ldp");
301 sprintf (operand
[0], "0x%06lX", (insn_word
& 0x000000FF) << 16);
302 operand
[1][0] = '\0';
306 /* Handle case for stack and rotate instructions. */
307 else if (insn
->tm
->operands
== 1)
309 if (insn
->tm
->opcode_modifier
== StackOp
)
311 get_register_operand ((insn_word
& 0x001F0000) >> 16, operand
[0]);
314 /* Output instruction to stream. */
315 info
->fprintf_func (info
->stream
, " %s %s%c%s", name
,
316 operand
[0][0] ? operand
[0] : "",
317 operand
[1][0] ? ',' : ' ',
318 operand
[1][0] ? operand
[1] : "");
323 print_three_operand (info
, insn_word
, insn
)
324 disassemble_info
*info
;
325 unsigned long insn_word
;
326 struct instruction
*insn
;
328 char operand
[3][13] =
334 if (insn
->tm
== NULL
)
336 switch (insn_word
& AddressMode
)
339 get_register_operand ((insn_word
& 0x000000FF), operand
[0]);
340 get_register_operand ((insn_word
& 0x0000FF00) >> 8, operand
[1]);
343 get_register_operand ((insn_word
& 0x000000FF), operand
[0]);
344 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1]);
347 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0]);
348 get_register_operand ((insn_word
& 0x0000FF00) >> 8, operand
[1]);
351 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0]);
352 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1]);
357 if (insn
->tm
->operands
== 3)
358 get_register_operand ((insn_word
& 0x001F0000) >> 16, operand
[2]);
359 info
->fprintf_func (info
->stream
, " %s %s,%s%c%s", insn
->tm
->name
,
360 operand
[0], operand
[1],
361 operand
[2][0] ? ',' : ' ',
362 operand
[2][0] ? operand
[2] : "");
367 print_par_insn (info
, insn_word
, insn
)
368 disassemble_info
*info
;
369 unsigned long insn_word
;
370 struct instruction
*insn
;
374 char operand
[2][3][13] =
385 if (insn
->ptm
== NULL
)
387 /* Parse out the names of each of the parallel instructions from the
388 q_insn1_insn2 format. */
389 name1
= (char *) strdup (insn
->ptm
->name
+ 2);
391 len
= strlen (name1
);
392 for (i
= 0; i
< len
; i
++)
396 name2
= &name1
[i
+ 1];
401 /* Get the operands of the instruction based on the operand order. */
402 switch (insn
->ptm
->oporder
)
405 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][0]);
406 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][1]);
407 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
408 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][1]);
411 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][0]);
412 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][0]);
413 get_register_operand ((insn_word
>> 19) & 0x07, operand
[1][1]);
414 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][1]);
417 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][1]);
418 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][1]);
419 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
420 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][0]);
423 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][0]);
424 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][1]);
425 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
426 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][1]);
427 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][2]);
430 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][1]);
431 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][1]);
432 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
433 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][0]);
434 get_register_operand ((insn_word
>> 22) & 0x07, operand
[0][2]);
437 if (insn_word
& 0x00800000)
438 get_register_operand (0x01, operand
[0][2]);
440 get_register_operand (0x00, operand
[0][2]);
441 if (insn_word
& 0x00400000)
442 get_register_operand (0x03, operand
[1][2]);
444 get_register_operand (0x02, operand
[1][2]);
445 switch (insn_word
& P_FIELD
)
448 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[0][1]);
449 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[0][0]);
450 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][1]);
451 get_register_operand ((insn_word
>> 19) & 0x07, operand
[1][0]);
454 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[1][0]);
455 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[0][0]);
456 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][1]);
457 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][1]);
460 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[1][1]);
461 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[1][0]);
462 get_register_operand ((insn_word
>> 16) & 0x07, operand
[0][1]);
463 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][0]);
466 get_indirect_operand ((insn_word
& 0x000000FF), 1, operand
[1][1]);
467 get_indirect_operand ((insn_word
& 0x0000FF00) >> 8, 1, operand
[0][0]);
468 get_register_operand ((insn_word
>> 16) & 0x07, operand
[1][0]);
469 get_register_operand ((insn_word
>> 19) & 0x07, operand
[0][1]);
476 info
->fprintf_func (info
->stream
, " %s %s,%s%c%s", name1
,
477 operand
[0][0], operand
[0][1],
478 operand
[0][2][0] ? ',' : ' ',
479 operand
[0][2][0] ? operand
[0][2] : "");
480 info
->fprintf_func (info
->stream
, "\n\t\t\t|| %s %s,%s%c%s", name2
,
481 operand
[1][0], operand
[1][1],
482 operand
[1][2][0] ? ',' : ' ',
483 operand
[1][2][0] ? operand
[1][2] : "");
489 print_branch (info
, insn_word
, insn
)
490 disassemble_info
*info
;
491 unsigned long insn_word
;
492 struct instruction
*insn
;
494 char operand
[2][13] =
498 unsigned long address
;
501 if (insn
->tm
== NULL
)
503 /* Get the operands for 24-bit immediate jumps. */
504 if (insn
->tm
->operand_types
[0] & Imm24
)
506 address
= insn_word
& 0x00FFFFFF;
507 sprintf (operand
[0], "0x%lX", address
);
510 /* Get the operand for the trap instruction. */
511 else if (insn
->tm
->operand_types
[0] & IVector
)
513 address
= insn_word
& 0x0000001F;
514 sprintf (operand
[0], "0x%lX", address
);
518 address
= insn_word
& 0x0000FFFF;
519 /* Get the operands for the DB instructions. */
520 if (insn
->tm
->operands
== 2)
522 get_register_operand (((insn_word
& 0x01C00000) >> 22) + REG_AR0
, operand
[0]);
523 if (insn_word
& PCRel
)
525 sprintf (operand
[1], "%d", (short) address
);
529 get_register_operand (insn_word
& 0x0000001F, operand
[1]);
531 /* Get the operands for the standard branches. */
532 else if (insn
->tm
->operands
== 1)
534 if (insn_word
& PCRel
)
536 address
= (short) address
;
537 sprintf (operand
[0], "%ld", address
);
541 get_register_operand (insn_word
& 0x0000001F, operand
[0]);
544 info
->fprintf_func (info
->stream
, " %s %s%c%s", insn
->tm
->name
,
545 operand
[0][0] ? operand
[0] : "",
546 operand
[1][0] ? ',' : ' ',
547 operand
[1][0] ? operand
[1] : "");
548 /* Print destination of branch in relation to current symbol. */
549 if (print_label
&& info
->symbols
)
551 asymbol
*sym
= *info
->symbols
;
553 if ((insn
->tm
->opcode_modifier
== PCRel
) && (insn_word
& PCRel
))
555 address
= (_pc
+ 1 + (short) address
) - ((sym
->section
->vma
+ sym
->value
) / 4);
556 /* Check for delayed instruction, if so adjust destination. */
557 if (insn_word
& 0x00200000)
562 address
-= ((sym
->section
->vma
+ sym
->value
) / 4);
565 info
->fprintf_func (info
->stream
, " <%s>", sym
->name
);
567 info
->fprintf_func (info
->stream
, " <%s %c %d>", sym
->name
,
568 ((short) address
< 0) ? '-' : '+',
575 get_indirect_operand (fragment
, size
, buffer
)
576 unsigned short fragment
;
586 /* Determine which bits identify the sections of the indirect operand based on the
591 mod
= (fragment
& 0x00F8) >> 3;
592 arnum
= (fragment
& 0x0007);
596 mod
= (fragment
& 0xF800) >> 11;
597 arnum
= (fragment
& 0x0700) >> 8;
598 disp
= (fragment
& 0x00FF);
604 const ind_addr_type
*current_ind
= tic30_indaddr_tab
;
605 for (; current_ind
< tic30_indaddrtab_end
; current_ind
++)
607 if (current_ind
->modfield
== mod
)
609 if (current_ind
->displacement
== IMPLIED_DISP
&& size
== 2)
618 len
= strlen (current_ind
->syntax
);
619 for (i
= 0, bufcnt
= 0; i
< len
; i
++, bufcnt
++)
621 buffer
[bufcnt
] = current_ind
->syntax
[i
];
622 if (buffer
[bufcnt
- 1] == 'a' && buffer
[bufcnt
] == 'r')
623 buffer
[++bufcnt
] = arnum
+ '0';
624 if (buffer
[bufcnt
] == '(' && current_ind
->displacement
== DISP_REQUIRED
)
626 sprintf (&buffer
[bufcnt
+ 1], "%u", disp
);
627 bufcnt
+= strlen (&buffer
[bufcnt
+ 1]);
630 buffer
[bufcnt
+ 1] = '\0';
640 get_register_operand (fragment
, buffer
)
641 unsigned char fragment
;
644 const reg
*current_reg
= tic30_regtab
;
648 for (; current_reg
< tic30_regtab_end
; current_reg
++)
650 if ((fragment
& 0x1F) == current_reg
->opcode
)
652 strcpy (buffer
, current_reg
->name
);
660 cnvt_tmsfloat_ieee (tmsfloat
, size
, ieeefloat
)
661 unsigned long tmsfloat
;
665 unsigned long exp
, sign
, mant
;
669 if ((tmsfloat
& 0x0000F000) == 0x00008000)
670 tmsfloat
= 0x80000000;
674 tmsfloat
= (long) tmsfloat
>> 4;
677 exp
= tmsfloat
& 0xFF000000;
678 if (exp
== 0x80000000)
684 sign
= (tmsfloat
& 0x00800000) << 8;
685 mant
= tmsfloat
& 0x007FFFFF;
686 if (exp
== 0xFF000000)
691 *ieeefloat
= 1.0 / 0.0;
693 *ieeefloat
= -1.0 / 0.0;
699 mant
= (~mant
) & 0x007FFFFF;
701 exp
+= mant
& 0x00800000;
705 if (tmsfloat
== 0x80000000)
706 sign
= mant
= exp
= 0;
707 tmsfloat
= sign
| exp
| mant
;
708 *ieeefloat
= *((float *) &tmsfloat
);
This page took 0.042947 seconds and 4 git commands to generate.