Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Core/Opcode.h
Line
Count
Source (jump to first uncovered line)
1
//===-- Opcode.h ------------------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLDB_CORE_OPCODE_H
10
#define LLDB_CORE_OPCODE_H
11
12
#include "lldb/Utility/Endian.h"
13
#include "lldb/lldb-enumerations.h"
14
15
#include "llvm/Support/SwapByteOrder.h"
16
17
#include <cassert>
18
#include <cstdint>
19
#include <cstring>
20
21
namespace lldb {
22
class SBInstruction;
23
}
24
25
namespace lldb_private {
26
class DataExtractor;
27
class Stream;
28
29
class Opcode {
30
public:
31
  enum Type {
32
    eTypeInvalid,
33
    eType8,
34
    eType16,
35
    eType16_2, // a 32-bit Thumb instruction, made up of two words
36
    eType32,
37
    eType64,
38
    eTypeBytes
39
  };
40
41
984k
  Opcode() = default;
42
43
  Opcode(uint8_t inst, lldb::ByteOrder order)
44
0
      : m_byte_order(order), m_type(eType8) {
45
0
    m_data.inst8 = inst;
46
0
  }
47
48
  Opcode(uint16_t inst, lldb::ByteOrder order)
49
0
      : m_byte_order(order), m_type(eType16) {
50
0
    m_data.inst16 = inst;
51
0
  }
52
53
  Opcode(uint32_t inst, lldb::ByteOrder order)
54
0
      : m_byte_order(order), m_type(eType32) {
55
0
    m_data.inst32 = inst;
56
0
  }
57
58
  Opcode(uint64_t inst, lldb::ByteOrder order)
59
0
      : m_byte_order(order), m_type(eType64) {
60
0
    m_data.inst64 = inst;
61
0
  }
62
63
  Opcode(uint8_t *bytes, size_t length)
64
0
      : m_byte_order(lldb::eByteOrderInvalid) {
65
0
    SetOpcodeBytes(bytes, length);
66
0
  }
67
68
280
  void Clear() {
69
280
    m_byte_order = lldb::eByteOrderInvalid;
70
280
    m_type = Opcode::eTypeInvalid;
71
280
  }
72
73
9.08k
  Opcode::Type GetType() const { return m_type; }
74
75
0
  uint8_t GetOpcode8(uint8_t invalid_opcode = UINT8_MAX) const {
76
0
    switch (m_type) {
77
0
    case Opcode::eTypeInvalid:
78
0
      break;
79
0
    case Opcode::eType8:
80
0
      return m_data.inst8;
81
0
    case Opcode::eType16:
82
0
      break;
83
0
    case Opcode::eType16_2:
84
0
      break;
85
0
    case Opcode::eType32:
86
0
      break;
87
0
    case Opcode::eType64:
88
0
      break;
89
0
    case Opcode::eTypeBytes:
90
0
      break;
91
0
    }
92
0
    return invalid_opcode;
93
0
  }
94
95
0
  uint16_t GetOpcode16(uint16_t invalid_opcode = UINT16_MAX) const {
96
0
    switch (m_type) {
97
0
    case Opcode::eTypeInvalid:
98
0
      break;
99
0
    case Opcode::eType8:
100
0
      return m_data.inst8;
101
0
    case Opcode::eType16:
102
0
      return GetEndianSwap() ? llvm::ByteSwap_16(m_data.inst16) : m_data.inst16;
103
0
    case Opcode::eType16_2:
104
0
      break;
105
0
    case Opcode::eType32:
106
0
      break;
107
0
    case Opcode::eType64:
108
0
      break;
109
0
    case Opcode::eTypeBytes:
110
0
      break;
111
0
    }
112
0
    return invalid_opcode;
113
0
  }
114
115
478
  uint32_t GetOpcode32(uint32_t invalid_opcode = UINT32_MAX) const {
116
478
    switch (m_type) {
117
0
    case Opcode::eTypeInvalid:
118
0
      break;
119
0
    case Opcode::eType8:
120
0
      return m_data.inst8;
121
138
    case Opcode::eType16:
122
138
      return GetEndianSwap() ? 
llvm::ByteSwap_16(m_data.inst16)0
: m_data.inst16;
123
20
    case Opcode::eType16_2: // passthrough
124
340
    case Opcode::eType32:
125
340
      return GetEndianSwap() ? 
llvm::ByteSwap_32(m_data.inst32)0
: m_data.inst32;
126
0
    case Opcode::eType64:
127
0
      break;
128
0
    case Opcode::eTypeBytes:
129
0
      break;
130
478
    }
131
0
    return invalid_opcode;
132
478
  }
133
134
0
  uint64_t GetOpcode64(uint64_t invalid_opcode = UINT64_MAX) const {
135
0
    switch (m_type) {
136
0
    case Opcode::eTypeInvalid:
137
0
      break;
138
0
    case Opcode::eType8:
139
0
      return m_data.inst8;
140
0
    case Opcode::eType16:
141
0
      return GetEndianSwap() ? llvm::ByteSwap_16(m_data.inst16) : m_data.inst16;
142
0
    case Opcode::eType16_2: // passthrough
143
0
    case Opcode::eType32:
144
0
      return GetEndianSwap() ? llvm::ByteSwap_32(m_data.inst32) : m_data.inst32;
145
0
    case Opcode::eType64:
146
0
      return GetEndianSwap() ? llvm::ByteSwap_64(m_data.inst64) : m_data.inst64;
147
0
    case Opcode::eTypeBytes:
148
0
      break;
149
0
    }
150
0
    return invalid_opcode;
151
0
  }
152
153
0
  void SetOpcode8(uint8_t inst, lldb::ByteOrder order) {
154
0
    m_type = eType8;
155
0
    m_data.inst8 = inst;
156
0
    m_byte_order = order;
157
0
  }
158
159
72
  void SetOpcode16(uint16_t inst, lldb::ByteOrder order) {
160
72
    m_type = eType16;
161
72
    m_data.inst16 = inst;
162
72
    m_byte_order = order;
163
72
  }
164
165
12
  void SetOpcode16_2(uint32_t inst, lldb::ByteOrder order) {
166
12
    m_type = eType16_2;
167
12
    m_data.inst32 = inst;
168
12
    m_byte_order = order;
169
12
  }
170
171
274
  void SetOpcode32(uint32_t inst, lldb::ByteOrder order) {
172
274
    m_type = eType32;
173
274
    m_data.inst32 = inst;
174
274
    m_byte_order = order;
175
274
  }
176
177
0
  void SetOpcode64(uint64_t inst, lldb::ByteOrder order) {
178
0
    m_type = eType64;
179
0
    m_data.inst64 = inst;
180
0
    m_byte_order = order;
181
0
  }
182
183
983k
  void SetOpcodeBytes(const void *bytes, size_t length) {
184
983k
    if (bytes != nullptr && length > 0) {
185
983k
      m_type = eTypeBytes;
186
983k
      m_data.inst.length = length;
187
983k
      assert(length < sizeof(m_data.inst.bytes));
188
0
      memcpy(m_data.inst.bytes, bytes, length);
189
983k
      m_byte_order = lldb::eByteOrderInvalid;
190
983k
    } else {
191
0
      m_type = eTypeInvalid;
192
0
      m_data.inst.length = 0;
193
0
    }
194
983k
  }
195
196
  int Dump(Stream *s, uint32_t min_byte_width);
197
198
0
  const void *GetOpcodeBytes() const {
199
0
    return ((m_type == Opcode::eTypeBytes) ? m_data.inst.bytes : nullptr);
200
0
  }
201
202
2.94M
  uint32_t GetByteSize() const {
203
2.94M
    switch (m_type) {
204
280
    case Opcode::eTypeInvalid:
205
280
      break;
206
0
    case Opcode::eType8:
207
0
      return sizeof(m_data.inst8);
208
147
    case Opcode::eType16:
209
147
      return sizeof(m_data.inst16);
210
38
    case Opcode::eType16_2: // passthrough
211
582
    case Opcode::eType32:
212
582
      return sizeof(m_data.inst32);
213
0
    case Opcode::eType64:
214
0
      return sizeof(m_data.inst64);
215
2.94M
    case Opcode::eTypeBytes:
216
2.94M
      return m_data.inst.length;
217
2.94M
    }
218
280
    return 0;
219
2.94M
  }
220
221
  // Get the opcode exactly as it would be laid out in memory.
222
  uint32_t GetData(DataExtractor &data) const;
223
224
protected:
225
  friend class lldb::SBInstruction;
226
227
981k
  const void *GetOpcodeDataBytes() const {
228
981k
    switch (m_type) {
229
0
    case Opcode::eTypeInvalid:
230
0
      break;
231
0
    case Opcode::eType8:
232
0
      return &m_data.inst8;
233
5
    case Opcode::eType16:
234
5
      return &m_data.inst16;
235
0
    case Opcode::eType16_2: // passthrough
236
40
    case Opcode::eType32:
237
40
      return &m_data.inst32;
238
0
    case Opcode::eType64:
239
0
      return &m_data.inst64;
240
981k
    case Opcode::eTypeBytes:
241
981k
      return m_data.inst.bytes;
242
981k
    }
243
0
    return nullptr;
244
981k
  }
245
246
  lldb::ByteOrder GetDataByteOrder() const;
247
248
981k
  bool GetEndianSwap() const {
249
981k
    return (m_byte_order == lldb::eByteOrderBig &&
250
981k
            
endian::InlHostByteOrder() == lldb::eByteOrderLittle0
) ||
251
981k
           (m_byte_order == lldb::eByteOrderLittle &&
252
981k
            
endian::InlHostByteOrder() == lldb::eByteOrderBig530
);
253
981k
  }
254
255
  lldb::ByteOrder m_byte_order = lldb::eByteOrderInvalid;
256
257
  Opcode::Type m_type = eTypeInvalid;
258
  union {
259
    uint8_t inst8;
260
    uint16_t inst16;
261
    uint32_t inst32;
262
    uint64_t inst64;
263
    struct {
264
      uint8_t bytes[16]; // This must be big enough to handle any opcode for any
265
                         // supported target.
266
      uint8_t length;
267
    } inst;
268
  } m_data;
269
};
270
271
} // namespace lldb_private
272
273
#endif // LLDB_CORE_OPCODE_H