Coverage Report

Created: 2026-09-14 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/mmix-dis.c
Line
Count
Source
1
/* mmix-dis.c -- Disassemble MMIX instructions.
2
   Copyright (C) 2000-2026 Free Software Foundation, Inc.
3
   Written by Hans-Peter Nilsson (hp@bitrange.com)
4
5
   This file is part of the GNU opcodes library.
6
7
   This library 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 3, or (at your option)
10
   any later version.
11
12
   It is distributed in the hope that it will be useful, but WITHOUT
13
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15
   License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this file; see the file COPYING.  If not, write to the Free
19
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include <stdio.h>
24
#include "opcode/mmix.h"
25
#include "disassemble.h"
26
#include "libiberty.h"
27
#include "bfd.h"
28
#include "opintl.h"
29
30
#define BAD_CASE(x)           \
31
0
  do                \
32
0
   {               \
33
0
     opcodes_error_handler (_("bad case %d (%s) in %s:%d"),  \
34
0
          x, #x, __FILE__, __LINE__);    \
35
0
     abort ();              \
36
0
   }                \
37
0
 while (0)
38
39
#define FATAL_DEBUG           \
40
0
 do                \
41
0
   {               \
42
0
     opcodes_error_handler (_("internal: non-debugged code " \
43
0
            "(test-case missing): %s:%d"),  \
44
0
          __FILE__, __LINE__);    \
45
0
     abort ();              \
46
0
   }                \
47
0
 while (0)
48
49
#define ROUND_MODE(n)         \
50
1.90k
 ((n) == 1 ? "ROUND_OFF" : (n) == 2 ? "ROUND_UP" :  \
51
1.47k
  (n) == 3 ? "ROUND_DOWN" : (n) == 4 ? "ROUND_NEAR" :  \
52
476
  _("(unknown)"))
53
54
67.1k
#define INSN_IMMEDIATE_BIT (IMM_OFFSET_BIT << 24)
55
13.7k
#define INSN_BACKWARD_OFFSET_BIT (1 << 24)
56
57
323k
#define MAX_REG_NAME_LEN       256
58
806
#define MAX_SPEC_REG_NAME_LEN  32
59
struct mmix_dis_info
60
 {
61
   const char *reg_name[MAX_REG_NAME_LEN];
62
   const char *spec_reg_name[MAX_SPEC_REG_NAME_LEN];
63
64
   /* Waste a little memory so we don't have to allocate each separately.
65
      We could have an array with static contents for these, but on the
66
      other hand, we don't have to.  */
67
   char basic_reg_name[MAX_REG_NAME_LEN][sizeof ("$255")];
68
 };
69
70
/* Initialize a target-specific array in INFO.  */
71
72
static bool
73
initialize_mmix_dis_info (struct disassemble_info *info)
74
369
{
75
369
  struct mmix_dis_info *minfop = malloc (sizeof (struct mmix_dis_info));
76
369
  long i;
77
78
369
  if (minfop == NULL)
79
0
    return false;
80
81
369
  memset (minfop, 0, sizeof (*minfop));
82
83
  /* Initialize register names from register symbols.  If there's no
84
     register section, then there are no register symbols.  */
85
369
  if ((info->section != NULL && info->section->owner != NULL)
86
290
      || (info->symbols != NULL
87
0
    && info->symbols[0] != NULL
88
0
    && bfd_asymbol_bfd (info->symbols[0]) != NULL))
89
79
    {
90
79
      bfd *abfd = info->section && info->section->owner != NULL
91
79
  ? info->section->owner
92
79
  : bfd_asymbol_bfd (info->symbols[0]);
93
79
      asection *reg_section = bfd_get_section_by_name (abfd, "*REG*");
94
95
79
      if (reg_section != NULL)
96
33
  {
97
    /* The returned symcount *does* include the ending NULL.  */
98
33
    long symsize = bfd_get_symtab_upper_bound (abfd);
99
33
    asymbol **syms = malloc (symsize);
100
33
    long nsyms;
101
102
33
    if (syms == NULL)
103
0
      {
104
0
        FATAL_DEBUG;
105
0
        free (minfop);
106
0
        return false;
107
0
      }
108
33
    nsyms = bfd_canonicalize_symtab (abfd, syms);
109
110
    /* We use the first name for a register.  If this is MMO, then
111
       it's the name with the first sequence number, presumably the
112
       first in the source.  */
113
1.52k
    for (i = 0; i < nsyms && syms[i] != NULL; i++)
114
1.49k
      {
115
1.49k
        if (syms[i]->section == reg_section
116
317
      && syms[i]->value < MAX_REG_NAME_LEN
117
317
      && minfop->reg_name[syms[i]->value] == NULL)
118
70
    minfop->reg_name[syms[i]->value] = syms[i]->name;
119
1.49k
      }
120
33
    free (syms);
121
33
  }
122
79
    }
123
124
  /* Fill in the rest with the canonical names.  */
125
94.8k
  for (i = 0; i < MAX_REG_NAME_LEN; i++)
126
94.4k
    if (minfop->reg_name[i] == NULL)
127
94.3k
      {
128
94.3k
  sprintf (minfop->basic_reg_name[i], "$%ld", i);
129
94.3k
  minfop->reg_name[i] = minfop->basic_reg_name[i];
130
94.3k
      }
131
132
  /* We assume it's actually a one-to-one mapping of number-to-name.  */
133
12.1k
  for (i = 0; mmix_spec_regs[i].name != NULL; i++)
134
11.8k
    minfop->spec_reg_name[mmix_spec_regs[i].number] = mmix_spec_regs[i].name;
135
136
369
  info->private_data = (void *) minfop;
137
369
  return true;
138
369
}
139
140
/* A table indexed by the first byte is constructed as we disassemble each
141
   tetrabyte.  The contents is a pointer into mmix_insns reflecting the
142
   first found entry with matching match-bits and lose-bits.  Further
143
   entries are considered one after one until the operand constraints
144
   match or the match-bits and lose-bits do not match.  Normally a
145
   "further entry" will just show that there was no other match.  */
146
147
static const struct mmix_opcode *
148
get_opcode (unsigned long insn)
149
172k
{
150
172k
  static const struct mmix_opcode **opcodes = NULL;
151
172k
  const struct mmix_opcode *opcodep = mmix_opcodes;
152
172k
  unsigned int opcode_part = (insn >> 24) & 255;
153
154
172k
  if (opcodes == NULL)
155
2
    opcodes = xcalloc (256, sizeof (struct mmix_opcode *));
156
157
172k
  opcodep = opcodes[opcode_part];
158
172k
  if (opcodep == NULL
159
39.8k
      || (opcodep->match & insn) != opcodep->match
160
39.8k
      || (opcodep->lose & insn) != 0)
161
133k
    {
162
      /* Search through the table.  */
163
9.76M
      for (opcodep = mmix_opcodes; opcodep->name != NULL; opcodep++)
164
9.75M
  {
165
    /* FIXME: Break out this into an initialization function.  */
166
9.75M
    if ((opcodep->match & (opcode_part << 24)) == opcode_part
167
2
        && (opcodep->lose & (opcode_part << 24)) == 0)
168
2
      opcodes[opcode_part] = opcodep;
169
170
9.75M
    if ((opcodep->match & insn) == opcodep->match
171
4.15M
        && (opcodep->lose & insn) == 0)
172
128k
      break;
173
9.75M
  }
174
133k
    }
175
176
172k
  if (opcodep->name == NULL)
177
4.39k
    return NULL;
178
179
  /* Check constraints.  If they don't match, loop through the next opcode
180
     entries.  */
181
168k
  do
182
168k
    {
183
168k
      switch (opcodep->operands)
184
168k
  {
185
    /* These have no restraint on what can be in the lower three
186
       bytes.  */
187
14.8k
  case mmix_operands_regs:
188
20.3k
  case mmix_operands_reg_yz:
189
35.6k
  case mmix_operands_regs_z_opt:
190
76.5k
  case mmix_operands_regs_z:
191
78.1k
  case mmix_operands_jmp:
192
79.0k
  case mmix_operands_pushgo:
193
79.7k
  case mmix_operands_pop:
194
80.9k
  case mmix_operands_sync:
195
85.0k
  case mmix_operands_x_regs_z:
196
87.1k
  case mmix_operands_neg:
197
88.2k
  case mmix_operands_pushj:
198
99.4k
  case mmix_operands_regaddr:
199
99.8k
  case mmix_operands_get:
200
100k
  case mmix_operands_set:
201
100k
  case mmix_operands_save:
202
100k
  case mmix_operands_unsave:
203
154k
  case mmix_operands_xyz_opt:
204
154k
    return opcodep;
205
206
    /* For a ROUND_MODE, the middle byte must be 0..4.  */
207
9.95k
  case mmix_operands_roundregs_z:
208
13.3k
  case mmix_operands_roundregs:
209
13.3k
    {
210
13.3k
      int midbyte = (insn >> 8) & 255;
211
212
13.3k
      if (midbyte <= 4)
213
4.56k
        return opcodep;
214
13.3k
    }
215
8.78k
  break;
216
217
8.78k
  case mmix_operands_put:
218
    /* A "PUT".  If it is "immediate", then no restrictions,
219
       otherwise we have to make sure the register number is < 32.  */
220
468
    if ((insn & INSN_IMMEDIATE_BIT)
221
334
        || ((insn >> 16) & 255) < 32)
222
421
      return opcodep;
223
47
    break;
224
225
216
  case mmix_operands_resume:
226
    /* Middle bytes must be zero.  */
227
216
    if ((insn & 0x00ffff00) == 0)
228
216
      return opcodep;
229
0
    break;
230
231
0
  default:
232
0
    BAD_CASE (opcodep->operands);
233
168k
  }
234
235
8.82k
      opcodep++;
236
8.82k
    }
237
168k
  while ((opcodep->match & insn) == opcodep->match
238
0
   && (opcodep->lose & insn) == 0);
239
240
  /* If we got here, we had no match.  */
241
8.82k
  return NULL;
242
168k
}
243
244
static inline const char *
245
get_reg_name (const struct mmix_dis_info * minfop, unsigned int x)
246
226k
{
247
226k
  if (x >= MAX_REG_NAME_LEN)
248
0
    return _("*illegal*");
249
226k
  return minfop->reg_name[x];
250
226k
}
251
252
static inline const char *
253
get_spec_reg_name (const struct mmix_dis_info * minfop, unsigned int x)
254
806
{
255
806
  if (x >= MAX_SPEC_REG_NAME_LEN)
256
61
    return _("*illegal*");
257
745
  return minfop->spec_reg_name[x];
258
806
}
259
260
/* The main disassembly function.  */
261
262
int
263
print_insn_mmix (bfd_vma memaddr, struct disassemble_info *info)
264
173k
{
265
173k
  unsigned char buffer[4];
266
173k
  unsigned long insn;
267
173k
  unsigned int x, y, z;
268
173k
  const struct mmix_opcode *opcodep;
269
173k
  int status = (*info->read_memory_func) (memaddr, buffer, 4, info);
270
173k
  struct mmix_dis_info *minfop;
271
272
173k
  if (status != 0)
273
199
    {
274
199
      (*info->memory_error_func) (status, memaddr, info);
275
199
      return -1;
276
199
    }
277
278
  /* FIXME: Is -1 suitable?  */
279
172k
  if (info->private_data == NULL
280
369
      && ! initialize_mmix_dis_info (info))
281
0
    return -1;
282
283
172k
  minfop = (struct mmix_dis_info *) info->private_data;
284
172k
  x = buffer[1];
285
172k
  y = buffer[2];
286
172k
  z = buffer[3];
287
288
172k
  insn = bfd_getb32 (buffer);
289
290
172k
  opcodep = get_opcode (insn);
291
292
172k
  if (opcodep == NULL)
293
13.2k
    {
294
13.2k
      (*info->fprintf_func) (info->stream, _("*unknown*"));
295
13.2k
      return 4;
296
13.2k
    }
297
298
159k
  (*info->fprintf_func) (info->stream, "%s ", opcodep->name);
299
300
  /* Present bytes in the order they are laid out in memory.  */
301
159k
  info->display_endian = BFD_ENDIAN_BIG;
302
303
159k
  info->insn_info_valid = 1;
304
159k
  info->bytes_per_chunk = 4;
305
159k
  info->branch_delay_insns = 0;
306
159k
  info->target = 0;
307
159k
  switch (opcodep->type)
308
159k
    {
309
72.9k
    case mmix_type_normal:
310
76.3k
    case mmix_type_memaccess_block:
311
76.3k
      info->insn_type = dis_nonbranch;
312
76.3k
      break;
313
314
2.83k
    case mmix_type_branch:
315
2.83k
      info->insn_type = dis_branch;
316
2.83k
      break;
317
318
10.9k
    case mmix_type_condbranch:
319
10.9k
      info->insn_type = dis_condbranch;
320
10.9k
      break;
321
322
4.50k
    case mmix_type_memaccess_octa:
323
4.50k
      info->insn_type = dis_dref;
324
4.50k
      info->data_size = 8;
325
4.50k
      break;
326
327
3.93k
    case mmix_type_memaccess_tetra:
328
3.93k
      info->insn_type = dis_dref;
329
3.93k
      info->data_size = 4;
330
3.93k
      break;
331
332
1.58k
    case mmix_type_memaccess_wyde:
333
1.58k
      info->insn_type = dis_dref;
334
1.58k
      info->data_size = 2;
335
1.58k
      break;
336
337
5.05k
    case mmix_type_memaccess_byte:
338
5.05k
      info->insn_type = dis_dref;
339
5.05k
      info->data_size = 1;
340
5.05k
      break;
341
342
54.5k
    case mmix_type_jsr:
343
54.5k
      info->insn_type = dis_jsr;
344
54.5k
      break;
345
346
0
    default:
347
0
      BAD_CASE(opcodep->type);
348
159k
    }
349
350
159k
  switch (opcodep->operands)
351
159k
    {
352
14.8k
    case mmix_operands_regs:
353
      /*  All registers: "$X,$Y,$Z".  */
354
14.8k
      (*info->fprintf_func) (info->stream, "%s,%s,%s",
355
14.8k
           get_reg_name (minfop, x),
356
14.8k
           get_reg_name (minfop, y),
357
14.8k
           get_reg_name (minfop, z));
358
14.8k
      break;
359
360
5.47k
    case mmix_operands_reg_yz:
361
      /* Like SETH - "$X,YZ".  */
362
5.47k
      (*info->fprintf_func) (info->stream, "%s,0x%x",
363
5.47k
           get_reg_name (minfop, x), y * 256 + z);
364
5.47k
      break;
365
366
15.3k
    case mmix_operands_regs_z_opt:
367
56.2k
    case mmix_operands_regs_z:
368
57.1k
    case mmix_operands_pushgo:
369
      /* The regular "$X,$Y,$Z|Z".  */
370
57.1k
      if (insn & INSN_IMMEDIATE_BIT)
371
25.9k
  (*info->fprintf_func) (info->stream, "%s,%s,%d",
372
25.9k
             get_reg_name (minfop, x),
373
25.9k
             get_reg_name (minfop, y), z);
374
31.1k
      else
375
31.1k
  (*info->fprintf_func) (info->stream, "%s,%s,%s",
376
31.1k
             get_reg_name (minfop, x),
377
31.1k
             get_reg_name (minfop, y),
378
31.1k
             get_reg_name (minfop, z));
379
57.1k
      break;
380
381
1.50k
    case mmix_operands_jmp:
382
      /* Address; only JMP.  */
383
1.50k
      {
384
1.50k
  bfd_signed_vma offset = (x * 65536 + y * 256 + z) * 4;
385
386
1.50k
  if (insn & INSN_BACKWARD_OFFSET_BIT)
387
634
    offset -= (256 * 65536) * 4;
388
389
1.50k
  info->target = memaddr + offset;
390
1.50k
  (*info->print_address_func) (memaddr + offset, info);
391
1.50k
      }
392
1.50k
      break;
393
394
2.90k
    case mmix_operands_roundregs_z:
395
      /* Two registers, like FLOT, possibly with rounding: "$X,$Z|Z"
396
   "$X,ROUND_MODE,$Z|Z".  */
397
2.90k
      if (y != 0)
398
901
  {
399
901
    if (insn & INSN_IMMEDIATE_BIT)
400
382
      (*info->fprintf_func) (info->stream, "%s,%s,%d",
401
382
           get_reg_name (minfop, x),
402
382
           ROUND_MODE (y), z);
403
519
    else
404
519
      (*info->fprintf_func) (info->stream, "%s,%s,%s",
405
519
           get_reg_name (minfop, x),
406
519
           ROUND_MODE (y),
407
519
           get_reg_name (minfop, z));
408
901
  }
409
2.00k
      else
410
2.00k
  {
411
2.00k
    if (insn & INSN_IMMEDIATE_BIT)
412
1.22k
      (*info->fprintf_func) (info->stream, "%s,%d",
413
1.22k
           get_reg_name (minfop, x), z);
414
772
    else
415
772
      (*info->fprintf_func) (info->stream, "%s,%s",
416
772
           get_reg_name (minfop, x),
417
772
           get_reg_name (minfop, z));
418
2.00k
  }
419
2.90k
      break;
420
421
704
    case mmix_operands_pop:
422
      /* Like POP - "X,YZ".  */
423
704
      (*info->fprintf_func) (info->stream, "%d,%d", x, y*256 + z);
424
704
      break;
425
426
1.66k
    case mmix_operands_roundregs:
427
      /* Two registers, possibly with rounding: "$X,$Z" or
428
   "$X,ROUND_MODE,$Z".  */
429
1.66k
      if (y != 0)
430
1.00k
  (*info->fprintf_func) (info->stream, "%s,%s,%s",
431
1.00k
             get_reg_name (minfop, x),
432
1.00k
             ROUND_MODE (y),
433
1.00k
             get_reg_name (minfop, z));
434
656
      else
435
656
  (*info->fprintf_func) (info->stream, "%s,%s",
436
656
             get_reg_name (minfop, x),
437
656
             get_reg_name (minfop, z));
438
1.66k
      break;
439
440
1.28k
    case mmix_operands_sync:
441
  /* Like SYNC - "XYZ".  */
442
1.28k
      (*info->fprintf_func) (info->stream, "%u",
443
1.28k
           x * 65536 + y * 256 + z);
444
1.28k
      break;
445
446
4.09k
    case mmix_operands_x_regs_z:
447
      /* Like SYNCD - "X,$Y,$Z|Z".  */
448
4.09k
      if (insn & INSN_IMMEDIATE_BIT)
449
1.51k
  (*info->fprintf_func) (info->stream, "%d,%s,%d",
450
1.51k
             x, get_reg_name (minfop, y), z);
451
2.57k
      else
452
2.57k
  (*info->fprintf_func) (info->stream, "%d,%s,%s",
453
2.57k
             x, get_reg_name (minfop, y),
454
2.57k
             get_reg_name (minfop, z));
455
4.09k
      break;
456
457
2.10k
    case mmix_operands_neg:
458
      /* Like NEG and NEGU - "$X,Y,$Z|Z".  */
459
2.10k
      if (insn & INSN_IMMEDIATE_BIT)
460
886
  (*info->fprintf_func) (info->stream, "%s,%d,%d",
461
886
             get_reg_name (minfop, x), y, z);
462
1.21k
      else
463
1.21k
  (*info->fprintf_func) (info->stream, "%s,%d,%s",
464
1.21k
             get_reg_name (minfop, x), y,
465
1.21k
             get_reg_name (minfop, z));
466
2.10k
      break;
467
468
1.07k
    case mmix_operands_pushj:
469
12.2k
    case mmix_operands_regaddr:
470
      /* Like GETA or branches - "$X,Address".  */
471
12.2k
      {
472
12.2k
  bfd_signed_vma offset = (y * 256 + z) * 4;
473
474
12.2k
  if (insn & INSN_BACKWARD_OFFSET_BIT)
475
6.05k
    offset -= 65536 * 4;
476
477
12.2k
  info->target = memaddr + offset;
478
479
12.2k
  (*info->fprintf_func) (info->stream, "%s,", get_reg_name (minfop, x));
480
12.2k
  (*info->print_address_func) (memaddr + offset, info);
481
12.2k
      }
482
12.2k
      break;
483
484
385
    case mmix_operands_get:
485
      /* GET - "X,spec_reg".  */
486
385
      (*info->fprintf_func) (info->stream, "%s,%s",
487
385
           get_reg_name (minfop, x),
488
385
           get_spec_reg_name (minfop, z));
489
385
      break;
490
491
421
    case mmix_operands_put:
492
      /* PUT - "spec_reg,$Z|Z".  */
493
421
      if (insn & INSN_IMMEDIATE_BIT)
494
134
  (*info->fprintf_func) (info->stream, "%s,%d",
495
134
             get_spec_reg_name (minfop, x), z);
496
287
      else
497
287
  (*info->fprintf_func) (info->stream, "%s,%s",
498
287
             get_spec_reg_name (minfop, x),
499
287
             get_reg_name (minfop, z));
500
421
      break;
501
502
333
    case mmix_operands_set:
503
      /*  Two registers, "$X,$Y".  */
504
333
      (*info->fprintf_func) (info->stream, "%s,%s",
505
333
           get_reg_name (minfop, x),
506
333
           get_reg_name (minfop, y));
507
333
      break;
508
509
281
    case mmix_operands_save:
510
      /* SAVE - "$X,0".  */
511
281
      (*info->fprintf_func) (info->stream, "%s,0", minfop->reg_name[x]);
512
281
      break;
513
514
104
    case mmix_operands_unsave:
515
      /* UNSAVE - "0,$Z".  */
516
104
      (*info->fprintf_func) (info->stream, "0,%s", minfop->reg_name[z]);
517
104
      break;
518
519
53.9k
    case mmix_operands_xyz_opt:
520
      /* Like SWYM or TRAP - "X,Y,Z".  */
521
53.9k
      (*info->fprintf_func) (info->stream, "%d,%d,%d", x, y, z);
522
53.9k
      break;
523
524
216
    case mmix_operands_resume:
525
      /* Just "Z", like RESUME.  */
526
216
      (*info->fprintf_func) (info->stream, "%d", z);
527
216
      break;
528
529
0
    default:
530
0
      (*info->fprintf_func) (info->stream, _("*unknown operands type: %d*"),
531
0
           opcodep->operands);
532
0
      break;
533
159k
    }
534
535
159k
  return 4;
536
159k
}