Coverage Report

Created: 2023-08-28 06:30

/src/binutils-gdb/opcodes/pru-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* TI PRU disassemble routines
2
   Copyright (C) 2014-2023 Free Software Foundation, Inc.
3
   Contributed by Dimitar Dimitrov <dimitar@dinux.eu>
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 file; see the file COPYING.  If not, write to the
19
   Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include "disassemble.h"
24
#include "opcode/pru.h"
25
#include "libiberty.h"
26
#include <string.h>
27
#include <assert.h>
28
29
/* No symbol table is available when this code runs out in an embedded
30
   system as when it is used for disassembler support in a monitor.  */
31
#if !defined (EMBEDDED_ENV)
32
#define SYMTAB_AVAILABLE 1
33
#include "elf-bfd.h"
34
#include "elf/pru.h"
35
#endif
36
37
/* Length of PRU instruction in bytes.  */
38
0
#define INSNLEN 4
39
40
/* Return a pointer to an pru_opcode struct for a given instruction
41
   opcode, or NULL if there is an error.  */
42
const struct pru_opcode *
43
pru_find_opcode (unsigned long opcode)
44
0
{
45
0
  const struct pru_opcode *p;
46
0
  const struct pru_opcode *op = NULL;
47
0
  const struct pru_opcode *pseudo_op = NULL;
48
49
0
  for (p = pru_opcodes; p < &pru_opcodes[NUMOPCODES]; p++)
50
0
    {
51
0
      if ((p->mask & opcode) == p->match)
52
0
  {
53
0
    if ((p->pinfo & PRU_INSN_MACRO) == PRU_INSN_MACRO)
54
0
      pseudo_op = p;
55
0
    else if ((p->pinfo & PRU_INSN_LDI32) == PRU_INSN_LDI32)
56
0
      /* ignore - should be caught with regular patterns */;
57
0
    else
58
0
      op = p;
59
0
  }
60
0
    }
61
62
0
  return pseudo_op ? pseudo_op : op;
63
0
}
64
65
/* There are 32 regular registers, each with 8 possible subfield selectors.  */
66
#define NUMREGNAMES (32 * 8)
67
68
static void
69
pru_print_insn_arg_reg (unsigned int r, unsigned int sel,
70
      disassemble_info *info)
71
0
{
72
0
  unsigned int i = r * RSEL_NUM_ITEMS + sel;
73
0
  assert (i < (unsigned int)pru_num_regs);
74
0
  assert (i < NUMREGNAMES);
75
0
  (*info->fprintf_func) (info->stream, "%s", pru_regs[i].name);
76
0
}
77
78
/* The function pru_print_insn_arg uses the character pointed
79
   to by ARGPTR to determine how it print the next token or separator
80
   character in the arguments to an instruction.  */
81
static int
82
pru_print_insn_arg (const char *argptr,
83
          unsigned long opcode, bfd_vma address,
84
          disassemble_info *info)
85
0
{
86
0
  long offs = 0;
87
0
  unsigned long i = 0;
88
0
  unsigned long io = 0;
89
90
0
  switch (*argptr)
91
0
    {
92
0
    case ',':
93
0
      (*info->fprintf_func) (info->stream, "%c ", *argptr);
94
0
      break;
95
0
    case 'd':
96
0
      pru_print_insn_arg_reg (GET_INSN_FIELD (RD, opcode),
97
0
            GET_INSN_FIELD (RDSEL, opcode),
98
0
            info);
99
0
      break;
100
0
    case 'D':
101
      /* The first 4 values for RDB and RSEL are the same, so we
102
   can reuse some code.  */
103
0
      pru_print_insn_arg_reg (GET_INSN_FIELD (RD, opcode),
104
0
            GET_INSN_FIELD (RDB, opcode),
105
0
            info);
106
0
      break;
107
0
    case 's':
108
0
      pru_print_insn_arg_reg (GET_INSN_FIELD (RS1, opcode),
109
0
            GET_INSN_FIELD (RS1SEL, opcode),
110
0
            info);
111
0
      break;
112
0
    case 'S':
113
0
      pru_print_insn_arg_reg (GET_INSN_FIELD (RS1, opcode),
114
0
            RSEL_31_0,
115
0
            info);
116
0
      break;
117
0
    case 'b':
118
0
      io = GET_INSN_FIELD (IO, opcode);
119
120
0
      if (io)
121
0
  {
122
0
    i = GET_INSN_FIELD (IMM8, opcode);
123
0
    (*info->fprintf_func) (info->stream, "%ld", i);
124
0
  }
125
0
      else
126
0
  {
127
0
  pru_print_insn_arg_reg (GET_INSN_FIELD (RS2, opcode),
128
0
        GET_INSN_FIELD (RS2SEL, opcode),
129
0
        info);
130
0
  }
131
0
      break;
132
0
    case 'B':
133
0
      io = GET_INSN_FIELD (IO, opcode);
134
135
0
      if (io)
136
0
  {
137
0
    i = GET_INSN_FIELD (IMM8, opcode) + 1;
138
0
    (*info->fprintf_func) (info->stream, "%ld", i);
139
0
  }
140
0
      else
141
0
  {
142
0
  pru_print_insn_arg_reg (GET_INSN_FIELD (RS2, opcode),
143
0
        GET_INSN_FIELD (RS2SEL, opcode),
144
0
        info);
145
0
  }
146
0
      break;
147
0
    case 'j':
148
0
      io = GET_INSN_FIELD (IO, opcode);
149
150
0
      if (io)
151
0
  {
152
    /* For the sake of pretty-printing, dump text addresses with
153
       their "virtual" offset that we use for distinguishing
154
       PMEM vs DMEM. This is needed for printing the correct text
155
       labels.  */
156
0
    bfd_vma text_offset = address & ~0x3fffff;
157
0
    i = GET_INSN_FIELD (IMM16, opcode) * 4;
158
0
    (*info->print_address_func) (i + text_offset, info);
159
0
  }
160
0
      else
161
0
  {
162
0
    pru_print_insn_arg_reg (GET_INSN_FIELD (RS2, opcode),
163
0
        GET_INSN_FIELD (RS2SEL, opcode),
164
0
        info);
165
0
  }
166
0
      break;
167
0
    case 'W':
168
0
      i = GET_INSN_FIELD (IMM16, opcode);
169
0
      (*info->fprintf_func) (info->stream, "%ld", i);
170
0
      break;
171
0
    case 'o':
172
0
      offs = GET_BROFF_SIGNED (opcode) * 4;
173
0
      (*info->print_address_func) (address + offs, info);
174
0
      break;
175
0
    case 'O':
176
0
      offs = GET_INSN_FIELD (LOOP_JMPOFFS, opcode) * 4;
177
0
      (*info->print_address_func) (address + offs, info);
178
0
      break;
179
0
    case 'l':
180
0
      i = GET_BURSTLEN (opcode);
181
0
      if (i < LSSBBO_BYTECOUNT_R0_BITS7_0)
182
0
  (*info->fprintf_func) (info->stream, "%ld", i + 1);
183
0
      else
184
0
  {
185
0
    i -= LSSBBO_BYTECOUNT_R0_BITS7_0;
186
0
    (*info->fprintf_func) (info->stream, "r0.b%ld", i);
187
0
  }
188
0
      break;
189
0
    case 'n':
190
0
      i = GET_INSN_FIELD (XFR_LENGTH, opcode);
191
0
      if (i < LSSBBO_BYTECOUNT_R0_BITS7_0)
192
0
  (*info->fprintf_func) (info->stream, "%ld", i + 1);
193
0
      else
194
0
  {
195
0
    i -= LSSBBO_BYTECOUNT_R0_BITS7_0;
196
0
    (*info->fprintf_func) (info->stream, "r0.b%ld", i);
197
0
  }
198
0
      break;
199
0
    case 'c':
200
0
      i = GET_INSN_FIELD (CB, opcode);
201
0
      (*info->fprintf_func) (info->stream, "%ld", i);
202
0
      break;
203
0
    case 'w':
204
0
      i = GET_INSN_FIELD (WAKEONSTATUS, opcode);
205
0
      (*info->fprintf_func) (info->stream, "%ld", i);
206
0
      break;
207
0
    case 'x':
208
0
      i = GET_INSN_FIELD (XFR_WBA, opcode);
209
0
      (*info->fprintf_func) (info->stream, "%ld", i);
210
0
      break;
211
0
    default:
212
0
      (*info->fprintf_func) (info->stream, "unknown");
213
0
      break;
214
0
    }
215
0
  return 0;
216
0
}
217
218
/* pru_disassemble does all the work of disassembling a PRU
219
   instruction opcode.  */
220
static int
221
pru_disassemble (bfd_vma address, unsigned long opcode,
222
       disassemble_info *info)
223
0
{
224
0
  const struct pru_opcode *op;
225
226
0
  info->bytes_per_line = INSNLEN;
227
0
  info->bytes_per_chunk = INSNLEN;
228
0
  info->display_endian = info->endian;
229
0
  info->insn_info_valid = 1;
230
0
  info->branch_delay_insns = 0;
231
0
  info->data_size = 0;
232
0
  info->insn_type = dis_nonbranch;
233
0
  info->target = 0;
234
0
  info->target2 = 0;
235
236
  /* Find the major opcode and use this to disassemble
237
     the instruction and its arguments.  */
238
0
  op = pru_find_opcode (opcode);
239
240
0
  if (op != NULL)
241
0
    {
242
0
      (*info->fprintf_func) (info->stream, "%s", op->name);
243
244
0
      const char *argstr = op->args;
245
0
      if (argstr != NULL && *argstr != '\0')
246
0
  {
247
0
    (*info->fprintf_func) (info->stream, "\t");
248
0
    while (*argstr != '\0')
249
0
      {
250
0
        pru_print_insn_arg (argstr, opcode, address, info);
251
0
        ++argstr;
252
0
      }
253
0
  }
254
0
    }
255
0
  else
256
0
    {
257
      /* Handle undefined instructions.  */
258
0
      info->insn_type = dis_noninsn;
259
0
      (*info->fprintf_func) (info->stream, "0x%lx", opcode);
260
0
    }
261
  /* Tell the caller how far to advance the program counter.  */
262
0
  return INSNLEN;
263
0
}
264
265
266
/* print_insn_pru is the main disassemble function for PRU.  */
267
int
268
print_insn_pru (bfd_vma address, disassemble_info *info)
269
0
{
270
0
  bfd_byte buffer[INSNLEN];
271
0
  int status;
272
273
0
  status = (*info->read_memory_func) (address, buffer, INSNLEN, info);
274
0
  if (status == 0)
275
0
    {
276
0
      unsigned long insn;
277
0
      insn = (unsigned long) bfd_getl32 (buffer);
278
0
      status = pru_disassemble (address, insn, info);
279
0
    }
280
0
  else
281
0
    {
282
0
      (*info->memory_error_func) (status, address, info);
283
0
      status = -1;
284
0
    }
285
0
  return status;
286
0
}