1 //===-- Opcode.cpp ----------------------------------------------*- 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 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Opcode.h"
14 // Other libraries and framework includes
15 #include "llvm/ADT/Triple.h"
18 #include "lldb/Core/ArchSpec.h"
19 #include "lldb/Core/DataBufferHeap.h"
20 #include "lldb/Core/DataExtractor.h"
21 #include "lldb/Core/Stream.h"
22 #include "lldb/Host/Endian.h"
25 using namespace lldb_private;
27 int Opcode::Dump(Stream *s, uint32_t min_byte_width) {
28 int bytes_written = 0;
30 case Opcode::eTypeInvalid:
31 bytes_written = s->PutCString("<invalid>");
34 bytes_written = s->Printf("0x%2.2x", m_data.inst8);
37 bytes_written = s->Printf("0x%4.4x", m_data.inst16);
39 case Opcode::eType16_2:
41 bytes_written = s->Printf("0x%8.8x", m_data.inst32);
45 bytes_written = s->Printf("0x%16.16" PRIx64, m_data.inst64);
48 case Opcode::eTypeBytes:
49 for (uint32_t i = 0; i < m_data.inst.length; ++i) {
51 bytes_written += s->PutChar(' ');
52 bytes_written += s->Printf("%2.2x", m_data.inst.bytes[i]);
57 // Add spaces to make sure bytes dispay comes out even in case opcodes
58 // aren't all the same size
59 if (static_cast<uint32_t>(bytes_written) < min_byte_width)
60 bytes_written = s->Printf("%*s", min_byte_width - bytes_written, "");
64 lldb::ByteOrder Opcode::GetDataByteOrder() const {
65 if (m_byte_order != eByteOrderInvalid) {
69 case Opcode::eTypeInvalid:
73 case Opcode::eType16_2:
76 return endian::InlHostByteOrder();
77 case Opcode::eTypeBytes:
80 return eByteOrderInvalid;
83 uint32_t Opcode::GetData(DataExtractor &data) const {
84 uint32_t byte_size = GetByteSize();
86 const void *buf = nullptr;
89 if (!GetEndianSwap()) {
90 if (m_type == Opcode::eType16_2) {
91 // 32 bit thumb instruction, we need to sizzle this a bit
92 swap_buf[0] = m_data.inst.bytes[2];
93 swap_buf[1] = m_data.inst.bytes[3];
94 swap_buf[2] = m_data.inst.bytes[0];
95 swap_buf[3] = m_data.inst.bytes[1];
98 buf = GetOpcodeDataBytes();
102 case Opcode::eTypeInvalid:
105 buf = GetOpcodeDataBytes();
107 case Opcode::eType16:
108 *(uint16_t *)swap_buf = llvm::ByteSwap_16(m_data.inst16);
111 case Opcode::eType16_2:
112 swap_buf[0] = m_data.inst.bytes[1];
113 swap_buf[1] = m_data.inst.bytes[0];
114 swap_buf[2] = m_data.inst.bytes[3];
115 swap_buf[3] = m_data.inst.bytes[2];
118 case Opcode::eType32:
119 *(uint32_t *)swap_buf = llvm::ByteSwap_32(m_data.inst32);
122 case Opcode::eType64:
123 *(uint32_t *)swap_buf = llvm::ByteSwap_64(m_data.inst64);
126 case Opcode::eTypeBytes:
127 buf = GetOpcodeDataBytes();
132 if (buf != nullptr) {
133 DataBufferSP buffer_sp;
135 buffer_sp.reset(new DataBufferHeap(buf, byte_size));
136 data.SetByteOrder(GetDataByteOrder());
137 data.SetData(buffer_sp);