aboutsummaryrefslogtreecommitdiff
path: root/source/Core/Opcode.cpp
blob: 11b913841ecbc97e54bba35336fc48d02fa96f8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//===-- Opcode.cpp ----------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Core/Opcode.h"

// C Includes
// C++ Includes
// Other libraries and framework includes
#include "llvm/ADT/Triple.h"

// Project includes
#include "lldb/Core/ArchSpec.h"
#include "lldb/Core/DataBufferHeap.h"
#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/Stream.h"
#include "lldb/Host/Endian.h"

using namespace lldb;
using namespace lldb_private;

int Opcode::Dump(Stream *s, uint32_t min_byte_width) {
  int bytes_written = 0;
  switch (m_type) {
  case Opcode::eTypeInvalid:
    bytes_written = s->PutCString("<invalid>");
    break;
  case Opcode::eType8:
    bytes_written = s->Printf("0x%2.2x", m_data.inst8);
    break;
  case Opcode::eType16:
    bytes_written = s->Printf("0x%4.4x", m_data.inst16);
    break;
  case Opcode::eType16_2:
  case Opcode::eType32:
    bytes_written = s->Printf("0x%8.8x", m_data.inst32);
    break;

  case Opcode::eType64:
    bytes_written = s->Printf("0x%16.16" PRIx64, m_data.inst64);
    break;

  case Opcode::eTypeBytes:
    for (uint32_t i = 0; i < m_data.inst.length; ++i) {
      if (i > 0)
        bytes_written += s->PutChar(' ');
      bytes_written += s->Printf("%2.2x", m_data.inst.bytes[i]);
    }
    break;
  }

  // Add spaces to make sure bytes dispay comes out even in case opcodes
  // aren't all the same size
  if (static_cast<uint32_t>(bytes_written) < min_byte_width)
    bytes_written = s->Printf("%*s", min_byte_width - bytes_written, "");
  return bytes_written;
}

lldb::ByteOrder Opcode::GetDataByteOrder() const {
  if (m_byte_order != eByteOrderInvalid) {
    return m_byte_order;
  }
  switch (m_type) {
  case Opcode::eTypeInvalid:
    break;
  case Opcode::eType8:
  case Opcode::eType16:
  case Opcode::eType16_2:
  case Opcode::eType32:
  case Opcode::eType64:
    return endian::InlHostByteOrder();
  case Opcode::eTypeBytes:
    break;
  }
  return eByteOrderInvalid;
}

uint32_t Opcode::GetData(DataExtractor &data) const {
  uint32_t byte_size = GetByteSize();
  uint8_t swap_buf[8];
  const void *buf = nullptr;

  if (byte_size > 0) {
    if (!GetEndianSwap()) {
      if (m_type == Opcode::eType16_2) {
        // 32 bit thumb instruction, we need to sizzle this a bit
        swap_buf[0] = m_data.inst.bytes[2];
        swap_buf[1] = m_data.inst.bytes[3];
        swap_buf[2] = m_data.inst.bytes[0];
        swap_buf[3] = m_data.inst.bytes[1];
        buf = swap_buf;
      } else {
        buf = GetOpcodeDataBytes();
      }
    } else {
      switch (m_type) {
      case Opcode::eTypeInvalid:
        break;
      case Opcode::eType8:
        buf = GetOpcodeDataBytes();
        break;
      case Opcode::eType16:
        *(uint16_t *)swap_buf = llvm::ByteSwap_16(m_data.inst16);
        buf = swap_buf;
        break;
      case Opcode::eType16_2:
        swap_buf[0] = m_data.inst.bytes[1];
        swap_buf[1] = m_data.inst.bytes[0];
        swap_buf[2] = m_data.inst.bytes[3];
        swap_buf[3] = m_data.inst.bytes[2];
        buf = swap_buf;
        break;
      case Opcode::eType32:
        *(uint32_t *)swap_buf = llvm::ByteSwap_32(m_data.inst32);
        buf = swap_buf;
        break;
      case Opcode::eType64:
        *(uint32_t *)swap_buf = llvm::ByteSwap_64(m_data.inst64);
        buf = swap_buf;
        break;
      case Opcode::eTypeBytes:
        buf = GetOpcodeDataBytes();
        break;
      }
    }
  }
  if (buf != nullptr) {
    DataBufferSP buffer_sp;

    buffer_sp.reset(new DataBufferHeap(buf, byte_size));
    data.SetByteOrder(GetDataByteOrder());
    data.SetData(buffer_sp);
    return byte_size;
  }
  data.Clear();
  return 0;
}