1 //===-- Opcode.h ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
17 // Other libraries and framework includes
18 #include "llvm/Support/MathExtras.h"
21 #include "lldb/Host/Endian.h"
22 #include "lldb/lldb-public.h"
29 namespace lldb_private {
39 eType16_2, // a 32-bit Thumb instruction, made up of two words
45 Opcode () : m_byte_order (lldb::eByteOrderInvalid), m_type (eTypeInvalid)
49 Opcode (uint8_t inst, lldb::ByteOrder order) : m_byte_order (order), m_type (eType8)
54 Opcode (uint16_t inst, lldb::ByteOrder order) : m_byte_order (order), m_type (eType16)
59 Opcode (uint32_t inst, lldb::ByteOrder order) : m_byte_order (order), m_type (eType32)
64 Opcode (uint64_t inst, lldb::ByteOrder order) : m_byte_order (order), m_type (eType64)
69 Opcode (uint8_t *bytes, size_t length) : m_byte_order (lldb::eByteOrderInvalid)
71 SetOpcodeBytes (bytes, length);
77 m_byte_order = lldb::eByteOrderInvalid;
78 m_type = Opcode::eTypeInvalid;
88 GetOpcode8 (uint8_t invalid_opcode = UINT8_MAX) const
92 case Opcode::eTypeInvalid: break;
93 case Opcode::eType8: return m_data.inst8;
94 case Opcode::eType16: break;
95 case Opcode::eType16_2: break;
96 case Opcode::eType32: break;
97 case Opcode::eType64: break;
98 case Opcode::eTypeBytes: break;
100 return invalid_opcode;
104 GetOpcode16 (uint16_t invalid_opcode = UINT16_MAX) const
108 case Opcode::eTypeInvalid: break;
109 case Opcode::eType8: return m_data.inst8;
110 case Opcode::eType16: return GetEndianSwap() ? llvm::ByteSwap_16(m_data.inst16) : m_data.inst16;
111 case Opcode::eType16_2: break;
112 case Opcode::eType32: break;
113 case Opcode::eType64: break;
114 case Opcode::eTypeBytes: break;
116 return invalid_opcode;
120 GetOpcode32 (uint32_t invalid_opcode = UINT32_MAX) const
124 case Opcode::eTypeInvalid: break;
125 case Opcode::eType8: return m_data.inst8;
126 case Opcode::eType16: return GetEndianSwap() ? llvm::ByteSwap_16(m_data.inst16) : m_data.inst16;
127 case Opcode::eType16_2: // passthrough
128 case Opcode::eType32: return GetEndianSwap() ? llvm::ByteSwap_32(m_data.inst32) : m_data.inst32;
129 case Opcode::eType64: break;
130 case Opcode::eTypeBytes: break;
132 return invalid_opcode;
136 GetOpcode64 (uint64_t invalid_opcode = UINT64_MAX) const
140 case Opcode::eTypeInvalid: break;
141 case Opcode::eType8: return m_data.inst8;
142 case Opcode::eType16: return GetEndianSwap() ? llvm::ByteSwap_16(m_data.inst16) : m_data.inst16;
143 case Opcode::eType16_2: // passthrough
144 case Opcode::eType32: return GetEndianSwap() ? llvm::ByteSwap_32(m_data.inst32) : m_data.inst32;
145 case Opcode::eType64: return GetEndianSwap() ? llvm::ByteSwap_64(m_data.inst64) : m_data.inst64;
146 case Opcode::eTypeBytes: break;
148 return invalid_opcode;
152 SetOpcode8 (uint8_t inst, lldb::ByteOrder order)
156 m_byte_order = order;
160 SetOpcode16 (uint16_t inst, lldb::ByteOrder order)
163 m_data.inst16 = inst;
164 m_byte_order = order;
168 SetOpcode16_2 (uint32_t inst, lldb::ByteOrder order)
171 m_data.inst32 = inst;
172 m_byte_order = order;
176 SetOpcode32 (uint32_t inst, lldb::ByteOrder order)
179 m_data.inst32 = inst;
180 m_byte_order = order;
184 SetOpcode64 (uint64_t inst, lldb::ByteOrder order)
187 m_data.inst64 = inst;
188 m_byte_order = order;
192 SetOpcodeBytes (const void *bytes, size_t length)
194 if (bytes != nullptr && length > 0)
197 m_data.inst.length = length;
198 assert (length < sizeof (m_data.inst.bytes));
199 memcpy (m_data.inst.bytes, bytes, length);
200 m_byte_order = lldb::eByteOrderInvalid;
204 m_type = eTypeInvalid;
205 m_data.inst.length = 0;
210 Dump (Stream *s, uint32_t min_byte_width);
213 GetOpcodeBytes () const
215 return ((m_type == Opcode::eTypeBytes) ? m_data.inst.bytes : nullptr);
223 case Opcode::eTypeInvalid: break;
224 case Opcode::eType8: return sizeof(m_data.inst8);
225 case Opcode::eType16: return sizeof(m_data.inst16);
226 case Opcode::eType16_2: // passthrough
227 case Opcode::eType32: return sizeof(m_data.inst32);
228 case Opcode::eType64: return sizeof(m_data.inst64);
229 case Opcode::eTypeBytes: return m_data.inst.length;
234 // Get the opcode exactly as it would be laid out in memory.
236 GetData (DataExtractor &data) const;
240 friend class lldb::SBInstruction;
243 GetOpcodeDataBytes () const
247 case Opcode::eTypeInvalid: break;
248 case Opcode::eType8: return &m_data.inst8;
249 case Opcode::eType16: return &m_data.inst16;
250 case Opcode::eType16_2: // passthrough
251 case Opcode::eType32: return &m_data.inst32;
252 case Opcode::eType64: return &m_data.inst64;
253 case Opcode::eTypeBytes: return m_data.inst.bytes;
259 GetDataByteOrder () const;
262 GetEndianSwap() const
264 return (m_byte_order == lldb::eByteOrderBig && endian::InlHostByteOrder() == lldb::eByteOrderLittle) ||
265 (m_byte_order == lldb::eByteOrderLittle && endian::InlHostByteOrder() == lldb::eByteOrderBig);
268 lldb::ByteOrder m_byte_order;
279 uint8_t bytes[16]; // This must be big enough to handle any opcode for any supported target.
285 } // namespace lldb_private
287 #endif // lldb_Opcode_h