1 /* Disassembly routines for TMS320C54X architecture
2 Copyright (C) 1999,2000 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 get_insn_size
PARAMS ((unsigned short, instruction
*));
35 static int get_instruction
PARAMS ((disassemble_info
*, bfd_vma
,
36 unsigned short, instruction
*));
37 static int print_instruction
PARAMS ((disassemble_info
*, bfd_vma
,
38 unsigned short, char *,
39 enum optype
[], int, int));
40 static int print_parallel_instruction
PARAMS ((disassemble_info
*, bfd_vma
,
41 unsigned short, partemplate
*,
43 static int sprint_dual_address (disassemble_info
*,char [],
45 static int sprint_indirect_address (disassemble_info
*,char [],
47 static int sprint_direct_address (disassemble_info
*,char [],
49 static int sprint_mmr (disassemble_info
*,char [],int);
50 static int sprint_condition (disassemble_info
*,char *,unsigned short);
51 static int sprint_cc2 (disassemble_info
*,char *,unsigned short);
54 print_insn_tic54x(memaddr
, info
)
56 disassemble_info
*info
;
59 unsigned short opcode
;
63 status
= (*info
->read_memory_func
) (memaddr
, opbuf
, 2, info
);
66 (*info
->memory_error_func
)(status
, memaddr
, info
);
70 opcode
= bfd_getl16(opbuf
);
71 if (!get_instruction (info
, memaddr
, opcode
, &insn
))
74 size
= get_insn_size (opcode
, &insn
);
75 info
->bytes_per_line
= 2;
76 info
->bytes_per_chunk
= 2;
77 info
->octets_per_byte
= 2;
78 info
->display_endian
= BFD_ENDIAN_LITTLE
;
82 if (!print_parallel_instruction (info
, memaddr
, opcode
, insn
.ptm
, size
))
87 if (!print_instruction (info
, memaddr
, opcode
,
88 (char *)insn
.tm
->name
,
89 insn
.tm
->operand_types
,
90 size
, (insn
.tm
->flags
& FL_EXT
)))
98 has_lkaddr(opcode
, tm
)
99 unsigned short opcode
;
102 return IS_LKADDR(opcode
) &&
103 (OPTYPE(tm
->operand_types
[0]) == OP_Smem
||
104 OPTYPE(tm
->operand_types
[1]) == OP_Smem
||
105 OPTYPE(tm
->operand_types
[2]) == OP_Smem
||
106 OPTYPE(tm
->operand_types
[1]) == OP_Sind
);
109 /* always returns 1 (whether an insn template was found) since we provide an
110 "unknown instruction" template */
112 get_instruction (info
, addr
, opcode
, insn
)
113 disassemble_info
*info
;
115 unsigned short opcode
;
122 for (tm
= (template *)tic54x_optab
; tm
->name
; tm
++)
124 if (tm
->opcode
== (opcode
& tm
->mask
))
126 /* a few opcodes span two words */
127 if (tm
->flags
& FL_EXT
)
129 /* if lk addressing is used, the second half of the opcode gets
130 pushed one word later */
132 bfd_vma addr2
= addr
+ 1 + has_lkaddr(opcode
, tm
);
133 int status
= (*info
->read_memory_func
)(addr2
, opbuf
, 2, info
);
136 unsigned short opcode2
= bfd_getl16(opbuf
);
137 if (tm
->opcode2
== (opcode2
& tm
->mask2
))
151 for (ptm
= (partemplate
*)tic54x_paroptab
; ptm
->name
; ptm
++)
153 if (ptm
->opcode
== (opcode
& ptm
->mask
))
161 insn
->tm
= (template *)&tic54x_unknown_opcode
;
166 get_insn_size (opcode
, insn
)
167 unsigned short opcode
;
174 /* only non-parallel instructions support lk addressing */
175 size
= insn
->ptm
->words
;
179 size
= insn
->tm
->words
+ has_lkaddr(opcode
, insn
->tm
);
186 print_instruction (info
, memaddr
, opcode
, tm_name
, tm_operands
, size
, ext
)
187 disassemble_info
*info
;
189 unsigned short opcode
;
191 enum optype tm_operands
[];
196 /* string storage for multiple operands */
197 char operand
[4][64] = { {0},{0},{0},{0}, };
199 unsigned long opcode2
, lkaddr
;
200 enum optype src
= OP_None
;
201 enum optype dst
= OP_None
;
205 info
->fprintf_func (info
->stream
, "%-7s", tm_name
);
209 int status
= (*info
->read_memory_func
) (memaddr
+1, buf
, 2, info
);
212 lkaddr
= opcode2
= bfd_getl16(buf
);
215 status
= (*info
->read_memory_func
) (memaddr
+2, buf
, 2, info
);
218 opcode2
= bfd_getl16(buf
);
222 for (i
=0;i
< MAX_OPERANDS
&& OPTYPE(tm_operands
[i
]) != OP_None
;i
++)
224 char *next_comma
= ",";
225 int optional
= (tm_operands
[i
] & OPT
) != 0;
227 switch (OPTYPE(tm_operands
[i
]))
230 sprint_dual_address (info
, operand
[i
], XMEM(opcode
));
231 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
234 sprint_dual_address (info
, operand
[i
], YMEM(opcode
));
235 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
240 info
->fprintf_func (info
->stream
, "%s", comma
);
241 if (INDIRECT(opcode
))
243 if (MOD(opcode
) >= 12)
245 bfd_vma addr
= lkaddr
;
246 int arf
= ARF(opcode
);
247 int mod
= MOD(opcode
);
249 info
->fprintf_func (info
->stream
, "*(");
251 info
->fprintf_func (info
->stream
, "*%sar%d(",
252 (mod
== 13 || mod
== 14 ? "+" : ""),
254 (*(info
->print_address_func
))((bfd_vma
)addr
, info
);
255 info
->fprintf_func (info
->stream
, ")%s",
256 mod
== 14 ? "%" : "");
260 sprint_indirect_address (info
, operand
[i
], opcode
);
261 info
->fprintf_func (info
->stream
, "%s", operand
[i
]);
266 /* FIXME -- use labels (print_address_func) */
267 /* in order to do this, we need to guess what DP is */
268 sprint_direct_address (info
, operand
[i
], opcode
);
269 info
->fprintf_func (info
->stream
, "%s", operand
[i
]);
273 info
->fprintf_func (info
->stream
, "%s", comma
);
274 (*(info
->print_address_func
))((bfd_vma
)opcode2
, info
);
277 /* upper 7 bits of address are in the opcode */
278 opcode2
+= ((unsigned long)opcode
& 0x7F) << 16;
281 info
->fprintf_func (info
->stream
, "%s", comma
);
282 (*(info
->print_address_func
))((bfd_vma
)opcode2
, info
);
285 sprint_mmr (info
, operand
[i
], MMRX(opcode
));
286 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
289 sprint_mmr (info
, operand
[i
], MMRY(opcode
));
290 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
293 sprint_mmr (info
, operand
[i
], MMR(opcode
));
294 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
297 sprintf (operand
[i
], "pa%d", (unsigned)opcode2
);
298 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
301 src
= SRC(ext
? opcode2
: opcode
) ? OP_B
: OP_A
;
302 sprintf (operand
[i
], (src
== OP_B
) ? "b" : "a");
303 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
306 src
= SRC1(ext
? opcode2
: opcode
) ? OP_B
: OP_A
;
307 sprintf (operand
[i
], (src
== OP_B
) ? "b" : "a");
308 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
311 dst
= DST(opcode
) ? OP_B
: OP_A
;
312 sprintf (operand
[i
], (dst
== OP_B
) ? "a" : "b");
313 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
316 dst
= DST(ext
? opcode2
: opcode
) ? OP_B
: OP_A
;
317 if (!optional
|| dst
!= src
)
319 sprintf (operand
[i
], (dst
== OP_B
) ? "b" : "a");
320 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
326 sprintf (operand
[i
], "b");
327 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
330 sprintf (operand
[i
], "a");
331 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
334 sprintf (operand
[i
],"ar%d", (int)ARX(opcode
));
335 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
338 shift
= SHIFT(ext
? opcode2
: opcode
);
339 if (!optional
|| shift
!= 0)
341 sprintf (operand
[i
],"%d", shift
);
342 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
348 shift
= SHFT(opcode
);
349 if (!optional
|| shift
!= 0)
351 sprintf (operand
[i
],"%d", (unsigned)shift
);
352 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
358 sprintf (operand
[i
],"#%d", (int)(short)opcode2
);
359 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
362 sprintf (operand
[i
], "t");
363 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
366 sprintf (operand
[i
], "ts");
367 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
370 sprintf (operand
[i
], "%d", (int)((signed char)(opcode
& 0xFF)));
371 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
374 sprintf (operand
[i
], "16");
375 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
378 sprintf (operand
[i
], "asm");
379 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
382 sprintf (operand
[i
], "%d", (int)(opcode
& 0xF));
383 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
386 /* put all CC operands in the same operand */
387 sprint_condition (info
, operand
[i
], opcode
);
388 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
392 sprint_cc2 (info
, operand
[i
], opcode
);
393 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
397 const char *code
[] = { "eq", "lt", "gt", "neq" };
398 sprintf (operand
[i
], code
[CC3(opcode
)]);
399 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
404 int code
= (opcode
>>8) & 0x3;
405 sprintf (operand
[i
], "%d", (code
== 0) ? 1 : (code
== 2) ? 2 : 3);
406 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
410 sprintf (operand
[i
], "#%d",
411 (int)(((signed char)opcode
& 0x1F) << 3)>>3);
412 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
415 sprintf (operand
[i
], "#%d", (unsigned)(opcode
& 0xFF));
416 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
419 sprintf (operand
[i
], "#%d", (int)(opcode
& 0x7));
420 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
423 sprintf (operand
[i
], "#%d", (unsigned)opcode2
);
424 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
427 n
= (opcode
>> 9) & 0x1;
428 sprintf (operand
[i
], "st%d", n
);
429 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
433 const char *status0
[] = {
434 "0", "1", "2", "3", "4", "5", "6", "7", "8",
435 "ovb", "ova", "c", "tc", "13", "14", "15"
437 const char *status1
[] = {
438 "0", "1", "2", "3", "4",
439 "cmpt", "frct", "c16", "sxm", "ovm", "10",
440 "intm", "hm", "xf", "cpl", "braf"
442 sprintf (operand
[i
], "%s",
443 n
? status1
[SBIT(opcode
)] : status0
[SBIT(opcode
)]);
444 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
448 sprintf (operand
[i
], "%d", (int)((opcode
>> 9)&1) + 1);
449 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
452 sprintf (operand
[i
], "trn");
453 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
456 sprintf (operand
[i
], "dp");
457 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
460 /* FIXME-- this is DP, print the original address? */
461 sprintf (operand
[i
], "#%d", (int)(opcode
& 0x1FF));
462 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
465 sprintf (operand
[i
], "arp");
466 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
469 sprintf (operand
[i
], "%d", (int)(opcode
& 0x1F));
470 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
473 sprintf (operand
[i
], "??? (0x%x)", tm_operands
[i
]);
474 info
->fprintf_func (info
->stream
, "%s%s", comma
, operand
[i
]);
483 print_parallel_instruction (info
, memaddr
, opcode
, ptm
, size
)
484 disassemble_info
*info
;
486 unsigned short opcode
;
490 print_instruction (info
, memaddr
, opcode
,
491 ptm
->name
, ptm
->operand_types
, size
, 0);
492 info
->fprintf_func (info
->stream
, " || ");
493 return print_instruction (info
, memaddr
, opcode
,
494 ptm
->parname
, ptm
->paroperand_types
, size
, 0);
498 sprint_dual_address (info
, buf
, code
)
499 disassemble_info
*info
;
503 const char *formats
[] = {
509 return sprintf (buf
, formats
[XMOD(code
)], XARX(code
));
513 sprint_indirect_address (info
, buf
, opcode
)
514 disassemble_info
*info
;
516 unsigned short opcode
;
518 const char *formats
[] = {
532 return sprintf (buf
, formats
[MOD(opcode
)], ARF(opcode
));
536 sprint_direct_address (info
, buf
, opcode
)
537 disassemble_info
*info
;
539 unsigned short opcode
;
541 /* FIXME -- look up relocation if available */
542 return sprintf (buf
, "0x??%02x", (int)(opcode
& 0x7F));
546 sprint_mmr (info
, buf
, mmr
)
547 disassemble_info
*info
;
551 symbol
*reg
= (symbol
*)mmregs
;
552 while (reg
->name
!= NULL
)
554 if (mmr
== reg
->value
)
556 sprintf (buf
, "%s", (reg
+1)->name
);
561 sprintf (buf
, "MMR(%d)", mmr
); /* FIXME -- different targets. */
566 sprint_cc2 (info
, buf
, opcode
)
567 disassemble_info
*info
;
569 unsigned short opcode
;
571 const char *cc2
[] = {
572 "??", "??", "ageq", "alt", "aneq", "aeq", "agt", "aleq",
573 "??", "??", "bgeq", "blt", "bneq", "beq", "bgt", "bleq",
575 return sprintf (buf
, "%s", cc2
[opcode
& 0xF]);
579 sprint_condition (info
, buf
, opcode
)
580 disassemble_info
*info
;
582 unsigned short opcode
;
585 const char *cmp
[] = {
586 "??", "??", "geq", "lt", "neq", "eq", "gt", "leq"
590 char acc
= (opcode
& 0x8) ? 'b' : 'a';
592 buf
+= sprintf (buf
, "%c%s%s", acc
, cmp
[(opcode
&0x7)],
593 (opcode
&0x20) ? ", " : "");
595 buf
+= sprintf (buf
, "%c%s", acc
, (opcode
&0x10) ? "ov" : "nov");
597 else if (opcode
& 0x3F)
600 buf
+= sprintf (buf
, "%s%s",
601 ((opcode
& 0x30) == 0x30) ? "tc" : "ntc",
602 (opcode
& 0x0F) ? ", " : "");
604 buf
+= sprintf (buf
, "%s%s",
605 ((opcode
& 0x0C) == 0x0C) ? "c" : "nc",
606 (opcode
& 0x03) ? ", " : "");
608 buf
+= sprintf (buf
, "%s",
609 ((opcode
& 0x03) == 0x03) ? "bio" : "nbio");
612 buf
+= sprintf (buf
, "unc");