Coverage Report

Created: 2026-09-14 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/mcore-dis.c
Line
Count
Source
1
/* Disassemble Motorola M*Core instructions.
2
   Copyright (C) 1993-2026 Free Software Foundation, Inc.
3
4
   This file is part of the GNU opcodes library.
5
6
   This library is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3, or (at your option)
9
   any later version.
10
11
   It is distributed in the hope that it will be useful, but WITHOUT
12
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14
   License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include <stdio.h>
23
#include "libiberty.h"
24
#define STATIC_TABLE
25
#define DEFINE_TABLE
26
27
#include "mcore-opc.h"
28
#include "disassemble.h"
29
30
/* Mask for each mcore_opclass: */
31
static const unsigned short imsk[] = {
32
    /* O0  */ 0xFFFF,
33
    /* OT  */ 0xFFFC,
34
    /* O1  */ 0xFFF0,
35
    /* OC  */ 0xFE00,
36
    /* O2  */ 0xFF00,
37
    /* X1  */ 0xFFF0,
38
    /* OI  */ 0xFE00,
39
    /* OB  */ 0xFE00,
40
41
    /* OMa */ 0xFFF0,
42
    /* SI  */ 0xFE00,
43
    /* I7  */ 0xF800,
44
    /* LS  */ 0xF000,
45
    /* BR  */ 0xF800,
46
    /* BL  */ 0xFF00,
47
    /* LR  */ 0xF000,
48
    /* LJ  */ 0xFF00,
49
50
    /* RM  */ 0xFFF0,
51
    /* RQ  */ 0xFFF0,
52
    /* JSR */ 0xFFF0,
53
    /* JMP */ 0xFFF0,
54
    /* OBRa*/ 0xFFF0,
55
    /* OBRb*/ 0xFF80,
56
    /* OBRc*/ 0xFF00,
57
    /* OBR2*/ 0xFE00,
58
59
    /* O1R1*/ 0xFFF0,
60
    /* OMb */ 0xFF80,
61
    /* OMc */ 0xFF00,
62
    /* SIa */ 0xFE00,
63
64
  /* MULSH */ 0xFF00,
65
  /* OPSR  */ 0xFFF8,   /* psrset/psrclr */
66
67
    /* JC  */ 0,    /* JC,JU,JL don't appear in object */
68
    /* JU  */ 0,
69
    /* JL  */ 0,
70
    /* RSI */ 0,
71
    /* DO21*/ 0,
72
    /* OB2 */ 0     /* OB2 won't appear in object.  */
73
};
74
75
static const char *grname[] = {
76
 "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
77
 "r8",  "r9", "r10", "r11", "r12", "r13", "r14", "r15"
78
};
79
80
static const char X[] = "??";
81
82
static const char *crname[] = {
83
  "psr",  "vbr", "epsr", "fpsr", "epc",  "fpc",  "ss0",  "ss1",
84
  "ss2",  "ss3", "ss4",  "gcr",  "gsr",     X,      X,      X,
85
     X,      X,      X,      X,      X,     X,      X,      X,
86
     X,      X,      X,      X,      X,     X,      X,      X
87
};
88
89
static const unsigned isiz[] = { 2, 0, 1, 0 };
90
91
int
92
print_insn_mcore (bfd_vma memaddr,
93
      struct disassemble_info *info)
94
179k
{
95
179k
  unsigned char ibytes[4];
96
179k
  fprintf_ftype print_func = info->fprintf_func;
97
179k
  void *stream = info->stream;
98
179k
  unsigned int inst;
99
179k
  unsigned int i;
100
179k
  int status;
101
102
179k
  info->bytes_per_chunk = 2;
103
104
179k
  status = info->read_memory_func (memaddr, ibytes, 2, info);
105
106
179k
  if (status != 0)
107
105
    {
108
105
      info->memory_error_func (status, memaddr, info);
109
105
      return -1;
110
105
    }
111
112
179k
  if (info->endian == BFD_ENDIAN_BIG)
113
86
    inst = (ibytes[0] << 8) | ibytes[1];
114
179k
  else if (info->endian == BFD_ENDIAN_LITTLE)
115
179k
    inst = (ibytes[1] << 8) | ibytes[0];
116
0
  else
117
0
    abort ();
118
119
  /* Just a linear search of the table.  */
120
14.2M
  for (i = 0; i < ARRAY_SIZE (mcore_table); i++)
121
14.2M
    if (mcore_table[i].inst == (inst & imsk[mcore_table[i].opclass]))
122
155k
      break;
123
124
179k
  if (i == ARRAY_SIZE (mcore_table))
125
24.2k
    (*print_func) (stream, ".short 0x%04x", inst);
126
155k
  else
127
155k
    {
128
155k
      const char *name = grname[inst & 0x0F];
129
130
155k
      (*print_func) (stream, "%s", mcore_table[i].name);
131
132
155k
      switch (mcore_table[i].opclass)
133
155k
  {
134
31.6k
  case O0:
135
31.6k
    break;
136
137
895
  case OT:
138
895
    (*print_func) (stream, "\t%d", inst & 0x3);
139
895
    break;
140
141
7.10k
  case O1:
142
7.81k
  case JMP:
143
8.48k
  case JSR:
144
8.48k
    (*print_func) (stream, "\t%s", name);
145
8.48k
    break;
146
147
2.75k
  case OC:
148
2.75k
    (*print_func) (stream, "\t%s, %s", name, crname[(inst >> 4) & 0x1F]);
149
2.75k
    break;
150
151
225
  case O1R1:
152
225
    (*print_func) (stream, "\t%s, r1", name);
153
225
    break;
154
155
1.17k
  case MULSH:
156
11.8k
  case O2:
157
11.8k
    (*print_func) (stream, "\t%s, %s", name, grname[(inst >> 4) & 0xF]);
158
11.8k
    break;
159
160
1.86k
  case X1:
161
1.86k
    (*print_func) (stream, "\tr1, %s", name);
162
1.86k
    break;
163
164
4.65k
  case OI:
165
4.65k
    (*print_func) (stream, "\t%s, %d", name, ((inst >> 4) & 0x1F) + 1);
166
4.65k
    break;
167
168
1.99k
  case RM:
169
1.99k
    (*print_func) (stream, "\t%s-r15, (r0)", name);
170
1.99k
    break;
171
172
1.91k
  case RQ:
173
1.91k
    (*print_func) (stream, "\tr4-r7, (%s)", name);
174
1.91k
    break;
175
176
7.50k
  case OB:
177
7.63k
  case OBRa:
178
8.13k
  case OBRb:
179
9.27k
  case OBRc:
180
9.27k
  case SI:
181
12.0k
  case SIa:
182
12.2k
  case OMb:
183
12.7k
  case OMc:
184
12.7k
    (*print_func) (stream, "\t%s, %d", name, (inst >> 4) & 0x1F);
185
12.7k
    break;
186
187
366
  case OMa:
188
366
    (*print_func) (stream, "\t%s, %d", name, 32);
189
366
    break;
190
191
5.72k
  case I7:
192
5.72k
    (*print_func) (stream, "\t%s, %d", name, (inst >> 4) & 0x7F);
193
5.72k
    break;
194
195
30.7k
  case LS:
196
30.7k
    (*print_func) (stream, "\t%s, (%s, %d)", grname[(inst >> 8) & 0xF],
197
30.7k
       name, ((inst >> 4) & 0xF) << isiz[(inst >> 13) & 3]);
198
30.7k
    break;
199
200
24.4k
  case BR:
201
24.4k
    {
202
24.4k
      uint32_t val = ((inst & 0x7FF) ^ 0x400) - 0x400;
203
204
24.4k
      val = memaddr + 2 + (val << 1);
205
24.4k
      (*print_func) (stream, "\t0x%x", val);
206
207
24.4k
      if (strcmp (mcore_table[i].name, "bsr") == 0)
208
16.5k
        {
209
    /* For bsr, we'll try to get a symbol for the target.  */
210
16.5k
    if (info->print_address_func && val != 0)
211
16.5k
      {
212
16.5k
        (*print_func) (stream, "\t// ");
213
16.5k
        info->print_address_func (val, info);
214
16.5k
      }
215
16.5k
        }
216
24.4k
    }
217
24.4k
    break;
218
219
1.29k
  case BL:
220
1.29k
    {
221
1.29k
      uint32_t val = memaddr + 2 + ((inst | ~0xF) << 1);
222
223
1.29k
      (*print_func) (stream, "\t%s, 0x%x",
224
1.29k
         grname[(inst >> 4) & 0xF], val);
225
1.29k
    }
226
1.29k
    break;
227
228
8.97k
  case LR:
229
8.97k
    {
230
8.97k
      uint32_t val;
231
232
8.97k
      val = (memaddr + 2 + ((inst & 0xFF) << 2)) & ~3;
233
234
      /* We are not reading an instruction, so allow
235
         reads to extend beyond the next symbol.  */
236
8.97k
      info->stop_vma = 0;
237
8.97k
      status = info->read_memory_func (val, ibytes, 4, info);
238
8.97k
      if (status != 0)
239
1.76k
        {
240
1.76k
    info->memory_error_func (status, memaddr, info);
241
1.76k
    break;
242
1.76k
        }
243
244
7.20k
      if (info->endian == BFD_ENDIAN_LITTLE)
245
7.20k
        val = (((unsigned) ibytes[3] << 24) | (ibytes[2] << 16)
246
7.20k
         | (ibytes[1] << 8) | (ibytes[0]));
247
0
      else
248
0
        val = (((unsigned) ibytes[0] << 24) | (ibytes[1] << 16)
249
0
         | (ibytes[2] << 8) | (ibytes[3]));
250
251
      /* Removed [] around literal value to match ABI syntax 12/95.  */
252
7.20k
      (*print_func) (stream, "\t%s, 0x%X", grname[(inst >> 8) & 0xF], val);
253
254
7.20k
      if (val == 0)
255
813
        (*print_func) (stream, "\t// from address pool at 0x%x",
256
813
           (uint32_t) (memaddr + 2
257
813
           + ((inst & 0xFF) << 2)) & ~3);
258
7.20k
    }
259
0
    break;
260
261
3.13k
  case LJ:
262
3.13k
    {
263
3.13k
      uint32_t val;
264
265
3.13k
      val = (memaddr + 2 + ((inst & 0xFF) << 2)) & ~3;
266
267
      /* We are not reading an instruction, so allow
268
         reads to extend beyond the next symbol.  */
269
3.13k
      info->stop_vma = 0;
270
3.13k
      status = info->read_memory_func (val, ibytes, 4, info);
271
3.13k
      if (status != 0)
272
983
        {
273
983
    info->memory_error_func (status, memaddr, info);
274
983
    break;
275
983
        }
276
277
2.14k
      if (info->endian == BFD_ENDIAN_LITTLE)
278
2.14k
        val = (((unsigned) ibytes[3] << 24) | (ibytes[2] << 16)
279
2.14k
         | (ibytes[1] << 8) | (ibytes[0]));
280
2
      else
281
2
        val = (((unsigned) ibytes[0] << 24) | (ibytes[1] << 16)
282
2
         | (ibytes[2] << 8) | (ibytes[3]));
283
284
      /* Removed [] around literal value to match ABI syntax 12/95.  */
285
2.14k
      (*print_func) (stream, "\t0x%X", val);
286
      /* For jmpi/jsri, we'll try to get a symbol for the target.  */
287
2.14k
      if (info->print_address_func && val != 0)
288
1.84k
        {
289
1.84k
    (*print_func) (stream, "\t// ");
290
1.84k
    info->print_address_func (val, info);
291
1.84k
        }
292
299
      else
293
299
        {
294
299
    (*print_func) (stream, "\t// from address pool at 0x%x",
295
299
             (uint32_t) (memaddr + 2
296
299
             + ((inst & 0xFF) << 2)) & ~3);
297
299
        }
298
2.14k
    }
299
0
    break;
300
301
0
  case OPSR:
302
0
    {
303
0
      static char *fields[] = {
304
0
        "af", "ie",    "fe",    "fe,ie",
305
0
        "ee", "ee,ie", "ee,fe", "ee,fe,ie"
306
0
      };
307
308
0
      (*print_func) (stream, "\t%s", fields[inst & 0x7]);
309
0
    }
310
0
    break;
311
312
1.32k
  default:
313
    /* If the disassembler lags the instruction set.  */
314
1.32k
    (*print_func) (stream, "\tundecoded operands, inst is 0x%04x", inst);
315
1.32k
    break;
316
155k
  }
317
155k
    }
318
319
  /* Say how many bytes we consumed.  */
320
179k
  return 2;
321
179k
}