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" 160ae96273SGreg Clayton // Project includes 17ba812f42SGreg Clayton #include "lldb/Core/ArchSpec.h" 18d1411e1aSGreg Clayton #include "lldb/Core/DataBufferHeap.h" 19d1411e1aSGreg Clayton #include "lldb/Core/DataExtractor.h" 201080edbcSGreg Clayton #include "lldb/Core/Stream.h" 218f7180b1SGreg Clayton #include "lldb/Host/Endian.h" 220ae96273SGreg Clayton 23ba812f42SGreg Clayton 240ae96273SGreg Clayton using namespace lldb; 250ae96273SGreg Clayton using namespace lldb_private; 261080edbcSGreg Clayton 271080edbcSGreg Clayton 281080edbcSGreg Clayton int 291080edbcSGreg Clayton Opcode::Dump (Stream *s, uint32_t min_byte_width) 301080edbcSGreg Clayton { 311080edbcSGreg Clayton int bytes_written = 0; 321080edbcSGreg Clayton switch (m_type) 331080edbcSGreg Clayton { 341080edbcSGreg Clayton case Opcode::eTypeInvalid: 351080edbcSGreg Clayton bytes_written = s->PutCString ("<invalid>"); 361080edbcSGreg Clayton break; 371080edbcSGreg Clayton case Opcode::eType8: 381080edbcSGreg Clayton bytes_written = s->Printf ("0x%2.2x", m_data.inst8); 391080edbcSGreg Clayton break; 401080edbcSGreg Clayton case Opcode::eType16: 411080edbcSGreg Clayton bytes_written = s->Printf ("0x%4.4x", m_data.inst16); 421080edbcSGreg Clayton break; 435c97c2f7SSean Callanan case Opcode::eType16_2: 441080edbcSGreg Clayton case Opcode::eType32: 451080edbcSGreg Clayton bytes_written = s->Printf ("0x%8.8x", m_data.inst32); 461080edbcSGreg Clayton break; 471080edbcSGreg Clayton 481080edbcSGreg Clayton case Opcode::eType64: 491080edbcSGreg Clayton bytes_written = s->Printf ("0x%16.16llx", m_data.inst64); 501080edbcSGreg Clayton break; 511080edbcSGreg Clayton 521080edbcSGreg Clayton case Opcode::eTypeBytes: 531080edbcSGreg Clayton { 541080edbcSGreg Clayton for (uint32_t i=0; i<m_data.inst.length; ++i) 551080edbcSGreg Clayton { 561080edbcSGreg Clayton if (i > 0) 57357132ebSGreg Clayton bytes_written += s->PutChar (' '); 581080edbcSGreg Clayton bytes_written += s->Printf ("%2.2x", m_data.inst.bytes[i]); 591080edbcSGreg Clayton } 601080edbcSGreg Clayton } 611080edbcSGreg Clayton break; 621080edbcSGreg Clayton } 631080edbcSGreg Clayton 641080edbcSGreg Clayton // Add spaces to make sure bytes dispay comes out even in case opcodes 651080edbcSGreg Clayton // aren't all the same size 661080edbcSGreg Clayton if (bytes_written < min_byte_width) 671080edbcSGreg Clayton bytes_written = s->Printf ("%*s", min_byte_width - bytes_written, ""); 681080edbcSGreg Clayton return bytes_written; 691080edbcSGreg Clayton } 701080edbcSGreg Clayton 718f7180b1SGreg Clayton lldb::ByteOrder 728f7180b1SGreg Clayton Opcode::GetDataByteOrder () const 738f7180b1SGreg Clayton { 748f7180b1SGreg Clayton switch (m_type) 758f7180b1SGreg Clayton { 768f7180b1SGreg Clayton case Opcode::eTypeInvalid: break; 778f7180b1SGreg Clayton case Opcode::eType8: 788f7180b1SGreg Clayton case Opcode::eType16: 795c97c2f7SSean Callanan case Opcode::eType16_2: 808f7180b1SGreg Clayton case Opcode::eType32: 818f7180b1SGreg Clayton case Opcode::eType64: return lldb::endian::InlHostByteOrder(); 828f7180b1SGreg Clayton case Opcode::eTypeBytes: 838f7180b1SGreg Clayton break; 848f7180b1SGreg Clayton } 858f7180b1SGreg Clayton return eByteOrderInvalid; 868f7180b1SGreg Clayton } 878f7180b1SGreg Clayton 88d1411e1aSGreg Clayton uint32_t 89*cd4ae1abSSean Callanan Opcode::GetData (DataExtractor &data) const 90d1411e1aSGreg Clayton { 91d1411e1aSGreg Clayton uint32_t byte_size = GetByteSize (); 92ba812f42SGreg Clayton 93d1411e1aSGreg Clayton DataBufferSP buffer_sp; 94d1411e1aSGreg Clayton if (byte_size > 0) 95d1411e1aSGreg Clayton { 96d1411e1aSGreg Clayton switch (m_type) 97d1411e1aSGreg Clayton { 98d1411e1aSGreg Clayton case Opcode::eTypeInvalid: 99d1411e1aSGreg Clayton break; 100d1411e1aSGreg Clayton 101d1411e1aSGreg Clayton case Opcode::eType8: buffer_sp.reset (new DataBufferHeap (&m_data.inst8, byte_size)); break; 102d1411e1aSGreg Clayton case Opcode::eType16: buffer_sp.reset (new DataBufferHeap (&m_data.inst16, byte_size)); break; 10379101b5cSGreg Clayton case Opcode::eType16_2: 10479101b5cSGreg Clayton { 10579101b5cSGreg Clayton // 32 bit thumb instruction, we need to sizzle this a bit 10679101b5cSGreg Clayton uint8_t buf[4]; 10779101b5cSGreg Clayton buf[0] = m_data.inst.bytes[2]; 10879101b5cSGreg Clayton buf[1] = m_data.inst.bytes[3]; 10979101b5cSGreg Clayton buf[2] = m_data.inst.bytes[0]; 11079101b5cSGreg Clayton buf[3] = m_data.inst.bytes[1]; 11179101b5cSGreg Clayton buffer_sp.reset (new DataBufferHeap (buf, byte_size)); 11279101b5cSGreg Clayton } 113*cd4ae1abSSean Callanan break; 114*cd4ae1abSSean Callanan case Opcode::eType32: 11579101b5cSGreg Clayton buffer_sp.reset (new DataBufferHeap (&m_data.inst32, byte_size)); 11679101b5cSGreg Clayton break; 117d1411e1aSGreg Clayton case Opcode::eType64: buffer_sp.reset (new DataBufferHeap (&m_data.inst64, byte_size)); break; 118d1411e1aSGreg Clayton case Opcode::eTypeBytes:buffer_sp.reset (new DataBufferHeap (GetOpcodeBytes(), byte_size)); break; 119d1411e1aSGreg Clayton break; 120d1411e1aSGreg Clayton } 121d1411e1aSGreg Clayton } 122d1411e1aSGreg Clayton 123d1411e1aSGreg Clayton if (buffer_sp) 124d1411e1aSGreg Clayton { 125d1411e1aSGreg Clayton data.SetByteOrder(GetDataByteOrder()); 126d1411e1aSGreg Clayton data.SetData (buffer_sp); 127d1411e1aSGreg Clayton return byte_size; 128d1411e1aSGreg Clayton } 129d1411e1aSGreg Clayton data.Clear(); 130d1411e1aSGreg Clayton return 0; 131d1411e1aSGreg Clayton } 132d1411e1aSGreg Clayton 133d1411e1aSGreg Clayton 134d1411e1aSGreg Clayton 135