2005-09-01 Dmitry Diky <diwil@spec.ru>
[binutils.git] / opcodes / tic4x-dis.c
blobec56cb3c5b33227945b9061c93ca0246fc4210f4
1 /* Print instructions for the Texas TMS320C[34]X, for GDB and GNU Binutils.
3 Copyright 2002, 2003, 2005 Free Software Foundation, Inc.
5 Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz)
7 This program 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 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include <math.h>
23 #include "libiberty.h"
24 #include "dis-asm.h"
25 #include "opcode/tic4x.h"
27 #define TIC4X_DEBUG 0
29 #define TIC4X_HASH_SIZE 11 /* 11 (bits) and above should give unique entries. */
30 #define TIC4X_SPESOP_SIZE 8 /* Max 8. ops for special instructions. */
32 typedef enum
34 IMMED_SINT,
35 IMMED_SUINT,
36 IMMED_SFLOAT,
37 IMMED_INT,
38 IMMED_UINT,
39 IMMED_FLOAT
41 immed_t;
43 typedef enum
45 INDIRECT_SHORT,
46 INDIRECT_LONG,
47 INDIRECT_TIC4X
49 indirect_t;
51 static int tic4x_version = 0;
52 static int tic4x_dp = 0;
54 static int
55 tic4x_pc_offset (unsigned int op)
57 /* Determine the PC offset for a C[34]x instruction.
58 This could be simplified using some boolean algebra
59 but at the expense of readability. */
60 switch (op >> 24)
62 case 0x60: /* br */
63 case 0x62: /* call (C4x) */
64 case 0x64: /* rptb (C4x) */
65 return 1;
66 case 0x61: /* brd */
67 case 0x63: /* laj */
68 case 0x65: /* rptbd (C4x) */
69 return 3;
70 case 0x66: /* swi */
71 case 0x67:
72 return 0;
73 default:
74 break;
77 switch ((op & 0xffe00000) >> 20)
79 case 0x6a0: /* bB */
80 case 0x720: /* callB */
81 case 0x740: /* trapB */
82 return 1;
84 case 0x6a2: /* bBd */
85 case 0x6a6: /* bBat */
86 case 0x6aa: /* bBaf */
87 case 0x722: /* lajB */
88 case 0x748: /* latB */
89 case 0x798: /* rptbd */
90 return 3;
92 default:
93 break;
96 switch ((op & 0xfe200000) >> 20)
98 case 0x6e0: /* dbB */
99 return 1;
101 case 0x6e2: /* dbBd */
102 return 3;
104 default:
105 break;
108 return 0;
111 static int
112 tic4x_print_char (struct disassemble_info * info, char ch)
114 if (info != NULL)
115 (*info->fprintf_func) (info->stream, "%c", ch);
116 return 1;
119 static int
120 tic4x_print_str (struct disassemble_info *info, char *str)
122 if (info != NULL)
123 (*info->fprintf_func) (info->stream, "%s", str);
124 return 1;
127 static int
128 tic4x_print_register (struct disassemble_info *info, unsigned long regno)
130 static tic4x_register_t ** registertable = NULL;
131 unsigned int i;
133 if (registertable == NULL)
135 registertable = xmalloc (sizeof (tic4x_register_t *) * REG_TABLE_SIZE);
136 for (i = 0; i < tic3x_num_registers; i++)
137 registertable[tic3x_registers[i].regno] = (tic4x_register_t *) (tic3x_registers + i);
138 if (IS_CPU_TIC4X (tic4x_version))
140 /* Add C4x additional registers, overwriting
141 any C3x registers if necessary. */
142 for (i = 0; i < tic4x_num_registers; i++)
143 registertable[tic4x_registers[i].regno] =
144 (tic4x_register_t *)(tic4x_registers + i);
147 if ((int) regno > (IS_CPU_TIC4X (tic4x_version) ? TIC4X_REG_MAX : TIC3X_REG_MAX))
148 return 0;
149 if (info != NULL)
150 (*info->fprintf_func) (info->stream, "%s", registertable[regno]->name);
151 return 1;
154 static int
155 tic4x_print_addr (struct disassemble_info *info, unsigned long addr)
157 if (info != NULL)
158 (*info->print_address_func)(addr, info);
159 return 1;
162 static int
163 tic4x_print_relative (struct disassemble_info *info,
164 unsigned long pc,
165 long offset,
166 unsigned long opcode)
168 return tic4x_print_addr (info, pc + offset + tic4x_pc_offset (opcode));
171 static int
172 tic4x_print_direct (struct disassemble_info *info, unsigned long arg)
174 if (info != NULL)
176 (*info->fprintf_func) (info->stream, "@");
177 tic4x_print_addr (info, arg + (tic4x_dp << 16));
179 return 1;
181 #if 0
182 /* FIXME: make the floating point stuff not rely on host
183 floating point arithmetic. */
185 static void
186 tic4x_print_ftoa (unsigned int val, FILE *stream, fprintf_ftype pfunc)
188 int e;
189 int s;
190 int f;
191 double num = 0.0;
193 e = EXTRS (val, 31, 24); /* Exponent. */
194 if (e != -128)
196 s = EXTRU (val, 23, 23); /* Sign bit. */
197 f = EXTRU (val, 22, 0); /* Mantissa. */
198 if (s)
199 f += -2 * (1 << 23);
200 else
201 f += (1 << 23);
202 num = f / (double)(1 << 23);
203 num = ldexp (num, e);
205 (*pfunc)(stream, "%.9g", num);
207 #endif
209 static int
210 tic4x_print_immed (struct disassemble_info *info,
211 immed_t type,
212 unsigned long arg)
214 int s;
215 int f;
216 int e;
217 double num = 0.0;
219 if (info == NULL)
220 return 1;
221 switch (type)
223 case IMMED_SINT:
224 case IMMED_INT:
225 (*info->fprintf_func) (info->stream, "%ld", (long) arg);
226 break;
228 case IMMED_SUINT:
229 case IMMED_UINT:
230 (*info->fprintf_func) (info->stream, "%lu", arg);
231 break;
233 case IMMED_SFLOAT:
234 e = EXTRS (arg, 15, 12);
235 if (e != -8)
237 s = EXTRU (arg, 11, 11);
238 f = EXTRU (arg, 10, 0);
239 if (s)
240 f += -2 * (1 << 11);
241 else
242 f += (1 << 11);
243 num = f / (double)(1 << 11);
244 num = ldexp (num, e);
246 (*info->fprintf_func) (info->stream, "%f", num);
247 break;
248 case IMMED_FLOAT:
249 e = EXTRS (arg, 31, 24);
250 if (e != -128)
252 s = EXTRU (arg, 23, 23);
253 f = EXTRU (arg, 22, 0);
254 if (s)
255 f += -2 * (1 << 23);
256 else
257 f += (1 << 23);
258 num = f / (double)(1 << 23);
259 num = ldexp (num, e);
261 (*info->fprintf_func) (info->stream, "%f", num);
262 break;
264 return 1;
267 static int
268 tic4x_print_cond (struct disassemble_info *info, unsigned int cond)
270 static tic4x_cond_t **condtable = NULL;
271 unsigned int i;
273 if (condtable == NULL)
275 condtable = xmalloc (sizeof (tic4x_cond_t *) * 32);
276 for (i = 0; i < tic4x_num_conds; i++)
277 condtable[tic4x_conds[i].cond] = (tic4x_cond_t *)(tic4x_conds + i);
279 if (cond > 31 || condtable[cond] == NULL)
280 return 0;
281 if (info != NULL)
282 (*info->fprintf_func) (info->stream, "%s", condtable[cond]->name);
283 return 1;
286 static int
287 tic4x_print_indirect (struct disassemble_info *info,
288 indirect_t type,
289 unsigned long arg)
291 unsigned int aregno;
292 unsigned int modn;
293 unsigned int disp;
294 char *a;
296 aregno = 0;
297 modn = 0;
298 disp = 1;
299 switch(type)
301 case INDIRECT_TIC4X: /* *+ARn(disp) */
302 disp = EXTRU (arg, 7, 3);
303 aregno = EXTRU (arg, 2, 0) + REG_AR0;
304 modn = 0;
305 break;
306 case INDIRECT_SHORT:
307 disp = 1;
308 aregno = EXTRU (arg, 2, 0) + REG_AR0;
309 modn = EXTRU (arg, 7, 3);
310 break;
311 case INDIRECT_LONG:
312 disp = EXTRU (arg, 7, 0);
313 aregno = EXTRU (arg, 10, 8) + REG_AR0;
314 modn = EXTRU (arg, 15, 11);
315 if (modn > 7 && disp != 0)
316 return 0;
317 break;
318 default:
319 (*info->fprintf_func)(info->stream, "# internal error: Unknown indirect type %d", type);
320 return 0;
322 if (modn > TIC3X_MODN_MAX)
323 return 0;
324 a = tic4x_indirects[modn].name;
325 while (*a)
327 switch (*a)
329 case 'a':
330 tic4x_print_register (info, aregno);
331 break;
332 case 'd':
333 tic4x_print_immed (info, IMMED_UINT, disp);
334 break;
335 case 'y':
336 tic4x_print_str (info, "ir0");
337 break;
338 case 'z':
339 tic4x_print_str (info, "ir1");
340 break;
341 default:
342 tic4x_print_char (info, *a);
343 break;
345 a++;
347 return 1;
350 static int
351 tic4x_print_op (struct disassemble_info *info,
352 unsigned long instruction,
353 tic4x_inst_t *p,
354 unsigned long pc)
356 int val;
357 char *s;
358 char *parallel = NULL;
360 /* Print instruction name. */
361 s = p->name;
362 while (*s && parallel == NULL)
364 switch (*s)
366 case 'B':
367 if (! tic4x_print_cond (info, EXTRU (instruction, 20, 16)))
368 return 0;
369 break;
370 case 'C':
371 if (! tic4x_print_cond (info, EXTRU (instruction, 27, 23)))
372 return 0;
373 break;
374 case '_':
375 parallel = s + 1; /* Skip past `_' in name. */
376 break;
377 default:
378 tic4x_print_char (info, *s);
379 break;
381 s++;
384 /* Print arguments. */
385 s = p->args;
386 if (*s)
387 tic4x_print_char (info, ' ');
389 while (*s)
391 switch (*s)
393 case '*': /* Indirect 0--15. */
394 if (! tic4x_print_indirect (info, INDIRECT_LONG,
395 EXTRU (instruction, 15, 0)))
396 return 0;
397 break;
399 case '#': /* Only used for ldp, ldpk. */
400 tic4x_print_immed (info, IMMED_UINT, EXTRU (instruction, 15, 0));
401 break;
403 case '@': /* Direct 0--15. */
404 tic4x_print_direct (info, EXTRU (instruction, 15, 0));
405 break;
407 case 'A': /* Address register 24--22. */
408 if (! tic4x_print_register (info, EXTRU (instruction, 24, 22) +
409 REG_AR0))
410 return 0;
411 break;
413 case 'B': /* 24-bit unsigned int immediate br(d)/call/rptb
414 address 0--23. */
415 if (IS_CPU_TIC4X (tic4x_version))
416 tic4x_print_relative (info, pc, EXTRS (instruction, 23, 0),
417 p->opcode);
418 else
419 tic4x_print_addr (info, EXTRU (instruction, 23, 0));
420 break;
422 case 'C': /* Indirect (short C4x) 0--7. */
423 if (! IS_CPU_TIC4X (tic4x_version))
424 return 0;
425 if (! tic4x_print_indirect (info, INDIRECT_TIC4X,
426 EXTRU (instruction, 7, 0)))
427 return 0;
428 break;
430 case 'D':
431 /* Cockup if get here... */
432 break;
434 case 'E': /* Register 0--7. */
435 case 'e':
436 if (! tic4x_print_register (info, EXTRU (instruction, 7, 0)))
437 return 0;
438 break;
440 case 'F': /* 16-bit float immediate 0--15. */
441 tic4x_print_immed (info, IMMED_SFLOAT,
442 EXTRU (instruction, 15, 0));
443 break;
445 case 'i': /* Extended indirect 0--7. */
446 if (EXTRU (instruction, 7, 5) == 7)
448 if (!tic4x_print_register (info, EXTRU (instruction, 4, 0)))
449 return 0;
450 break;
452 /* Fallthrough */
454 case 'I': /* Indirect (short) 0--7. */
455 if (! tic4x_print_indirect (info, INDIRECT_SHORT,
456 EXTRU (instruction, 7, 0)))
457 return 0;
458 break;
460 case 'j': /* Extended indirect 8--15 */
461 if (EXTRU (instruction, 15, 13) == 7)
463 if (! tic4x_print_register (info, EXTRU (instruction, 12, 8)))
464 return 0;
465 break;
468 case 'J': /* Indirect (short) 8--15. */
469 if (! tic4x_print_indirect (info, INDIRECT_SHORT,
470 EXTRU (instruction, 15, 8)))
471 return 0;
472 break;
474 case 'G': /* Register 8--15. */
475 case 'g':
476 if (! tic4x_print_register (info, EXTRU (instruction, 15, 8)))
477 return 0;
478 break;
480 case 'H': /* Register 16--18. */
481 if (! tic4x_print_register (info, EXTRU (instruction, 18, 16)))
482 return 0;
483 break;
485 case 'K': /* Register 19--21. */
486 if (! tic4x_print_register (info, EXTRU (instruction, 21, 19)))
487 return 0;
488 break;
490 case 'L': /* Register 22--24. */
491 if (! tic4x_print_register (info, EXTRU (instruction, 24, 22)))
492 return 0;
493 break;
495 case 'M': /* Register 22--22. */
496 tic4x_print_register (info, EXTRU (instruction, 22, 22) + REG_R2);
497 break;
499 case 'N': /* Register 23--23. */
500 tic4x_print_register (info, EXTRU (instruction, 23, 23) + REG_R0);
501 break;
503 case 'O': /* Indirect (short C4x) 8--15. */
504 if (! IS_CPU_TIC4X (tic4x_version))
505 return 0;
506 if (! tic4x_print_indirect (info, INDIRECT_TIC4X,
507 EXTRU (instruction, 15, 8)))
508 return 0;
509 break;
511 case 'P': /* Displacement 0--15 (used by Bcond and BcondD). */
512 tic4x_print_relative (info, pc, EXTRS (instruction, 15, 0),
513 p->opcode);
514 break;
516 case 'Q': /* Register 0--15. */
517 case 'q':
518 if (! tic4x_print_register (info, EXTRU (instruction, 15, 0)))
519 return 0;
520 break;
522 case 'R': /* Register 16--20. */
523 case 'r':
524 if (! tic4x_print_register (info, EXTRU (instruction, 20, 16)))
525 return 0;
526 break;
528 case 'S': /* 16-bit signed immediate 0--15. */
529 tic4x_print_immed (info, IMMED_SINT,
530 EXTRS (instruction, 15, 0));
531 break;
533 case 'T': /* 5-bit signed immediate 16--20 (C4x stik). */
534 if (! IS_CPU_TIC4X (tic4x_version))
535 return 0;
536 if (! tic4x_print_immed (info, IMMED_SUINT,
537 EXTRU (instruction, 20, 16)))
538 return 0;
539 break;
541 case 'U': /* 16-bit unsigned int immediate 0--15. */
542 tic4x_print_immed (info, IMMED_SUINT, EXTRU (instruction, 15, 0));
543 break;
545 case 'V': /* 5/9-bit unsigned vector 0--4/8. */
546 tic4x_print_immed (info, IMMED_SUINT,
547 IS_CPU_TIC4X (tic4x_version) ?
548 EXTRU (instruction, 8, 0) :
549 EXTRU (instruction, 4, 0) & ~0x20);
550 break;
552 case 'W': /* 8-bit signed immediate 0--7. */
553 if (! IS_CPU_TIC4X (tic4x_version))
554 return 0;
555 tic4x_print_immed (info, IMMED_SINT, EXTRS (instruction, 7, 0));
556 break;
558 case 'X': /* Expansion register 4--0. */
559 val = EXTRU (instruction, 4, 0) + REG_IVTP;
560 if (val < REG_IVTP || val > REG_TVTP)
561 return 0;
562 if (! tic4x_print_register (info, val))
563 return 0;
564 break;
566 case 'Y': /* Address register 16--20. */
567 val = EXTRU (instruction, 20, 16);
568 if (val < REG_AR0 || val > REG_SP)
569 return 0;
570 if (! tic4x_print_register (info, val))
571 return 0;
572 break;
574 case 'Z': /* Expansion register 16--20. */
575 val = EXTRU (instruction, 20, 16) + REG_IVTP;
576 if (val < REG_IVTP || val > REG_TVTP)
577 return 0;
578 if (! tic4x_print_register (info, val))
579 return 0;
580 break;
582 case '|': /* Parallel instruction. */
583 tic4x_print_str (info, " || ");
584 tic4x_print_str (info, parallel);
585 tic4x_print_char (info, ' ');
586 break;
588 case ';':
589 tic4x_print_char (info, ',');
590 break;
592 default:
593 tic4x_print_char (info, *s);
594 break;
596 s++;
598 return 1;
601 static void
602 tic4x_hash_opcode_special (tic4x_inst_t **optable_special,
603 const tic4x_inst_t *inst)
605 int i;
607 for (i = 0;i < TIC4X_SPESOP_SIZE; i++)
608 if (optable_special[i] != NULL
609 && optable_special[i]->opcode == inst->opcode)
611 /* Collision (we have it already) - overwrite. */
612 optable_special[i] = (tic4x_inst_t *) inst;
613 return;
616 for (i = 0; i < TIC4X_SPESOP_SIZE; i++)
617 if (optable_special[i] == NULL)
619 /* Add the new opcode. */
620 optable_special[i] = (tic4x_inst_t *) inst;
621 return;
624 /* This should never occur. This happens if the number of special
625 instructions exceeds TIC4X_SPESOP_SIZE. Please increase the variable
626 of this variable */
627 #if TIC4X_DEBUG
628 printf ("optable_special[] is full, please increase TIC4X_SPESOP_SIZE!\n");
629 #endif
632 static void
633 tic4x_hash_opcode (tic4x_inst_t **optable,
634 tic4x_inst_t **optable_special,
635 const tic4x_inst_t *inst,
636 const unsigned long tic4x_oplevel)
638 int j;
639 int opcode = inst->opcode >> (32 - TIC4X_HASH_SIZE);
640 int opmask = inst->opmask >> (32 - TIC4X_HASH_SIZE);
642 /* Use a TIC4X_HASH_SIZE bit index as a hash index. We should
643 have unique entries so there's no point having a linked list
644 for each entry? */
645 for (j = opcode; j < opmask; j++)
646 if ((j & opmask) == opcode
647 && inst->oplevel & tic4x_oplevel)
649 #if TIC4X_DEBUG
650 /* We should only have collisions for synonyms like
651 ldp for ldi. */
652 if (optable[j] != NULL)
653 printf ("Collision at index %d, %s and %s\n",
654 j, optable[j]->name, inst->name);
655 #endif
656 /* Catch those ops that collide with others already inside the
657 hash, and have a opmask greater than the one we use in the
658 hash. Store them in a special-list, that will handle full
659 32-bit INSN, not only the first 11-bit (or so). */
660 if (optable[j] != NULL
661 && inst->opmask & ~(opmask << (32 - TIC4X_HASH_SIZE)))
663 /* Add the instruction already on the list. */
664 tic4x_hash_opcode_special (optable_special, optable[j]);
666 /* Add the new instruction. */
667 tic4x_hash_opcode_special (optable_special, inst);
670 optable[j] = (tic4x_inst_t *) inst;
674 /* Disassemble the instruction in 'instruction'.
675 'pc' should be the address of this instruction, it will
676 be used to print the target address if this is a relative jump or call
677 the disassembled instruction is written to 'info'.
678 The function returns the length of this instruction in words. */
680 static int
681 tic4x_disassemble (unsigned long pc,
682 unsigned long instruction,
683 struct disassemble_info *info)
685 static tic4x_inst_t **optable = NULL;
686 static tic4x_inst_t **optable_special = NULL;
687 tic4x_inst_t *p;
688 int i;
689 unsigned long tic4x_oplevel;
691 tic4x_version = info->mach;
693 tic4x_oplevel = (IS_CPU_TIC4X (tic4x_version)) ? OP_C4X : 0;
694 tic4x_oplevel |= OP_C3X | OP_LPWR | OP_IDLE2 | OP_ENH;
696 if (optable == NULL)
698 optable = xcalloc (sizeof (tic4x_inst_t *), (1 << TIC4X_HASH_SIZE));
700 optable_special = xcalloc (sizeof (tic4x_inst_t *), TIC4X_SPESOP_SIZE);
702 /* Install opcodes in reverse order so that preferred
703 forms overwrite synonyms. */
704 for (i = tic4x_num_insts - 1; i >= 0; i--)
705 tic4x_hash_opcode (optable, optable_special, &tic4x_insts[i],
706 tic4x_oplevel);
708 /* We now need to remove the insn that are special from the
709 "normal" optable, to make the disasm search this extra list
710 for them. */
711 for (i = 0; i < TIC4X_SPESOP_SIZE; i++)
712 if (optable_special[i] != NULL)
713 optable[optable_special[i]->opcode >> (32 - TIC4X_HASH_SIZE)] = NULL;
716 /* See if we can pick up any loading of the DP register... */
717 if ((instruction >> 16) == 0x5070 || (instruction >> 16) == 0x1f70)
718 tic4x_dp = EXTRU (instruction, 15, 0);
720 p = optable[instruction >> (32 - TIC4X_HASH_SIZE)];
721 if (p != NULL)
723 if (((instruction & p->opmask) == p->opcode)
724 && tic4x_print_op (NULL, instruction, p, pc))
725 tic4x_print_op (info, instruction, p, pc);
726 else
727 (*info->fprintf_func) (info->stream, "%08lx", instruction);
729 else
731 for (i = 0; i<TIC4X_SPESOP_SIZE; i++)
732 if (optable_special[i] != NULL
733 && optable_special[i]->opcode == instruction)
735 (*info->fprintf_func)(info->stream, "%s", optable_special[i]->name);
736 break;
738 if (i == TIC4X_SPESOP_SIZE)
739 (*info->fprintf_func) (info->stream, "%08lx", instruction);
742 /* Return size of insn in words. */
743 return 1;
746 /* The entry point from objdump and gdb. */
748 print_insn_tic4x (bfd_vma memaddr, struct disassemble_info *info)
750 int status;
751 unsigned long pc;
752 unsigned long op;
753 bfd_byte buffer[4];
755 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
756 if (status != 0)
758 (*info->memory_error_func) (status, memaddr, info);
759 return -1;
762 pc = memaddr;
763 op = bfd_getl32 (buffer);
764 info->bytes_per_line = 4;
765 info->bytes_per_chunk = 4;
766 info->octets_per_byte = 4;
767 info->display_endian = BFD_ENDIAN_LITTLE;
768 return tic4x_disassemble (pc, op, info) * 4;