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:
49d01b2953SDaniel Malea         bytes_written = s->Printf ("0x%16.16" PRIx64, 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
663985c8c6SSaleem Abdulrasool     if (static_cast<uint32_t>(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 {
7490359963SEd Maste     if (m_byte_order != eByteOrderInvalid)
7590359963SEd Maste     {
7690359963SEd Maste         return m_byte_order;
7790359963SEd Maste     }
788f7180b1SGreg Clayton     switch (m_type)
798f7180b1SGreg Clayton     {
808f7180b1SGreg Clayton         case Opcode::eTypeInvalid: break;
818f7180b1SGreg Clayton         case Opcode::eType8:
828f7180b1SGreg Clayton         case Opcode::eType16:
835c97c2f7SSean Callanan         case Opcode::eType16_2:
848f7180b1SGreg Clayton         case Opcode::eType32:
85*9ccb970fSBruce Mitchener         case Opcode::eType64:    return endian::InlHostByteOrder();
868f7180b1SGreg Clayton         case Opcode::eTypeBytes:
878f7180b1SGreg Clayton             break;
888f7180b1SGreg Clayton     }
898f7180b1SGreg Clayton     return eByteOrderInvalid;
908f7180b1SGreg Clayton }
918f7180b1SGreg Clayton 
92d1411e1aSGreg Clayton uint32_t
93cd4ae1abSSean Callanan Opcode::GetData (DataExtractor &data) const
94d1411e1aSGreg Clayton {
95d1411e1aSGreg Clayton     uint32_t byte_size = GetByteSize ();
9690359963SEd Maste     uint8_t swap_buf[8];
9790359963SEd Maste     const void *buf = NULL;
98ba812f42SGreg Clayton 
99d1411e1aSGreg Clayton     if (byte_size > 0)
100d1411e1aSGreg Clayton     {
10190359963SEd Maste         if (!GetEndianSwap())
10290359963SEd Maste         {
10390359963SEd Maste             if (m_type == Opcode::eType16_2)
10490359963SEd Maste             {
10590359963SEd Maste                 // 32 bit thumb instruction, we need to sizzle this a bit
10690359963SEd Maste                 swap_buf[0] = m_data.inst.bytes[2];
10790359963SEd Maste                 swap_buf[1] = m_data.inst.bytes[3];
10890359963SEd Maste                 swap_buf[2] = m_data.inst.bytes[0];
10990359963SEd Maste                 swap_buf[3] = m_data.inst.bytes[1];
11090359963SEd Maste                 buf = swap_buf;
11190359963SEd Maste             }
11290359963SEd Maste             else
11390359963SEd Maste             {
11490359963SEd Maste                 buf = GetOpcodeDataBytes();
11590359963SEd Maste             }
11690359963SEd Maste         }
11790359963SEd Maste         else
11890359963SEd Maste         {
119d1411e1aSGreg Clayton             switch (m_type)
120d1411e1aSGreg Clayton             {
121d1411e1aSGreg Clayton                 case Opcode::eTypeInvalid:
122d1411e1aSGreg Clayton                     break;
12390359963SEd Maste                 case Opcode::eType8:
12490359963SEd Maste                     buf = GetOpcodeDataBytes();
12590359963SEd Maste                     break;
12690359963SEd Maste                 case Opcode::eType16:
12790359963SEd Maste                     *(uint16_t *)swap_buf = llvm::ByteSwap_16(m_data.inst16);
12890359963SEd Maste                     buf = swap_buf;
12990359963SEd Maste                     break;
13079101b5cSGreg Clayton                 case Opcode::eType16_2:
13190359963SEd Maste                     swap_buf[0] = m_data.inst.bytes[1];
13290359963SEd Maste                     swap_buf[1] = m_data.inst.bytes[0];
13390359963SEd Maste                     swap_buf[2] = m_data.inst.bytes[3];
13490359963SEd Maste                     swap_buf[3] = m_data.inst.bytes[2];
13590359963SEd Maste                     buf = swap_buf;
136cd4ae1abSSean Callanan                     break;
137cd4ae1abSSean Callanan                 case Opcode::eType32:
13890359963SEd Maste                     *(uint32_t *)swap_buf = llvm::ByteSwap_32(m_data.inst32);
13990359963SEd Maste                     buf = swap_buf;
14079101b5cSGreg Clayton                     break;
14190359963SEd Maste                 case Opcode::eType64:
14290359963SEd Maste                     *(uint32_t *)swap_buf = llvm::ByteSwap_64(m_data.inst64);
14390359963SEd Maste                     buf = swap_buf;
14490359963SEd Maste                     break;
14590359963SEd Maste                 case Opcode::eTypeBytes:
14690359963SEd Maste                     buf = GetOpcodeDataBytes();
147d1411e1aSGreg Clayton                     break;
148d1411e1aSGreg Clayton             }
149d1411e1aSGreg Clayton         }
15090359963SEd Maste     }
15190359963SEd Maste     if (buf)
152d1411e1aSGreg Clayton     {
15390359963SEd Maste         DataBufferSP buffer_sp;
15490359963SEd Maste 
15590359963SEd Maste         buffer_sp.reset (new DataBufferHeap (buf, byte_size));
156d1411e1aSGreg Clayton         data.SetByteOrder(GetDataByteOrder());
157d1411e1aSGreg Clayton         data.SetData (buffer_sp);
158d1411e1aSGreg Clayton         return byte_size;
159d1411e1aSGreg Clayton     }
160d1411e1aSGreg Clayton     data.Clear();
161d1411e1aSGreg Clayton     return 0;
162d1411e1aSGreg Clayton }
163d1411e1aSGreg Clayton 
164d1411e1aSGreg Clayton 
165d1411e1aSGreg Clayton 
166