Coverage Report

Created: 2023-08-28 06:23

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