Newer
Older
//===-- IRInterpreter.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/DataExtractor.h"
#include "lldb/Core/Error.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Expression/IRMemoryMap.h"
#include "lldb/Expression/IRInterpreter.h"
Chandler Carruth
committed
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
Chandler Carruth
committed
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
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
#include "llvm/Support/raw_ostream.h"
#include <map>
using namespace llvm;
static std::string
PrintValue(const Value *value, bool truncate = false)
{
std::string s;
raw_string_ostream rso(s);
value->print(rso);
rso.flush();
if (truncate)
s.resize(s.length() - 1);
size_t offset;
while ((offset = s.find('\n')) != s.npos)
s.erase(offset, 1);
while (s[0] == ' ' || s[0] == '\t')
s.erase(0, 1);
return s;
}
static std::string
PrintType(const Type *type, bool truncate = false)
{
std::string s;
raw_string_ostream rso(s);
type->print(rso);
rso.flush();
if (truncate)
s.resize(s.length() - 1);
return s;
}
typedef std::map <const Value*, lldb::addr_t> ValueMap;
ValueMap m_values;
lldb_private::IRMemoryMap &m_memory_map;
const BasicBlock *m_bb;
BasicBlock::const_iterator m_ii;
BasicBlock::const_iterator m_ie;
lldb::addr_t m_frame_process_address;
size_t m_frame_size;
lldb::addr_t m_stack_pointer;
lldb::ByteOrder m_byte_order;
size_t m_addr_byte_size;
InterpreterStackFrame (DataLayout &target_data,
lldb_private::IRMemoryMap &memory_map) :
m_target_data (target_data),
m_memory_map (memory_map)
{
m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
m_addr_byte_size = (target_data.getPointerSize(0));
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
m_frame_size = 512 * 1024;
lldb_private::Error alloc_error;
m_frame_process_address = memory_map.Malloc(m_frame_size,
m_addr_byte_size,
lldb::ePermissionsReadable | lldb::ePermissionsWritable,
lldb_private::IRMemoryMap::eAllocationPolicyMirror,
alloc_error);
if (alloc_error.Success())
{
m_stack_pointer = m_frame_process_address + m_frame_size;
}
else
{
m_frame_process_address = LLDB_INVALID_ADDRESS;
m_stack_pointer = LLDB_INVALID_ADDRESS;
}
}
~InterpreterStackFrame ()
{
if (m_frame_process_address != LLDB_INVALID_ADDRESS)
{
lldb_private::Error free_error;
m_memory_map.Free(m_frame_process_address, free_error);
m_frame_process_address = LLDB_INVALID_ADDRESS;
}
}
void Jump (const BasicBlock *bb)
{
m_bb = bb;
m_ii = m_bb->begin();
m_ie = m_bb->end();
}
std::string SummarizeValue (const Value *value)
{
lldb_private::StreamString ss;
ss.Printf("%s", PrintValue(value).c_str());
ValueMap::iterator i = m_values.find(value);
if (i != m_values.end())
{
lldb::addr_t addr = i->second;
ss.Printf(" 0x%llx", (unsigned long long)addr);
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
}
return ss.GetString();
}
bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
{
size_t type_size = m_target_data.getTypeStoreSize(type);
switch (type_size)
{
case 1:
scalar = (uint8_t)u64value;
break;
case 2:
scalar = (uint16_t)u64value;
break;
case 4:
scalar = (uint32_t)u64value;
break;
case 8:
scalar = (uint64_t)u64value;
break;
default:
return false;
}
return true;
}
bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
{
const Constant *constant = dyn_cast<Constant>(value);
if (constant)
{
if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
{
return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType());
}
}
else
{
lldb::addr_t process_address = ResolveValue(value, module);
size_t value_size = m_target_data.getTypeStoreSize(value->getType());
lldb_private::DataExtractor value_extractor;
lldb_private::Error extract_error;
m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error);
if (!extract_error.Success())
Greg Clayton
committed
if (value_size <= 8)
{
uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
return AssignToMatchType(scalar, u64value, value->getType());
}
}
return false;
Loading
Loading full blame...