Coverage Report

Created: 2023-06-29 07:13

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