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