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 
12666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
13666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h"
1401c3243fSZachary Turner #include "lldb/Utility/Endian.h"
15bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
16*2f3df613SZachary Turner #include "lldb/lldb-forward.h" // for DataBufferSP
17*2f3df613SZachary Turner 
18*2f3df613SZachary Turner #include <memory> // for make_shared
19*2f3df613SZachary Turner 
20*2f3df613SZachary Turner #include <inttypes.h> // for PRIx64
210ae96273SGreg Clayton 
220ae96273SGreg Clayton using namespace lldb;
230ae96273SGreg Clayton using namespace lldb_private;
241080edbcSGreg Clayton 
25b9c1b51eSKate Stone int Opcode::Dump(Stream *s, uint32_t min_byte_width) {
261080edbcSGreg Clayton   int bytes_written = 0;
27b9c1b51eSKate Stone   switch (m_type) {
281080edbcSGreg Clayton   case Opcode::eTypeInvalid:
291080edbcSGreg Clayton     bytes_written = s->PutCString("<invalid>");
301080edbcSGreg Clayton     break;
311080edbcSGreg Clayton   case Opcode::eType8:
321080edbcSGreg Clayton     bytes_written = s->Printf("0x%2.2x", m_data.inst8);
331080edbcSGreg Clayton     break;
341080edbcSGreg Clayton   case Opcode::eType16:
351080edbcSGreg Clayton     bytes_written = s->Printf("0x%4.4x", m_data.inst16);
361080edbcSGreg Clayton     break;
375c97c2f7SSean Callanan   case Opcode::eType16_2:
381080edbcSGreg Clayton   case Opcode::eType32:
391080edbcSGreg Clayton     bytes_written = s->Printf("0x%8.8x", m_data.inst32);
401080edbcSGreg Clayton     break;
411080edbcSGreg Clayton 
421080edbcSGreg Clayton   case Opcode::eType64:
43d01b2953SDaniel Malea     bytes_written = s->Printf("0x%16.16" PRIx64, m_data.inst64);
441080edbcSGreg Clayton     break;
451080edbcSGreg Clayton 
461080edbcSGreg Clayton   case Opcode::eTypeBytes:
47b9c1b51eSKate Stone     for (uint32_t i = 0; i < m_data.inst.length; ++i) {
481080edbcSGreg Clayton       if (i > 0)
49357132ebSGreg Clayton         bytes_written += s->PutChar(' ');
501080edbcSGreg Clayton       bytes_written += s->Printf("%2.2x", m_data.inst.bytes[i]);
511080edbcSGreg Clayton     }
521080edbcSGreg Clayton     break;
531080edbcSGreg Clayton   }
541080edbcSGreg Clayton 
551080edbcSGreg Clayton   // Add spaces to make sure bytes dispay comes out even in case opcodes
561080edbcSGreg Clayton   // aren't all the same size
573985c8c6SSaleem Abdulrasool   if (static_cast<uint32_t>(bytes_written) < min_byte_width)
581080edbcSGreg Clayton     bytes_written = s->Printf("%*s", min_byte_width - bytes_written, "");
591080edbcSGreg Clayton   return bytes_written;
601080edbcSGreg Clayton }
611080edbcSGreg Clayton 
62b9c1b51eSKate Stone lldb::ByteOrder Opcode::GetDataByteOrder() const {
63b9c1b51eSKate Stone   if (m_byte_order != eByteOrderInvalid) {
6490359963SEd Maste     return m_byte_order;
6590359963SEd Maste   }
66b9c1b51eSKate Stone   switch (m_type) {
67b9c1b51eSKate Stone   case Opcode::eTypeInvalid:
68b9c1b51eSKate Stone     break;
698f7180b1SGreg Clayton   case Opcode::eType8:
708f7180b1SGreg Clayton   case Opcode::eType16:
715c97c2f7SSean Callanan   case Opcode::eType16_2:
728f7180b1SGreg Clayton   case Opcode::eType32:
73b9c1b51eSKate Stone   case Opcode::eType64:
74b9c1b51eSKate Stone     return endian::InlHostByteOrder();
758f7180b1SGreg Clayton   case Opcode::eTypeBytes:
768f7180b1SGreg Clayton     break;
778f7180b1SGreg Clayton   }
788f7180b1SGreg Clayton   return eByteOrderInvalid;
798f7180b1SGreg Clayton }
808f7180b1SGreg Clayton 
81b9c1b51eSKate Stone uint32_t Opcode::GetData(DataExtractor &data) const {
82d1411e1aSGreg Clayton   uint32_t byte_size = GetByteSize();
8390359963SEd Maste   uint8_t swap_buf[8];
848918372dSEugene Zelenko   const void *buf = nullptr;
85ba812f42SGreg Clayton 
86b9c1b51eSKate Stone   if (byte_size > 0) {
87b9c1b51eSKate Stone     if (!GetEndianSwap()) {
88b9c1b51eSKate Stone       if (m_type == Opcode::eType16_2) {
8990359963SEd Maste         // 32 bit thumb instruction, we need to sizzle this a bit
9090359963SEd Maste         swap_buf[0] = m_data.inst.bytes[2];
9190359963SEd Maste         swap_buf[1] = m_data.inst.bytes[3];
9290359963SEd Maste         swap_buf[2] = m_data.inst.bytes[0];
9390359963SEd Maste         swap_buf[3] = m_data.inst.bytes[1];
9490359963SEd Maste         buf = swap_buf;
95b9c1b51eSKate Stone       } else {
9690359963SEd Maste         buf = GetOpcodeDataBytes();
9790359963SEd Maste       }
98b9c1b51eSKate Stone     } else {
99b9c1b51eSKate Stone       switch (m_type) {
100d1411e1aSGreg Clayton       case Opcode::eTypeInvalid:
101d1411e1aSGreg Clayton         break;
10290359963SEd Maste       case Opcode::eType8:
10390359963SEd Maste         buf = GetOpcodeDataBytes();
10490359963SEd Maste         break;
10590359963SEd Maste       case Opcode::eType16:
10690359963SEd Maste         *(uint16_t *)swap_buf = llvm::ByteSwap_16(m_data.inst16);
10790359963SEd Maste         buf = swap_buf;
10890359963SEd Maste         break;
10979101b5cSGreg Clayton       case Opcode::eType16_2:
11090359963SEd Maste         swap_buf[0] = m_data.inst.bytes[1];
11190359963SEd Maste         swap_buf[1] = m_data.inst.bytes[0];
11290359963SEd Maste         swap_buf[2] = m_data.inst.bytes[3];
11390359963SEd Maste         swap_buf[3] = m_data.inst.bytes[2];
11490359963SEd Maste         buf = swap_buf;
115cd4ae1abSSean Callanan         break;
116cd4ae1abSSean Callanan       case Opcode::eType32:
11790359963SEd Maste         *(uint32_t *)swap_buf = llvm::ByteSwap_32(m_data.inst32);
11890359963SEd Maste         buf = swap_buf;
11979101b5cSGreg Clayton         break;
12090359963SEd Maste       case Opcode::eType64:
12190359963SEd Maste         *(uint32_t *)swap_buf = llvm::ByteSwap_64(m_data.inst64);
12290359963SEd Maste         buf = swap_buf;
12390359963SEd Maste         break;
12490359963SEd Maste       case Opcode::eTypeBytes:
12590359963SEd Maste         buf = GetOpcodeDataBytes();
126d1411e1aSGreg Clayton         break;
127d1411e1aSGreg Clayton       }
128d1411e1aSGreg Clayton     }
12990359963SEd Maste   }
130b9c1b51eSKate Stone   if (buf != nullptr) {
13190359963SEd Maste     DataBufferSP buffer_sp;
13290359963SEd Maste 
133*2f3df613SZachary Turner     buffer_sp = std::make_shared<DataBufferHeap>(buf, byte_size);
134d1411e1aSGreg Clayton     data.SetByteOrder(GetDataByteOrder());
135d1411e1aSGreg Clayton     data.SetData(buffer_sp);
136d1411e1aSGreg Clayton     return byte_size;
137d1411e1aSGreg Clayton   }
138d1411e1aSGreg Clayton   data.Clear();
139d1411e1aSGreg Clayton   return 0;
140d1411e1aSGreg Clayton }
141