blob: 5aeaae778fb27cc918a7b7c539f5662a3ec32c16 [file] [log] [blame]
Greg Clayton6da4ca82011-01-21 22:02:52 +00001//===-- EmulateInstruction.h ------------------------------------*- 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 "EmulateInstruction.h"
11
12#include "lldb/Core/DataExtractor.h"
13#include "lldb/Core/StreamString.h"
14using namespace lldb;
15using namespace lldb_private;
16
17
18EmulateInstruction::EmulateInstruction
19(
20 lldb::ByteOrder byte_order,
21 uint32_t addr_byte_size,
22 void *baton,
23 ReadMemory read_mem_callback,
24 WriteMemory write_mem_callback,
25 ReadRegister read_reg_callback,
26 WriteRegister write_reg_callback
27) :
28 m_byte_order (lldb::eByteOrderHost),
29 m_addr_byte_size (sizeof (void *)),
30 m_baton (baton),
31 m_read_mem_callback (read_mem_callback),
32 m_write_mem_callback (write_mem_callback),
33 m_read_reg_callback (read_reg_callback),
34 m_write_reg_callback (write_reg_callback),
35 m_inst_pc (LLDB_INVALID_ADDRESS)
36{
37 ::bzero (&m_inst, sizeof (m_inst));
38}
39
40uint64_t
41EmulateInstruction::ReadRegisterUnsigned (uint32_t reg_kind, uint32_t reg_num, uint64_t fail_value, bool *success_ptr)
42{
43 uint64_t uval64 = 0;
44 bool success = m_read_reg_callback (m_baton, reg_kind, reg_num, uval64);
45 if (success_ptr)
46 *success_ptr = success;
47 if (!success)
48 uval64 = fail_value;
49 return uval64;
50}
51
52bool
53EmulateInstruction::WriteRegisterUnsigned (const Context &context, uint32_t reg_kind, uint32_t reg_num, uint64_t reg_value)
54{
55 return m_write_reg_callback (m_baton, context, reg_kind, reg_num, reg_value);
56}
57
58uint64_t
59EmulateInstruction::ReadMemoryUnsigned (const Context &context, lldb::addr_t addr, size_t byte_size, uint64_t fail_value, bool *success_ptr)
60{
61 uint64_t uval64 = 0;
62 bool success = false;
63 if (byte_size <= 8)
64 {
65 uint8_t buf[sizeof(uint64_t)];
66 size_t bytes_read = m_read_mem_callback (m_baton, context, addr, buf, byte_size);
67 if (bytes_read == byte_size)
68 {
69 uint32_t offset = 0;
70 DataExtractor data (buf, byte_size, m_byte_order, m_addr_byte_size);
71 uval64 = data.GetMaxU64 (&offset, byte_size);
72 success = true;
73 }
74 }
75
76 if (success_ptr)
77 *success_ptr = success;
78
79 if (!success)
80 uval64 = fail_value;
81 return uval64;
82}
83
84
85bool
86EmulateInstruction::WriteMemoryUnsigned (const Context &context,
87 lldb::addr_t addr,
88 uint64_t uval,
89 size_t uval_byte_size)
90{
91 StreamString strm(Stream::eBinary, GetAddressByteSize(), GetByteOrder());
92 strm.PutMaxHex64 (uval, uval_byte_size);
93
94 size_t bytes_written = m_write_mem_callback (m_baton, context, addr, strm.GetData(), uval_byte_size);
95 if (bytes_written == uval_byte_size)
96 return true;
97 return false;
98}