Coverage Report

Created: 2026-09-14 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/xgate-dis.c
Line
Count
Source
1
/* xgate-dis.c -- Freescale XGATE disassembly
2
   Copyright (C) 2009-2026 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
398k
#define XGATE_TWO_BYTES      0x02
31
6.73k
#define XGATE_NINE_BITS      0x1FF
32
3.04k
#define XGATE_TEN_BITS       0x3FF
33
16.6k
#define XGATE_NINE_SIGNBIT   0x100
34
4.21k
#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
199k
{
64
199k
  int status;
65
199k
  unsigned int raw_code;
66
199k
  char *s = 0;
67
199k
  long bytesRead = 0;
68
199k
  int i = 0;
69
199k
  struct xgate_opcode *opcodePTR = (struct xgate_opcode*) xgate_opcodes;
70
199k
  struct decodeInfo *decodeTablePTR = 0;
71
199k
  struct decodeInfo *decodePTR = 0;
72
199k
  unsigned int operandRegisterBits = 0;
73
199k
  signed int relAddr = 0;
74
199k
  signed int operandOne = 0;
75
199k
  signed int operandTwo = 0;
76
199k
  bfd_byte buffer[4];
77
199k
  bfd_vma absAddress;
78
79
199k
  unsigned int operMaskReg = 0;
80
  /* Initialize our array of opcode masks and check them against our constant
81
     table.  */
82
199k
  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
199k
  bytesRead += XGATE_TWO_BYTES;
112
199k
  status = read_memory (memaddr, buffer, XGATE_TWO_BYTES, info);
113
199k
  if (status == 0)
114
199k
    {
115
199k
      raw_code = buffer[0];
116
199k
      raw_code <<= 8;
117
199k
      raw_code += buffer[1];
118
119
199k
      decodePTR = find_match (raw_code);
120
199k
      if (decodePTR)
121
167k
        {
122
167k
          operMaskReg = decodePTR->operMasksRegisterBits;
123
167k
          (*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
167k
          if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON_R_C))
129
671
          {
130
671
            (*info->fprintf_func)(info->stream, " R%x, CCR",
131
671
              (raw_code >> 8) & 0x7);
132
671
          }
133
166k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON_C_R))
134
337
            {
135
337
            (*info->fprintf_func)(info->stream, " CCR, R%x",
136
337
                (raw_code >> 8) & 0x7);
137
337
            }
138
166k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON_R_P))
139
253
            {
140
253
            (*info->fprintf_func)(info->stream, " R%x, PC",
141
253
                (raw_code >> 8) & 0x7);
142
253
            }
143
166k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_TRI))
144
10.2k
            {
145
10.2k
                  (*info->fprintf_func)(info->stream, " R%x, R%x, R%x",
146
10.2k
                      (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7,
147
10.2k
                      (raw_code >> 2) & 0x7);
148
10.2k
            }
149
155k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IDR))
150
16.1k
            {
151
16.1k
                  if (raw_code & 0x01)
152
4.76k
                    {
153
4.76k
                      (*info->fprintf_func)(info->stream, " R%x, (R%x, R%x+)",
154
4.76k
                          (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7,
155
4.76k
                          (raw_code >> 2) & 0x7);
156
4.76k
                    }
157
11.4k
                   else if (raw_code & 0x02)
158
5.02k
                          {
159
5.02k
                            (*info->fprintf_func)(info->stream, " R%x, (R%x, -R%x)",
160
5.02k
                                (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7,
161
5.02k
                                (raw_code >> 2) & 0x7);
162
5.02k
                          }
163
6.38k
                   else
164
6.38k
                     {
165
6.38k
                       (*info->fprintf_func)(info->stream, " R%x, (R%x, R%x)",
166
6.38k
                           (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7,
167
6.38k
                           (raw_code >> 2) & 0x7);
168
6.38k
                     }
169
16.1k
            }
170
139k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_DYA))
171
1.08k
            {
172
1.08k
            operandOne = ripBits (&operMaskReg, 3, decodePTR->opcodePTR, raw_code);
173
1.08k
            operandTwo = ripBits (&operMaskReg, 3, decodePTR->opcodePTR, raw_code);
174
1.08k
           ( *info->fprintf_func)(info->stream, " R%x, R%x", operandOne,
175
1.08k
                operandTwo);
176
1.08k
            }
177
138k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IDO5))
178
14.5k
            {
179
14.5k
            (*info->fprintf_func)(info->stream, " R%x, (R%x, #0x%x)",
180
14.5k
                (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7, raw_code & 0x1f);
181
14.5k
            }
182
124k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON))
183
871
            {
184
871
            operandOne = ripBits (&operMaskReg, 3, decodePTR->opcodePTR,
185
871
               raw_code);
186
871
           (*info->fprintf_func)(info->stream, " R%x", operandOne);
187
871
            }
188
123k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_REL9))
189
16.6k
            {
190
              /* If address is negative handle it accordingly.  */
191
16.6k
              if (raw_code & XGATE_NINE_SIGNBIT)
192
6.73k
                {
193
6.73k
                  relAddr = XGATE_NINE_BITS >> 1; /* Clip sign bit.  */
194
6.73k
                  relAddr = ~relAddr; /* Make signed.  */
195
6.73k
                  relAddr |= (raw_code & 0xFF) + 1; /* Apply our value.  */
196
6.73k
                  relAddr *= 2; /* Multiply by two as per processor docs.  */
197
6.73k
                }
198
9.89k
              else
199
9.89k
                {
200
9.89k
                  relAddr = raw_code & 0xff;
201
9.89k
                  relAddr = relAddr * 2 + 2;
202
9.89k
                }
203
16.6k
             (*info->fprintf_func)(info->stream, " *%d", relAddr);
204
16.6k
             (*info->fprintf_func)(info->stream, "  Abs* 0x");
205
16.6k
             (*info->print_address_func)(memaddr + relAddr, info);
206
16.6k
           }
207
106k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_REL10))
208
4.21k
            {
209
              /* If address is negative handle it accordingly.  */
210
4.21k
              if (raw_code & XGATE_TEN_SIGNBIT)
211
3.04k
                {
212
3.04k
                  relAddr = XGATE_TEN_BITS >> 1; /* Clip sign bit.  */
213
3.04k
                  relAddr = ~relAddr; /* Make signed.  */
214
3.04k
                  relAddr |= (raw_code & 0x1FF) + 1; /* Apply our value.  */
215
3.04k
                  relAddr *= 2; /* Multiply by two as per processor docs.  */
216
3.04k
                }
217
1.17k
              else
218
1.17k
                {
219
1.17k
                  relAddr = raw_code & 0x1FF;
220
1.17k
                  relAddr = relAddr * 2 + 2;
221
1.17k
                }
222
4.21k
              (*info->fprintf_func)(info->stream, " *%d", relAddr);
223
4.21k
              (*info->fprintf_func)(info->stream, "  Abs* 0x");
224
4.21k
              (*info->print_address_func)(memaddr + relAddr, info);
225
4.21k
            }
226
102k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IMM4))
227
2.38k
            {
228
2.38k
              (*info->fprintf_func)(info->stream, " R%x, #0x%02x",
229
2.38k
              (raw_code >> 8) & 0x7, (raw_code >> 4) & 0xF);
230
2.38k
            }
231
99.9k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IMM8))
232
64.5k
            {
233
64.5k
              if (macro_search (decodePTR->opcodePTR->name, previousOpName) &&
234
36.3k
                 previousOpName[0])
235
19.2k
               {
236
19.2k
                 absAddress = (0xFF & raw_code) << 8;
237
19.2k
                 absAddress |= perviousBin & 0xFF;
238
19.2k
                 (*info->fprintf_func)(info->stream, " R%x, #0x%02x Abs* 0x",
239
19.2k
                     (raw_code >> 8) & 0x7, raw_code & 0xff);
240
19.2k
                 (*info->print_address_func)(absAddress, info);
241
19.2k
                 previousOpName[0] = 0;
242
19.2k
               }
243
45.3k
              else
244
45.3k
               {
245
45.3k
                 strcpy (previousOpName, decodePTR->opcodePTR->name);
246
45.3k
                 (*info->fprintf_func)(info->stream, " R%x, #0x%02x",
247
45.3k
                     (raw_code >> 8) & 0x7, raw_code & 0xff);
248
45.3k
               }
249
64.5k
            }
250
35.4k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IMM3))
251
398
            {
252
398
            (*info->fprintf_func)(info->stream, " #0x%x",
253
398
               (raw_code >> 8) & 0x7);
254
398
            }
255
35.0k
          else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_INH))
256
35.0k
            {
257
35.0k
            }
258
0
          else
259
0
            {
260
0
              (*info->fprintf_func)(info->stream, " unhandled mode %s",
261
0
            decodePTR->opcodePTR->constraints);
262
0
            }
263
167k
          perviousBin = raw_code;
264
167k
        }
265
31.6k
      else
266
31.6k
        {
267
31.6k
          (*info->fprintf_func)(info->stream,
268
31.6k
        " unable to find opcode match #0%x", raw_code);
269
31.6k
        }
270
199k
    }
271
199k
  return bytesRead;
272
199k
}
273
274
int
275
print_insn_xgate (bfd_vma memaddr, struct disassemble_info* info)
276
199k
{
277
199k
  return print_insn (memaddr, info);
278
199k
}
279
280
static int
281
read_memory (bfd_vma memaddr, bfd_byte* buffer, int size,
282
    struct disassemble_info* info)
283
199k
{
284
199k
  int status;
285
199k
  status = (*info->read_memory_func) (memaddr, buffer, size, info);
286
199k
  if (status != 0)
287
82
    {
288
82
      (*info->memory_error_func) (status, memaddr, info);
289
82
      return -1;
290
82
    }
291
199k
  return 0;
292
199k
}
293
294
static int
295
ripBits (unsigned int *operandBitsRemaining,
296
   int numBitsRequested,
297
   struct xgate_opcode *opcodePTR,
298
   unsigned int memory)
299
3.03k
{
300
3.03k
  unsigned int currentBit;
301
3.03k
  unsigned int operand = 0;
302
3.03k
  int numBitsFound;
303
304
3.03k
  for (numBitsFound = 0, currentBit = 1u << ((opcodePTR->size * 8) - 1);
305
30.5k
       numBitsFound < numBitsRequested && currentBit != 0;
306
27.5k
       currentBit >>= 1)
307
27.5k
    {
308
27.5k
      if (currentBit & *operandBitsRemaining)
309
9.11k
  {
310
9.11k
    *operandBitsRemaining &= ~(currentBit); /* Consume the current bit.  */
311
9.11k
    operand <<= 1; /* Make room for our next bit.  */
312
9.11k
    numBitsFound++;
313
9.11k
    operand |= (currentBit & memory) > 0;
314
9.11k
  }
315
27.5k
    }
316
3.03k
  return operand;
317
3.03k
}
318
319
static int
320
macro_search (char *currentName, char *lastName)
321
64.5k
{
322
64.5k
  int i;
323
64.5k
  int length = 0;
324
64.5k
  char *where;
325
326
5.90M
  for (i = 0; i < xgate_num_opcodes; i++)
327
5.88M
    {
328
5.88M
      where = strstr (xgate_opcodes[i].constraints, lastName);
329
330
5.88M
      if (where)
331
1.73M
        {
332
1.73M
          length = strlen (where);
333
1.73M
        }
334
5.88M
      if (length)
335
1.74M
        {
336
1.74M
          where = strstr (xgate_opcodes[i].constraints, currentName);
337
1.74M
          if (where)
338
36.3k
            {
339
36.3k
              length = strlen (where);
340
36.3k
              return 1;
341
36.3k
            }
342
1.74M
        }
343
5.88M
    }
344
28.2k
  return 0;
345
64.5k
}
346
347
static struct decodeInfo *
348
find_match (unsigned int raw_code)
349
199k
{
350
199k
  struct decodeInfo *decodeTablePTR = 0;
351
199k
  int i;
352
353
9.32M
  for (i = 0, decodeTablePTR = decodeTable; i < xgate_num_opcodes;
354
9.12M
      i++, decodeTablePTR++)
355
9.28M
    {
356
9.28M
      if ((raw_code & decodeTablePTR->operMask)
357
9.28M
          == decodeTablePTR->opcodePTR->bin_opcode)
358
357k
        {
359
          /* Make sure we didn't run into a macro or alias.  */
360
357k
          if (decodeTablePTR->opcodePTR->cycles_min != 0)
361
167k
            {
362
167k
              return decodeTablePTR;
363
0
              break;
364
167k
            }
365
189k
          else
366
189k
      continue;
367
357k
        }
368
9.28M
    }
369
31.6k
  return 0;
370
199k
}