* h8500-opc.h: Add default initializers to h8500_table to shut up
[deliverable/binutils-gdb.git] / opcodes / tic54x-dis.c
1 /* Disassembly routines for TMS320C54X architecture
2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Timothy Wall (twall@cygnus.com)
4
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.
9
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.
14
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
18 02111-1307, USA. */
19
20 #include <errno.h>
21 #include <math.h>
22 #include <stdlib.h>
23 #include "sysdep.h"
24 #include "dis-asm.h"
25 #include "opcode/tic54x.h"
26 #include "coff/tic54x.h"
27
28 typedef struct _instruction {
29 int parallel;
30 template *tm;
31 partemplate *ptm;
32 } instruction;
33
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 *,
43 int));
44 static int sprint_dual_address (disassemble_info *,char [],
45 unsigned short);
46 static int sprint_indirect_address (disassemble_info *,char [],
47 unsigned short);
48 static int sprint_direct_address (disassemble_info *,char [],
49 unsigned short);
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);
53
54 int
55 print_insn_tic54x (memaddr, info)
56 bfd_vma memaddr;
57 disassemble_info *info;
58 {
59 bfd_byte opbuf[2];
60 unsigned short opcode;
61 int status, size;
62 instruction insn;
63
64 status = (*info->read_memory_func) (memaddr, opbuf, 2, info);
65 if (status != 0)
66 {
67 (*info->memory_error_func) (status, memaddr, info);
68 return -1;
69 }
70
71 opcode = bfd_getl16 (opbuf);
72 if (!get_instruction (info, memaddr, opcode, &insn))
73 return -1;
74
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;
80
81 if (insn.parallel)
82 {
83 if (!print_parallel_instruction (info, memaddr, opcode, insn.ptm, size))
84 return -1;
85 }
86 else
87 {
88 if (!print_instruction (info, memaddr, opcode,
89 (char *) insn.tm->name,
90 insn.tm->operand_types,
91 size, (insn.tm->flags & FL_EXT)))
92 return -1;
93 }
94
95 return size * 2;
96 }
97
98 static int
99 has_lkaddr (opcode, tm)
100 unsigned short opcode;
101 template *tm;
102 {
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));
108 }
109
110 /* always returns 1 (whether an insn template was found) since we provide an
111 "unknown instruction" template */
112 static int
113 get_instruction (info, addr, opcode, insn)
114 disassemble_info *info;
115 bfd_vma addr;
116 unsigned short opcode;
117 instruction *insn;
118 {
119 template * tm;
120 partemplate * ptm;
121
122 insn->parallel = 0;
123 for (tm = (template *) tic54x_optab; tm->name; tm++)
124 {
125 if (tm->opcode == (opcode & tm->mask))
126 {
127 /* a few opcodes span two words */
128 if (tm->flags & FL_EXT)
129 {
130 /* if lk addressing is used, the second half of the opcode gets
131 pushed one word later */
132 bfd_byte opbuf[2];
133 bfd_vma addr2 = addr + 1 + has_lkaddr (opcode, tm);
134 int status = (*info->read_memory_func) (addr2, opbuf, 2, info);
135 if (status == 0)
136 {
137 unsigned short opcode2 = bfd_getl16 (opbuf);
138 if (tm->opcode2 == (opcode2 & tm->mask2))
139 {
140 insn->tm = tm;
141 return 1;
142 }
143 }
144 }
145 else
146 {
147 insn->tm = tm;
148 return 1;
149 }
150 }
151 }
152 for (ptm = (partemplate *) tic54x_paroptab; ptm->name; ptm++)
153 {
154 if (ptm->opcode == (opcode & ptm->mask))
155 {
156 insn->parallel = 1;
157 insn->ptm = ptm;
158 return 1;
159 }
160 }
161
162 insn->tm = (template *) &tic54x_unknown_opcode;
163 return 1;
164 }
165
166 static int
167 get_insn_size (opcode, insn)
168 unsigned short opcode;
169 instruction *insn;
170 {
171 int size;
172
173 if (insn->parallel)
174 {
175 /* only non-parallel instructions support lk addressing */
176 size = insn->ptm->words;
177 }
178 else
179 {
180 size = insn->tm->words + has_lkaddr (opcode, insn->tm);
181 }
182
183 return size;
184 }
185
186 int
187 print_instruction (info, memaddr, opcode, tm_name, tm_operands, size, ext)
188 disassemble_info *info;
189 bfd_vma memaddr;
190 unsigned short opcode;
191 char *tm_name;
192 enum optype tm_operands[];
193 int size;
194 int ext;
195 {
196 static int n;
197 /* string storage for multiple operands */
198 char operand[4][64] = { {0},{0},{0},{0}, };
199 bfd_byte buf[2];
200 unsigned long opcode2, lkaddr;
201 enum optype src = OP_None;
202 enum optype dst = OP_None;
203 int i, shift;
204 char *comma = "";
205
206 info->fprintf_func (info->stream, "%-7s", tm_name);
207
208 if (size > 1)
209 {
210 int status = (*info->read_memory_func) (memaddr + 1, buf, 2, info);
211 if (status != 0)
212 return 0;
213 lkaddr = opcode2 = bfd_getl16 (buf);
214 if (size > 2)
215 {
216 status = (*info->read_memory_func) (memaddr + 2, buf, 2, info);
217 if (status != 0)
218 return 0;
219 opcode2 = bfd_getl16 (buf);
220 }
221 }
222
223 for (i = 0; i < MAX_OPERANDS && OPTYPE (tm_operands[i]) != OP_None; i++)
224 {
225 char *next_comma = ",";
226 int optional = (tm_operands[i] & OPT) != 0;
227
228 switch (OPTYPE (tm_operands[i]))
229 {
230 case OP_Xmem:
231 sprint_dual_address (info, operand[i], XMEM (opcode));
232 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
233 break;
234 case OP_Ymem:
235 sprint_dual_address (info, operand[i], YMEM (opcode));
236 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
237 break;
238 case OP_Smem:
239 case OP_Sind:
240 case OP_Lmem:
241 info->fprintf_func (info->stream, "%s", comma);
242 if (INDIRECT (opcode))
243 {
244 if (MOD (opcode) >= 12)
245 {
246 bfd_vma addr = lkaddr;
247 int arf = ARF (opcode);
248 int mod = MOD (opcode);
249 if (mod == 15)
250 info->fprintf_func (info->stream, "*(");
251 else
252 info->fprintf_func (info->stream, "*%sar%d(",
253 (mod == 13 || mod == 14 ? "+" : ""),
254 arf);
255 (*(info->print_address_func)) ((bfd_vma) addr, info);
256 info->fprintf_func (info->stream, ")%s",
257 mod == 14 ? "%" : "");
258 }
259 else
260 {
261 sprint_indirect_address (info, operand[i], opcode);
262 info->fprintf_func (info->stream, "%s", operand[i]);
263 }
264 }
265 else
266 {
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]);
271 }
272 break;
273 case OP_dmad:
274 info->fprintf_func (info->stream, "%s", comma);
275 (*(info->print_address_func)) ((bfd_vma) opcode2, info);
276 break;
277 case OP_xpmad:
278 /* upper 7 bits of address are in the opcode */
279 opcode2 += ((unsigned long) opcode & 0x7F) << 16;
280 /* fall through */
281 case OP_pmad:
282 info->fprintf_func (info->stream, "%s", comma);
283 (*(info->print_address_func)) ((bfd_vma) opcode2, info);
284 break;
285 case OP_MMRX:
286 sprint_mmr (info, operand[i], MMRX (opcode));
287 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
288 break;
289 case OP_MMRY:
290 sprint_mmr (info, operand[i], MMRY (opcode));
291 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
292 break;
293 case OP_MMR:
294 sprint_mmr (info, operand[i], MMR (opcode));
295 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
296 break;
297 case OP_PA:
298 sprintf (operand[i], "pa%d", (unsigned) opcode2);
299 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
300 break;
301 case OP_SRC:
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]);
305 break;
306 case OP_SRC1:
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]);
310 break;
311 case OP_RND:
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]);
315 break;
316 case OP_DST:
317 dst = DST (ext ? opcode2 : opcode) ? OP_B : OP_A;
318 if (!optional || dst != src)
319 {
320 sprintf (operand[i], (dst == OP_B) ? "b" : "a");
321 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
322 }
323 else
324 next_comma = comma;
325 break;
326 case OP_B:
327 sprintf (operand[i], "b");
328 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
329 break;
330 case OP_A:
331 sprintf (operand[i], "a");
332 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
333 break;
334 case OP_ARX:
335 sprintf (operand[i], "ar%d", (int) ARX (opcode));
336 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
337 break;
338 case OP_SHIFT:
339 shift = SHIFT (ext ? opcode2 : opcode);
340 if (!optional || shift != 0)
341 {
342 sprintf (operand[i], "%d", shift);
343 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
344 }
345 else
346 next_comma = comma;
347 break;
348 case OP_SHFT:
349 shift = SHFT (opcode);
350 if (!optional || shift != 0)
351 {
352 sprintf (operand[i], "%d", (unsigned) shift);
353 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
354 }
355 else
356 next_comma = comma;
357 break;
358 case OP_lk:
359 sprintf (operand[i], "#%d", (int) (short) opcode2);
360 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
361 break;
362 case OP_T:
363 sprintf (operand[i], "t");
364 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
365 break;
366 case OP_TS:
367 sprintf (operand[i], "ts");
368 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
369 break;
370 case OP_k8:
371 sprintf (operand[i], "%d", (int) ((signed char) (opcode & 0xFF)));
372 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
373 break;
374 case OP_16:
375 sprintf (operand[i], "16");
376 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
377 break;
378 case OP_ASM:
379 sprintf (operand[i], "asm");
380 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
381 break;
382 case OP_BITC:
383 sprintf (operand[i], "%d", (int) (opcode & 0xF));
384 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
385 break;
386 case OP_CC:
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]);
390 i = MAX_OPERANDS;
391 break;
392 case OP_CC2:
393 sprint_cc2 (info, operand[i], opcode);
394 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
395 break;
396 case OP_CC3:
397 {
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]);
401 break;
402 }
403 case OP_123:
404 {
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]);
408 break;
409 }
410 case OP_k5:
411 sprintf (operand[i], "#%d",
412 (int) (((signed char) opcode & 0x1F) << 3) >> 3);
413 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
414 break;
415 case OP_k8u:
416 sprintf (operand[i], "#%d", (unsigned) (opcode & 0xFF));
417 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
418 break;
419 case OP_k3:
420 sprintf (operand[i], "#%d", (int) (opcode & 0x7));
421 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
422 break;
423 case OP_lku:
424 sprintf (operand[i], "#%d", (unsigned) opcode2);
425 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
426 break;
427 case OP_N:
428 n = (opcode >> 9) & 0x1;
429 sprintf (operand[i], "st%d", n);
430 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
431 break;
432 case OP_SBIT:
433 {
434 const char *status0[] = {
435 "0", "1", "2", "3", "4", "5", "6", "7", "8",
436 "ovb", "ova", "c", "tc", "13", "14", "15"
437 };
438 const char *status1[] = {
439 "0", "1", "2", "3", "4",
440 "cmpt", "frct", "c16", "sxm", "ovm", "10",
441 "intm", "hm", "xf", "cpl", "braf"
442 };
443 sprintf (operand[i], "%s",
444 n ? status1[SBIT (opcode)] : status0[SBIT (opcode)]);
445 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
446 break;
447 }
448 case OP_12:
449 sprintf (operand[i], "%d", (int) ((opcode >> 9) & 1) + 1);
450 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
451 break;
452 case OP_TRN:
453 sprintf (operand[i], "trn");
454 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
455 break;
456 case OP_DP:
457 sprintf (operand[i], "dp");
458 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
459 break;
460 case OP_k9:
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]);
464 break;
465 case OP_ARP:
466 sprintf (operand[i], "arp");
467 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
468 break;
469 case OP_031:
470 sprintf (operand[i], "%d", (int) (opcode & 0x1F));
471 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
472 break;
473 default:
474 sprintf (operand[i], "??? (0x%x)", tm_operands[i]);
475 info->fprintf_func (info->stream, "%s%s", comma, operand[i]);
476 break;
477 }
478 comma = next_comma;
479 }
480 return 1;
481 }
482
483 static int
484 print_parallel_instruction (info, memaddr, opcode, ptm, size)
485 disassemble_info *info;
486 bfd_vma memaddr;
487 unsigned short opcode;
488 partemplate *ptm;
489 int size;
490 {
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);
496 }
497
498 static int
499 sprint_dual_address (info, buf, code)
500 disassemble_info *info ATTRIBUTE_UNUSED;
501 char buf[];
502 unsigned short code;
503 {
504 const char *formats[] = {
505 "*ar%d",
506 "*ar%d-",
507 "*ar%d+",
508 "*ar%d+0%%",
509 };
510 return sprintf (buf, formats[XMOD (code)], XARX (code));
511 }
512
513 static int
514 sprint_indirect_address (info, buf, opcode)
515 disassemble_info *info ATTRIBUTE_UNUSED;
516 char buf[];
517 unsigned short opcode;
518 {
519 const char *formats[] = {
520 "*ar%d",
521 "*ar%d-",
522 "*ar%d+",
523 "*+ar%d",
524 "*ar%d-0B",
525 "*ar%d-0",
526 "*ar%d+0",
527 "*ar%d+0B",
528 "*ar%d-%%",
529 "*ar%d-0%%",
530 "*ar%d+%%",
531 "*ar%d+0%%",
532 };
533 return sprintf (buf, formats[MOD (opcode)], ARF (opcode));
534 }
535
536 static int
537 sprint_direct_address (info, buf, opcode)
538 disassemble_info *info ATTRIBUTE_UNUSED;
539 char buf[];
540 unsigned short opcode;
541 {
542 /* FIXME -- look up relocation if available */
543 return sprintf (buf, "0x??%02x", (int) (opcode & 0x7F));
544 }
545
546 static int
547 sprint_mmr (info, buf, mmr)
548 disassemble_info *info ATTRIBUTE_UNUSED;
549 char buf[];
550 int mmr;
551 {
552 symbol *reg = (symbol *) mmregs;
553 while (reg->name != NULL)
554 {
555 if (mmr == reg->value)
556 {
557 sprintf (buf, "%s", (reg + 1)->name);
558 return 1;
559 }
560 ++reg;
561 }
562 sprintf (buf, "MMR(%d)", mmr); /* FIXME -- different targets. */
563 return 0;
564 }
565
566 static int
567 sprint_cc2 (info, buf, opcode)
568 disassemble_info *info ATTRIBUTE_UNUSED;
569 char *buf;
570 unsigned short opcode;
571 {
572 const char *cc2[] = {
573 "??", "??", "ageq", "alt", "aneq", "aeq", "agt", "aleq",
574 "??", "??", "bgeq", "blt", "bneq", "beq", "bgt", "bleq",
575 };
576 return sprintf (buf, "%s", cc2[opcode & 0xF]);
577 }
578
579 static int
580 sprint_condition (info, buf, opcode)
581 disassemble_info *info ATTRIBUTE_UNUSED;
582 char *buf;
583 unsigned short opcode;
584 {
585 char *start = buf;
586 const char *cmp[] = {
587 "??", "??", "geq", "lt", "neq", "eq", "gt", "leq"
588 };
589 if (opcode & 0x40)
590 {
591 char acc = (opcode & 0x8) ? 'b' : 'a';
592 if (opcode & 0x7)
593 buf += sprintf (buf, "%c%s%s", acc, cmp[(opcode & 0x7)],
594 (opcode & 0x20) ? ", " : "");
595 if (opcode & 0x20)
596 buf += sprintf (buf, "%c%s", acc, (opcode & 0x10) ? "ov" : "nov");
597 }
598 else if (opcode & 0x3F)
599 {
600 if (opcode & 0x30)
601 buf += sprintf (buf, "%s%s",
602 ((opcode & 0x30) == 0x30) ? "tc" : "ntc",
603 (opcode & 0x0F) ? ", " : "");
604 if (opcode & 0x0C)
605 buf += sprintf (buf, "%s%s",
606 ((opcode & 0x0C) == 0x0C) ? "c" : "nc",
607 (opcode & 0x03) ? ", " : "");
608 if (opcode & 0x03)
609 buf += sprintf (buf, "%s",
610 ((opcode & 0x03) == 0x03) ? "bio" : "nbio");
611 }
612 else
613 buf += sprintf (buf, "unc");
614
615 return buf - start;
616 }
This page took 0.04571 seconds and 4 git commands to generate.