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 120ae96273SGreg Clayton // C Includes 130ae96273SGreg Clayton // C++ Includes 140ae96273SGreg Clayton // Other libraries and framework includes 15ba812f42SGreg Clayton #include "llvm/ADT/Triple.h" 16*8918372dSEugene Zelenko 170ae96273SGreg Clayton // Project includes 18ba812f42SGreg Clayton #include "lldb/Core/ArchSpec.h" 19d1411e1aSGreg Clayton #include "lldb/Core/DataBufferHeap.h" 20d1411e1aSGreg Clayton #include "lldb/Core/DataExtractor.h" 211080edbcSGreg Clayton #include "lldb/Core/Stream.h" 228f7180b1SGreg Clayton #include "lldb/Host/Endian.h" 230ae96273SGreg Clayton 240ae96273SGreg Clayton using namespace lldb; 250ae96273SGreg Clayton using namespace lldb_private; 261080edbcSGreg Clayton 271080edbcSGreg Clayton int 281080edbcSGreg Clayton Opcode::Dump (Stream *s, uint32_t min_byte_width) 291080edbcSGreg Clayton { 301080edbcSGreg Clayton int bytes_written = 0; 311080edbcSGreg Clayton switch (m_type) 321080edbcSGreg Clayton { 331080edbcSGreg Clayton case Opcode::eTypeInvalid: 341080edbcSGreg Clayton bytes_written = s->PutCString ("<invalid>"); 351080edbcSGreg Clayton break; 361080edbcSGreg Clayton case Opcode::eType8: 371080edbcSGreg Clayton bytes_written = s->Printf ("0x%2.2x", m_data.inst8); 381080edbcSGreg Clayton break; 391080edbcSGreg Clayton case Opcode::eType16: 401080edbcSGreg Clayton bytes_written = s->Printf ("0x%4.4x", m_data.inst16); 411080edbcSGreg Clayton break; 425c97c2f7SSean Callanan case Opcode::eType16_2: 431080edbcSGreg Clayton case Opcode::eType32: 441080edbcSGreg Clayton bytes_written = s->Printf ("0x%8.8x", m_data.inst32); 451080edbcSGreg Clayton break; 461080edbcSGreg Clayton 471080edbcSGreg Clayton case Opcode::eType64: 48d01b2953SDaniel Malea bytes_written = s->Printf ("0x%16.16" PRIx64, m_data.inst64); 491080edbcSGreg Clayton break; 501080edbcSGreg Clayton 511080edbcSGreg Clayton case Opcode::eTypeBytes: 521080edbcSGreg Clayton for (uint32_t i = 0; i < m_data.inst.length; ++i) 531080edbcSGreg Clayton { 541080edbcSGreg Clayton if (i > 0) 55357132ebSGreg Clayton bytes_written += s->PutChar (' '); 561080edbcSGreg Clayton bytes_written += s->Printf ("%2.2x", m_data.inst.bytes[i]); 571080edbcSGreg Clayton } 581080edbcSGreg Clayton break; 591080edbcSGreg Clayton } 601080edbcSGreg Clayton 611080edbcSGreg Clayton // Add spaces to make sure bytes dispay comes out even in case opcodes 621080edbcSGreg Clayton // aren't all the same size 633985c8c6SSaleem Abdulrasool if (static_cast<uint32_t>(bytes_written) < min_byte_width) 641080edbcSGreg Clayton bytes_written = s->Printf ("%*s", min_byte_width - bytes_written, ""); 651080edbcSGreg Clayton return bytes_written; 661080edbcSGreg Clayton } 671080edbcSGreg Clayton 688f7180b1SGreg Clayton lldb::ByteOrder 698f7180b1SGreg Clayton Opcode::GetDataByteOrder () const 708f7180b1SGreg Clayton { 7190359963SEd Maste if (m_byte_order != eByteOrderInvalid) 7290359963SEd Maste { 7390359963SEd Maste return m_byte_order; 7490359963SEd Maste } 758f7180b1SGreg Clayton switch (m_type) 768f7180b1SGreg Clayton { 778f7180b1SGreg Clayton case Opcode::eTypeInvalid: break; 788f7180b1SGreg Clayton case Opcode::eType8: 798f7180b1SGreg Clayton case Opcode::eType16: 805c97c2f7SSean Callanan case Opcode::eType16_2: 818f7180b1SGreg Clayton case Opcode::eType32: 829ccb970fSBruce Mitchener case Opcode::eType64: return endian::InlHostByteOrder(); 838f7180b1SGreg Clayton case Opcode::eTypeBytes: 848f7180b1SGreg Clayton break; 858f7180b1SGreg Clayton } 868f7180b1SGreg Clayton return eByteOrderInvalid; 878f7180b1SGreg Clayton } 888f7180b1SGreg Clayton 89d1411e1aSGreg Clayton uint32_t 90cd4ae1abSSean Callanan Opcode::GetData (DataExtractor &data) const 91d1411e1aSGreg Clayton { 92d1411e1aSGreg Clayton uint32_t byte_size = GetByteSize (); 9390359963SEd Maste uint8_t swap_buf[8]; 94*8918372dSEugene Zelenko const void *buf = nullptr; 95ba812f42SGreg Clayton 96d1411e1aSGreg Clayton if (byte_size > 0) 97d1411e1aSGreg Clayton { 9890359963SEd Maste if (!GetEndianSwap()) 9990359963SEd Maste { 10090359963SEd Maste if (m_type == Opcode::eType16_2) 10190359963SEd Maste { 10290359963SEd Maste // 32 bit thumb instruction, we need to sizzle this a bit 10390359963SEd Maste swap_buf[0] = m_data.inst.bytes[2]; 10490359963SEd Maste swap_buf[1] = m_data.inst.bytes[3]; 10590359963SEd Maste swap_buf[2] = m_data.inst.bytes[0]; 10690359963SEd Maste swap_buf[3] = m_data.inst.bytes[1]; 10790359963SEd Maste buf = swap_buf; 10890359963SEd Maste } 10990359963SEd Maste else 11090359963SEd Maste { 11190359963SEd Maste buf = GetOpcodeDataBytes(); 11290359963SEd Maste } 11390359963SEd Maste } 11490359963SEd Maste else 11590359963SEd Maste { 116d1411e1aSGreg Clayton switch (m_type) 117d1411e1aSGreg Clayton { 118d1411e1aSGreg Clayton case Opcode::eTypeInvalid: 119d1411e1aSGreg Clayton break; 12090359963SEd Maste case Opcode::eType8: 12190359963SEd Maste buf = GetOpcodeDataBytes(); 12290359963SEd Maste break; 12390359963SEd Maste case Opcode::eType16: 12490359963SEd Maste *(uint16_t *)swap_buf = llvm::ByteSwap_16(m_data.inst16); 12590359963SEd Maste buf = swap_buf; 12690359963SEd Maste break; 12779101b5cSGreg Clayton case Opcode::eType16_2: 12890359963SEd Maste swap_buf[0] = m_data.inst.bytes[1]; 12990359963SEd Maste swap_buf[1] = m_data.inst.bytes[0]; 13090359963SEd Maste swap_buf[2] = m_data.inst.bytes[3]; 13190359963SEd Maste swap_buf[3] = m_data.inst.bytes[2]; 13290359963SEd Maste buf = swap_buf; 133cd4ae1abSSean Callanan break; 134cd4ae1abSSean Callanan case Opcode::eType32: 13590359963SEd Maste *(uint32_t *)swap_buf = llvm::ByteSwap_32(m_data.inst32); 13690359963SEd Maste buf = swap_buf; 13779101b5cSGreg Clayton break; 13890359963SEd Maste case Opcode::eType64: 13990359963SEd Maste *(uint32_t *)swap_buf = llvm::ByteSwap_64(m_data.inst64); 14090359963SEd Maste buf = swap_buf; 14190359963SEd Maste break; 14290359963SEd Maste case Opcode::eTypeBytes: 14390359963SEd Maste buf = GetOpcodeDataBytes(); 144d1411e1aSGreg Clayton break; 145d1411e1aSGreg Clayton } 146d1411e1aSGreg Clayton } 14790359963SEd Maste } 148*8918372dSEugene Zelenko if (buf != nullptr) 149d1411e1aSGreg Clayton { 15090359963SEd Maste DataBufferSP buffer_sp; 15190359963SEd Maste 15290359963SEd Maste buffer_sp.reset (new DataBufferHeap (buf, byte_size)); 153d1411e1aSGreg Clayton data.SetByteOrder(GetDataByteOrder()); 154d1411e1aSGreg Clayton data.SetData (buffer_sp); 155d1411e1aSGreg Clayton return byte_size; 156d1411e1aSGreg Clayton } 157d1411e1aSGreg Clayton data.Clear(); 158d1411e1aSGreg Clayton return 0; 159d1411e1aSGreg Clayton } 160