Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/opcodes/xgate-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* xgate-dis.c -- Freescale XGATE disassembly
2
   Copyright (C) 2009-2025 Free Software Foundation, Inc.
3
   Written by Sean Keys (skeys@ipdatasys.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 program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include <assert.h>
24
#include "disassemble.h"
25
#include "opintl.h"
26
#include "libiberty.h"
27
#include "ansidecl.h"
28
#include "opcode/xgate.h"
29
30
269k
#define XGATE_TWO_BYTES      0x02
31
3.80k
#define XGATE_NINE_BITS      0x1FF
32
1.57k
#define XGATE_TEN_BITS       0x3FF
33
9.49k
#define XGATE_NINE_SIGNBIT   0x100
34
2.26k
#define XGATE_TEN_SIGNBIT    0x200
35
36
/* Structures.  */
37
struct decodeInfo
38
{
39
  unsigned int operMask;
40
  unsigned int operMasksRegisterBits;
41
  struct xgate_opcode *opcodePTR;
42
};
43
44
/* Prototypes for local functions.  */
45
static int print_insn (bfd_vma, struct disassemble_info *);
46
static int read_memory (bfd_vma, bfd_byte*, int, struct disassemble_info *);
47
static int ripBits (unsigned int *, int,
48
        struct xgate_opcode *, unsigned int);
49
static int macro_search (char *, char *);
50
static struct decodeInfo * find_match (unsigned int);
51
52
/* Statics.  */
53
static struct decodeInfo *decodeTable;
54
static int initialized;
55
static char previousOpName[10];
56
static unsigned int perviousBin;
57
58
/* Disassemble one instruction at address 'memaddr'.  Returns the number
59
   of bytes used by that instruction.  */
60
61
static int
62
print_insn (bfd_vma memaddr, struct disassemble_info* info)
63
134k
{
64
134k
  int status;
65
134k
  unsigned int raw_code;
66
134k
  char *s = 0;
67
134k
  long bytesRead = 0;
68
134k
  int i = 0;
69
134k
  struct xgate_opcode *opcodePTR = (struct xgate_opcode*) xgate_opcodes;
70
134k
  struct decodeInfo *decodeTablePTR = 0;
71
134k
  struct decodeInfo *decodePTR = 0;
72
134k
  unsigned int operandRegisterBits = 0;
73
134k
  signed int relAddr = 0;
74
134k
  signed int operandOne = 0;
75
134k
  signed int operandTwo = 0;
76
134k
  bfd_byte buffer[4];
77
134k
  bfd_vma absAddress;
78
79
134k
  unsigned int operMaskReg = 0;
80
  /* Initialize our array of opcode masks and check them against our constant
81
     table.  */
82
134k
  if (!initialized)
83
2
    {
84
2
      decodeTable = xmalloc (sizeof (struct decodeInfo) * xgate_num_opcodes);
85
192
      for (i = 0, decodeTablePTR = decodeTable; i < xgate_num_opcodes;
86
190
          i++, decodeTablePTR++, opcodePTR++)
87
190
        {
88
190
          unsigned int bin = 0;
89
190
          unsigned int mask = 0;
90
3.23k
          for (s = opcodePTR->format; *s; s++)
91
3.04k
            {
92
3.04k
              bin <<= 1;
93
3.04k
              mask <<= 1;
94
3.04k
              operandRegisterBits <<= 1;
95
3.04k
              bin |= (*s == '1');
96
3.04k
              mask |= (*s == '0' || *s == '1');
97
3.04k
              operandRegisterBits |= (*s == 'r');
98
3.04k
            }
99
          /* Asserting will uncover inconsistencies in our table.  */
100
190
          assert ((s - opcodePTR->format) == 16 || (s - opcodePTR->format) == 32);
101
190
          assert (opcodePTR->bin_opcode == bin);
102
103
190
          decodeTablePTR->operMask = mask;
104
190
          decodeTablePTR->operMasksRegisterBits = operandRegisterBits;
105
190
          decodeTablePTR->opcodePTR = opcodePTR;
106
190
        }
107
2
      initialized = 1;
108
2
    }
109
110
  /* Read 16 bits.  */
111
134k
  bytesRead += XGATE_TWO_BYTES;
112
134k
  status = read_memory (memaddr, buffer, XGATE_TWO_BYTES, info);
113
134k
  if (status == 0)
114
134k
    {
115
134k
      raw_code = buffer[0];
116
134k
      raw_code <<= 8;
117
134k
      raw_code += buffer[1];
118
119
134k
      decodePTR = find_match (raw_code);
120
134k
      if (decodePTR)
121
113k
        {
122
113k
          operMaskReg = decodePTR->operMasksRegisterBits;
123
113k
          (*info->fprintf_func)(info->stream, "%s", decodePTR->opcodePTR->name);
124
125
          /* First we compare the shorthand format of the constraints. If we
126
        still are unable to pinpoint the operands
127
        we analyze the opcodes constraint string.  */
128
113k
          if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON_R_C))
129
575
          {
130
575
            (*info->fprintf_func)(info->stream, " R%x, CCR",
131
575
              (raw_code >> 8) & 0x7);
132
575
          }
133
113k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON_C_R))
134
231
            {
135
231
            (*info->fprintf_func)(info->stream, " CCR, R%x",
136
231
                (raw_code >> 8) & 0x7);
137
231
            }
138
113k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON_R_P))
139
260
            {
140
260
            (*info->fprintf_func)(info->stream, " R%x, PC",
141
260
                (raw_code >> 8) & 0x7);
142
260
            }
143
112k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_TRI))
144
6.66k
            {
145
6.66k
                  (*info->fprintf_func)(info->stream, " R%x, R%x, R%x",
146
6.66k
                      (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7,
147
6.66k
                      (raw_code >> 2) & 0x7);
148
6.66k
            }
149
106k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IDR))
150
11.0k
            {
151
11.0k
                  if (raw_code & 0x01)
152
3.27k
                    {
153
3.27k
                      (*info->fprintf_func)(info->stream, " R%x, (R%x, R%x+)",
154
3.27k
                          (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7,
155
3.27k
                          (raw_code >> 2) & 0x7);
156
3.27k
                    }
157
7.81k
                   else if (raw_code & 0x02)
158
2.95k
                          {
159
2.95k
                            (*info->fprintf_func)(info->stream, " R%x, (R%x, -R%x)",
160
2.95k
                                (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7,
161
2.95k
                                (raw_code >> 2) & 0x7);
162
2.95k
                          }
163
4.85k
                   else
164
4.85k
                     {
165
4.85k
                       (*info->fprintf_func)(info->stream, " R%x, (R%x, R%x)",
166
4.85k
                           (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7,
167
4.85k
                           (raw_code >> 2) & 0x7);
168
4.85k
                     }
169
11.0k
            }
170
94.9k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_DYA))
171
820
            {
172
820
            operandOne = ripBits (&operMaskReg, 3, decodePTR->opcodePTR, raw_code);
173
820
            operandTwo = ripBits (&operMaskReg, 3, decodePTR->opcodePTR, raw_code);
174
820
           ( *info->fprintf_func)(info->stream, " R%x, R%x", operandOne,
175
820
                operandTwo);
176
820
            }
177
94.1k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IDO5))
178
10.8k
            {
179
10.8k
            (*info->fprintf_func)(info->stream, " R%x, (R%x, #0x%x)",
180
10.8k
                (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7, raw_code & 0x1f);
181
10.8k
            }
182
83.2k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON))
183
530
            {
184
530
            operandOne = ripBits (&operMaskReg, 3, decodePTR->opcodePTR,
185
530
               raw_code);
186
530
           (*info->fprintf_func)(info->stream, " R%x", operandOne);
187
530
            }
188
82.7k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_REL9))
189
9.49k
            {
190
              /* If address is negative handle it accordingly.  */
191
9.49k
              if (raw_code & XGATE_NINE_SIGNBIT)
192
3.80k
                {
193
3.80k
                  relAddr = XGATE_NINE_BITS >> 1; /* Clip sign bit.  */
194
3.80k
                  relAddr = ~relAddr; /* Make signed.  */
195
3.80k
                  relAddr |= (raw_code & 0xFF) + 1; /* Apply our value.  */
196
3.80k
                  relAddr *= 2; /* Multiply by two as per processor docs.  */
197
3.80k
                }
198
5.68k
              else
199
5.68k
                {
200
5.68k
                  relAddr = raw_code & 0xff;
201
5.68k
                  relAddr = relAddr * 2 + 2;
202
5.68k
                }
203
9.49k
             (*info->fprintf_func)(info->stream, " *%d", relAddr);
204
9.49k
             (*info->fprintf_func)(info->stream, "  Abs* 0x");
205
9.49k
             (*info->print_address_func)(memaddr + relAddr, info);
206
9.49k
           }
207
73.2k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_REL10))
208
2.26k
            {
209
              /* If address is negative handle it accordingly.  */
210
2.26k
              if (raw_code & XGATE_TEN_SIGNBIT)
211
1.57k
                {
212
1.57k
                  relAddr = XGATE_TEN_BITS >> 1; /* Clip sign bit.  */
213
1.57k
                  relAddr = ~relAddr; /* Make signed.  */
214
1.57k
                  relAddr |= (raw_code & 0x1FF) + 1; /* Apply our value.  */
215
1.57k
                  relAddr *= 2; /* Multiply by two as per processor docs.  */
216
1.57k
                }
217
685
              else
218
685
                {
219
685
                  relAddr = raw_code & 0x1FF;
220
685
                  relAddr = relAddr * 2 + 2;
221
685
                }
222
2.26k
              (*info->fprintf_func)(info->stream, " *%d", relAddr);
223
2.26k
              (*info->fprintf_func)(info->stream, "  Abs* 0x");
224
2.26k
              (*info->print_address_func)(memaddr + relAddr, info);
225
2.26k
            }
226
70.9k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IMM4))
227
4.76k
            {
228
4.76k
              (*info->fprintf_func)(info->stream, " R%x, #0x%02x",
229
4.76k
              (raw_code >> 8) & 0x7, (raw_code >> 4) & 0xF);
230
4.76k
            }
231
66.2k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IMM8))
232
41.3k
            {
233
41.3k
              if (macro_search (decodePTR->opcodePTR->name, previousOpName) &&
234
41.3k
                 previousOpName[0])
235
12.5k
               {
236
12.5k
                 absAddress = (0xFF & raw_code) << 8;
237
12.5k
                 absAddress |= perviousBin & 0xFF;
238
12.5k
                 (*info->fprintf_func)(info->stream, " R%x, #0x%02x Abs* 0x",
239
12.5k
                     (raw_code >> 8) & 0x7, raw_code & 0xff);
240
12.5k
                 (*info->print_address_func)(absAddress, info);
241
12.5k
                 previousOpName[0] = 0;
242
12.5k
               }
243
28.8k
              else
244
28.8k
               {
245
28.8k
                 strcpy (previousOpName, decodePTR->opcodePTR->name);
246
28.8k
                 (*info->fprintf_func)(info->stream, " R%x, #0x%02x",
247
28.8k
                     (raw_code >> 8) & 0x7, raw_code & 0xff);
248
28.8k
               }
249
41.3k
            }
250
24.8k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IMM3))
251
281
            {
252
281
            (*info->fprintf_func)(info->stream, " #0x%x",
253
281
               (raw_code >> 8) & 0x7);
254
281
            }
255
24.5k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_INH))
256
24.5k
            {
257
24.5k
            }
258
0
          else
259
0
            {
260
0
              (*info->fprintf_func)(info->stream, " unhandled mode %s",
261
0
            decodePTR->opcodePTR->constraints);
262
0
            }
263
113k
          perviousBin = raw_code;
264
113k
        }
265
20.7k
      else
266
20.7k
        {
267
20.7k
          (*info->fprintf_func)(info->stream,
268
20.7k
        " unable to find opcode match #0%x", raw_code);
269
20.7k
        }
270
134k
    }
271
134k
  return bytesRead;
272
134k
}
273
274
int
275
print_insn_xgate (bfd_vma memaddr, struct disassemble_info* info)
276
134k
{
277
134k
  return print_insn (memaddr, info);
278
134k
}
279
280
static int
281
read_memory (bfd_vma memaddr, bfd_byte* buffer, int size,
282
    struct disassemble_info* info)
283
134k
{
284
134k
  int status;
285
134k
  status = (*info->read_memory_func) (memaddr, buffer, size, info);
286
134k
  if (status != 0)
287
81
    {
288
81
      (*info->memory_error_func) (status, memaddr, info);
289
81
      return -1;
290
81
    }
291
134k
  return 0;
292
134k
}
293
294
static int
295
ripBits (unsigned int *operandBitsRemaining,
296
   int numBitsRequested,
297
   struct xgate_opcode *opcodePTR,
298
   unsigned int memory)
299
2.17k
{
300
2.17k
  unsigned int currentBit;
301
2.17k
  unsigned int operand = 0;
302
2.17k
  int numBitsFound;
303
304
2.17k
  for (numBitsFound = 0, currentBit = 1u << ((opcodePTR->size * 8) - 1);
305
21.9k
       numBitsFound < numBitsRequested && currentBit != 0;
306
19.8k
       currentBit >>= 1)
307
19.8k
    {
308
19.8k
      if (currentBit & *operandBitsRemaining)
309
6.51k
  {
310
6.51k
    *operandBitsRemaining &= ~(currentBit); /* Consume the current bit.  */
311
6.51k
    operand <<= 1; /* Make room for our next bit.  */
312
6.51k
    numBitsFound++;
313
6.51k
    operand |= (currentBit & memory) > 0;
314
6.51k
  }
315
19.8k
    }
316
2.17k
  return operand;
317
2.17k
}
318
319
static int
320
macro_search (char *currentName, char *lastName)
321
41.3k
{
322
41.3k
  int i;
323
41.3k
  int length = 0;
324
41.3k
  char *where;
325
326
3.78M
  for (i = 0; i < xgate_num_opcodes; i++)
327
3.76M
    {
328
3.76M
      where = strstr (xgate_opcodes[i].constraints, lastName);
329
330
3.76M
      if (where)
331
1.13M
        {
332
1.13M
          length = strlen (where);
333
1.13M
        }
334
3.76M
      if (length)
335
1.14M
        {
336
1.14M
          where = strstr (xgate_opcodes[i].constraints, currentName);
337
1.14M
          if (where)
338
23.7k
            {
339
23.7k
              length = strlen (where);
340
23.7k
              return 1;
341
23.7k
            }
342
1.14M
        }
343
3.76M
    }
344
17.6k
  return 0;
345
41.3k
}
346
347
static struct decodeInfo *
348
find_match (unsigned int raw_code)
349
134k
{
350
134k
  struct decodeInfo *decodeTablePTR = 0;
351
134k
  int i;
352
353
6.27M
  for (i = 0, decodeTablePTR = decodeTable; i < xgate_num_opcodes;
354
6.14M
      i++, decodeTablePTR++)
355
6.25M
    {
356
6.25M
      if ((raw_code & decodeTablePTR->operMask)
357
6.25M
          == decodeTablePTR->opcodePTR->bin_opcode)
358
238k
        {
359
          /* Make sure we didn't run into a macro or alias.  */
360
238k
          if (decodeTablePTR->opcodePTR->cycles_min != 0)
361
113k
            {
362
113k
              return decodeTablePTR;
363
0
              break;
364
113k
            }
365
124k
          else
366
124k
      continue;
367
238k
        }
368
6.25M
    }
369
20.7k
  return 0;
370
134k
}