Coverage Report

Created: 2026-07-12 09:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/tic4x-dis.c
Line
Count
Source
1
/* Print instructions for the Texas TMS320C[34]X, for GDB and GNU Binutils.
2
3
   Copyright (C) 2002-2026 Free Software Foundation, Inc.
4
5
   Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz)
6
7
   This file is part of the GNU opcodes library.
8
9
   This library is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3, or (at your option)
12
   any later version.
13
14
   It is distributed in the hope that it will be useful, but WITHOUT
15
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17
   License for more details.
18
19
   You should have received a copy of the GNU General Public License
20
   along with this program; if not, write to the Free Software
21
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22
   MA 02110-1301, USA.  */
23
24
#include "sysdep.h"
25
#include <math.h>
26
#include "libiberty.h"
27
#include "disassemble.h"
28
#include "opcode/tic4x.h"
29
30
#define TIC4X_DEBUG 0
31
32
1.11M
#define TIC4X_HASH_SIZE   11   /* 11 (bits) and above should give unique entries.  */
33
522k
#define TIC4X_SPESOP_SIZE 8    /* Max 8. ops for special instructions.  */
34
35
typedef enum
36
{
37
  IMMED_SINT,
38
  IMMED_SUINT,
39
  IMMED_SFLOAT,
40
  IMMED_INT,
41
  IMMED_UINT,
42
  IMMED_FLOAT
43
}
44
immed_t;
45
46
typedef enum
47
{
48
  INDIRECT_SHORT,
49
  INDIRECT_LONG,
50
  INDIRECT_TIC4X
51
}
52
indirect_t;
53
54
static unsigned long tic4x_version = 0;
55
static unsigned int tic4x_dp = 0;
56
static tic4x_inst_t **optab = NULL;
57
static tic4x_inst_t **optab_special = NULL;
58
static const char *registernames[REG_TABLE_SIZE];
59
60
static int
61
tic4x_pc_offset (unsigned int op)
62
15.4k
{
63
  /* Determine the PC offset for a C[34]x instruction.
64
     This could be simplified using some boolean algebra
65
     but at the expense of readability.  */
66
15.4k
  switch (op >> 24)
67
15.4k
    {
68
1.10k
    case 0x60:  /* br */
69
2.52k
    case 0x62:  /* call  (C4x) */
70
4.90k
    case 0x64:  /* rptb  (C4x) */
71
4.90k
      return 1;
72
1.63k
    case 0x61:  /* brd */
73
2.13k
    case 0x63:  /* laj */
74
3.17k
    case 0x65:  /* rptbd (C4x) */
75
3.17k
      return 3;
76
0
    case 0x66:  /* swi */
77
0
    case 0x67:
78
0
      return 0;
79
7.33k
    default:
80
7.33k
      break;
81
15.4k
    }
82
83
7.33k
  switch ((op & 0xffe00000) >> 20)
84
7.33k
    {
85
728
    case 0x6a0: /* bB */
86
1.22k
    case 0x720: /* callB */
87
1.22k
    case 0x740: /* trapB */
88
1.22k
      return 1;
89
90
352
    case 0x6a2: /* bBd */
91
1.94k
    case 0x6a6: /* bBat */
92
2.31k
    case 0x6aa: /* bBaf */
93
3.07k
    case 0x722: /* lajB */
94
3.07k
    case 0x748: /* latB */
95
3.07k
    case 0x798: /* rptbd */
96
3.07k
      return 3;
97
98
3.03k
    default:
99
3.03k
      break;
100
7.33k
    }
101
102
3.03k
  switch ((op & 0xfe200000) >> 20)
103
3.03k
    {
104
910
    case 0x6e0: /* dbB */
105
910
      return 1;
106
107
2.12k
    case 0x6e2: /* dbBd */
108
2.12k
      return 3;
109
110
0
    default:
111
0
      break;
112
3.03k
    }
113
114
0
  return 0;
115
3.03k
}
116
117
static int
118
tic4x_print_char (struct disassemble_info * info, char ch)
119
1.65M
{
120
1.65M
  if (info != NULL)
121
696k
    (*info->fprintf_func) (info->stream, "%c", ch);
122
1.65M
  return 1;
123
1.65M
}
124
125
static int
126
tic4x_print_str (struct disassemble_info *info, const char *str)
127
138k
{
128
138k
  if (info != NULL)
129
64.1k
    (*info->fprintf_func) (info->stream, "%s", str);
130
138k
  return 1;
131
138k
}
132
133
static int
134
tic4x_print_register (struct disassemble_info *info, unsigned long regno)
135
398k
{
136
398k
  unsigned int i;
137
138
398k
  if (registernames[REG_R0] == NULL)
139
188
    {
140
6.95k
      for (i = 0; i < tic3x_num_registers; i++)
141
6.76k
  registernames[tic3x_registers[i].regno] = tic3x_registers[i].name;
142
188
      if (IS_CPU_TIC4X (tic4x_version))
143
140
  {
144
    /* Add C4x additional registers, overwriting
145
       any C3x registers if necessary.  */
146
1.96k
    for (i = 0; i < tic4x_num_registers; i++)
147
1.82k
      registernames[tic4x_registers[i].regno] = tic4x_registers[i].name;
148
140
  }
149
188
    }
150
398k
  if (regno > (IS_CPU_TIC4X (tic4x_version) ? TIC4X_REG_MAX : TIC3X_REG_MAX))
151
29.0k
    return 0;
152
368k
  if (info != NULL)
153
173k
    (*info->fprintf_func) (info->stream, "%s", registernames[regno]);
154
368k
  return 1;
155
398k
}
156
157
static int
158
tic4x_print_addr (struct disassemble_info *info, unsigned long addr)
159
26.3k
{
160
26.3k
  if (info != NULL)
161
17.9k
    (*info->print_address_func)(addr, info);
162
26.3k
  return 1;
163
26.3k
}
164
165
static int
166
tic4x_print_relative (struct disassemble_info *info,
167
          unsigned long pc,
168
          long offset,
169
          unsigned long opcode)
170
15.4k
{
171
15.4k
  return tic4x_print_addr (info, pc + offset + tic4x_pc_offset (opcode));
172
15.4k
}
173
174
static int
175
tic4x_print_direct (struct disassemble_info *info, unsigned long arg)
176
19.4k
{
177
19.4k
  if (info != NULL)
178
9.62k
    {
179
9.62k
      (*info->fprintf_func) (info->stream, "@");
180
9.62k
      tic4x_print_addr (info, arg + (tic4x_dp << 16));
181
9.62k
    }
182
19.4k
  return 1;
183
19.4k
}
184
#if 0
185
/* FIXME: make the floating point stuff not rely on host
186
   floating point arithmetic.  */
187
188
static void
189
tic4x_print_ftoa (unsigned int val, FILE *stream, fprintf_ftype pfunc)
190
{
191
  int e;
192
  int s;
193
  int f;
194
  double num = 0.0;
195
196
  e = EXTRS (val, 31, 24);  /* Exponent.  */
197
  if (e != -128)
198
    {
199
      s = EXTRU (val, 23, 23);  /* Sign bit.  */
200
      f = EXTRU (val, 22, 0); /* Mantissa.  */
201
      if (s)
202
  f += -2 * (1 << 23);
203
      else
204
  f += (1 << 23);
205
      num = f / (double)(1 << 23);
206
      num = ldexp (num, e);
207
    }
208
  (*pfunc)(stream, "%.9g", num);
209
}
210
#endif
211
212
static int
213
tic4x_print_immed (struct disassemble_info *info,
214
       immed_t type,
215
       unsigned long arg)
216
76.4k
{
217
76.4k
  int s;
218
76.4k
  int f;
219
76.4k
  int e;
220
76.4k
  double num = 0.0;
221
222
76.4k
  if (info == NULL)
223
41.1k
    return 1;
224
35.2k
  switch (type)
225
35.2k
    {
226
4.94k
    case IMMED_SINT:
227
4.94k
    case IMMED_INT:
228
4.94k
      (*info->fprintf_func) (info->stream, "%ld", (long) arg);
229
4.94k
      break;
230
231
3.37k
    case IMMED_SUINT:
232
26.1k
    case IMMED_UINT:
233
26.1k
      (*info->fprintf_func) (info->stream, "%lu", arg);
234
26.1k
      break;
235
236
4.23k
    case IMMED_SFLOAT:
237
4.23k
      e = EXTRS (arg, 15, 12);
238
4.23k
      if (e != -8)
239
4.04k
  {
240
4.04k
    s = EXTRU (arg, 11, 11);
241
4.04k
    f = EXTRU (arg, 10, 0);
242
4.04k
    if (s)
243
1.78k
      f += -2 * (1 << 11);
244
2.25k
    else
245
2.25k
      f += (1 << 11);
246
4.04k
    num = f / (double)(1 << 11);
247
4.04k
    num = ldexp (num, e);
248
4.04k
  }
249
4.23k
      (*info->fprintf_func) (info->stream, "%f", num);
250
4.23k
      break;
251
0
    case IMMED_FLOAT:
252
0
      e = EXTRS (arg, 31, 24);
253
0
      if (e != -128)
254
0
  {
255
0
    s = EXTRU (arg, 23, 23);
256
0
    f = EXTRU (arg, 22, 0);
257
0
    if (s)
258
0
      f += -2 * (1 << 23);
259
0
    else
260
0
      f += (1 << 23);
261
0
    num = f / (double)(1 << 23);
262
0
    num = ldexp (num, e);
263
0
  }
264
0
      (*info->fprintf_func) (info->stream, "%f", num);
265
0
      break;
266
35.2k
    }
267
35.2k
  return 1;
268
35.2k
}
269
270
static int
271
tic4x_print_cond (struct disassemble_info *info, unsigned int cond)
272
28.5k
{
273
28.5k
  static tic4x_cond_t **condtable = NULL;
274
28.5k
  unsigned int i;
275
276
28.5k
  if (condtable == NULL)
277
1
    {
278
1
      condtable = xcalloc (32, sizeof (tic4x_cond_t *));
279
29
      for (i = 0; i < tic4x_num_conds; i++)
280
28
  condtable[tic4x_conds[i].cond] = (tic4x_cond_t *)(tic4x_conds + i);
281
1
    }
282
28.5k
  if (cond > 31 || condtable[cond] == NULL)
283
4.53k
    return 0;
284
24.0k
  if (info != NULL)
285
9.43k
    (*info->fprintf_func) (info->stream, "%s", condtable[cond]->name);
286
24.0k
  return 1;
287
28.5k
}
288
289
static int
290
tic4x_print_indirect (struct disassemble_info *info,
291
          indirect_t type,
292
          unsigned long arg)
293
113k
{
294
113k
  unsigned int aregno;
295
113k
  unsigned int modn;
296
113k
  unsigned int disp;
297
113k
  const char *a;
298
299
113k
  aregno = 0;
300
113k
  modn = 0;
301
113k
  disp = 1;
302
113k
  switch(type)
303
113k
    {
304
9.10k
    case INDIRECT_TIC4X:    /* *+ARn(disp) */
305
9.10k
      disp = EXTRU (arg, 7, 3);
306
9.10k
      aregno = EXTRU (arg, 2, 0) + REG_AR0;
307
9.10k
      modn = 0;
308
9.10k
      break;
309
91.6k
    case INDIRECT_SHORT:
310
91.6k
      disp = 1;
311
91.6k
      aregno = EXTRU (arg, 2, 0) + REG_AR0;
312
91.6k
      modn = EXTRU (arg, 7, 3);
313
91.6k
      break;
314
12.7k
    case INDIRECT_LONG:
315
12.7k
      disp = EXTRU (arg, 7, 0);
316
12.7k
      aregno = EXTRU (arg, 10, 8) + REG_AR0;
317
12.7k
      modn = EXTRU (arg, 15, 11);
318
12.7k
      if (modn > 7 && disp != 0)
319
5.13k
  return 0;
320
7.65k
      break;
321
7.65k
    default:
322
0
        (*info->fprintf_func)(info->stream, "# internal error: Unknown indirect type %d", type);
323
0
        return 0;
324
113k
    }
325
108k
  if (modn > TIC3X_MODN_MAX)
326
7.60k
    return 0;
327
100k
  a = tic4x_indirects[modn].name;
328
761k
  while (*a)
329
660k
    {
330
660k
      switch (*a)
331
660k
  {
332
100k
  case 'a':
333
100k
    tic4x_print_register (info, aregno);
334
100k
    break;
335
49.9k
  case 'd':
336
49.9k
    tic4x_print_immed (info, IMMED_UINT, disp);
337
49.9k
    break;
338
27.0k
  case 'y':
339
27.0k
    tic4x_print_str (info, "ir0");
340
27.0k
    break;
341
20.0k
  case 'z':
342
20.0k
    tic4x_print_str (info, "ir1");
343
20.0k
    break;
344
462k
  default:
345
462k
    tic4x_print_char (info, *a);
346
462k
    break;
347
660k
  }
348
660k
      a++;
349
660k
    }
350
100k
  return 1;
351
100k
}
352
353
static int
354
tic4x_print_op (struct disassemble_info *info,
355
    unsigned long instruction,
356
    tic4x_inst_t *p,
357
    unsigned long pc)
358
192k
{
359
192k
  int val;
360
192k
  const char *s;
361
192k
  const char *parallel = NULL;
362
363
  /* Print instruction name.  */
364
192k
  s = p->name;
365
985k
  while (*s && parallel == NULL)
366
797k
    {
367
797k
      switch (*s)
368
797k
  {
369
11.4k
  case 'B':
370
11.4k
    if (! tic4x_print_cond (info, EXTRU (instruction, 20, 16)))
371
908
      return 0;
372
10.5k
    break;
373
17.0k
  case 'C':
374
17.0k
    if (! tic4x_print_cond (info, EXTRU (instruction, 27, 23)))
375
3.62k
      return 0;
376
13.4k
    break;
377
48.1k
  case '_':
378
48.1k
    parallel = s + 1; /* Skip past `_' in name.  */
379
48.1k
    break;
380
720k
  default:
381
720k
    tic4x_print_char (info, *s);
382
720k
    break;
383
797k
  }
384
792k
      s++;
385
792k
    }
386
387
  /* Print arguments.  */
388
188k
  s = p->args;
389
188k
  if (*s)
390
187k
    tic4x_print_char (info, ' ');
391
392
906k
  while (*s)
393
760k
    {
394
760k
      switch (*s)
395
760k
  {
396
12.7k
  case '*': /* Indirect 0--15.  */
397
12.7k
    if (! tic4x_print_indirect (info, INDIRECT_LONG,
398
12.7k
              EXTRU (instruction, 15, 0)))
399
5.19k
      return 0;
400
7.58k
    break;
401
402
7.58k
  case '#': /* Only used for ldp, ldpk.  */
403
156
    tic4x_print_immed (info, IMMED_UINT, EXTRU (instruction, 15, 0));
404
156
    break;
405
406
19.4k
  case '@': /* Direct 0--15.  */
407
19.4k
    tic4x_print_direct (info, EXTRU (instruction, 15, 0));
408
19.4k
    break;
409
410
4.18k
  case 'A': /* Address register 24--22.  */
411
4.18k
    if (! tic4x_print_register (info, EXTRU (instruction, 24, 22) +
412
4.18k
              REG_AR0))
413
0
      return 0;
414
4.18k
    break;
415
416
9.34k
  case 'B': /* 24-bit unsigned int immediate br(d)/call/rptb
417
         address 0--23.  */
418
9.34k
    if (IS_CPU_TIC4X (tic4x_version))
419
8.08k
      tic4x_print_relative (info, pc, EXTRS (instruction, 23, 0),
420
8.08k
          p->opcode);
421
1.26k
    else
422
1.26k
      tic4x_print_addr (info, EXTRU (instruction, 23, 0));
423
9.34k
    break;
424
425
6.18k
  case 'C': /* Indirect (short C4x) 0--7.  */
426
6.18k
    if (! IS_CPU_TIC4X (tic4x_version))
427
0
      return 0;
428
6.18k
    if (! tic4x_print_indirect (info, INDIRECT_TIC4X,
429
6.18k
              EXTRU (instruction, 7, 0)))
430
0
      return 0;
431
6.18k
    break;
432
433
6.18k
  case 'D':
434
    /* Cockup if get here...  */
435
0
    break;
436
437
6.42k
  case 'E': /* Register 0--7.  */
438
7.52k
        case 'e':
439
7.52k
    if (! tic4x_print_register (info, EXTRU (instruction, 7, 0)))
440
4.59k
      return 0;
441
2.93k
    break;
442
443
8.51k
  case 'F': /* 16-bit float immediate 0--15.  */
444
8.51k
    tic4x_print_immed (info, IMMED_SFLOAT,
445
8.51k
           EXTRU (instruction, 15, 0));
446
8.51k
    break;
447
448
48.0k
        case 'i': /* Extended indirect 0--7.  */
449
48.0k
          if (EXTRU (instruction, 7, 5) == 7)
450
6.59k
            {
451
6.59k
              if (!tic4x_print_register (info, EXTRU (instruction, 4, 0)))
452
239
                return 0;
453
6.35k
              break;
454
6.59k
            }
455
          /* Fallthrough */
456
457
44.7k
  case 'I': /* Indirect (short) 0--7.  */
458
44.7k
    if (! tic4x_print_indirect (info, INDIRECT_SHORT,
459
44.7k
              EXTRU (instruction, 7, 0)))
460
2.77k
      return 0;
461
41.9k
    break;
462
463
41.9k
        case 'j': /* Extended indirect 8--15 */
464
15.3k
          if (EXTRU (instruction, 15, 13) == 7)
465
2.86k
            {
466
2.86k
              if (! tic4x_print_register (info, EXTRU (instruction, 12, 8)))
467
162
                return 0;
468
2.70k
              break;
469
2.86k
            }
470
    /* Fall through.  */
471
472
46.9k
  case 'J': /* Indirect (short) 8--15.  */
473
46.9k
    if (! tic4x_print_indirect (info, INDIRECT_SHORT,
474
46.9k
              EXTRU (instruction, 15, 8)))
475
4.76k
      return 0;
476
42.1k
    break;
477
478
42.1k
  case 'G': /* Register 8--15.  */
479
7.60k
        case 'g':
480
7.60k
    if (! tic4x_print_register (info, EXTRU (instruction, 15, 8)))
481
4.46k
      return 0;
482
3.14k
    break;
483
484
42.5k
  case 'H': /* Register 16--18.  */
485
42.5k
    if (! tic4x_print_register (info, EXTRU (instruction, 18, 16)))
486
0
      return 0;
487
42.5k
    break;
488
489
42.5k
  case 'K': /* Register 19--21.  */
490
33.5k
    if (! tic4x_print_register (info, EXTRU (instruction, 21, 19)))
491
0
      return 0;
492
33.5k
    break;
493
494
33.5k
  case 'L': /* Register 22--24.  */
495
30.6k
    if (! tic4x_print_register (info, EXTRU (instruction, 24, 22)))
496
0
      return 0;
497
30.6k
    break;
498
499
30.6k
  case 'M': /* Register 22--22.  */
500
15.1k
    tic4x_print_register (info, EXTRU (instruction, 22, 22) + REG_R2);
501
15.1k
    break;
502
503
15.2k
  case 'N': /* Register 23--23.  */
504
15.2k
    tic4x_print_register (info, EXTRU (instruction, 23, 23) + REG_R0);
505
15.2k
    break;
506
507
2.91k
  case 'O': /* Indirect (short C4x) 8--15.  */
508
2.91k
    if (! IS_CPU_TIC4X (tic4x_version))
509
0
      return 0;
510
2.91k
    if (! tic4x_print_indirect (info, INDIRECT_TIC4X,
511
2.91k
              EXTRU (instruction, 15, 8)))
512
0
      return 0;
513
2.91k
    break;
514
515
7.33k
  case 'P': /* Displacement 0--15 (used by Bcond and BcondD).  */
516
7.33k
    tic4x_print_relative (info, pc, EXTRS (instruction, 15, 0),
517
7.33k
        p->opcode);
518
7.33k
    break;
519
520
8.79k
  case 'Q': /* Register 0--15.  */
521
49.4k
        case 'q':
522
49.4k
    if (! tic4x_print_register (info, EXTRU (instruction, 15, 0)))
523
19.0k
      return 0;
524
30.3k
    break;
525
526
31.4k
  case 'R': /* Register 16--20.  */
527
79.8k
        case 'r':
528
79.8k
    if (! tic4x_print_register (info, EXTRU (instruction, 20, 16)))
529
527
      return 0;
530
79.2k
    break;
531
532
79.2k
  case 'S': /* 16-bit signed immediate 0--15.  */
533
8.31k
    tic4x_print_immed (info, IMMED_SINT,
534
8.31k
           EXTRS (instruction, 15, 0));
535
8.31k
    break;
536
537
4.28k
  case 'T': /* 5-bit signed immediate 16--20  (C4x stik).  */
538
4.28k
    if (! IS_CPU_TIC4X (tic4x_version))
539
0
      return 0;
540
4.28k
    if (! tic4x_print_immed (info, IMMED_SUINT,
541
4.28k
           EXTRU (instruction, 20, 16)))
542
0
      return 0;
543
4.28k
    break;
544
545
4.28k
  case 'U': /* 16-bit unsigned int immediate 0--15.  */
546
1.35k
    tic4x_print_immed (info, IMMED_SUINT, EXTRU (instruction, 15, 0));
547
1.35k
    break;
548
549
1.18k
  case 'V': /* 5/9-bit unsigned vector 0--4/8.  */
550
1.18k
    tic4x_print_immed (info, IMMED_SUINT,
551
1.18k
           IS_CPU_TIC4X (tic4x_version) ?
552
1.03k
           EXTRU (instruction, 8, 0) :
553
1.18k
           EXTRU (instruction, 4, 0) & ~0x20);
554
1.18k
    break;
555
556
2.70k
  case 'W': /* 8-bit signed immediate 0--7.  */
557
2.70k
    if (! IS_CPU_TIC4X (tic4x_version))
558
0
      return 0;
559
2.70k
    tic4x_print_immed (info, IMMED_SINT, EXTRS (instruction, 7, 0));
560
2.70k
    break;
561
562
1.41k
  case 'X': /* Expansion register 4--0.  */
563
1.41k
    val = EXTRU (instruction, 4, 0) + REG_IVTP;
564
1.41k
    if (val < REG_IVTP || val > REG_TVTP)
565
309
      return 0;
566
1.10k
    if (! tic4x_print_register (info, val))
567
0
      return 0;
568
1.10k
    break;
569
570
1.10k
  case 'Y': /* Address register 16--20.  */
571
719
    val = EXTRU (instruction, 20, 16);
572
719
    if (val < REG_AR0 || val > REG_SP)
573
97
      return 0;
574
622
    if (! tic4x_print_register (info, val))
575
0
      return 0;
576
622
    break;
577
578
622
  case 'Z': /* Expansion register 16--20.  */
579
340
    val = EXTRU (instruction, 20, 16) + REG_IVTP;
580
340
    if (val < REG_IVTP || val > REG_TVTP)
581
36
      return 0;
582
304
    if (! tic4x_print_register (info, val))
583
0
      return 0;
584
304
    break;
585
586
45.8k
  case '|': /* Parallel instruction.  */
587
45.8k
    tic4x_print_str (info, " || ");
588
45.8k
    tic4x_print_str (info, parallel);
589
45.8k
    tic4x_print_char (info, ' ');
590
45.8k
    break;
591
592
134k
  case ';':
593
134k
    tic4x_print_char (info, ',');
594
134k
    break;
595
596
106k
  default:
597
106k
    tic4x_print_char (info, *s);
598
106k
    break;
599
760k
  }
600
718k
      s++;
601
718k
    }
602
146k
  return 1;
603
188k
}
604
605
static void
606
tic4x_hash_opcode_special (tic4x_inst_t **optable_special,
607
         const tic4x_inst_t *inst)
608
1.14k
{
609
1.14k
  int i;
610
611
8.36k
  for (i = 0;i < TIC4X_SPESOP_SIZE; i++)
612
7.60k
    if (optable_special[i] != NULL
613
2.66k
        && optable_special[i]->opcode == inst->opcode)
614
380
      {
615
        /* Collision (we have it already) - overwrite.  */
616
380
        optable_special[i] = (tic4x_inst_t *) inst;
617
380
        return;
618
380
      }
619
620
1.90k
  for (i = 0; i < TIC4X_SPESOP_SIZE; i++)
621
1.90k
    if (optable_special[i] == NULL)
622
760
      {
623
        /* Add the new opcode.  */
624
760
        optable_special[i] = (tic4x_inst_t *) inst;
625
760
        return;
626
760
      }
627
628
  /* This should never occur. This happens if the number of special
629
     instructions exceeds TIC4X_SPESOP_SIZE. Please increase the variable
630
     of this variable */
631
#if TIC4X_DEBUG
632
  printf ("optable_special[] is full, please increase TIC4X_SPESOP_SIZE!\n");
633
#endif
634
760
}
635
636
static void
637
tic4x_hash_opcode (tic4x_inst_t **optable,
638
       tic4x_inst_t **optable_special,
639
       const tic4x_inst_t *inst,
640
       const unsigned long tic4x_oplevel)
641
194k
{
642
194k
  unsigned int j;
643
194k
  unsigned int opcode = inst->opcode >> (32 - TIC4X_HASH_SIZE);
644
194k
  unsigned int opmask = inst->opmask >> (32 - TIC4X_HASH_SIZE);
645
646
  /* Use a TIC4X_HASH_SIZE bit index as a hash index.  We should
647
     have unique entries so there's no point having a linked list
648
     for each entry?  */
649
253M
  for (j = opcode; j < opmask; j++)
650
253M
    if ((j & opmask) == opcode
651
818k
         && inst->oplevel & tic4x_oplevel)
652
796k
      {
653
#if TIC4X_DEBUG
654
  /* We should only have collisions for synonyms like
655
     ldp for ldi.  */
656
  if (optable[j] != NULL)
657
    printf ("Collision at index %d, %s and %s\n",
658
      j, optable[j]->name, inst->name);
659
#endif
660
        /* Catch those ops that collide with others already inside the
661
           hash, and have a opmask greater than the one we use in the
662
           hash. Store them in a special-list, that will handle full
663
           32-bit INSN, not only the first 11-bit (or so). */
664
796k
        if (optable[j] != NULL
665
550k
      && inst->opmask & ~(opmask << (32 - TIC4X_HASH_SIZE)))
666
570
          {
667
            /* Add the instruction already on the list.  */
668
570
            tic4x_hash_opcode_special (optable_special, optable[j]);
669
670
            /* Add the new instruction.  */
671
570
            tic4x_hash_opcode_special (optable_special, inst);
672
570
          }
673
674
796k
        optable[j] = (tic4x_inst_t *) inst;
675
796k
      }
676
194k
}
677
678
/* Disassemble the instruction in 'instruction'.
679
   'pc' should be the address of this instruction, it will
680
   be used to print the target address if this is a relative jump or call
681
   the disassembled instruction is written to 'info'.
682
   The function returns the length of this instruction in words.  */
683
684
static int
685
tic4x_disassemble (unsigned long pc,
686
       unsigned long instruction,
687
       struct disassemble_info *info)
688
171k
{
689
171k
  tic4x_inst_t *p;
690
171k
  int i;
691
171k
  unsigned long tic4x_oplevel;
692
693
171k
  if (tic4x_version != info->mach)
694
189
    {
695
189
      tic4x_version = info->mach;
696
      /* Don't stash anything from a previous call using a different
697
   machine.  */
698
189
      free (optab);
699
189
      optab = NULL;
700
189
      free (optab_special);
701
189
      optab_special = NULL;
702
189
      registernames[REG_R0] = NULL;
703
189
    }
704
705
171k
  tic4x_oplevel  = (IS_CPU_TIC4X (tic4x_version)) ? OP_C4X : 0;
706
171k
  tic4x_oplevel |= OP_C3X | OP_LPWR | OP_IDLE2 | OP_ENH;
707
708
171k
  if (optab == NULL)
709
190
    {
710
190
      optab = xcalloc ((1 << TIC4X_HASH_SIZE), sizeof (tic4x_inst_t *));
711
712
190
      optab_special = xcalloc (TIC4X_SPESOP_SIZE, sizeof (tic4x_inst_t *));
713
714
      /* Install opcodes in reverse order so that preferred
715
   forms overwrite synonyms.  */
716
194k
      for (i = tic4x_num_insts - 1; i >= 0; i--)
717
194k
  tic4x_hash_opcode (optab, optab_special, &tic4x_insts[i],
718
194k
         tic4x_oplevel);
719
720
      /* We now need to remove the insn that are special from the
721
   "normal" optable, to make the disasm search this extra list
722
   for them.  */
723
1.71k
      for (i = 0; i < TIC4X_SPESOP_SIZE; i++)
724
1.52k
  if (optab_special[i] != NULL)
725
760
    optab[optab_special[i]->opcode >> (32 - TIC4X_HASH_SIZE)] = NULL;
726
190
    }
727
728
  /* See if we can pick up any loading of the DP register...  */
729
171k
  if ((instruction >> 16) == 0x5070 || (instruction >> 16) == 0x1f70)
730
105
    tic4x_dp = EXTRU (instruction, 15, 0);
731
732
171k
  p = optab[instruction >> (32 - TIC4X_HASH_SIZE)];
733
171k
  if (p != NULL)
734
120k
    {
735
120k
      if (((instruction & p->opmask) == p->opcode)
736
119k
    && tic4x_print_op (NULL, instruction, p, pc))
737
73.0k
  tic4x_print_op (info, instruction, p, pc);
738
47.6k
      else
739
47.6k
  (*info->fprintf_func) (info->stream, "%08lx", instruction);
740
120k
    }
741
51.1k
  else
742
51.1k
    {
743
459k
      for (i = 0; i<TIC4X_SPESOP_SIZE; i++)
744
408k
  if (optab_special[i] != NULL
745
204k
      && optab_special[i]->opcode == instruction)
746
244
    {
747
244
      (*info->fprintf_func)(info->stream, "%s", optab_special[i]->name);
748
244
      break;
749
244
    }
750
51.1k
      if (i == TIC4X_SPESOP_SIZE)
751
50.9k
  (*info->fprintf_func) (info->stream, "%08lx", instruction);
752
51.1k
    }
753
754
  /* Return size of insn in words.  */
755
171k
  return 1;
756
171k
}
757
758
/* The entry point from objdump and gdb.  */
759
int
760
print_insn_tic4x (bfd_vma memaddr, struct disassemble_info *info)
761
172k
{
762
172k
  int status;
763
172k
  unsigned long pc;
764
172k
  unsigned long op;
765
172k
  bfd_byte buffer[4];
766
767
172k
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
768
172k
  if (status != 0)
769
281
    {
770
281
      (*info->memory_error_func) (status, memaddr, info);
771
281
      return -1;
772
281
    }
773
774
171k
  pc = memaddr;
775
171k
  op = bfd_getl32 (buffer);
776
171k
  info->bytes_per_line = 4;
777
171k
  info->bytes_per_chunk = 4;
778
171k
  info->octets_per_byte = 4;
779
171k
  info->display_endian = BFD_ENDIAN_LITTLE;
780
171k
  return tic4x_disassemble (pc, op, info) * 4;
781
172k
}