2012-05-10 10:52:23 +08:00
|
|
|
//===-- Opcode.cpp ----------------------------------------------*- C++ -*-===//
|
2011-03-25 07:53:38 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Core/Opcode.h"
|
|
|
|
|
2017-03-04 09:30:05 +08:00
|
|
|
#include "lldb/Utility/DataBufferHeap.h"
|
|
|
|
#include "lldb/Utility/DataExtractor.h"
|
2017-02-15 03:06:07 +08:00
|
|
|
#include "lldb/Utility/Endian.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/Stream.h"
|
2017-04-07 05:28:29 +08:00
|
|
|
#include "lldb/lldb-forward.h" // for DataBufferSP
|
|
|
|
|
|
|
|
#include <memory> // for make_shared
|
|
|
|
|
|
|
|
#include <inttypes.h> // for PRIx64
|
2011-03-25 07:53:38 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
2011-03-26 02:03:16 +08:00
|
|
|
using namespace lldb_private;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
int Opcode::Dump(Stream *s, uint32_t min_byte_width) {
|
|
|
|
int bytes_written = 0;
|
|
|
|
switch (m_type) {
|
|
|
|
case Opcode::eTypeInvalid:
|
|
|
|
bytes_written = s->PutCString("<invalid>");
|
|
|
|
break;
|
|
|
|
case Opcode::eType8:
|
|
|
|
bytes_written = s->Printf("0x%2.2x", m_data.inst8);
|
|
|
|
break;
|
|
|
|
case Opcode::eType16:
|
|
|
|
bytes_written = s->Printf("0x%4.4x", m_data.inst16);
|
|
|
|
break;
|
|
|
|
case Opcode::eType16_2:
|
|
|
|
case Opcode::eType32:
|
|
|
|
bytes_written = s->Printf("0x%8.8x", m_data.inst32);
|
|
|
|
break;
|
2011-03-26 02:03:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
case Opcode::eType64:
|
|
|
|
bytes_written = s->Printf("0x%16.16" PRIx64, m_data.inst64);
|
|
|
|
break;
|
2011-03-26 02:03:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
case Opcode::eTypeBytes:
|
|
|
|
for (uint32_t i = 0; i < m_data.inst.length; ++i) {
|
|
|
|
if (i > 0)
|
|
|
|
bytes_written += s->PutChar(' ');
|
|
|
|
bytes_written += s->Printf("%2.2x", m_data.inst.bytes[i]);
|
2011-03-26 02:03:16 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-10-04 23:29:20 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Add spaces to make sure bytes dispay comes out even in case opcodes aren't
|
|
|
|
// all the same size
|
2016-09-07 04:57:50 +08:00
|
|
|
if (static_cast<uint32_t>(bytes_written) < min_byte_width)
|
|
|
|
bytes_written = s->Printf("%*s", min_byte_width - bytes_written, "");
|
|
|
|
return bytes_written;
|
2011-03-26 02:03:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::ByteOrder Opcode::GetDataByteOrder() const {
|
|
|
|
if (m_byte_order != eByteOrderInvalid) {
|
|
|
|
return m_byte_order;
|
|
|
|
}
|
|
|
|
switch (m_type) {
|
|
|
|
case Opcode::eTypeInvalid:
|
|
|
|
break;
|
|
|
|
case Opcode::eType8:
|
|
|
|
case Opcode::eType16:
|
|
|
|
case Opcode::eType16_2:
|
|
|
|
case Opcode::eType32:
|
|
|
|
case Opcode::eType64:
|
|
|
|
return endian::InlHostByteOrder();
|
|
|
|
case Opcode::eTypeBytes:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return eByteOrderInvalid;
|
2011-09-26 15:11:27 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
uint32_t Opcode::GetData(DataExtractor &data) const {
|
|
|
|
uint32_t byte_size = GetByteSize();
|
|
|
|
uint8_t swap_buf[8];
|
|
|
|
const void *buf = nullptr;
|
2013-10-04 23:29:20 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (byte_size > 0) {
|
|
|
|
if (!GetEndianSwap()) {
|
|
|
|
if (m_type == Opcode::eType16_2) {
|
|
|
|
// 32 bit thumb instruction, we need to sizzle this a bit
|
|
|
|
swap_buf[0] = m_data.inst.bytes[2];
|
|
|
|
swap_buf[1] = m_data.inst.bytes[3];
|
|
|
|
swap_buf[2] = m_data.inst.bytes[0];
|
|
|
|
swap_buf[3] = m_data.inst.bytes[1];
|
|
|
|
buf = swap_buf;
|
|
|
|
} else {
|
|
|
|
buf = GetOpcodeDataBytes();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (m_type) {
|
|
|
|
case Opcode::eTypeInvalid:
|
|
|
|
break;
|
|
|
|
case Opcode::eType8:
|
|
|
|
buf = GetOpcodeDataBytes();
|
|
|
|
break;
|
|
|
|
case Opcode::eType16:
|
|
|
|
*(uint16_t *)swap_buf = llvm::ByteSwap_16(m_data.inst16);
|
|
|
|
buf = swap_buf;
|
|
|
|
break;
|
|
|
|
case Opcode::eType16_2:
|
|
|
|
swap_buf[0] = m_data.inst.bytes[1];
|
|
|
|
swap_buf[1] = m_data.inst.bytes[0];
|
|
|
|
swap_buf[2] = m_data.inst.bytes[3];
|
|
|
|
swap_buf[3] = m_data.inst.bytes[2];
|
|
|
|
buf = swap_buf;
|
|
|
|
break;
|
|
|
|
case Opcode::eType32:
|
|
|
|
*(uint32_t *)swap_buf = llvm::ByteSwap_32(m_data.inst32);
|
|
|
|
buf = swap_buf;
|
|
|
|
break;
|
|
|
|
case Opcode::eType64:
|
|
|
|
*(uint32_t *)swap_buf = llvm::ByteSwap_64(m_data.inst64);
|
|
|
|
buf = swap_buf;
|
|
|
|
break;
|
|
|
|
case Opcode::eTypeBytes:
|
|
|
|
buf = GetOpcodeDataBytes();
|
|
|
|
break;
|
|
|
|
}
|
2012-04-12 05:13:31 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
if (buf != nullptr) {
|
|
|
|
DataBufferSP buffer_sp;
|
2013-12-10 03:45:33 +08:00
|
|
|
|
2017-04-07 05:28:29 +08:00
|
|
|
buffer_sp = std::make_shared<DataBufferHeap>(buf, byte_size);
|
2016-09-07 04:57:50 +08:00
|
|
|
data.SetByteOrder(GetDataByteOrder());
|
|
|
|
data.SetData(buffer_sp);
|
|
|
|
return byte_size;
|
|
|
|
}
|
|
|
|
data.Clear();
|
|
|
|
return 0;
|
2012-04-12 05:13:31 +08:00
|
|
|
}
|