1 /* Disassembly routines for TMS320C54X architecture
2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Timothy Wall (twall@cygnus.com)
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
25 #include "opcode/tic54x.h"
26 #include "coff/tic54x.h"
28 typedef struct _instruction
{
34 static int has_lkaddr
PARAMS ((unsigned short, template *));
35 static int get_insn_size
PARAMS ((unsigned short, instruction
*));
36 static int get_instruction
PARAMS ((disassemble_info
*, bfd_vma
,
37 unsigned short, instruction
*));
38 static int print_instruction
PARAMS ((disassemble_info
*, bfd_vma
,
39 unsigned short, char *,
40 enum optype
[], int, int));
41 static int print_parallel_instruction
PARAMS ((disassemble_info
*, bfd_vma
,
42 unsigned short, partemplate
*,
44 static int sprint_dual_address (disassemble_info
*,char [],
46 static int sprint_indirect_address (disassemble_info
*,char [],
48 static int sprint_direct_address (disassemble_info
*,char [],
50 static int sprint_mmr (disassemble_info
*,char [],int);
51 static int sprint_condition (disassemble_info
*,char *,unsigned short);
52 static int sprint_cc2 (disassemble_info
*,char *,unsigned short);
55 print_insn_tic54x (memaddr
, info
)
57 disassemble_info
*info
;
60 unsigned short opcode
;
64 status
= (*info
->read_memory_func
) (memaddr
, opbuf
, 2, info
);
67 (*info
->memory_error_func
) (status
, memaddr
, info
);
71 opcode
= bfd_getl16 (opbuf
);
72 if (!get_instruction (info
, memaddr
, opcode
, &insn
))
75 size
= get_insn_size (opcode
, &insn
);
76 info
->bytes_per_line
= 2;
77 info
->bytes_per_chunk
= 2;
78 info
->octets_per_byte
= 2;
79 info
->display_endian
= BFD_ENDIAN_LITTLE
;
83 if (!print_parallel_instruction (info
, memaddr
, opcode
, insn
.ptm
, size
))
88 if (!print_instruction (info
, memaddr
, opcode
,
89 (char *) insn
.tm
->name
,
90 insn
.tm
->operand_types
,
91 size
, (insn
.tm
->flags
& FL_EXT
)))
99 has_lkaddr (opcode
, tm
)
100 unsigned short opcode
;
103 return (IS_LKADDR (opcode
)
104 && (OPTYPE (tm
->operand_types
[0]) == OP_Smem
105 || OPTYPE (tm
->operand_types
[1]) == OP_Smem
106 || OPTYPE (tm
->operand_types
[2]) == OP_Smem
107 || OPTYPE (tm
->operand_types
[1]) == OP_Sind
));
110 /* always returns 1 (whether an insn template was found) since we provide an
111 "unknown instruction" template */
113 get_instruction (info
, addr
, opcode
, insn
)
114 disassemble_info
*info
;
116 unsigned short opcode
;
123 for (tm
= (template *) tic54x_optab
; tm
->name
; tm
++)
125 if (tm
->opcode
== (opcode
& tm
->mask
))
127 /* a few opcodes span two words */
128 if (tm
->flags
& FL_EXT
)
130 /* if lk addressing is used, the second half of the opcode gets
131 pushed one word later */
133 bfd_vma addr2
= addr
+ 1 + has_lkaddr (opcode
, tm
);
134 int status
= (*info
->read_memory_func
) (addr2
, opbuf
, 2, info
);
137 unsigned short opcode2
= bfd_getl16 (opbuf
);
138 if (tm
->opcode2
== (opcode2
& tm
->mask2
))
152 for (ptm
= (partemplate
*) tic54x_paroptab
; ptm
->name
; ptm
++)
154 if (ptm
->opcode
== (opcode
& ptm
->mask
))
162 insn
->tm
= (template *) &tic54x_unknown_opcode
;
167 get_insn_size (opcode
, insn
)
168 unsigned short opcode
;
175 /* only non-parallel instructions support lk addressing */
176 size
= insn
->ptm
->words
;
180 size
= insn
->tm
->words
+ has_lkaddr (opcode
, insn
->tm
);
187 print_instruction (info
, memaddr
, opcode
, tm_name
, tm_operands
, size
, ext
)
188 disassemble_info
*info
;
190 unsigned short opcode
;
192 enum optype tm_operands
[];
197 /* string storage for multiple operands */
198 char operand
[4][64] = { {0},{0},{0},{0}, };
200 unsigned long opcode2
, lkaddr
;
201 enum optype src
= OP_None
;
202 enum optype dst
= OP_None
;
206 info
->fprintf_func (info
->stream
, "%-7s", tm_name
);
210 int status
= (*info
->read_memory_func
) (memaddr
+ 1, buf
, 2, info
);
213 lkaddr
= opcode2
= bfd_getl16 (buf
);
216 status
= (*info
->read_memory_func
) (memaddr
+ 2, buf
, 2, info
);
219 opcode2
= bfd_getl16 (buf
);
223 for (i
= 0; i
< MAX_OPERANDS
&& OPTYPE (tm_operands
[i
]) != OP_None
; i
++)
225 char *next_comma
= ",";
226 int optional
= (tm_operands
[i
] & OPT
) != 0;
228 switch (OPTYPE (tm_operands
[i
]))
231 sprint_dual_address (info
, operand
[i
], XMEM (opcode
));
232 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
235 sprint_dual_address (info
, operand
[i
], YMEM (opcode
));
236 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
241 info
->fprintf_func (info
->stream
, "%s", comma
);
242 if (INDIRECT (opcode
))
244 if (MOD (opcode
) >= 12)
246 bfd_vma addr
= lkaddr
;
247 int arf
= ARF (opcode
);
248 int mod
= MOD (opcode
);
250 info
->fprintf_func (info
->stream
, "*(");
252 info
->fprintf_func (info
->stream
, "*%sar%d(",
253 (mod
== 13 || mod
== 14 ? "+" : ""),
255 (*(info
->print_address_func
)) ((bfd_vma
) addr
, info
);
256 info
->fprintf_func (info
->stream
, ")%s",
257 mod
== 14 ? "%" : "");
261 sprint_indirect_address (info
, operand
[i
], opcode
);
262 info
->fprintf_func (info
->stream
, "%s", operand
[i
]);
267 /* FIXME -- use labels (print_address_func) */
268 /* in order to do this, we need to guess what DP is */
269 sprint_direct_address (info
, operand
[i
], opcode
);
270 info
->fprintf_func (info
->stream
, "%s", operand
[i
]);
274 info
->fprintf_func (info
->stream
, "%s", comma
);
275 (*(info
->print_address_func
)) ((bfd_vma
) opcode2
, info
);
278 /* upper 7 bits of address are in the opcode */
279 opcode2
+= ((unsigned long) opcode
& 0x7F) << 16;
282 info
->fprintf_func (info
->stream
, "%s", comma
);
283 (*(info
->print_address_func
)) ((bfd_vma
) opcode2
, info
);
286 sprint_mmr (info
, operand
[i
], MMRX (opcode
));
287 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
290 sprint_mmr (info
, operand
[i
], MMRY (opcode
));
291 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
294 sprint_mmr (info
, operand
[i
], MMR (opcode
));
295 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
298 sprintf (operand
[i
], "pa%d", (unsigned) opcode2
);
299 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
302 src
= SRC (ext
? opcode2
: opcode
) ? OP_B
: OP_A
;
303 sprintf (operand
[i
], (src
== OP_B
) ? "b" : "a");
304 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
307 src
= SRC1 (ext
? opcode2
: opcode
) ? OP_B
: OP_A
;
308 sprintf (operand
[i
], (src
== OP_B
) ? "b" : "a");
309 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
312 dst
= DST (opcode
) ? OP_B
: OP_A
;
313 sprintf (operand
[i
], (dst
== OP_B
) ? "a" : "b");
314 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
317 dst
= DST (ext
? opcode2
: opcode
) ? OP_B
: OP_A
;
318 if (!optional
|| dst
!= src
)
320 sprintf (operand
[i
], (dst
== OP_B
) ? "b" : "a");
321 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
327 sprintf (operand
[i
], "b");
328 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
331 sprintf (operand
[i
], "a");
332 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
335 sprintf (operand
[i
], "ar%d", (int) ARX (opcode
));
336 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
339 shift
= SHIFT (ext
? opcode2
: opcode
);
340 if (!optional
|| shift
!= 0)
342 sprintf (operand
[i
], "%d", shift
);
343 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
349 shift
= SHFT (opcode
);
350 if (!optional
|| shift
!= 0)
352 sprintf (operand
[i
], "%d", (unsigned) shift
);
353 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
359 sprintf (operand
[i
], "#%d", (int) (short) opcode2
);
360 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
363 sprintf (operand
[i
], "t");
364 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
367 sprintf (operand
[i
], "ts");
368 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
371 sprintf (operand
[i
], "%d", (int) ((signed char) (opcode
& 0xFF)));
372 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
375 sprintf (operand
[i
], "16");
376 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
379 sprintf (operand
[i
], "asm");
380 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
383 sprintf (operand
[i
], "%d", (int) (opcode
& 0xF));
384 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
387 /* put all CC operands in the same operand */
388 sprint_condition (info
, operand
[i
], opcode
);
389 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
393 sprint_cc2 (info
, operand
[i
], opcode
);
394 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
398 const char *code
[] = { "eq", "lt", "gt", "neq" };
399 sprintf (operand
[i
], code
[CC3 (opcode
)]);
400 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
405 int code
= (opcode
>> 8) & 0x3;
406 sprintf (operand
[i
], "%d", (code
== 0) ? 1 : (code
== 2) ? 2 : 3);
407 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
411 sprintf (operand
[i
], "#%d",
412 (int) (((signed char) opcode
& 0x1F) << 3) >> 3);
413 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
416 sprintf (operand
[i
], "#%d", (unsigned) (opcode
& 0xFF));
417 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
420 sprintf (operand
[i
], "#%d", (int) (opcode
& 0x7));
421 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
424 sprintf (operand
[i
], "#%d", (unsigned) opcode2
);
425 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
428 n
= (opcode
>> 9) & 0x1;
429 sprintf (operand
[i
], "st%d", n
);
430 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
434 const char *status0
[] = {
435 "0", "1", "2", "3", "4", "5", "6", "7", "8",
436 "ovb", "ova", "c", "tc", "13", "14", "15"
438 const char *status1
[] = {
439 "0", "1", "2", "3", "4",
440 "cmpt", "frct", "c16", "sxm", "ovm", "10",
441 "intm", "hm", "xf", "cpl", "braf"
443 sprintf (operand
[i
], "%s",
444 n
? status1
[SBIT (opcode
)] : status0
[SBIT (opcode
)]);
445 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
449 sprintf (operand
[i
], "%d", (int) ((opcode
>> 9) & 1) + 1);
450 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
453 sprintf (operand
[i
], "trn");
454 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
457 sprintf (operand
[i
], "dp");
458 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
461 /* FIXME-- this is DP, print the original address? */
462 sprintf (operand
[i
], "#%d", (int) (opcode
& 0x1FF));
463 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
466 sprintf (operand
[i
], "arp");
467 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
470 sprintf (operand
[i
], "%d", (int) (opcode
& 0x1F));
471 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
474 sprintf (operand
[i
], "??? (0x%x)", tm_operands
[i
]);
475 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
484 print_parallel_instruction (info
, memaddr
, opcode
, ptm
, size
)
485 disassemble_info
*info
;
487 unsigned short opcode
;
491 print_instruction (info
, memaddr
, opcode
,
492 ptm
->name
, ptm
->operand_types
, size
, 0);
493 info
->fprintf_func (info
->stream
, " || ");
494 return print_instruction (info
, memaddr
, opcode
,
495 ptm
->parname
, ptm
->paroperand_types
, size
, 0);
499 sprint_dual_address (info
, buf
, code
)
500 disassemble_info
*info ATTRIBUTE_UNUSED
;
504 const char *formats
[] = {
510 return sprintf (buf
, formats
[XMOD (code
)], XARX (code
));
514 sprint_indirect_address (info
, buf
, opcode
)
515 disassemble_info
*info ATTRIBUTE_UNUSED
;
517 unsigned short opcode
;
519 const char *formats
[] = {
533 return sprintf (buf
, formats
[MOD (opcode
)], ARF (opcode
));
537 sprint_direct_address (info
, buf
, opcode
)
538 disassemble_info
*info ATTRIBUTE_UNUSED
;
540 unsigned short opcode
;
542 /* FIXME -- look up relocation if available */
543 return sprintf (buf
, "0x??%02x", (int) (opcode
& 0x7F));
547 sprint_mmr (info
, buf
, mmr
)
548 disassemble_info
*info ATTRIBUTE_UNUSED
;
552 symbol
*reg
= (symbol
*) mmregs
;
553 while (reg
->name
!= NULL
)
555 if (mmr
== reg
->value
)
557 sprintf (buf
, "%s", (reg
+ 1)->name
);
562 sprintf (buf
, "MMR(%d)", mmr
); /* FIXME -- different targets. */
567 sprint_cc2 (info
, buf
, opcode
)
568 disassemble_info
*info ATTRIBUTE_UNUSED
;
570 unsigned short opcode
;
572 const char *cc2
[] = {
573 "??", "??", "ageq", "alt", "aneq", "aeq", "agt", "aleq",
574 "??", "??", "bgeq", "blt", "bneq", "beq", "bgt", "bleq",
576 return sprintf (buf
, "%s", cc2
[opcode
& 0xF]);
580 sprint_condition (info
, buf
, opcode
)
581 disassemble_info
*info ATTRIBUTE_UNUSED
;
583 unsigned short opcode
;
586 const char *cmp
[] = {
587 "??", "??", "geq", "lt", "neq", "eq", "gt", "leq"
591 char acc
= (opcode
& 0x8) ? 'b' : 'a';
593 buf
+= sprintf (buf
, "%c%s%s", acc
, cmp
[(opcode
& 0x7)],
594 (opcode
& 0x20) ? ", " : "");
596 buf
+= sprintf (buf
, "%c%s", acc
, (opcode
& 0x10) ? "ov" : "nov");
598 else if (opcode
& 0x3F)
601 buf
+= sprintf (buf
, "%s%s",
602 ((opcode
& 0x30) == 0x30) ? "tc" : "ntc",
603 (opcode
& 0x0F) ? ", " : "");
605 buf
+= sprintf (buf
, "%s%s",
606 ((opcode
& 0x0C) == 0x0C) ? "c" : "nc",
607 (opcode
& 0x03) ? ", " : "");
609 buf
+= sprintf (buf
, "%s",
610 ((opcode
& 0x03) == 0x03) ? "bio" : "nbio");
613 buf
+= sprintf (buf
, "unc");