1 //===-- Opcode.cpp ----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Core/Opcode.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 #include "llvm/ADT/Triple.h"
16 // Project includes
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Core/DataBufferHeap.h"
19 #include "lldb/Core/DataExtractor.h"
20 #include "lldb/Core/Stream.h"
21 #include "lldb/Host/Endian.h"
22 
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 
28 int
29 Opcode::Dump (Stream *s, uint32_t min_byte_width)
30 {
31     int bytes_written = 0;
32     switch (m_type)
33     {
34     case Opcode::eTypeInvalid:
35         bytes_written = s->PutCString ("<invalid>");
36         break;
37     case Opcode::eType8:
38         bytes_written = s->Printf ("0x%2.2x", m_data.inst8);
39         break;
40     case Opcode::eType16:
41         bytes_written = s->Printf ("0x%4.4x", m_data.inst16);
42         break;
43 
44     case Opcode::eType32:
45         bytes_written = s->Printf ("0x%8.8x", m_data.inst32);
46         break;
47 
48     case Opcode::eType64:
49         bytes_written = s->Printf ("0x%16.16llx", m_data.inst64);
50         break;
51 
52     case Opcode::eTypeBytes:
53         {
54             for (uint32_t i=0; i<m_data.inst.length; ++i)
55             {
56                 if (i > 0)
57                     bytes_written += s->PutChar (' ');
58                 bytes_written += s->Printf ("%2.2x", m_data.inst.bytes[i]);
59             }
60         }
61         break;
62     }
63 
64     // Add spaces to make sure bytes dispay comes out even in case opcodes
65     // aren't all the same size
66     if (bytes_written < min_byte_width)
67         bytes_written = s->Printf ("%*s", min_byte_width - bytes_written, "");
68     return bytes_written;
69 }
70 
71 lldb::ByteOrder
72 Opcode::GetDataByteOrder () const
73 {
74     switch (m_type)
75     {
76         case Opcode::eTypeInvalid: break;
77         case Opcode::eType8:
78         case Opcode::eType16:
79         case Opcode::eType32:
80         case Opcode::eType64:    return lldb::endian::InlHostByteOrder();
81         case Opcode::eTypeBytes:
82             break;
83     }
84     return eByteOrderInvalid;
85 }
86 
87 uint32_t
88 Opcode::GetData (DataExtractor &data, lldb::AddressClass address_class) const
89 {
90     uint32_t byte_size = GetByteSize ();
91 
92     DataBufferSP buffer_sp;
93     if (byte_size > 0)
94     {
95         switch (m_type)
96         {
97             case Opcode::eTypeInvalid:
98                 break;
99 
100             case Opcode::eType8:    buffer_sp.reset (new DataBufferHeap (&m_data.inst8,  byte_size)); break;
101             case Opcode::eType16:   buffer_sp.reset (new DataBufferHeap (&m_data.inst16, byte_size)); break;
102             case Opcode::eType32:
103                 {
104                     // The only thing that uses eAddressClassCodeAlternateISA currently
105                     // is Thumb. If this ever changes, we will need to pass in more
106                     // information like an additional "const ArchSpec &arch". For now
107                     // this will do
108                     if (address_class == eAddressClassCodeAlternateISA)
109                     {
110                         // 32 bit thumb instruction, we need to sizzle this a bit
111                         uint8_t buf[4];
112                         buf[0] = m_data.inst.bytes[2];
113                         buf[1] = m_data.inst.bytes[3];
114                         buf[2] = m_data.inst.bytes[0];
115                         buf[3] = m_data.inst.bytes[1];
116                         buffer_sp.reset (new DataBufferHeap (buf, byte_size));
117                         break;
118                     }
119                     buffer_sp.reset (new DataBufferHeap (&m_data.inst32, byte_size));
120                 }
121                 break;
122 
123             case Opcode::eType64:   buffer_sp.reset (new DataBufferHeap (&m_data.inst64, byte_size)); break;
124             case Opcode::eTypeBytes:buffer_sp.reset (new DataBufferHeap (GetOpcodeBytes(), byte_size)); break;
125                 break;
126         }
127     }
128 
129     if (buffer_sp)
130     {
131         data.SetByteOrder(GetDataByteOrder());
132         data.SetData (buffer_sp);
133         return byte_size;
134     }
135     data.Clear();
136     return 0;
137 }
138 
139 
140 
141