summaryrefslogtreecommitdiffstats
path: root/lldb/source/Core/EmulateInstruction.cpp
blob: 3718c12d3208376bfcb3d50feac212c9524d81da (plain)
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
//===-- EmulateInstruction.h ------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "EmulateInstruction.h"

#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Host/Endian.h"
using namespace lldb;
using namespace lldb_private;


EmulateInstruction::EmulateInstruction 
(
    lldb::ByteOrder byte_order,
    uint32_t addr_byte_size,
    void *baton,
    ReadMemory read_mem_callback,
    WriteMemory write_mem_callback,
    ReadRegister read_reg_callback,
    WriteRegister write_reg_callback
) :
    m_byte_order (endian::InlHostByteOrder()),
    m_addr_byte_size (sizeof (void *)),
    m_baton (baton),
    m_read_mem_callback (read_mem_callback),
    m_write_mem_callback (write_mem_callback),
    m_read_reg_callback (read_reg_callback),
    m_write_reg_callback (write_reg_callback),
    m_inst_pc (LLDB_INVALID_ADDRESS)
{
    ::bzero (&m_inst, sizeof (m_inst));
}

uint64_t
EmulateInstruction::ReadRegisterUnsigned (uint32_t reg_kind, uint32_t reg_num, uint64_t fail_value, bool *success_ptr)
{
    uint64_t uval64 = 0;
    bool success = m_read_reg_callback (m_baton, reg_kind, reg_num, uval64);
    if (success_ptr)
        *success_ptr = success;
    if (!success)
        uval64 = fail_value;
    return uval64;
}

bool
EmulateInstruction::WriteRegisterUnsigned (const Context &context, uint32_t reg_kind, uint32_t reg_num, uint64_t reg_value)
{
    return m_write_reg_callback (m_baton, context, reg_kind, reg_num, reg_value);    
}

uint64_t
EmulateInstruction::ReadMemoryUnsigned (const Context &context, lldb::addr_t addr, size_t byte_size, uint64_t fail_value, bool *success_ptr)
{
    uint64_t uval64 = 0;
    bool success = false;
    if (byte_size <= 8)
    {
        uint8_t buf[sizeof(uint64_t)];
        size_t bytes_read = m_read_mem_callback (m_baton, context, addr, buf, byte_size);
        if (bytes_read == byte_size)
        {
            uint32_t offset = 0;
            DataExtractor data (buf, byte_size, m_byte_order, m_addr_byte_size);
            uval64 = data.GetMaxU64 (&offset, byte_size);
            success = true;
        }
    }

    if (success_ptr)
        *success_ptr = success;

    if (!success)
        uval64 = fail_value;
    return uval64;
}


bool
EmulateInstruction::WriteMemoryUnsigned (const Context &context, 
                                         lldb::addr_t addr, 
                                         uint64_t uval,
                                         size_t uval_byte_size)
{
    StreamString strm(Stream::eBinary, GetAddressByteSize(), GetByteOrder());
    strm.PutMaxHex64 (uval, uval_byte_size);
    
    size_t bytes_written = m_write_mem_callback (m_baton, context, addr, strm.GetData(), uval_byte_size);
    if (bytes_written == uval_byte_size)
        return true;
    return false;
}
OpenPOWER on IntegriCloud