1*ba812f42SGreg 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
15*ba812f42SGreg Clayton #include "llvm/ADT/Triple.h"
160ae96273SGreg Clayton // Project includes
17*ba812f42SGreg 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 
23*ba812f42SGreg 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;
431080edbcSGreg Clayton 
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:
798f7180b1SGreg Clayton         case Opcode::eType32:
808f7180b1SGreg Clayton         case Opcode::eType64:    return lldb::endian::InlHostByteOrder();
818f7180b1SGreg Clayton         case Opcode::eTypeBytes:
828f7180b1SGreg Clayton             break;
838f7180b1SGreg Clayton     }
848f7180b1SGreg Clayton     return eByteOrderInvalid;
858f7180b1SGreg Clayton }
868f7180b1SGreg Clayton 
87d1411e1aSGreg Clayton uint32_t
88*ba812f42SGreg Clayton Opcode::GetData (DataExtractor &data, lldb::AddressClass address_class) const
89d1411e1aSGreg Clayton {
90d1411e1aSGreg Clayton     uint32_t byte_size = GetByteSize ();
91*ba812f42SGreg Clayton 
92d1411e1aSGreg Clayton     DataBufferSP buffer_sp;
93d1411e1aSGreg Clayton     if (byte_size > 0)
94d1411e1aSGreg Clayton     {
95d1411e1aSGreg Clayton         switch (m_type)
96d1411e1aSGreg Clayton         {
97d1411e1aSGreg Clayton             case Opcode::eTypeInvalid:
98d1411e1aSGreg Clayton                 break;
99d1411e1aSGreg Clayton 
100d1411e1aSGreg Clayton             case Opcode::eType8:    buffer_sp.reset (new DataBufferHeap (&m_data.inst8,  byte_size)); break;
101d1411e1aSGreg Clayton             case Opcode::eType16:   buffer_sp.reset (new DataBufferHeap (&m_data.inst16, byte_size)); break;
102*ba812f42SGreg Clayton             case Opcode::eType32:
103*ba812f42SGreg Clayton                 {
104*ba812f42SGreg Clayton                     // The only thing that uses eAddressClassCodeAlternateISA currently
105*ba812f42SGreg Clayton                     // is Thumb. If this ever changes, we will need to pass in more
106*ba812f42SGreg Clayton                     // information like an additional "const ArchSpec &arch". For now
107*ba812f42SGreg Clayton                     // this will do
108*ba812f42SGreg Clayton                     if (address_class == eAddressClassCodeAlternateISA)
109*ba812f42SGreg Clayton                     {
110*ba812f42SGreg Clayton                         // 32 bit thumb instruction, we need to sizzle this a bit
111*ba812f42SGreg Clayton                         uint8_t buf[4];
112*ba812f42SGreg Clayton                         buf[0] = m_data.inst.bytes[2];
113*ba812f42SGreg Clayton                         buf[1] = m_data.inst.bytes[3];
114*ba812f42SGreg Clayton                         buf[2] = m_data.inst.bytes[0];
115*ba812f42SGreg Clayton                         buf[3] = m_data.inst.bytes[1];
116*ba812f42SGreg Clayton                         buffer_sp.reset (new DataBufferHeap (buf, byte_size));
117*ba812f42SGreg Clayton                         break;
118*ba812f42SGreg Clayton                     }
119*ba812f42SGreg Clayton                     buffer_sp.reset (new DataBufferHeap (&m_data.inst32, byte_size));
120*ba812f42SGreg Clayton                 }
121*ba812f42SGreg Clayton                 break;
122*ba812f42SGreg Clayton 
123d1411e1aSGreg Clayton             case Opcode::eType64:   buffer_sp.reset (new DataBufferHeap (&m_data.inst64, byte_size)); break;
124d1411e1aSGreg Clayton             case Opcode::eTypeBytes:buffer_sp.reset (new DataBufferHeap (GetOpcodeBytes(), byte_size)); break;
125d1411e1aSGreg Clayton                 break;
126d1411e1aSGreg Clayton         }
127d1411e1aSGreg Clayton     }
128d1411e1aSGreg Clayton 
129d1411e1aSGreg Clayton     if (buffer_sp)
130d1411e1aSGreg Clayton     {
131d1411e1aSGreg Clayton         data.SetByteOrder(GetDataByteOrder());
132d1411e1aSGreg Clayton         data.SetData (buffer_sp);
133d1411e1aSGreg Clayton         return byte_size;
134d1411e1aSGreg Clayton     }
135d1411e1aSGreg Clayton     data.Clear();
136d1411e1aSGreg Clayton     return 0;
137d1411e1aSGreg Clayton }
138d1411e1aSGreg Clayton 
139d1411e1aSGreg Clayton 
140d1411e1aSGreg Clayton 
141