1ba812f42SGreg Clayton //===-- Opcode.cpp ----------------------------------------------*- C++ -*-===// 20ae96273SGreg Clayton // 30ae96273SGreg Clayton // The LLVM Compiler Infrastructure 40ae96273SGreg Clayton // 50ae96273SGreg Clayton // This file is distributed under the University of Illinois Open Source 60ae96273SGreg Clayton // License. See LICENSE.TXT for details. 70ae96273SGreg Clayton // 80ae96273SGreg Clayton //===----------------------------------------------------------------------===// 90ae96273SGreg Clayton 100ae96273SGreg Clayton #include "lldb/Core/Opcode.h" 110ae96273SGreg Clayton 12666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 13666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h" 1401c3243fSZachary Turner #include "lldb/Utility/Endian.h" 15bf9a7730SZachary Turner #include "lldb/Utility/Stream.h" 16*672d2c12SJonas Devlieghere #include "lldb/lldb-forward.h" 172f3df613SZachary Turner 18*672d2c12SJonas Devlieghere #include <memory> 192f3df613SZachary Turner 20*672d2c12SJonas Devlieghere #include <inttypes.h> 210ae96273SGreg Clayton 220ae96273SGreg Clayton using namespace lldb; 230ae96273SGreg Clayton using namespace lldb_private; 241080edbcSGreg Clayton 25b9c1b51eSKate Stone int Opcode::Dump(Stream *s, uint32_t min_byte_width) { 265b06c228SRaphael Isemann const uint32_t previous_bytes = s->GetWrittenBytes(); 27b9c1b51eSKate Stone switch (m_type) { 281080edbcSGreg Clayton case Opcode::eTypeInvalid: 295b06c228SRaphael Isemann s->PutCString("<invalid>"); 301080edbcSGreg Clayton break; 311080edbcSGreg Clayton case Opcode::eType8: 325b06c228SRaphael Isemann s->Printf("0x%2.2x", m_data.inst8); 331080edbcSGreg Clayton break; 341080edbcSGreg Clayton case Opcode::eType16: 355b06c228SRaphael Isemann s->Printf("0x%4.4x", m_data.inst16); 361080edbcSGreg Clayton break; 375c97c2f7SSean Callanan case Opcode::eType16_2: 381080edbcSGreg Clayton case Opcode::eType32: 395b06c228SRaphael Isemann s->Printf("0x%8.8x", m_data.inst32); 401080edbcSGreg Clayton break; 411080edbcSGreg Clayton 421080edbcSGreg Clayton case Opcode::eType64: 435b06c228SRaphael Isemann s->Printf("0x%16.16" PRIx64, m_data.inst64); 441080edbcSGreg Clayton break; 451080edbcSGreg Clayton 461080edbcSGreg Clayton case Opcode::eTypeBytes: 47b9c1b51eSKate Stone for (uint32_t i = 0; i < m_data.inst.length; ++i) { 481080edbcSGreg Clayton if (i > 0) 495b06c228SRaphael Isemann s->PutChar(' '); 505b06c228SRaphael Isemann s->Printf("%2.2x", m_data.inst.bytes[i]); 511080edbcSGreg Clayton } 521080edbcSGreg Clayton break; 531080edbcSGreg Clayton } 541080edbcSGreg Clayton 555b06c228SRaphael Isemann uint32_t bytes_written_so_far = s->GetWrittenBytes() - previous_bytes; 565b06c228SRaphael Isemann // Add spaces to make sure bytes display comes out even in case opcodes aren't 575b06c228SRaphael Isemann // all the same size. 585b06c228SRaphael Isemann if (bytes_written_so_far < min_byte_width) 595b06c228SRaphael Isemann s->Printf("%*s", min_byte_width - bytes_written_so_far, ""); 605b06c228SRaphael Isemann return s->GetWrittenBytes() - previous_bytes; 611080edbcSGreg Clayton } 621080edbcSGreg Clayton 63b9c1b51eSKate Stone lldb::ByteOrder Opcode::GetDataByteOrder() const { 64b9c1b51eSKate Stone if (m_byte_order != eByteOrderInvalid) { 6590359963SEd Maste return m_byte_order; 6690359963SEd Maste } 67b9c1b51eSKate Stone switch (m_type) { 68b9c1b51eSKate Stone case Opcode::eTypeInvalid: 69b9c1b51eSKate Stone break; 708f7180b1SGreg Clayton case Opcode::eType8: 718f7180b1SGreg Clayton case Opcode::eType16: 725c97c2f7SSean Callanan case Opcode::eType16_2: 738f7180b1SGreg Clayton case Opcode::eType32: 74b9c1b51eSKate Stone case Opcode::eType64: 75b9c1b51eSKate Stone return endian::InlHostByteOrder(); 768f7180b1SGreg Clayton case Opcode::eTypeBytes: 778f7180b1SGreg Clayton break; 788f7180b1SGreg Clayton } 798f7180b1SGreg Clayton return eByteOrderInvalid; 808f7180b1SGreg Clayton } 818f7180b1SGreg Clayton 82b9c1b51eSKate Stone uint32_t Opcode::GetData(DataExtractor &data) const { 83d1411e1aSGreg Clayton uint32_t byte_size = GetByteSize(); 8490359963SEd Maste uint8_t swap_buf[8]; 858918372dSEugene Zelenko const void *buf = nullptr; 86ba812f42SGreg Clayton 87b9c1b51eSKate Stone if (byte_size > 0) { 88b9c1b51eSKate Stone if (!GetEndianSwap()) { 89b9c1b51eSKate Stone if (m_type == Opcode::eType16_2) { 9090359963SEd Maste // 32 bit thumb instruction, we need to sizzle this a bit 9190359963SEd Maste swap_buf[0] = m_data.inst.bytes[2]; 9290359963SEd Maste swap_buf[1] = m_data.inst.bytes[3]; 9390359963SEd Maste swap_buf[2] = m_data.inst.bytes[0]; 9490359963SEd Maste swap_buf[3] = m_data.inst.bytes[1]; 9590359963SEd Maste buf = swap_buf; 96b9c1b51eSKate Stone } else { 9790359963SEd Maste buf = GetOpcodeDataBytes(); 9890359963SEd Maste } 99b9c1b51eSKate Stone } else { 100b9c1b51eSKate Stone switch (m_type) { 101d1411e1aSGreg Clayton case Opcode::eTypeInvalid: 102d1411e1aSGreg Clayton break; 10390359963SEd Maste case Opcode::eType8: 10490359963SEd Maste buf = GetOpcodeDataBytes(); 10590359963SEd Maste break; 10690359963SEd Maste case Opcode::eType16: 10790359963SEd Maste *(uint16_t *)swap_buf = llvm::ByteSwap_16(m_data.inst16); 10890359963SEd Maste buf = swap_buf; 10990359963SEd Maste break; 11079101b5cSGreg Clayton case Opcode::eType16_2: 11190359963SEd Maste swap_buf[0] = m_data.inst.bytes[1]; 11290359963SEd Maste swap_buf[1] = m_data.inst.bytes[0]; 11390359963SEd Maste swap_buf[2] = m_data.inst.bytes[3]; 11490359963SEd Maste swap_buf[3] = m_data.inst.bytes[2]; 11590359963SEd Maste buf = swap_buf; 116cd4ae1abSSean Callanan break; 117cd4ae1abSSean Callanan case Opcode::eType32: 11890359963SEd Maste *(uint32_t *)swap_buf = llvm::ByteSwap_32(m_data.inst32); 11990359963SEd Maste buf = swap_buf; 12079101b5cSGreg Clayton break; 12190359963SEd Maste case Opcode::eType64: 12290359963SEd Maste *(uint32_t *)swap_buf = llvm::ByteSwap_64(m_data.inst64); 12390359963SEd Maste buf = swap_buf; 12490359963SEd Maste break; 12590359963SEd Maste case Opcode::eTypeBytes: 12690359963SEd Maste buf = GetOpcodeDataBytes(); 127d1411e1aSGreg Clayton break; 128d1411e1aSGreg Clayton } 129d1411e1aSGreg Clayton } 13090359963SEd Maste } 131b9c1b51eSKate Stone if (buf != nullptr) { 13290359963SEd Maste DataBufferSP buffer_sp; 13390359963SEd Maste 1342f3df613SZachary Turner buffer_sp = std::make_shared<DataBufferHeap>(buf, byte_size); 135d1411e1aSGreg Clayton data.SetByteOrder(GetDataByteOrder()); 136d1411e1aSGreg Clayton data.SetData(buffer_sp); 137d1411e1aSGreg Clayton return byte_size; 138d1411e1aSGreg Clayton } 139d1411e1aSGreg Clayton data.Clear(); 140d1411e1aSGreg Clayton return 0; 141d1411e1aSGreg Clayton } 142