Coverage Report

Created: 2023-06-29 07:13

/src/binutils-gdb/opcodes/tilepro-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* tilepro-dis.c.  Disassembly routines for the TILEPro architecture.
2
   Copyright (C) 2011-2023 Free Software Foundation, Inc.
3
4
   This file is part of the GNU opcodes library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include <stddef.h>
23
#include <assert.h>
24
#include "bfd.h"
25
#include "elf/tilepro.h"
26
#include "elf-bfd.h"
27
#include "disassemble.h"
28
#include "opcode/tilepro.h"
29
30
31
2.31k
#define TREG_ZERO 63
32
33
static int
34
contains_insn (tilepro_mnemonic expected_mnemonic,
35
         int expected_first_operand,
36
         int expected_second_operand,
37
         bfd_vma memaddr,
38
         int *last_operand_ret,
39
         disassemble_info *info)
40
5.16k
{
41
5.16k
  struct tilepro_decoded_instruction
42
5.16k
    decoded[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE];
43
5.16k
  bfd_byte opbuf[TILEPRO_BUNDLE_SIZE_IN_BYTES];
44
5.16k
  int i, num_instructions;
45
46
5.16k
  if ((*info->read_memory_func) (memaddr, opbuf,
47
5.16k
         TILEPRO_BUNDLE_SIZE_IN_BYTES, info) != 0)
48
    /* If we cannot even read the memory, it obviously does not have the
49
       instruction for which we are looking. */
50
5.16k
    return 0;
51
52
  /* Parse the instructions in the bundle. */
53
0
  num_instructions = parse_insn_tilepro (bfd_getl64 (opbuf), memaddr, decoded);
54
55
0
  for (i = 0; i < num_instructions; i++)
56
0
    {
57
0
      const struct tilepro_opcode *opcode = decoded[i].opcode;
58
59
0
      if (opcode->mnemonic != expected_mnemonic)
60
0
  continue;
61
62
0
      if (expected_first_operand != -1
63
0
    && decoded[i].operand_values[0] != expected_first_operand)
64
0
  continue;
65
66
0
      if (expected_second_operand != -1
67
0
    && decoded[i].operand_values[1] != expected_second_operand)
68
0
  continue;
69
70
0
      *last_operand_ret = decoded[i].operand_values[opcode->num_operands - 1];
71
0
      return 1;
72
0
    }
73
74
  /* No match. */
75
0
  return 0;
76
0
}
77
78
79
int
80
print_insn_tilepro (bfd_vma memaddr, disassemble_info *info)
81
29.3k
{
82
29.3k
  struct tilepro_decoded_instruction
83
29.3k
    decoded[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE];
84
29.3k
  bfd_byte opbuf[TILEPRO_BUNDLE_SIZE_IN_BYTES];
85
29.3k
  int status, i, num_instructions, num_printed;
86
29.3k
  tilepro_mnemonic padding_mnemonic;
87
88
29.3k
  status = (*info->read_memory_func) (memaddr, opbuf,
89
29.3k
                                      TILEPRO_BUNDLE_SIZE_IN_BYTES, info);
90
29.3k
  if (status != 0)
91
83
    {
92
83
      (*info->memory_error_func) (status, memaddr, info);
93
83
      return -1;
94
83
    }
95
96
29.2k
  info->bytes_per_line = TILEPRO_BUNDLE_SIZE_IN_BYTES;
97
29.2k
  info->bytes_per_chunk = TILEPRO_BUNDLE_SIZE_IN_BYTES;
98
29.2k
  info->octets_per_byte = 1;
99
29.2k
  info->display_endian = BFD_ENDIAN_LITTLE;
100
101
  /* Parse the instructions in the bundle.  */
102
29.2k
  num_instructions = parse_insn_tilepro (bfd_getl64 (opbuf), memaddr, decoded);
103
104
  /* Print the instructions in the bundle.  */
105
29.2k
  info->fprintf_func (info->stream, "{ ");
106
29.2k
  num_printed = 0;
107
108
  /* Determine which nop opcode is used for padding and should be skipped.  */
109
29.2k
  padding_mnemonic = TILEPRO_OPC_FNOP;
110
63.4k
  for (i = 0; i < num_instructions; i++)
111
53.1k
    {
112
53.1k
      if (!decoded[i].opcode->can_bundle)
113
19.0k
  {
114
    /* Instructions that cannot be bundled are padded out with nops,
115
       rather than fnops. Displaying them is always clutter.  */
116
19.0k
    padding_mnemonic = TILEPRO_OPC_NOP;
117
19.0k
    break;
118
19.0k
  }
119
53.1k
    }
120
121
98.7k
  for (i = 0; i < num_instructions; i++)
122
69.4k
    {
123
69.4k
      const struct tilepro_opcode *opcode = decoded[i].opcode;
124
69.4k
      const char *name;
125
69.4k
      int j;
126
127
      /* Do not print out fnops, unless everything is an fnop, in
128
   which case we will print out just the last one.  */
129
69.4k
      if (opcode->mnemonic == padding_mnemonic
130
69.4k
    && (num_printed > 0 || i + 1 < num_instructions))
131
54
  continue;
132
133
69.4k
      if (num_printed > 0)
134
40.1k
  info->fprintf_func (info->stream, " ; ");
135
69.4k
      ++num_printed;
136
137
69.4k
      name = opcode->name;
138
69.4k
      if (name == NULL)
139
25.4k
  name = "<invalid>";
140
69.4k
      info->fprintf_func (info->stream, "%s", name);
141
142
188k
      for (j = 0; j < opcode->num_operands; j++)
143
119k
  {
144
119k
    int num;
145
119k
    const struct tilepro_operand *op;
146
119k
    const char *spr_name;
147
148
119k
    if (j > 0)
149
75.5k
      info->fprintf_func (info->stream, ",");
150
119k
    info->fprintf_func (info->stream, " ");
151
152
119k
    num = decoded[i].operand_values[j];
153
154
119k
    op = decoded[i].operands[j];
155
119k
    switch (op->type)
156
119k
      {
157
91.8k
      case TILEPRO_OP_TYPE_REGISTER:
158
91.8k
        info->fprintf_func (info->stream, "%s",
159
91.8k
          tilepro_register_names[num]);
160
91.8k
        break;
161
162
157
      case TILEPRO_OP_TYPE_SPR:
163
157
        spr_name = get_tilepro_spr_name(num);
164
157
        if (spr_name != NULL)
165
71
    info->fprintf_func (info->stream, "%s", spr_name);
166
86
        else
167
86
    info->fprintf_func (info->stream, "%d", num);
168
157
        break;
169
170
23.0k
      case TILEPRO_OP_TYPE_IMMEDIATE:
171
23.0k
        {
172
23.0k
    bfd_vma addr = 0;
173
23.0k
    int found_addr = 0;
174
23.0k
    int addr_piece;
175
176
23.0k
    switch (opcode->mnemonic)
177
23.0k
      {
178
2.31k
      case TILEPRO_OPC_ADDLI:
179
2.31k
        if (contains_insn (TILEPRO_OPC_AULI,
180
2.31k
               decoded[i].operand_values[1],
181
2.31k
               TREG_ZERO,
182
2.31k
               memaddr - TILEPRO_BUNDLE_SIZE_IN_BYTES,
183
2.31k
               &addr_piece,
184
2.31k
               info))
185
0
          {
186
0
      addr = num + (addr_piece << 16);
187
0
      found_addr = 1;
188
0
          }
189
2.31k
        break;
190
191
2.85k
      case TILEPRO_OPC_AULI:
192
2.85k
        if (contains_insn (TILEPRO_OPC_MOVELI,
193
2.85k
               decoded[i].operand_values[1],
194
2.85k
               -1,
195
2.85k
               memaddr - TILEPRO_BUNDLE_SIZE_IN_BYTES,
196
2.85k
               &addr_piece,
197
2.85k
               info))
198
0
          {
199
0
      addr = (num << 16) + addr_piece;
200
0
      found_addr = 1;
201
0
          }
202
2.85k
        break;
203
204
17.9k
      default:
205
        /* Operand does not look like a constructed address.  */
206
17.9k
        break;
207
23.0k
      }
208
209
23.0k
    info->fprintf_func (info->stream, "%d", num);
210
211
23.0k
    if (found_addr)
212
0
      {
213
0
        info->fprintf_func (info->stream, " /* ");
214
0
        info->print_address_func (addr, info);
215
0
        info->fprintf_func (info->stream, " */");
216
0
      }
217
23.0k
        }
218
0
        break;
219
220
4.44k
      case TILEPRO_OP_TYPE_ADDRESS:
221
4.44k
        info->print_address_func ((bfd_vma)(unsigned int) num, info);
222
4.44k
        break;
223
224
0
      default:
225
0
        abort ();
226
119k
      }
227
119k
  }
228
69.4k
    }
229
29.2k
  info->fprintf_func (info->stream, " }");
230
231
29.2k
  return TILEPRO_BUNDLE_SIZE_IN_BYTES;
232
29.2k
}