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;
43*5c97c2f7SSean Callanan     case Opcode::eType16_2:
44*5c97c2f7SSean Callanan         if (GetDataByteOrder() == eByteOrderLittle)
45*5c97c2f7SSean Callanan             bytes_written = s->Printf ("0x%4.4x%4.4x", m_data.inst32 & 0xffff, m_data.inst32 >> 16);
46*5c97c2f7SSean Callanan         else
47*5c97c2f7SSean Callanan             bytes_written = s->Printf ("0x%2.2x%2.2x%2.2x%2.2x", (m_data.inst32 >> 16) & 0xff, (m_data.inst32 >> 24),
48*5c97c2f7SSean Callanan                                                                  (m_data.inst32 & 0xff), (m_data.inst32 >> 8) & 0xff);
49*5c97c2f7SSean Callanan         break;
501080edbcSGreg Clayton     case Opcode::eType32:
511080edbcSGreg Clayton         bytes_written = s->Printf ("0x%8.8x", m_data.inst32);
521080edbcSGreg Clayton         break;
531080edbcSGreg Clayton 
541080edbcSGreg Clayton     case Opcode::eType64:
551080edbcSGreg Clayton         bytes_written = s->Printf ("0x%16.16llx", m_data.inst64);
561080edbcSGreg Clayton         break;
571080edbcSGreg Clayton 
581080edbcSGreg Clayton     case Opcode::eTypeBytes:
591080edbcSGreg Clayton         {
601080edbcSGreg Clayton             for (uint32_t i=0; i<m_data.inst.length; ++i)
611080edbcSGreg Clayton             {
621080edbcSGreg Clayton                 if (i > 0)
63357132ebSGreg Clayton                     bytes_written += s->PutChar (' ');
641080edbcSGreg Clayton                 bytes_written += s->Printf ("%2.2x", m_data.inst.bytes[i]);
651080edbcSGreg Clayton             }
661080edbcSGreg Clayton         }
671080edbcSGreg Clayton         break;
681080edbcSGreg Clayton     }
691080edbcSGreg Clayton 
701080edbcSGreg Clayton     // Add spaces to make sure bytes dispay comes out even in case opcodes
711080edbcSGreg Clayton     // aren't all the same size
721080edbcSGreg Clayton     if (bytes_written < min_byte_width)
731080edbcSGreg Clayton         bytes_written = s->Printf ("%*s", min_byte_width - bytes_written, "");
741080edbcSGreg Clayton     return bytes_written;
751080edbcSGreg Clayton }
761080edbcSGreg Clayton 
778f7180b1SGreg Clayton lldb::ByteOrder
788f7180b1SGreg Clayton Opcode::GetDataByteOrder () const
798f7180b1SGreg Clayton {
808f7180b1SGreg Clayton     switch (m_type)
818f7180b1SGreg Clayton     {
828f7180b1SGreg Clayton         case Opcode::eTypeInvalid: break;
838f7180b1SGreg Clayton         case Opcode::eType8:
848f7180b1SGreg Clayton         case Opcode::eType16:
85*5c97c2f7SSean Callanan         case Opcode::eType16_2:
868f7180b1SGreg Clayton         case Opcode::eType32:
878f7180b1SGreg Clayton         case Opcode::eType64:    return lldb::endian::InlHostByteOrder();
888f7180b1SGreg Clayton         case Opcode::eTypeBytes:
898f7180b1SGreg Clayton             break;
908f7180b1SGreg Clayton     }
918f7180b1SGreg Clayton     return eByteOrderInvalid;
928f7180b1SGreg Clayton }
938f7180b1SGreg Clayton 
94d1411e1aSGreg Clayton uint32_t
95ba812f42SGreg Clayton Opcode::GetData (DataExtractor &data, lldb::AddressClass address_class) const
96d1411e1aSGreg Clayton {
97d1411e1aSGreg Clayton     uint32_t byte_size = GetByteSize ();
98ba812f42SGreg Clayton 
99d1411e1aSGreg Clayton     DataBufferSP buffer_sp;
100d1411e1aSGreg Clayton     if (byte_size > 0)
101d1411e1aSGreg Clayton     {
102d1411e1aSGreg Clayton         switch (m_type)
103d1411e1aSGreg Clayton         {
104d1411e1aSGreg Clayton             case Opcode::eTypeInvalid:
105d1411e1aSGreg Clayton                 break;
106d1411e1aSGreg Clayton 
107d1411e1aSGreg Clayton             case Opcode::eType8:    buffer_sp.reset (new DataBufferHeap (&m_data.inst8,  byte_size)); break;
108d1411e1aSGreg Clayton             case Opcode::eType16:   buffer_sp.reset (new DataBufferHeap (&m_data.inst16, byte_size)); break;
109*5c97c2f7SSean Callanan             case Opcode::eType16_2: // passthrough
110*5c97c2f7SSean Callanan             case Opcode::eType32:   buffer_sp.reset (new DataBufferHeap (&m_data.inst32, byte_size)); break;
111d1411e1aSGreg Clayton             case Opcode::eType64:   buffer_sp.reset (new DataBufferHeap (&m_data.inst64, byte_size)); break;
112d1411e1aSGreg Clayton             case Opcode::eTypeBytes:buffer_sp.reset (new DataBufferHeap (GetOpcodeBytes(), byte_size)); break;
113d1411e1aSGreg Clayton                 break;
114d1411e1aSGreg Clayton         }
115d1411e1aSGreg Clayton     }
116d1411e1aSGreg Clayton 
117d1411e1aSGreg Clayton     if (buffer_sp)
118d1411e1aSGreg Clayton     {
119d1411e1aSGreg Clayton         data.SetByteOrder(GetDataByteOrder());
120d1411e1aSGreg Clayton         data.SetData (buffer_sp);
121d1411e1aSGreg Clayton         return byte_size;
122d1411e1aSGreg Clayton     }
123d1411e1aSGreg Clayton     data.Clear();
124d1411e1aSGreg Clayton     return 0;
125d1411e1aSGreg Clayton }
126d1411e1aSGreg Clayton 
127d1411e1aSGreg Clayton 
128d1411e1aSGreg Clayton 
129