1ac7ddfbfSEd Maste //===-- Opcode.cpp ----------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/Core/Opcode.h"
11ac7ddfbfSEd Maste 
12f678e45dSDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
13f678e45dSDimitry Andric #include "lldb/Utility/DataExtractor.h"
14f678e45dSDimitry Andric #include "lldb/Utility/Endian.h"
15f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
16*b5893f02SDimitry Andric #include "lldb/lldb-forward.h"
174bb0738eSEd Maste 
18*b5893f02SDimitry Andric #include <memory>
19f678e45dSDimitry Andric 
20*b5893f02SDimitry Andric #include <inttypes.h>
21ac7ddfbfSEd Maste 
22ac7ddfbfSEd Maste using namespace lldb;
23ac7ddfbfSEd Maste using namespace lldb_private;
24ac7ddfbfSEd Maste 
Dump(Stream * s,uint32_t min_byte_width)25435933ddSDimitry Andric int Opcode::Dump(Stream *s, uint32_t min_byte_width) {
26*b5893f02SDimitry Andric   const uint32_t previous_bytes = s->GetWrittenBytes();
27435933ddSDimitry Andric   switch (m_type) {
28ac7ddfbfSEd Maste   case Opcode::eTypeInvalid:
29*b5893f02SDimitry Andric     s->PutCString("<invalid>");
30ac7ddfbfSEd Maste     break;
31ac7ddfbfSEd Maste   case Opcode::eType8:
32*b5893f02SDimitry Andric     s->Printf("0x%2.2x", m_data.inst8);
33ac7ddfbfSEd Maste     break;
34ac7ddfbfSEd Maste   case Opcode::eType16:
35*b5893f02SDimitry Andric     s->Printf("0x%4.4x", m_data.inst16);
36ac7ddfbfSEd Maste     break;
37ac7ddfbfSEd Maste   case Opcode::eType16_2:
38ac7ddfbfSEd Maste   case Opcode::eType32:
39*b5893f02SDimitry Andric     s->Printf("0x%8.8x", m_data.inst32);
40ac7ddfbfSEd Maste     break;
41ac7ddfbfSEd Maste 
42ac7ddfbfSEd Maste   case Opcode::eType64:
43*b5893f02SDimitry Andric     s->Printf("0x%16.16" PRIx64, m_data.inst64);
44ac7ddfbfSEd Maste     break;
45ac7ddfbfSEd Maste 
46ac7ddfbfSEd Maste   case Opcode::eTypeBytes:
47435933ddSDimitry Andric     for (uint32_t i = 0; i < m_data.inst.length; ++i) {
48ac7ddfbfSEd Maste       if (i > 0)
49*b5893f02SDimitry Andric         s->PutChar(' ');
50*b5893f02SDimitry Andric       s->Printf("%2.2x", m_data.inst.bytes[i]);
51ac7ddfbfSEd Maste     }
52ac7ddfbfSEd Maste     break;
53ac7ddfbfSEd Maste   }
54ac7ddfbfSEd Maste 
55*b5893f02SDimitry Andric   uint32_t bytes_written_so_far = s->GetWrittenBytes() - previous_bytes;
56*b5893f02SDimitry Andric   // Add spaces to make sure bytes display comes out even in case opcodes aren't
57*b5893f02SDimitry Andric   // all the same size.
58*b5893f02SDimitry Andric   if (bytes_written_so_far < min_byte_width)
59*b5893f02SDimitry Andric     s->Printf("%*s", min_byte_width - bytes_written_so_far, "");
60*b5893f02SDimitry Andric   return s->GetWrittenBytes() - previous_bytes;
61ac7ddfbfSEd Maste }
62ac7ddfbfSEd Maste 
GetDataByteOrder() const63435933ddSDimitry Andric lldb::ByteOrder Opcode::GetDataByteOrder() const {
64435933ddSDimitry Andric   if (m_byte_order != eByteOrderInvalid) {
6512b93ac6SEd Maste     return m_byte_order;
6612b93ac6SEd Maste   }
67435933ddSDimitry Andric   switch (m_type) {
68435933ddSDimitry Andric   case Opcode::eTypeInvalid:
69435933ddSDimitry Andric     break;
70ac7ddfbfSEd Maste   case Opcode::eType8:
71ac7ddfbfSEd Maste   case Opcode::eType16:
72ac7ddfbfSEd Maste   case Opcode::eType16_2:
73ac7ddfbfSEd Maste   case Opcode::eType32:
74435933ddSDimitry Andric   case Opcode::eType64:
75435933ddSDimitry Andric     return endian::InlHostByteOrder();
76ac7ddfbfSEd Maste   case Opcode::eTypeBytes:
77ac7ddfbfSEd Maste     break;
78ac7ddfbfSEd Maste   }
79ac7ddfbfSEd Maste   return eByteOrderInvalid;
80ac7ddfbfSEd Maste }
81ac7ddfbfSEd Maste 
GetData(DataExtractor & data) const82435933ddSDimitry Andric uint32_t Opcode::GetData(DataExtractor &data) const {
83ac7ddfbfSEd Maste   uint32_t byte_size = GetByteSize();
8412b93ac6SEd Maste   uint8_t swap_buf[8];
854bb0738eSEd Maste   const void *buf = nullptr;
86ac7ddfbfSEd Maste 
87435933ddSDimitry Andric   if (byte_size > 0) {
88435933ddSDimitry Andric     if (!GetEndianSwap()) {
89435933ddSDimitry Andric       if (m_type == Opcode::eType16_2) {
9012b93ac6SEd Maste         // 32 bit thumb instruction, we need to sizzle this a bit
9112b93ac6SEd Maste         swap_buf[0] = m_data.inst.bytes[2];
9212b93ac6SEd Maste         swap_buf[1] = m_data.inst.bytes[3];
9312b93ac6SEd Maste         swap_buf[2] = m_data.inst.bytes[0];
9412b93ac6SEd Maste         swap_buf[3] = m_data.inst.bytes[1];
9512b93ac6SEd Maste         buf = swap_buf;
96435933ddSDimitry Andric       } else {
9712b93ac6SEd Maste         buf = GetOpcodeDataBytes();
9812b93ac6SEd Maste       }
99435933ddSDimitry Andric     } else {
100435933ddSDimitry Andric       switch (m_type) {
101ac7ddfbfSEd Maste       case Opcode::eTypeInvalid:
102ac7ddfbfSEd Maste         break;
10312b93ac6SEd Maste       case Opcode::eType8:
10412b93ac6SEd Maste         buf = GetOpcodeDataBytes();
10512b93ac6SEd Maste         break;
10612b93ac6SEd Maste       case Opcode::eType16:
10712b93ac6SEd Maste         *(uint16_t *)swap_buf = llvm::ByteSwap_16(m_data.inst16);
10812b93ac6SEd Maste         buf = swap_buf;
10912b93ac6SEd Maste         break;
110ac7ddfbfSEd Maste       case Opcode::eType16_2:
11112b93ac6SEd Maste         swap_buf[0] = m_data.inst.bytes[1];
11212b93ac6SEd Maste         swap_buf[1] = m_data.inst.bytes[0];
11312b93ac6SEd Maste         swap_buf[2] = m_data.inst.bytes[3];
11412b93ac6SEd Maste         swap_buf[3] = m_data.inst.bytes[2];
11512b93ac6SEd Maste         buf = swap_buf;
116ac7ddfbfSEd Maste         break;
117ac7ddfbfSEd Maste       case Opcode::eType32:
11812b93ac6SEd Maste         *(uint32_t *)swap_buf = llvm::ByteSwap_32(m_data.inst32);
11912b93ac6SEd Maste         buf = swap_buf;
120ac7ddfbfSEd Maste         break;
12112b93ac6SEd Maste       case Opcode::eType64:
12212b93ac6SEd Maste         *(uint32_t *)swap_buf = llvm::ByteSwap_64(m_data.inst64);
12312b93ac6SEd Maste         buf = swap_buf;
12412b93ac6SEd Maste         break;
12512b93ac6SEd Maste       case Opcode::eTypeBytes:
12612b93ac6SEd Maste         buf = GetOpcodeDataBytes();
127ac7ddfbfSEd Maste         break;
128ac7ddfbfSEd Maste       }
129ac7ddfbfSEd Maste     }
13012b93ac6SEd Maste   }
131435933ddSDimitry Andric   if (buf != nullptr) {
13212b93ac6SEd Maste     DataBufferSP buffer_sp;
13312b93ac6SEd Maste 
134f678e45dSDimitry Andric     buffer_sp = std::make_shared<DataBufferHeap>(buf, byte_size);
135ac7ddfbfSEd Maste     data.SetByteOrder(GetDataByteOrder());
136ac7ddfbfSEd Maste     data.SetData(buffer_sp);
137ac7ddfbfSEd Maste     return byte_size;
138ac7ddfbfSEd Maste   }
139ac7ddfbfSEd Maste   data.Clear();
140ac7ddfbfSEd Maste   return 0;
141ac7ddfbfSEd Maste }
142