1ba812f42SGreg Clayton //===-- Opcode.cpp ----------------------------------------------*- C++ -*-===// 20ae96273SGreg Clayton // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60ae96273SGreg Clayton // 70ae96273SGreg Clayton //===----------------------------------------------------------------------===// 80ae96273SGreg Clayton 90ae96273SGreg Clayton #include "lldb/Core/Opcode.h" 100ae96273SGreg Clayton 11666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 12666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h" 1301c3243fSZachary Turner #include "lldb/Utility/Endian.h" 14bf9a7730SZachary Turner #include "lldb/Utility/Stream.h" 15672d2c12SJonas Devlieghere #include "lldb/lldb-forward.h" 162f3df613SZachary Turner 17672d2c12SJonas Devlieghere #include <memory> 182f3df613SZachary Turner 19672d2c12SJonas Devlieghere #include <inttypes.h> 200ae96273SGreg Clayton 210ae96273SGreg Clayton using namespace lldb; 220ae96273SGreg Clayton using namespace lldb_private; 231080edbcSGreg Clayton 24b9c1b51eSKate Stone int Opcode::Dump(Stream *s, uint32_t min_byte_width) { 255b06c228SRaphael Isemann const uint32_t previous_bytes = s->GetWrittenBytes(); 26b9c1b51eSKate Stone switch (m_type) { 271080edbcSGreg Clayton case Opcode::eTypeInvalid: 285b06c228SRaphael Isemann s->PutCString("<invalid>"); 291080edbcSGreg Clayton break; 301080edbcSGreg Clayton case Opcode::eType8: 315b06c228SRaphael Isemann s->Printf("0x%2.2x", m_data.inst8); 321080edbcSGreg Clayton break; 331080edbcSGreg Clayton case Opcode::eType16: 345b06c228SRaphael Isemann s->Printf("0x%4.4x", m_data.inst16); 351080edbcSGreg Clayton break; 365c97c2f7SSean Callanan case Opcode::eType16_2: 371080edbcSGreg Clayton case Opcode::eType32: 385b06c228SRaphael Isemann s->Printf("0x%8.8x", m_data.inst32); 391080edbcSGreg Clayton break; 401080edbcSGreg Clayton 411080edbcSGreg Clayton case Opcode::eType64: 425b06c228SRaphael Isemann s->Printf("0x%16.16" PRIx64, m_data.inst64); 431080edbcSGreg Clayton break; 441080edbcSGreg Clayton 451080edbcSGreg Clayton case Opcode::eTypeBytes: 46b9c1b51eSKate Stone for (uint32_t i = 0; i < m_data.inst.length; ++i) { 471080edbcSGreg Clayton if (i > 0) 485b06c228SRaphael Isemann s->PutChar(' '); 495b06c228SRaphael Isemann s->Printf("%2.2x", m_data.inst.bytes[i]); 501080edbcSGreg Clayton } 511080edbcSGreg Clayton break; 521080edbcSGreg Clayton } 531080edbcSGreg Clayton 545b06c228SRaphael Isemann uint32_t bytes_written_so_far = s->GetWrittenBytes() - previous_bytes; 555b06c228SRaphael Isemann // Add spaces to make sure bytes display comes out even in case opcodes aren't 565b06c228SRaphael Isemann // all the same size. 575b06c228SRaphael Isemann if (bytes_written_so_far < min_byte_width) 585b06c228SRaphael Isemann s->Printf("%*s", min_byte_width - bytes_written_so_far, ""); 595b06c228SRaphael Isemann return s->GetWrittenBytes() - previous_bytes; 601080edbcSGreg Clayton } 611080edbcSGreg Clayton 62b9c1b51eSKate Stone lldb::ByteOrder Opcode::GetDataByteOrder() const { 63b9c1b51eSKate Stone if (m_byte_order != eByteOrderInvalid) { 6490359963SEd Maste return m_byte_order; 6590359963SEd Maste } 66b9c1b51eSKate Stone switch (m_type) { 67b9c1b51eSKate Stone case Opcode::eTypeInvalid: 68b9c1b51eSKate Stone break; 698f7180b1SGreg Clayton case Opcode::eType8: 708f7180b1SGreg Clayton case Opcode::eType16: 715c97c2f7SSean Callanan case Opcode::eType16_2: 728f7180b1SGreg Clayton case Opcode::eType32: 73b9c1b51eSKate Stone case Opcode::eType64: 74b9c1b51eSKate Stone return endian::InlHostByteOrder(); 758f7180b1SGreg Clayton case Opcode::eTypeBytes: 768f7180b1SGreg Clayton break; 778f7180b1SGreg Clayton } 788f7180b1SGreg Clayton return eByteOrderInvalid; 798f7180b1SGreg Clayton } 808f7180b1SGreg Clayton 81b9c1b51eSKate Stone uint32_t Opcode::GetData(DataExtractor &data) const { 82d1411e1aSGreg Clayton uint32_t byte_size = GetByteSize(); 8390359963SEd Maste uint8_t swap_buf[8]; 848918372dSEugene Zelenko const void *buf = nullptr; 85ba812f42SGreg Clayton 86b9c1b51eSKate Stone if (byte_size > 0) { 87b9c1b51eSKate Stone if (!GetEndianSwap()) { 88b9c1b51eSKate Stone if (m_type == Opcode::eType16_2) { 8990359963SEd Maste // 32 bit thumb instruction, we need to sizzle this a bit 9090359963SEd Maste swap_buf[0] = m_data.inst.bytes[2]; 9190359963SEd Maste swap_buf[1] = m_data.inst.bytes[3]; 9290359963SEd Maste swap_buf[2] = m_data.inst.bytes[0]; 9390359963SEd Maste swap_buf[3] = m_data.inst.bytes[1]; 9490359963SEd Maste buf = swap_buf; 95b9c1b51eSKate Stone } else { 9690359963SEd Maste buf = GetOpcodeDataBytes(); 9790359963SEd Maste } 98b9c1b51eSKate Stone } else { 99b9c1b51eSKate Stone switch (m_type) { 100d1411e1aSGreg Clayton case Opcode::eTypeInvalid: 101d1411e1aSGreg Clayton break; 10290359963SEd Maste case Opcode::eType8: 10390359963SEd Maste buf = GetOpcodeDataBytes(); 10490359963SEd Maste break; 10590359963SEd Maste case Opcode::eType16: 10690359963SEd Maste *(uint16_t *)swap_buf = llvm::ByteSwap_16(m_data.inst16); 10790359963SEd Maste buf = swap_buf; 10890359963SEd Maste break; 10979101b5cSGreg Clayton case Opcode::eType16_2: 11090359963SEd Maste swap_buf[0] = m_data.inst.bytes[1]; 11190359963SEd Maste swap_buf[1] = m_data.inst.bytes[0]; 11290359963SEd Maste swap_buf[2] = m_data.inst.bytes[3]; 11390359963SEd Maste swap_buf[3] = m_data.inst.bytes[2]; 11490359963SEd Maste buf = swap_buf; 115cd4ae1abSSean Callanan break; 116cd4ae1abSSean Callanan case Opcode::eType32: 11790359963SEd Maste *(uint32_t *)swap_buf = llvm::ByteSwap_32(m_data.inst32); 11890359963SEd Maste buf = swap_buf; 11979101b5cSGreg Clayton break; 12090359963SEd Maste case Opcode::eType64: 12190359963SEd Maste *(uint32_t *)swap_buf = llvm::ByteSwap_64(m_data.inst64); 12290359963SEd Maste buf = swap_buf; 12390359963SEd Maste break; 12490359963SEd Maste case Opcode::eTypeBytes: 12590359963SEd Maste buf = GetOpcodeDataBytes(); 126d1411e1aSGreg Clayton break; 127d1411e1aSGreg Clayton } 128d1411e1aSGreg Clayton } 12990359963SEd Maste } 130b9c1b51eSKate Stone if (buf != nullptr) { 13190359963SEd Maste DataBufferSP buffer_sp; 13290359963SEd Maste 1332f3df613SZachary Turner buffer_sp = std::make_shared<DataBufferHeap>(buf, byte_size); 134d1411e1aSGreg Clayton data.SetByteOrder(GetDataByteOrder()); 135d1411e1aSGreg Clayton data.SetData(buffer_sp); 136d1411e1aSGreg Clayton return byte_size; 137d1411e1aSGreg Clayton } 138d1411e1aSGreg Clayton data.Clear(); 139d1411e1aSGreg Clayton return 0; 140d1411e1aSGreg Clayton } 141