blob: 5986ebb4a004055513b7d0a7df6aee92da842606 [file] [log] [blame]
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001//===-- IRInterpreter.cpp ---------------------------------------*- 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
Sean Callananfefe43c2013-04-25 18:55:45 +000010#include "lldb/Core/DataExtractor.h"
11#include "lldb/Core/Error.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000012#include "lldb/Core/Log.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000013#include "lldb/Core/Scalar.h"
14#include "lldb/Core/StreamString.h"
15#include "lldb/Expression/IRMemoryMap.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000016#include "lldb/Expression/IRInterpreter.h"
17
Chandler Carruth1e157582013-01-02 12:20:07 +000018#include "llvm/IR/Constants.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000019#include "llvm/IR/DataLayout.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000020#include "llvm/IR/Function.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/Module.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000023#include "llvm/Support/raw_ostream.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000024
25#include <map>
26
27using namespace llvm;
28
Sean Callanan3bfdaa22011-09-15 02:13:07 +000029static std::string
30PrintValue(const Value *value, bool truncate = false)
31{
32 std::string s;
33 raw_string_ostream rso(s);
34 value->print(rso);
35 rso.flush();
36 if (truncate)
37 s.resize(s.length() - 1);
38
39 size_t offset;
40 while ((offset = s.find('\n')) != s.npos)
41 s.erase(offset, 1);
42 while (s[0] == ' ' || s[0] == '\t')
43 s.erase(0, 1);
44
45 return s;
46}
47
48static std::string
49PrintType(const Type *type, bool truncate = false)
50{
51 std::string s;
52 raw_string_ostream rso(s);
53 type->print(rso);
54 rso.flush();
55 if (truncate)
56 s.resize(s.length() - 1);
57 return s;
58}
59
Sean Callanan3bfdaa22011-09-15 02:13:07 +000060class InterpreterStackFrame
61{
62public:
Sean Callanan08052af2013-04-17 07:50:58 +000063 typedef std::map <const Value*, lldb::addr_t> ValueMap;
64
Sean Callanan3bfdaa22011-09-15 02:13:07 +000065 ValueMap m_values;
Micah Villmow8468dbe2012-10-08 16:28:57 +000066 DataLayout &m_target_data;
Sean Callanan179b5482013-04-16 23:49:09 +000067 lldb_private::IRMemoryMap &m_memory_map;
Sean Callanan3bfdaa22011-09-15 02:13:07 +000068 const BasicBlock *m_bb;
69 BasicBlock::const_iterator m_ii;
70 BasicBlock::const_iterator m_ie;
71
Sean Callanan1582ee62013-04-18 22:06:33 +000072 lldb::addr_t m_frame_process_address;
73 size_t m_frame_size;
74 lldb::addr_t m_stack_pointer;
75
Sean Callanan3bfdaa22011-09-15 02:13:07 +000076 lldb::ByteOrder m_byte_order;
77 size_t m_addr_byte_size;
78
Micah Villmow8468dbe2012-10-08 16:28:57 +000079 InterpreterStackFrame (DataLayout &target_data,
Sean Callanandf565402013-04-27 02:19:33 +000080 lldb_private::IRMemoryMap &memory_map,
81 lldb::addr_t stack_frame_bottom,
82 lldb::addr_t stack_frame_top) :
Daniel Dunbara08823f2011-10-31 22:50:49 +000083 m_target_data (target_data),
Sean Callanan179b5482013-04-16 23:49:09 +000084 m_memory_map (memory_map)
Sean Callanan3bfdaa22011-09-15 02:13:07 +000085 {
86 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Sean Callanan95769bf2012-10-11 22:00:52 +000087 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanandf565402013-04-27 02:19:33 +000088
89 m_frame_process_address = stack_frame_bottom;
90 m_frame_size = stack_frame_top - stack_frame_bottom;
91 m_stack_pointer = stack_frame_top;
Sean Callanan1582ee62013-04-18 22:06:33 +000092 }
93
94 ~InterpreterStackFrame ()
95 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +000096 }
97
98 void Jump (const BasicBlock *bb)
99 {
100 m_bb = bb;
101 m_ii = m_bb->begin();
102 m_ie = m_bb->end();
103 }
104
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000105 std::string SummarizeValue (const Value *value)
106 {
107 lldb_private::StreamString ss;
108
109 ss.Printf("%s", PrintValue(value).c_str());
110
111 ValueMap::iterator i = m_values.find(value);
112
113 if (i != m_values.end())
114 {
Sean Callanan08052af2013-04-17 07:50:58 +0000115 lldb::addr_t addr = i->second;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000116
Sean Callanan08052af2013-04-17 07:50:58 +0000117 ss.Printf(" 0x%llx", (unsigned long long)addr);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000118 }
119
120 return ss.GetString();
121 }
122
123 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
124 {
125 size_t type_size = m_target_data.getTypeStoreSize(type);
126
127 switch (type_size)
128 {
129 case 1:
130 scalar = (uint8_t)u64value;
131 break;
132 case 2:
133 scalar = (uint16_t)u64value;
134 break;
135 case 4:
136 scalar = (uint32_t)u64value;
137 break;
138 case 8:
139 scalar = (uint64_t)u64value;
140 break;
141 default:
142 return false;
143 }
144
145 return true;
146 }
147
148 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
149 {
150 const Constant *constant = dyn_cast<Constant>(value);
151
152 if (constant)
153 {
154 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
155 {
156 return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType());
157 }
158 }
159 else
160 {
Sean Callanan08052af2013-04-17 07:50:58 +0000161 lldb::addr_t process_address = ResolveValue(value, module);
162 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
163
164 lldb_private::DataExtractor value_extractor;
165 lldb_private::Error extract_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000166
Sean Callanan08052af2013-04-17 07:50:58 +0000167 m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error);
168
169 if (!extract_error.Success())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000170 return false;
171
Greg Claytonc7bece562013-01-25 18:06:21 +0000172 lldb::offset_t offset = 0;
Greg Clayton78e44bd2013-04-25 00:57:05 +0000173 if (value_size <= 8)
174 {
175 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
176 return AssignToMatchType(scalar, u64value, value->getType());
177 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000178 }
179
180 return false;
181 }
182
183 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
184 {
Sean Callanan08052af2013-04-17 07:50:58 +0000185 lldb::addr_t process_address = ResolveValue (value, module);
186
187 if (process_address == LLDB_INVALID_ADDRESS)
188 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000189
190 lldb_private::Scalar cast_scalar;
191
192 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
193 return false;
Sean Callanan08052af2013-04-17 07:50:58 +0000194
195 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000196
Sean Callanan08052af2013-04-17 07:50:58 +0000197 lldb_private::DataBufferHeap buf(value_byte_size, 0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000198
Sean Callanan08052af2013-04-17 07:50:58 +0000199 lldb_private::Error get_data_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000200
Sean Callanan08052af2013-04-17 07:50:58 +0000201 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000202 return false;
203
Sean Callanan08052af2013-04-17 07:50:58 +0000204 lldb_private::Error write_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000205
Sean Callanan08052af2013-04-17 07:50:58 +0000206 m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
Sean Callananc8675502013-02-15 23:07:52 +0000207
Sean Callanan08052af2013-04-17 07:50:58 +0000208 return write_error.Success();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000209 }
210
Sean Callanan94a9a392012-02-08 01:27:49 +0000211 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000212 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000213 switch (constant->getValueID())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000214 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000215 default:
216 break;
217 case Value::ConstantIntVal:
218 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
Sean Callanan80c48c12011-10-21 05:18:02 +0000219 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000220 value = constant_int->getValue();
221 return true;
222 }
223 break;
224 case Value::ConstantFPVal:
225 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
226 {
227 value = constant_fp->getValueAPF().bitcastToAPInt();
228 return true;
229 }
230 break;
231 case Value::ConstantExprVal:
232 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
233 {
234 switch (constant_expr->getOpcode())
Sean Callanan94a9a392012-02-08 01:27:49 +0000235 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000236 default:
Sean Callanan94a9a392012-02-08 01:27:49 +0000237 return false;
Sean Callanan1582ee62013-04-18 22:06:33 +0000238 case Instruction::IntToPtr:
239 case Instruction::PtrToInt:
240 case Instruction::BitCast:
241 return ResolveConstantValue(value, constant_expr->getOperand(0));
242 case Instruction::GetElementPtr:
243 {
244 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
245 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
246
247 Constant *base = dyn_cast<Constant>(*op_cursor);
248
249 if (!base)
250 return false;
251
252 if (!ResolveConstantValue(value, base))
253 return false;
254
255 op_cursor++;
256
257 if (op_cursor == op_end)
258 return true; // no offset to apply!
259
260 SmallVector <Value *, 8> indices (op_cursor, op_end);
261
262 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
263
264 const bool is_signed = true;
265 value += APInt(value.getBitWidth(), offset, is_signed);
266
267 return true;
268 }
Sean Callanan94a9a392012-02-08 01:27:49 +0000269 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000270 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000271 break;
272 case Value::ConstantPointerNullVal:
273 if (isa<ConstantPointerNull>(constant))
274 {
275 value = APInt(m_target_data.getPointerSizeInBits(), 0);
276 return true;
277 }
278 break;
279 }
280 return false;
281 }
282
283 bool MakeArgument(const Argument *value, uint64_t address)
284 {
285 lldb::addr_t data_address = Malloc(value->getType());
286
287 if (data_address == LLDB_INVALID_ADDRESS)
288 return false;
289
290 lldb_private::Error write_error;
291
292 m_memory_map.WritePointerToMemory(data_address, address, write_error);
293
294 if (!write_error.Success())
295 {
296 lldb_private::Error free_error;
297 m_memory_map.Free(data_address, free_error);
298 return false;
Sean Callanan80c48c12011-10-21 05:18:02 +0000299 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000300
Sean Callanan1582ee62013-04-18 22:06:33 +0000301 m_values[value] = data_address;
302
303 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
304
305 if (log)
306 {
307 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str());
308 log->Printf(" Data region : %llx", (unsigned long long)address);
309 log->Printf(" Ref region : %llx", (unsigned long long)data_address);
310 }
311
312 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000313 }
314
Sean Callanan08052af2013-04-17 07:50:58 +0000315 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant)
Sean Callanan94a9a392012-02-08 01:27:49 +0000316 {
317 APInt resolved_value;
318
319 if (!ResolveConstantValue(resolved_value, constant))
320 return false;
321
322 const uint64_t *raw_data = resolved_value.getRawData();
323
324 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
Sean Callanan94a9a392012-02-08 01:27:49 +0000325
Sean Callanan08052af2013-04-17 07:50:58 +0000326 lldb_private::Error write_error;
327
328 m_memory_map.WriteMemory(process_address, (uint8_t*)raw_data, constant_size, write_error);
329
330 return write_error.Success();
331 }
332
Sean Callanan1582ee62013-04-18 22:06:33 +0000333 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
334 {
335 lldb::addr_t ret = m_stack_pointer;
336
337 ret -= size;
338 ret -= (ret % byte_alignment);
339
340 if (ret < m_frame_process_address)
341 return LLDB_INVALID_ADDRESS;
342
343 m_stack_pointer = ret;
344 return ret;
345 }
346
Sean Callanan08052af2013-04-17 07:50:58 +0000347 lldb::addr_t MallocPointer ()
348 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000349 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
Sean Callanan08052af2013-04-17 07:50:58 +0000350 }
351
Sean Callanan1582ee62013-04-18 22:06:33 +0000352 lldb::addr_t Malloc (llvm::Type *type)
Sean Callanan08052af2013-04-17 07:50:58 +0000353 {
354 lldb_private::Error alloc_error;
355
Sean Callanan1582ee62013-04-18 22:06:33 +0000356 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
Sean Callanan08052af2013-04-17 07:50:58 +0000357 }
358
Sean Callanan08052af2013-04-17 07:50:58 +0000359 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
360 {
361 size_t length = m_target_data.getTypeStoreSize(type);
362
363 lldb_private::DataBufferHeap buf(length, 0);
364
365 lldb_private::Error read_error;
366
367 m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error);
368
369 if (!read_error.Success())
370 return std::string("<couldn't read data>");
371
372 lldb_private::StreamString ss;
373
374 for (size_t i = 0; i < length; i++)
375 {
376 if ((!(i & 0xf)) && i)
377 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
378 else
379 ss.Printf("%02hhx ", buf.GetBytes()[i]);
380 }
381
382 return ss.GetString();
383 }
384
385 lldb::addr_t ResolveValue (const Value *value, Module &module)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000386 {
387 ValueMap::iterator i = m_values.find(value);
388
389 if (i != m_values.end())
390 return i->second;
391
Sean Callanan1582ee62013-04-18 22:06:33 +0000392 // Fall back and allocate space [allocation type Alloca]
393
394 lldb::addr_t data_address = Malloc(value->getType());
395
396 if (const Constant *constant = dyn_cast<Constant>(value))
397 {
398 if (!ResolveConstant (data_address, constant))
399 {
400 lldb_private::Error free_error;
401 m_memory_map.Free(data_address, free_error);
402 return LLDB_INVALID_ADDRESS;
403 }
404 }
405
406 m_values[value] = data_address;
407 return data_address;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000408 }
409};
410
Sean Callanan175a0d02012-01-24 22:06:48 +0000411static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
Sean Callanan3fa3e652013-05-02 00:33:44 +0000412static const char *unsupported_operand_error = "Interpreter doesn't handle one of the expression's operands";
Greg Claytone01e07b2013-04-18 18:10:51 +0000413//static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
Sean Callanan175a0d02012-01-24 22:06:48 +0000414static const char *interpreter_internal_error = "Interpreter encountered an internal error";
415static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
416static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
417static const char *memory_write_error = "Interpreter couldn't write to memory";
418static const char *memory_read_error = "Interpreter couldn't read from memory";
419static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan44342732013-04-19 08:14:32 +0000420//static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callanan175a0d02012-01-24 22:06:48 +0000421
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000422bool
Sean Callanan44342732013-04-19 08:14:32 +0000423IRInterpreter::CanInterpret (llvm::Module &module,
424 llvm::Function &function,
425 lldb_private::Error &error)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000426{
Greg Clayton5160ce52013-03-27 23:08:40 +0000427 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000428
Sean Callanan44342732013-04-19 08:14:32 +0000429 for (Function::iterator bbi = function.begin(), bbe = function.end();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000430 bbi != bbe;
431 ++bbi)
432 {
433 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
434 ii != ie;
435 ++ii)
436 {
437 switch (ii->getOpcode())
438 {
439 default:
440 {
441 if (log)
442 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
Sean Callanan44342732013-04-19 08:14:32 +0000443 error.SetErrorToGenericError();
444 error.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000445 return false;
446 }
447 case Instruction::Add:
448 case Instruction::Alloca:
449 case Instruction::BitCast:
450 case Instruction::Br:
451 case Instruction::GetElementPtr:
452 break;
453 case Instruction::ICmp:
454 {
455 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
456
457 if (!icmp_inst)
Sean Callanan175a0d02012-01-24 22:06:48 +0000458 {
Sean Callanan44342732013-04-19 08:14:32 +0000459 error.SetErrorToGenericError();
460 error.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000461 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +0000462 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000463
464 switch (icmp_inst->getPredicate())
465 {
466 default:
Sean Callanan44342732013-04-19 08:14:32 +0000467 {
468 if (log)
469 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
470
471 error.SetErrorToGenericError();
472 error.SetErrorString(unsupported_opcode_error);
473 return false;
474 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000475 case CmpInst::ICMP_EQ:
476 case CmpInst::ICMP_NE:
477 case CmpInst::ICMP_UGT:
478 case CmpInst::ICMP_UGE:
479 case CmpInst::ICMP_ULT:
480 case CmpInst::ICMP_ULE:
481 case CmpInst::ICMP_SGT:
482 case CmpInst::ICMP_SGE:
483 case CmpInst::ICMP_SLT:
484 case CmpInst::ICMP_SLE:
485 break;
486 }
487 }
488 break;
Sean Callanan087f4372013-01-09 22:44:41 +0000489 case Instruction::And:
490 case Instruction::AShr:
Sean Callanan80c48c12011-10-21 05:18:02 +0000491 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +0000492 case Instruction::PtrToInt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000493 case Instruction::Load:
Sean Callanan087f4372013-01-09 22:44:41 +0000494 case Instruction::LShr:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000495 case Instruction::Mul:
Sean Callanan087f4372013-01-09 22:44:41 +0000496 case Instruction::Or:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000497 case Instruction::Ret:
498 case Instruction::SDiv:
Sean Callanan087f4372013-01-09 22:44:41 +0000499 case Instruction::Shl:
Sean Callananf466a6e2012-12-21 22:27:55 +0000500 case Instruction::SRem:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000501 case Instruction::Store:
502 case Instruction::Sub:
503 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +0000504 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +0000505 case Instruction::Xor:
Sean Callanan1ef77432012-04-23 17:25:38 +0000506 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000507 break;
508 }
Sean Callanan3fa3e652013-05-02 00:33:44 +0000509
510 for (int oi = 0, oe = ii->getNumOperands();
511 oi != oe;
512 ++oi)
513 {
514 Value *operand = ii->getOperand(oi);
515 Type *operand_type = operand->getType();
516
517 switch (operand_type->getTypeID())
518 {
519 default:
520 break;
521 case Type::VectorTyID:
522 {
523 if (log)
524 log->Printf("Unsupported operand type: %s", PrintType(operand_type).c_str());
525 error.SetErrorString(unsupported_operand_error);
526 return false;
527 }
528 }
529 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000530 }
Sean Callanan3fa3e652013-05-02 00:33:44 +0000531
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000532 }
533
Sean Callanan44342732013-04-19 08:14:32 +0000534 return true;}
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000535
536bool
537IRInterpreter::Interpret (llvm::Module &module,
538 llvm::Function &function,
Sean Callanan1582ee62013-04-18 22:06:33 +0000539 llvm::ArrayRef<lldb::addr_t> args,
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000540 lldb_private::IRMemoryMap &memory_map,
Sean Callanandf565402013-04-27 02:19:33 +0000541 lldb_private::Error &error,
542 lldb::addr_t stack_frame_bottom,
543 lldb::addr_t stack_frame_top)
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000544{
545 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
546
Sean Callanan1582ee62013-04-18 22:06:33 +0000547 if (log)
548 {
549 std::string s;
550 raw_string_ostream oss(s);
551
552 module.print(oss, NULL);
553
554 oss.flush();
555
556 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
557 }
558
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000559 DataLayout data_layout(&module);
560
Sean Callanandf565402013-04-27 02:19:33 +0000561 InterpreterStackFrame frame(data_layout, memory_map, stack_frame_bottom, stack_frame_top);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000562
Sean Callanan1582ee62013-04-18 22:06:33 +0000563 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
564 {
565 error.SetErrorString("Couldn't allocate stack frame");
566 }
567
568 int arg_index = 0;
569
570 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
571 ai != ae;
572 ++ai, ++arg_index)
573 {
574 if (args.size() < arg_index)
575 {
576 error.SetErrorString ("Not enough arguments passed in to function");
577 return false;
578 }
579
580 lldb::addr_t ptr = args[arg_index];
581
582 frame.MakeArgument(ai, ptr);
583 }
584
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000585 uint32_t num_insts = 0;
586
587 frame.Jump(function.begin());
588
589 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
590 {
591 const Instruction *inst = frame.m_ii;
592
593 if (log)
594 log->Printf("Interpreting %s", PrintValue(inst).c_str());
595
596 switch (inst->getOpcode())
597 {
598 default:
599 break;
600 case Instruction::Add:
601 case Instruction::Sub:
602 case Instruction::Mul:
603 case Instruction::SDiv:
604 case Instruction::UDiv:
605 case Instruction::SRem:
606 case Instruction::URem:
607 case Instruction::Shl:
608 case Instruction::LShr:
609 case Instruction::AShr:
610 case Instruction::And:
611 case Instruction::Or:
612 case Instruction::Xor:
613 {
614 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
615
616 if (!bin_op)
617 {
618 if (log)
619 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
620 error.SetErrorToGenericError();
621 error.SetErrorString(interpreter_internal_error);
622 return false;
623 }
624
625 Value *lhs = inst->getOperand(0);
626 Value *rhs = inst->getOperand(1);
627
628 lldb_private::Scalar L;
629 lldb_private::Scalar R;
630
631 if (!frame.EvaluateValue(L, lhs, module))
632 {
633 if (log)
634 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
635 error.SetErrorToGenericError();
636 error.SetErrorString(bad_value_error);
637 return false;
638 }
639
640 if (!frame.EvaluateValue(R, rhs, module))
641 {
642 if (log)
643 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
644 error.SetErrorToGenericError();
645 error.SetErrorString(bad_value_error);
646 return false;
647 }
648
649 lldb_private::Scalar result;
650
651 switch (inst->getOpcode())
652 {
653 default:
654 break;
655 case Instruction::Add:
656 result = L + R;
657 break;
658 case Instruction::Mul:
659 result = L * R;
660 break;
661 case Instruction::Sub:
662 result = L - R;
663 break;
664 case Instruction::SDiv:
Sean Callanan0b342b62013-05-24 20:36:56 +0000665 L.MakeSigned();
666 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000667 result = L / R;
668 break;
669 case Instruction::UDiv:
670 result = L.GetRawBits64(0) / R.GetRawBits64(1);
671 break;
672 case Instruction::SRem:
Sean Callanan0b342b62013-05-24 20:36:56 +0000673 L.MakeSigned();
674 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000675 result = L % R;
676 break;
677 case Instruction::URem:
678 result = L.GetRawBits64(0) % R.GetRawBits64(1);
679 break;
680 case Instruction::Shl:
681 result = L << R;
682 break;
683 case Instruction::AShr:
684 result = L >> R;
685 break;
686 case Instruction::LShr:
687 result = L;
688 result.ShiftRightLogical(R);
689 break;
690 case Instruction::And:
691 result = L & R;
692 break;
693 case Instruction::Or:
694 result = L | R;
695 break;
696 case Instruction::Xor:
697 result = L ^ R;
698 break;
699 }
700
701 frame.AssignValue(inst, result, module);
702
703 if (log)
704 {
705 log->Printf("Interpreted a %s", inst->getOpcodeName());
706 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
707 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
708 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
709 }
710 }
711 break;
712 case Instruction::Alloca:
713 {
714 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
715
716 if (!alloca_inst)
717 {
718 if (log)
719 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
720 error.SetErrorToGenericError();
721 error.SetErrorString(interpreter_internal_error);
722 return false;
723 }
724
725 if (alloca_inst->isArrayAllocation())
726 {
727 if (log)
728 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
729 error.SetErrorToGenericError();
730 error.SetErrorString(unsupported_opcode_error);
731 return false;
732 }
733
734 // The semantics of Alloca are:
735 // Create a region R of virtual memory of type T, backed by a data buffer
736 // Create a region P of virtual memory of type T*, backed by a data buffer
737 // Write the virtual address of R into P
738
739 Type *T = alloca_inst->getAllocatedType();
740 Type *Tptr = alloca_inst->getType();
741
742 lldb::addr_t R = frame.Malloc(T);
743
744 if (R == LLDB_INVALID_ADDRESS)
745 {
746 if (log)
747 log->Printf("Couldn't allocate memory for an AllocaInst");
748 error.SetErrorToGenericError();
749 error.SetErrorString(memory_allocation_error);
750 return false;
751 }
752
753 lldb::addr_t P = frame.Malloc(Tptr);
754
755 if (P == LLDB_INVALID_ADDRESS)
756 {
757 if (log)
758 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
759 error.SetErrorToGenericError();
760 error.SetErrorString(memory_allocation_error);
761 return false;
762 }
763
764 lldb_private::Error write_error;
765
766 memory_map.WritePointerToMemory(P, R, write_error);
767
768 if (!write_error.Success())
769 {
770 if (log)
771 log->Printf("Couldn't write the result pointer for an AllocaInst");
772 error.SetErrorToGenericError();
773 error.SetErrorString(memory_write_error);
774 lldb_private::Error free_error;
775 memory_map.Free(P, free_error);
776 memory_map.Free(R, free_error);
777 return false;
778 }
779
780 frame.m_values[alloca_inst] = P;
781
782 if (log)
783 {
784 log->Printf("Interpreted an AllocaInst");
785 log->Printf(" R : 0x%llx", R);
786 log->Printf(" P : 0x%llx", P);
787 }
788 }
789 break;
790 case Instruction::BitCast:
791 case Instruction::ZExt:
792 {
793 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
794
795 if (!cast_inst)
796 {
797 if (log)
798 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
799 error.SetErrorToGenericError();
800 error.SetErrorString(interpreter_internal_error);
801 return false;
802 }
803
804 Value *source = cast_inst->getOperand(0);
805
806 lldb_private::Scalar S;
807
808 if (!frame.EvaluateValue(S, source, module))
809 {
810 if (log)
811 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
812 error.SetErrorToGenericError();
813 error.SetErrorString(bad_value_error);
814 return false;
815 }
816
817 frame.AssignValue(inst, S, module);
818 }
819 break;
820 case Instruction::Br:
821 {
822 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
823
824 if (!br_inst)
825 {
826 if (log)
827 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
828 error.SetErrorToGenericError();
829 error.SetErrorString(interpreter_internal_error);
830 return false;
831 }
832
833 if (br_inst->isConditional())
834 {
835 Value *condition = br_inst->getCondition();
836
837 lldb_private::Scalar C;
838
839 if (!frame.EvaluateValue(C, condition, module))
840 {
841 if (log)
842 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
843 error.SetErrorToGenericError();
844 error.SetErrorString(bad_value_error);
845 return false;
846 }
847
848 if (C.GetRawBits64(0))
849 frame.Jump(br_inst->getSuccessor(0));
850 else
851 frame.Jump(br_inst->getSuccessor(1));
852
853 if (log)
854 {
855 log->Printf("Interpreted a BrInst with a condition");
856 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
857 }
858 }
859 else
860 {
861 frame.Jump(br_inst->getSuccessor(0));
862
863 if (log)
864 {
865 log->Printf("Interpreted a BrInst with no condition");
866 }
867 }
868 }
869 continue;
870 case Instruction::GetElementPtr:
871 {
872 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
873
874 if (!gep_inst)
875 {
876 if (log)
877 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
878 error.SetErrorToGenericError();
879 error.SetErrorString(interpreter_internal_error);
880 return false;
881 }
882
883 const Value *pointer_operand = gep_inst->getPointerOperand();
884 Type *pointer_type = pointer_operand->getType();
885
886 lldb_private::Scalar P;
887
888 if (!frame.EvaluateValue(P, pointer_operand, module))
889 {
890 if (log)
891 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
892 error.SetErrorToGenericError();
893 error.SetErrorString(bad_value_error);
894 return false;
895 }
896
897 typedef SmallVector <Value *, 8> IndexVector;
898 typedef IndexVector::iterator IndexIterator;
899
900 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
901 gep_inst->idx_end());
902
903 SmallVector <Value *, 8> const_indices;
904
905 for (IndexIterator ii = indices.begin(), ie = indices.end();
906 ii != ie;
907 ++ii)
908 {
909 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
910
911 if (!constant_index)
912 {
913 lldb_private::Scalar I;
914
915 if (!frame.EvaluateValue(I, *ii, module))
916 {
917 if (log)
918 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
919 error.SetErrorToGenericError();
920 error.SetErrorString(bad_value_error);
921 return false;
922 }
923
924 if (log)
925 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
926
927 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
928 }
929
930 const_indices.push_back(constant_index);
931 }
932
933 uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices);
934
935 lldb_private::Scalar Poffset = P + offset;
936
937 frame.AssignValue(inst, Poffset, module);
938
939 if (log)
940 {
941 log->Printf("Interpreted a GetElementPtrInst");
942 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
943 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
944 }
945 }
946 break;
947 case Instruction::ICmp:
948 {
949 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
950
951 if (!icmp_inst)
952 {
953 if (log)
954 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
955 error.SetErrorToGenericError();
956 error.SetErrorString(interpreter_internal_error);
957 return false;
958 }
959
960 CmpInst::Predicate predicate = icmp_inst->getPredicate();
961
962 Value *lhs = inst->getOperand(0);
963 Value *rhs = inst->getOperand(1);
964
965 lldb_private::Scalar L;
966 lldb_private::Scalar R;
967
968 if (!frame.EvaluateValue(L, lhs, module))
969 {
970 if (log)
971 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
972 error.SetErrorToGenericError();
973 error.SetErrorString(bad_value_error);
974 return false;
975 }
976
977 if (!frame.EvaluateValue(R, rhs, module))
978 {
979 if (log)
980 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
981 error.SetErrorToGenericError();
982 error.SetErrorString(bad_value_error);
983 return false;
984 }
985
986 lldb_private::Scalar result;
987
988 switch (predicate)
989 {
990 default:
991 return false;
992 case CmpInst::ICMP_EQ:
993 result = (L == R);
994 break;
995 case CmpInst::ICMP_NE:
996 result = (L != R);
997 break;
998 case CmpInst::ICMP_UGT:
999 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1000 break;
1001 case CmpInst::ICMP_UGE:
1002 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1003 break;
1004 case CmpInst::ICMP_ULT:
1005 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1006 break;
1007 case CmpInst::ICMP_ULE:
1008 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1009 break;
1010 case CmpInst::ICMP_SGT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001011 L.MakeSigned();
1012 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001013 result = (L > R);
1014 break;
1015 case CmpInst::ICMP_SGE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001016 L.MakeSigned();
1017 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001018 result = (L >= R);
1019 break;
1020 case CmpInst::ICMP_SLT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001021 L.MakeSigned();
1022 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001023 result = (L < R);
1024 break;
1025 case CmpInst::ICMP_SLE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001026 L.MakeSigned();
1027 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001028 result = (L <= R);
1029 break;
1030 }
1031
1032 frame.AssignValue(inst, result, module);
1033
1034 if (log)
1035 {
1036 log->Printf("Interpreted an ICmpInst");
1037 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1038 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1039 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1040 }
1041 }
1042 break;
1043 case Instruction::IntToPtr:
1044 {
1045 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1046
1047 if (!int_to_ptr_inst)
1048 {
1049 if (log)
1050 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
1051 error.SetErrorToGenericError();
1052 error.SetErrorString(interpreter_internal_error);
1053 return false;
1054 }
1055
1056 Value *src_operand = int_to_ptr_inst->getOperand(0);
1057
1058 lldb_private::Scalar I;
1059
1060 if (!frame.EvaluateValue(I, src_operand, module))
1061 {
1062 if (log)
1063 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1064 error.SetErrorToGenericError();
1065 error.SetErrorString(bad_value_error);
1066 return false;
1067 }
1068
1069 frame.AssignValue(inst, I, module);
1070
1071 if (log)
1072 {
1073 log->Printf("Interpreted an IntToPtr");
1074 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1075 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1076 }
1077 }
1078 break;
1079 case Instruction::PtrToInt:
1080 {
1081 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1082
1083 if (!ptr_to_int_inst)
1084 {
1085 if (log)
1086 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1087 error.SetErrorToGenericError();
1088 error.SetErrorString(interpreter_internal_error);
1089 return false;
1090 }
1091
1092 Value *src_operand = ptr_to_int_inst->getOperand(0);
1093
1094 lldb_private::Scalar I;
1095
1096 if (!frame.EvaluateValue(I, src_operand, module))
1097 {
1098 if (log)
1099 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1100 error.SetErrorToGenericError();
1101 error.SetErrorString(bad_value_error);
1102 return false;
1103 }
1104
1105 frame.AssignValue(inst, I, module);
1106
1107 if (log)
1108 {
1109 log->Printf("Interpreted a PtrToInt");
1110 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1111 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1112 }
1113 }
1114 break;
1115 case Instruction::Load:
1116 {
1117 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1118
1119 if (!load_inst)
1120 {
1121 if (log)
1122 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
1123 error.SetErrorToGenericError();
1124 error.SetErrorString(interpreter_internal_error);
1125 return false;
1126 }
1127
1128 // The semantics of Load are:
1129 // Create a region D that will contain the loaded data
1130 // Resolve the region P containing a pointer
1131 // Dereference P to get the region R that the data should be loaded from
1132 // Transfer a unit of type type(D) from R to D
1133
1134 const Value *pointer_operand = load_inst->getPointerOperand();
1135
1136 Type *pointer_ty = pointer_operand->getType();
1137 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1138 if (!pointer_ptr_ty)
1139 {
1140 if (log)
1141 log->Printf("getPointerOperand()->getType() is not a PointerType");
1142 error.SetErrorToGenericError();
1143 error.SetErrorString(interpreter_internal_error);
1144 return false;
1145 }
1146 Type *target_ty = pointer_ptr_ty->getElementType();
1147
1148 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1149 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1150
1151 if (D == LLDB_INVALID_ADDRESS)
1152 {
1153 if (log)
1154 log->Printf("LoadInst's value doesn't resolve to anything");
1155 error.SetErrorToGenericError();
1156 error.SetErrorString(bad_value_error);
1157 return false;
1158 }
1159
1160 if (P == LLDB_INVALID_ADDRESS)
1161 {
1162 if (log)
1163 log->Printf("LoadInst's pointer doesn't resolve to anything");
1164 error.SetErrorToGenericError();
1165 error.SetErrorString(bad_value_error);
1166 return false;
1167 }
1168
1169 lldb::addr_t R;
1170 lldb_private::Error read_error;
1171 memory_map.ReadPointerFromMemory(&R, P, read_error);
1172
1173 if (!read_error.Success())
1174 {
1175 if (log)
1176 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1177 error.SetErrorToGenericError();
1178 error.SetErrorString(memory_read_error);
1179 return false;
1180 }
1181
1182 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1183 lldb_private::DataBufferHeap buffer(target_size, 0);
1184
1185 read_error.Clear();
1186 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
1187 if (!read_error.Success())
1188 {
1189 if (log)
1190 log->Printf("Couldn't read from a region on behalf of a LoadInst");
1191 error.SetErrorToGenericError();
1192 error.SetErrorString(memory_read_error);
1193 return false;
1194 }
1195
1196 lldb_private::Error write_error;
1197 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1198 if (!write_error.Success())
1199 {
1200 if (log)
1201 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1202 error.SetErrorToGenericError();
1203 error.SetErrorString(memory_read_error);
1204 return false;
1205 }
1206
1207 if (log)
1208 {
1209 log->Printf("Interpreted a LoadInst");
1210 log->Printf(" P : 0x%llx", P);
1211 log->Printf(" R : 0x%llx", R);
1212 log->Printf(" D : 0x%llx", D);
1213 }
1214 }
1215 break;
1216 case Instruction::Ret:
1217 {
1218 return true;
1219 }
1220 case Instruction::Store:
1221 {
1222 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1223
1224 if (!store_inst)
1225 {
1226 if (log)
1227 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
1228 error.SetErrorToGenericError();
1229 error.SetErrorString(interpreter_internal_error);
1230 return false;
1231 }
1232
1233 // The semantics of Store are:
1234 // Resolve the region D containing the data to be stored
1235 // Resolve the region P containing a pointer
1236 // Dereference P to get the region R that the data should be stored in
1237 // Transfer a unit of type type(D) from D to R
1238
1239 const Value *value_operand = store_inst->getValueOperand();
1240 const Value *pointer_operand = store_inst->getPointerOperand();
1241
1242 Type *pointer_ty = pointer_operand->getType();
1243 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1244 if (!pointer_ptr_ty)
1245 return false;
1246 Type *target_ty = pointer_ptr_ty->getElementType();
1247
1248 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1249 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1250
1251 if (D == LLDB_INVALID_ADDRESS)
1252 {
1253 if (log)
1254 log->Printf("StoreInst's value doesn't resolve to anything");
1255 error.SetErrorToGenericError();
1256 error.SetErrorString(bad_value_error);
1257 return false;
1258 }
1259
1260 if (P == LLDB_INVALID_ADDRESS)
1261 {
1262 if (log)
1263 log->Printf("StoreInst's pointer doesn't resolve to anything");
1264 error.SetErrorToGenericError();
1265 error.SetErrorString(bad_value_error);
1266 return false;
1267 }
1268
1269 lldb::addr_t R;
1270 lldb_private::Error read_error;
1271 memory_map.ReadPointerFromMemory(&R, P, read_error);
1272
1273 if (!read_error.Success())
1274 {
1275 if (log)
1276 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1277 error.SetErrorToGenericError();
1278 error.SetErrorString(memory_read_error);
1279 return false;
1280 }
1281
1282 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1283 lldb_private::DataBufferHeap buffer(target_size, 0);
1284
1285 read_error.Clear();
1286 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
1287 if (!read_error.Success())
1288 {
1289 if (log)
1290 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1291 error.SetErrorToGenericError();
1292 error.SetErrorString(memory_read_error);
1293 return false;
1294 }
1295
1296 lldb_private::Error write_error;
1297 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1298 if (!write_error.Success())
1299 {
1300 if (log)
1301 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1302 error.SetErrorToGenericError();
Sean Callanan49630e72013-04-20 02:39:24 +00001303 error.SetErrorString(memory_write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001304 return false;
1305 }
1306
1307 if (log)
1308 {
1309 log->Printf("Interpreted a StoreInst");
1310 log->Printf(" D : 0x%llx", D);
1311 log->Printf(" P : 0x%llx", P);
1312 log->Printf(" R : 0x%llx", R);
1313 }
1314 }
1315 break;
1316 }
1317
1318 ++frame.m_ii;
1319 }
1320
1321 if (num_insts >= 4096)
1322 {
1323 error.SetErrorToGenericError();
1324 error.SetErrorString(infinite_loop_error);
1325 return false;
1326 }
1327
1328 return false;
1329}