Coverage Report

Created: 2024-05-21 06:29

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