2006-08-10 Andrew Stubbs <andrew.stubbs@st.com>
[deliverable/binutils-gdb.git] / opcodes / tic4x-dis.c
CommitLineData
026df7c5
NC
1/* Print instructions for the Texas TMS320C[34]X, for GDB and GNU Binutils.
2
47b0e7ad 3 Copyright 2002, 2003, 2005 Free Software Foundation, Inc.
026df7c5
NC
4
5 Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz)
47b0e7ad 6
026df7c5
NC
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.
11
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.
16
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
47b0e7ad
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
026df7c5
NC
21
22#include <math.h>
23#include "libiberty.h"
24#include "dis-asm.h"
25#include "opcode/tic4x.h"
26
be33c5dd 27#define TIC4X_DEBUG 0
026df7c5 28
be33c5dd 29#define TIC4X_HASH_SIZE 11 /* 11 (bits) and above should give unique entries. */
47b0e7ad 30#define TIC4X_SPESOP_SIZE 8 /* Max 8. ops for special instructions. */
026df7c5
NC
31
32typedef enum
47b0e7ad
NC
33{
34 IMMED_SINT,
35 IMMED_SUINT,
36 IMMED_SFLOAT,
37 IMMED_INT,
38 IMMED_UINT,
39 IMMED_FLOAT
40}
026df7c5
NC
41immed_t;
42
43typedef enum
47b0e7ad
NC
44{
45 INDIRECT_SHORT,
46 INDIRECT_LONG,
47 INDIRECT_TIC4X
48}
026df7c5
NC
49indirect_t;
50
be33c5dd
SS
51static int tic4x_version = 0;
52static int tic4x_dp = 0;
026df7c5
NC
53
54static int
47b0e7ad 55tic4x_pc_offset (unsigned int op)
026df7c5
NC
56{
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)
61 {
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;
75 }
47b0e7ad 76
026df7c5
NC
77 switch ((op & 0xffe00000) >> 20)
78 {
79 case 0x6a0: /* bB */
80 case 0x720: /* callB */
81 case 0x740: /* trapB */
82 return 1;
47b0e7ad 83
026df7c5
NC
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;
47b0e7ad 91
026df7c5
NC
92 default:
93 break;
94 }
47b0e7ad 95
026df7c5
NC
96 switch ((op & 0xfe200000) >> 20)
97 {
98 case 0x6e0: /* dbB */
99 return 1;
47b0e7ad 100
026df7c5
NC
101 case 0x6e2: /* dbBd */
102 return 3;
47b0e7ad 103
026df7c5
NC
104 default:
105 break;
106 }
47b0e7ad 107
026df7c5
NC
108 return 0;
109}
110
111static int
47b0e7ad 112tic4x_print_char (struct disassemble_info * info, char ch)
026df7c5
NC
113{
114 if (info != NULL)
115 (*info->fprintf_func) (info->stream, "%c", ch);
116 return 1;
117}
118
119static int
47b0e7ad 120tic4x_print_str (struct disassemble_info *info, char *str)
026df7c5
NC
121{
122 if (info != NULL)
123 (*info->fprintf_func) (info->stream, "%s", str);
124 return 1;
125}
126
127static int
47b0e7ad 128tic4x_print_register (struct disassemble_info *info, unsigned long regno)
026df7c5 129{
47b0e7ad 130 static tic4x_register_t ** registertable = NULL;
026df7c5 131 unsigned int i;
47b0e7ad 132
026df7c5
NC
133 if (registertable == NULL)
134 {
47b0e7ad 135 registertable = xmalloc (sizeof (tic4x_register_t *) * REG_TABLE_SIZE);
be33c5dd 136 for (i = 0; i < tic3x_num_registers; i++)
47b0e7ad 137 registertable[tic3x_registers[i].regno] = (tic4x_register_t *) (tic3x_registers + i);
be33c5dd 138 if (IS_CPU_TIC4X (tic4x_version))
026df7c5
NC
139 {
140 /* Add C4x additional registers, overwriting
141 any C3x registers if necessary. */
be33c5dd 142 for (i = 0; i < tic4x_num_registers; i++)
47b0e7ad
NC
143 registertable[tic4x_registers[i].regno] =
144 (tic4x_register_t *)(tic4x_registers + i);
026df7c5
NC
145 }
146 }
be33c5dd 147 if ((int) regno > (IS_CPU_TIC4X (tic4x_version) ? TIC4X_REG_MAX : TIC3X_REG_MAX))
026df7c5
NC
148 return 0;
149 if (info != NULL)
150 (*info->fprintf_func) (info->stream, "%s", registertable[regno]->name);
151 return 1;
152}
153
154static int
47b0e7ad 155tic4x_print_addr (struct disassemble_info *info, unsigned long addr)
026df7c5
NC
156{
157 if (info != NULL)
158 (*info->print_address_func)(addr, info);
159 return 1;
160}
161
162static int
47b0e7ad
NC
163tic4x_print_relative (struct disassemble_info *info,
164 unsigned long pc,
165 long offset,
166 unsigned long opcode)
026df7c5 167{
be33c5dd 168 return tic4x_print_addr (info, pc + offset + tic4x_pc_offset (opcode));
026df7c5
NC
169}
170
171static int
47b0e7ad 172tic4x_print_direct (struct disassemble_info *info, unsigned long arg)
026df7c5
NC
173{
174 if (info != NULL)
175 {
176 (*info->fprintf_func) (info->stream, "@");
be33c5dd 177 tic4x_print_addr (info, arg + (tic4x_dp << 16));
026df7c5
NC
178 }
179 return 1;
180}
47b0e7ad 181#if 0
026df7c5
NC
182/* FIXME: make the floating point stuff not rely on host
183 floating point arithmetic. */
47b0e7ad
NC
184
185static void
186tic4x_print_ftoa (unsigned int val, FILE *stream, fprintf_ftype pfunc)
026df7c5
NC
187{
188 int e;
189 int s;
190 int f;
191 double num = 0.0;
47b0e7ad
NC
192
193 e = EXTRS (val, 31, 24); /* Exponent. */
026df7c5
NC
194 if (e != -128)
195 {
47b0e7ad
NC
196 s = EXTRU (val, 23, 23); /* Sign bit. */
197 f = EXTRU (val, 22, 0); /* Mantissa. */
026df7c5
NC
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);
47b0e7ad 204 }
026df7c5
NC
205 (*pfunc)(stream, "%.9g", num);
206}
47b0e7ad 207#endif
026df7c5
NC
208
209static int
47b0e7ad
NC
210tic4x_print_immed (struct disassemble_info *info,
211 immed_t type,
212 unsigned long arg)
026df7c5
NC
213{
214 int s;
215 int f;
216 int e;
217 double num = 0.0;
47b0e7ad 218
026df7c5
NC
219 if (info == NULL)
220 return 1;
221 switch (type)
222 {
223 case IMMED_SINT:
224 case IMMED_INT:
0fd3a477 225 (*info->fprintf_func) (info->stream, "%ld", (long) arg);
026df7c5 226 break;
47b0e7ad 227
026df7c5
NC
228 case IMMED_SUINT:
229 case IMMED_UINT:
0fd3a477 230 (*info->fprintf_func) (info->stream, "%lu", arg);
026df7c5 231 break;
47b0e7ad 232
026df7c5
NC
233 case IMMED_SFLOAT:
234 e = EXTRS (arg, 15, 12);
235 if (e != -8)
236 {
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);
245 }
246 (*info->fprintf_func) (info->stream, "%f", num);
247 break;
248 case IMMED_FLOAT:
249 e = EXTRS (arg, 31, 24);
250 if (e != -128)
251 {
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);
260 }
261 (*info->fprintf_func) (info->stream, "%f", num);
262 break;
263 }
264 return 1;
265}
266
267static int
47b0e7ad 268tic4x_print_cond (struct disassemble_info *info, unsigned int cond)
026df7c5 269{
be33c5dd 270 static tic4x_cond_t **condtable = NULL;
026df7c5 271 unsigned int i;
47b0e7ad 272
026df7c5
NC
273 if (condtable == NULL)
274 {
47b0e7ad 275 condtable = xmalloc (sizeof (tic4x_cond_t *) * 32);
be33c5dd 276 for (i = 0; i < tic4x_num_conds; i++)
47b0e7ad 277 condtable[tic4x_conds[i].cond] = (tic4x_cond_t *)(tic4x_conds + i);
026df7c5
NC
278 }
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;
284}
285
286static int
47b0e7ad
NC
287tic4x_print_indirect (struct disassemble_info *info,
288 indirect_t type,
289 unsigned long arg)
026df7c5
NC
290{
291 unsigned int aregno;
292 unsigned int modn;
293 unsigned int disp;
294 char *a;
295
296 aregno = 0;
297 modn = 0;
298 disp = 1;
299 switch(type)
300 {
be33c5dd 301 case INDIRECT_TIC4X: /* *+ARn(disp) */
026df7c5
NC
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:
be33c5dd
SS
319 (*info->fprintf_func)(info->stream, "# internal error: Unknown indirect type %d", type);
320 return 0;
026df7c5 321 }
be33c5dd 322 if (modn > TIC3X_MODN_MAX)
026df7c5 323 return 0;
be33c5dd 324 a = tic4x_indirects[modn].name;
026df7c5
NC
325 while (*a)
326 {
327 switch (*a)
328 {
329 case 'a':
be33c5dd 330 tic4x_print_register (info, aregno);
026df7c5
NC
331 break;
332 case 'd':
be33c5dd 333 tic4x_print_immed (info, IMMED_UINT, disp);
026df7c5
NC
334 break;
335 case 'y':
be33c5dd 336 tic4x_print_str (info, "ir0");
026df7c5
NC
337 break;
338 case 'z':
be33c5dd 339 tic4x_print_str (info, "ir1");
026df7c5
NC
340 break;
341 default:
be33c5dd 342 tic4x_print_char (info, *a);
026df7c5
NC
343 break;
344 }
345 a++;
346 }
347 return 1;
348}
349
350static int
47b0e7ad
NC
351tic4x_print_op (struct disassemble_info *info,
352 unsigned long instruction,
353 tic4x_inst_t *p,
354 unsigned long pc)
026df7c5
NC
355{
356 int val;
357 char *s;
358 char *parallel = NULL;
359
360 /* Print instruction name. */
361 s = p->name;
362 while (*s && parallel == NULL)
363 {
364 switch (*s)
365 {
366 case 'B':
be33c5dd 367 if (! tic4x_print_cond (info, EXTRU (instruction, 20, 16)))
026df7c5
NC
368 return 0;
369 break;
370 case 'C':
be33c5dd 371 if (! tic4x_print_cond (info, EXTRU (instruction, 27, 23)))
026df7c5
NC
372 return 0;
373 break;
374 case '_':
47b0e7ad 375 parallel = s + 1; /* Skip past `_' in name. */
026df7c5
NC
376 break;
377 default:
be33c5dd 378 tic4x_print_char (info, *s);
026df7c5
NC
379 break;
380 }
381 s++;
382 }
47b0e7ad 383
026df7c5
NC
384 /* Print arguments. */
385 s = p->args;
386 if (*s)
be33c5dd 387 tic4x_print_char (info, ' ');
026df7c5
NC
388
389 while (*s)
390 {
391 switch (*s)
392 {
47b0e7ad 393 case '*': /* Indirect 0--15. */
be33c5dd 394 if (! tic4x_print_indirect (info, INDIRECT_LONG,
47b0e7ad 395 EXTRU (instruction, 15, 0)))
026df7c5
NC
396 return 0;
397 break;
398
47b0e7ad 399 case '#': /* Only used for ldp, ldpk. */
be33c5dd 400 tic4x_print_immed (info, IMMED_UINT, EXTRU (instruction, 15, 0));
026df7c5
NC
401 break;
402
47b0e7ad 403 case '@': /* Direct 0--15. */
be33c5dd 404 tic4x_print_direct (info, EXTRU (instruction, 15, 0));
026df7c5
NC
405 break;
406
47b0e7ad 407 case 'A': /* Address register 24--22. */
be33c5dd 408 if (! tic4x_print_register (info, EXTRU (instruction, 24, 22) +
47b0e7ad 409 REG_AR0))
026df7c5
NC
410 return 0;
411 break;
412
413 case 'B': /* 24-bit unsigned int immediate br(d)/call/rptb
414 address 0--23. */
be33c5dd
SS
415 if (IS_CPU_TIC4X (tic4x_version))
416 tic4x_print_relative (info, pc, EXTRS (instruction, 23, 0),
47b0e7ad 417 p->opcode);
026df7c5 418 else
be33c5dd 419 tic4x_print_addr (info, EXTRU (instruction, 23, 0));
026df7c5
NC
420 break;
421
47b0e7ad 422 case 'C': /* Indirect (short C4x) 0--7. */
be33c5dd 423 if (! IS_CPU_TIC4X (tic4x_version))
026df7c5 424 return 0;
be33c5dd 425 if (! tic4x_print_indirect (info, INDIRECT_TIC4X,
47b0e7ad 426 EXTRU (instruction, 7, 0)))
026df7c5
NC
427 return 0;
428 break;
429
430 case 'D':
431 /* Cockup if get here... */
432 break;
433
47b0e7ad 434 case 'E': /* Register 0--7. */
44287f60 435 case 'e':
be33c5dd 436 if (! tic4x_print_register (info, EXTRU (instruction, 7, 0)))
026df7c5
NC
437 return 0;
438 break;
439
47b0e7ad 440 case 'F': /* 16-bit float immediate 0--15. */
be33c5dd 441 tic4x_print_immed (info, IMMED_SFLOAT,
47b0e7ad 442 EXTRU (instruction, 15, 0));
026df7c5
NC
443 break;
444
47b0e7ad
NC
445 case 'i': /* Extended indirect 0--7. */
446 if (EXTRU (instruction, 7, 5) == 7)
9c87d6c7 447 {
47b0e7ad 448 if (!tic4x_print_register (info, EXTRU (instruction, 4, 0)))
9c87d6c7
SS
449 return 0;
450 break;
451 }
452 /* Fallthrough */
453
47b0e7ad 454 case 'I': /* Indirect (short) 0--7. */
be33c5dd 455 if (! tic4x_print_indirect (info, INDIRECT_SHORT,
47b0e7ad 456 EXTRU (instruction, 7, 0)))
026df7c5
NC
457 return 0;
458 break;
459
9c87d6c7 460 case 'j': /* Extended indirect 8--15 */
47b0e7ad 461 if (EXTRU (instruction, 15, 13) == 7)
9c87d6c7 462 {
47b0e7ad 463 if (! tic4x_print_register (info, EXTRU (instruction, 12, 8)))
9c87d6c7
SS
464 return 0;
465 break;
466 }
467
47b0e7ad 468 case 'J': /* Indirect (short) 8--15. */
be33c5dd 469 if (! tic4x_print_indirect (info, INDIRECT_SHORT,
47b0e7ad 470 EXTRU (instruction, 15, 8)))
026df7c5
NC
471 return 0;
472 break;
473
47b0e7ad 474 case 'G': /* Register 8--15. */
44287f60 475 case 'g':
be33c5dd 476 if (! tic4x_print_register (info, EXTRU (instruction, 15, 8)))
026df7c5
NC
477 return 0;
478 break;
479
47b0e7ad 480 case 'H': /* Register 16--18. */
be33c5dd 481 if (! tic4x_print_register (info, EXTRU (instruction, 18, 16)))
026df7c5
NC
482 return 0;
483 break;
484
47b0e7ad 485 case 'K': /* Register 19--21. */
be33c5dd 486 if (! tic4x_print_register (info, EXTRU (instruction, 21, 19)))
026df7c5
NC
487 return 0;
488 break;
489
47b0e7ad 490 case 'L': /* Register 22--24. */
be33c5dd 491 if (! tic4x_print_register (info, EXTRU (instruction, 24, 22)))
026df7c5
NC
492 return 0;
493 break;
494
47b0e7ad 495 case 'M': /* Register 22--22. */
be33c5dd 496 tic4x_print_register (info, EXTRU (instruction, 22, 22) + REG_R2);
026df7c5
NC
497 break;
498
47b0e7ad 499 case 'N': /* Register 23--23. */
be33c5dd 500 tic4x_print_register (info, EXTRU (instruction, 23, 23) + REG_R0);
026df7c5
NC
501 break;
502
47b0e7ad 503 case 'O': /* Indirect (short C4x) 8--15. */
be33c5dd 504 if (! IS_CPU_TIC4X (tic4x_version))
026df7c5 505 return 0;
be33c5dd 506 if (! tic4x_print_indirect (info, INDIRECT_TIC4X,
47b0e7ad 507 EXTRU (instruction, 15, 8)))
026df7c5
NC
508 return 0;
509 break;
510
47b0e7ad 511 case 'P': /* Displacement 0--15 (used by Bcond and BcondD). */
be33c5dd 512 tic4x_print_relative (info, pc, EXTRS (instruction, 15, 0),
47b0e7ad 513 p->opcode);
026df7c5
NC
514 break;
515
47b0e7ad 516 case 'Q': /* Register 0--15. */
44287f60 517 case 'q':
be33c5dd 518 if (! tic4x_print_register (info, EXTRU (instruction, 15, 0)))
026df7c5
NC
519 return 0;
520 break;
521
47b0e7ad 522 case 'R': /* Register 16--20. */
44287f60 523 case 'r':
be33c5dd 524 if (! tic4x_print_register (info, EXTRU (instruction, 20, 16)))
026df7c5
NC
525 return 0;
526 break;
527
47b0e7ad 528 case 'S': /* 16-bit signed immediate 0--15. */
be33c5dd 529 tic4x_print_immed (info, IMMED_SINT,
47b0e7ad 530 EXTRS (instruction, 15, 0));
026df7c5
NC
531 break;
532
47b0e7ad 533 case 'T': /* 5-bit signed immediate 16--20 (C4x stik). */
be33c5dd 534 if (! IS_CPU_TIC4X (tic4x_version))
026df7c5 535 return 0;
be33c5dd 536 if (! tic4x_print_immed (info, IMMED_SUINT,
47b0e7ad 537 EXTRU (instruction, 20, 16)))
026df7c5
NC
538 return 0;
539 break;
540
47b0e7ad 541 case 'U': /* 16-bit unsigned int immediate 0--15. */
be33c5dd 542 tic4x_print_immed (info, IMMED_SUINT, EXTRU (instruction, 15, 0));
026df7c5
NC
543 break;
544
47b0e7ad 545 case 'V': /* 5/9-bit unsigned vector 0--4/8. */
be33c5dd 546 tic4x_print_immed (info, IMMED_SUINT,
47b0e7ad
NC
547 IS_CPU_TIC4X (tic4x_version) ?
548 EXTRU (instruction, 8, 0) :
549 EXTRU (instruction, 4, 0) & ~0x20);
026df7c5
NC
550 break;
551
47b0e7ad 552 case 'W': /* 8-bit signed immediate 0--7. */
be33c5dd 553 if (! IS_CPU_TIC4X (tic4x_version))
026df7c5 554 return 0;
be33c5dd 555 tic4x_print_immed (info, IMMED_SINT, EXTRS (instruction, 7, 0));
026df7c5
NC
556 break;
557
47b0e7ad 558 case 'X': /* Expansion register 4--0. */
026df7c5
NC
559 val = EXTRU (instruction, 4, 0) + REG_IVTP;
560 if (val < REG_IVTP || val > REG_TVTP)
561 return 0;
be33c5dd 562 if (! tic4x_print_register (info, val))
026df7c5
NC
563 return 0;
564 break;
565
47b0e7ad 566 case 'Y': /* Address register 16--20. */
026df7c5
NC
567 val = EXTRU (instruction, 20, 16);
568 if (val < REG_AR0 || val > REG_SP)
569 return 0;
be33c5dd 570 if (! tic4x_print_register (info, val))
026df7c5
NC
571 return 0;
572 break;
573
47b0e7ad 574 case 'Z': /* Expansion register 16--20. */
026df7c5
NC
575 val = EXTRU (instruction, 20, 16) + REG_IVTP;
576 if (val < REG_IVTP || val > REG_TVTP)
577 return 0;
be33c5dd 578 if (! tic4x_print_register (info, val))
026df7c5
NC
579 return 0;
580 break;
581
47b0e7ad 582 case '|': /* Parallel instruction. */
be33c5dd
SS
583 tic4x_print_str (info, " || ");
584 tic4x_print_str (info, parallel);
585 tic4x_print_char (info, ' ');
026df7c5
NC
586 break;
587
588 case ';':
be33c5dd 589 tic4x_print_char (info, ',');
026df7c5
NC
590 break;
591
592 default:
be33c5dd 593 tic4x_print_char (info, *s);
026df7c5
NC
594 break;
595 }
596 s++;
597 }
598 return 1;
599}
600
601static void
47b0e7ad
NC
602tic4x_hash_opcode_special (tic4x_inst_t **optable_special,
603 const tic4x_inst_t *inst)
9c87d6c7
SS
604{
605 int i;
606
47b0e7ad
NC
607 for (i = 0;i < TIC4X_SPESOP_SIZE; i++)
608 if (optable_special[i] != NULL
609 && optable_special[i]->opcode == inst->opcode)
9c87d6c7 610 {
47b0e7ad
NC
611 /* Collision (we have it already) - overwrite. */
612 optable_special[i] = (tic4x_inst_t *) inst;
9c87d6c7
SS
613 return;
614 }
615
47b0e7ad
NC
616 for (i = 0; i < TIC4X_SPESOP_SIZE; i++)
617 if (optable_special[i] == NULL)
9c87d6c7 618 {
47b0e7ad
NC
619 /* Add the new opcode. */
620 optable_special[i] = (tic4x_inst_t *) inst;
9c87d6c7
SS
621 return;
622 }
623
624 /* This should never occur. This happens if the number of special
be33c5dd 625 instructions exceeds TIC4X_SPESOP_SIZE. Please increase the variable
9c87d6c7 626 of this variable */
be33c5dd 627#if TIC4X_DEBUG
47b0e7ad 628 printf ("optable_special[] is full, please increase TIC4X_SPESOP_SIZE!\n");
9c87d6c7
SS
629#endif
630}
631
632static void
47b0e7ad
NC
633tic4x_hash_opcode (tic4x_inst_t **optable,
634 tic4x_inst_t **optable_special,
635 const tic4x_inst_t *inst,
636 const unsigned long tic4x_oplevel)
026df7c5
NC
637{
638 int j;
be33c5dd
SS
639 int opcode = inst->opcode >> (32 - TIC4X_HASH_SIZE);
640 int opmask = inst->opmask >> (32 - TIC4X_HASH_SIZE);
47b0e7ad 641
be33c5dd 642 /* Use a TIC4X_HASH_SIZE bit index as a hash index. We should
026df7c5 643 have unique entries so there's no point having a linked list
47b0e7ad 644 for each entry? */
026df7c5 645 for (j = opcode; j < opmask; j++)
47b0e7ad
NC
646 if ((j & opmask) == opcode
647 && inst->oplevel & tic4x_oplevel)
026df7c5 648 {
be33c5dd 649#if TIC4X_DEBUG
026df7c5
NC
650 /* We should only have collisions for synonyms like
651 ldp for ldi. */
652 if (optable[j] != NULL)
47b0e7ad
NC
653 printf ("Collision at index %d, %s and %s\n",
654 j, optable[j]->name, inst->name);
026df7c5 655#endif
9c87d6c7
SS
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). */
47b0e7ad
NC
660 if (optable[j] != NULL
661 && inst->opmask & ~(opmask << (32 - TIC4X_HASH_SIZE)))
9c87d6c7 662 {
47b0e7ad
NC
663 /* Add the instruction already on the list. */
664 tic4x_hash_opcode_special (optable_special, optable[j]);
9c87d6c7 665
47b0e7ad
NC
666 /* Add the new instruction. */
667 tic4x_hash_opcode_special (optable_special, inst);
9c87d6c7
SS
668 }
669
47b0e7ad 670 optable[j] = (tic4x_inst_t *) inst;
026df7c5
NC
671 }
672}
673
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. */
679
680static int
47b0e7ad
NC
681tic4x_disassemble (unsigned long pc,
682 unsigned long instruction,
683 struct disassemble_info *info)
026df7c5 684{
be33c5dd
SS
685 static tic4x_inst_t **optable = NULL;
686 static tic4x_inst_t **optable_special = NULL;
687 tic4x_inst_t *p;
026df7c5 688 int i;
be33c5dd 689 unsigned long tic4x_oplevel;
47b0e7ad 690
be33c5dd 691 tic4x_version = info->mach;
9c87d6c7 692
be33c5dd 693 tic4x_oplevel = (IS_CPU_TIC4X (tic4x_version)) ? OP_C4X : 0;
47b0e7ad
NC
694 tic4x_oplevel |= OP_C3X | OP_LPWR | OP_IDLE2 | OP_ENH;
695
026df7c5
NC
696 if (optable == NULL)
697 {
47b0e7ad 698 optable = xcalloc (sizeof (tic4x_inst_t *), (1 << TIC4X_HASH_SIZE));
9c87d6c7 699
47b0e7ad 700 optable_special = xcalloc (sizeof (tic4x_inst_t *), TIC4X_SPESOP_SIZE);
9c87d6c7 701
026df7c5
NC
702 /* Install opcodes in reverse order so that preferred
703 forms overwrite synonyms. */
be33c5dd 704 for (i = tic4x_num_insts - 1; i >= 0; i--)
47b0e7ad
NC
705 tic4x_hash_opcode (optable, optable_special, &tic4x_insts[i],
706 tic4x_oplevel);
9c87d6c7
SS
707
708 /* We now need to remove the insn that are special from the
709 "normal" optable, to make the disasm search this extra list
47b0e7ad
NC
710 for them. */
711 for (i = 0; i < TIC4X_SPESOP_SIZE; i++)
712 if (optable_special[i] != NULL)
be33c5dd 713 optable[optable_special[i]->opcode >> (32 - TIC4X_HASH_SIZE)] = NULL;
026df7c5 714 }
47b0e7ad 715
026df7c5
NC
716 /* See if we can pick up any loading of the DP register... */
717 if ((instruction >> 16) == 0x5070 || (instruction >> 16) == 0x1f70)
be33c5dd 718 tic4x_dp = EXTRU (instruction, 15, 0);
9c87d6c7 719
be33c5dd 720 p = optable[instruction >> (32 - TIC4X_HASH_SIZE)];
47b0e7ad 721 if (p != NULL)
9c87d6c7 722 {
47b0e7ad
NC
723 if (((instruction & p->opmask) == p->opcode)
724 && tic4x_print_op (NULL, instruction, p, pc))
be33c5dd 725 tic4x_print_op (info, instruction, p, pc);
9c87d6c7 726 else
0fd3a477 727 (*info->fprintf_func) (info->stream, "%08lx", instruction);
9c87d6c7 728 }
026df7c5 729 else
9c87d6c7 730 {
be33c5dd 731 for (i = 0; i<TIC4X_SPESOP_SIZE; i++)
9c87d6c7 732 if (optable_special[i] != NULL
47b0e7ad 733 && optable_special[i]->opcode == instruction)
9c87d6c7
SS
734 {
735 (*info->fprintf_func)(info->stream, "%s", optable_special[i]->name);
736 break;
737 }
47b0e7ad 738 if (i == TIC4X_SPESOP_SIZE)
0fd3a477 739 (*info->fprintf_func) (info->stream, "%08lx", instruction);
9c87d6c7 740 }
026df7c5
NC
741
742 /* Return size of insn in words. */
47b0e7ad 743 return 1;
026df7c5
NC
744}
745
746/* The entry point from objdump and gdb. */
747int
47b0e7ad 748print_insn_tic4x (bfd_vma memaddr, struct disassemble_info *info)
026df7c5
NC
749{
750 int status;
751 unsigned long pc;
752 unsigned long op;
753 bfd_byte buffer[4];
47b0e7ad 754
026df7c5
NC
755 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
756 if (status != 0)
757 {
758 (*info->memory_error_func) (status, memaddr, info);
759 return -1;
760 }
47b0e7ad 761
026df7c5
NC
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;
be33c5dd 768 return tic4x_disassemble (pc, op, info) * 4;
026df7c5 769}
This page took 0.225807 seconds and 4 git commands to generate.