blob: 122d337ddcc53cd634a7794026a42a042f376a8e [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 {
Sean Callanan7d01ddd2013-05-31 17:29:03 +0000154 if (isa<ConstantPointerNull>(constant))
155 {
156 return AssignToMatchType(scalar, 0, value->getType());
157 }
158 else if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000159 {
160 return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType());
161 }
162 }
163 else
164 {
Sean Callanan08052af2013-04-17 07:50:58 +0000165 lldb::addr_t process_address = ResolveValue(value, module);
166 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
167
168 lldb_private::DataExtractor value_extractor;
169 lldb_private::Error extract_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000170
Sean Callanan08052af2013-04-17 07:50:58 +0000171 m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error);
172
173 if (!extract_error.Success())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000174 return false;
175
Greg Claytonc7bece562013-01-25 18:06:21 +0000176 lldb::offset_t offset = 0;
Greg Clayton78e44bd2013-04-25 00:57:05 +0000177 if (value_size <= 8)
178 {
179 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
180 return AssignToMatchType(scalar, u64value, value->getType());
181 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000182 }
183
184 return false;
185 }
186
187 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
188 {
Sean Callanan08052af2013-04-17 07:50:58 +0000189 lldb::addr_t process_address = ResolveValue (value, module);
190
191 if (process_address == LLDB_INVALID_ADDRESS)
192 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000193
194 lldb_private::Scalar cast_scalar;
195
196 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
197 return false;
Sean Callanan08052af2013-04-17 07:50:58 +0000198
199 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000200
Sean Callanan08052af2013-04-17 07:50:58 +0000201 lldb_private::DataBufferHeap buf(value_byte_size, 0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000202
Sean Callanan08052af2013-04-17 07:50:58 +0000203 lldb_private::Error get_data_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000204
Sean Callanan08052af2013-04-17 07:50:58 +0000205 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000206 return false;
207
Sean Callanan08052af2013-04-17 07:50:58 +0000208 lldb_private::Error write_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000209
Sean Callanan08052af2013-04-17 07:50:58 +0000210 m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
Sean Callananc8675502013-02-15 23:07:52 +0000211
Sean Callanan08052af2013-04-17 07:50:58 +0000212 return write_error.Success();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000213 }
214
Sean Callanan94a9a392012-02-08 01:27:49 +0000215 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000216 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000217 switch (constant->getValueID())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000218 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000219 default:
220 break;
221 case Value::ConstantIntVal:
222 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
Sean Callanan80c48c12011-10-21 05:18:02 +0000223 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000224 value = constant_int->getValue();
225 return true;
226 }
227 break;
228 case Value::ConstantFPVal:
229 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
230 {
231 value = constant_fp->getValueAPF().bitcastToAPInt();
232 return true;
233 }
234 break;
235 case Value::ConstantExprVal:
236 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
237 {
238 switch (constant_expr->getOpcode())
Sean Callanan94a9a392012-02-08 01:27:49 +0000239 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000240 default:
Sean Callanan94a9a392012-02-08 01:27:49 +0000241 return false;
Sean Callanan1582ee62013-04-18 22:06:33 +0000242 case Instruction::IntToPtr:
243 case Instruction::PtrToInt:
244 case Instruction::BitCast:
245 return ResolveConstantValue(value, constant_expr->getOperand(0));
246 case Instruction::GetElementPtr:
247 {
248 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
249 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
250
251 Constant *base = dyn_cast<Constant>(*op_cursor);
252
253 if (!base)
254 return false;
255
256 if (!ResolveConstantValue(value, base))
257 return false;
258
259 op_cursor++;
260
261 if (op_cursor == op_end)
262 return true; // no offset to apply!
263
264 SmallVector <Value *, 8> indices (op_cursor, op_end);
265
266 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
267
268 const bool is_signed = true;
269 value += APInt(value.getBitWidth(), offset, is_signed);
270
271 return true;
272 }
Sean Callanan94a9a392012-02-08 01:27:49 +0000273 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000274 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000275 break;
276 case Value::ConstantPointerNullVal:
277 if (isa<ConstantPointerNull>(constant))
278 {
279 value = APInt(m_target_data.getPointerSizeInBits(), 0);
280 return true;
281 }
282 break;
283 }
284 return false;
285 }
286
287 bool MakeArgument(const Argument *value, uint64_t address)
288 {
289 lldb::addr_t data_address = Malloc(value->getType());
290
291 if (data_address == LLDB_INVALID_ADDRESS)
292 return false;
293
294 lldb_private::Error write_error;
295
296 m_memory_map.WritePointerToMemory(data_address, address, write_error);
297
298 if (!write_error.Success())
299 {
300 lldb_private::Error free_error;
301 m_memory_map.Free(data_address, free_error);
302 return false;
Sean Callanan80c48c12011-10-21 05:18:02 +0000303 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000304
Sean Callanan1582ee62013-04-18 22:06:33 +0000305 m_values[value] = data_address;
306
307 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
308
309 if (log)
310 {
311 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str());
312 log->Printf(" Data region : %llx", (unsigned long long)address);
313 log->Printf(" Ref region : %llx", (unsigned long long)data_address);
314 }
315
316 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000317 }
318
Sean Callanan08052af2013-04-17 07:50:58 +0000319 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant)
Sean Callanan94a9a392012-02-08 01:27:49 +0000320 {
321 APInt resolved_value;
322
323 if (!ResolveConstantValue(resolved_value, constant))
324 return false;
325
326 const uint64_t *raw_data = resolved_value.getRawData();
327
328 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
Sean Callanan94a9a392012-02-08 01:27:49 +0000329
Sean Callanan08052af2013-04-17 07:50:58 +0000330 lldb_private::Error write_error;
331
332 m_memory_map.WriteMemory(process_address, (uint8_t*)raw_data, constant_size, write_error);
333
334 return write_error.Success();
335 }
336
Sean Callanan1582ee62013-04-18 22:06:33 +0000337 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
338 {
339 lldb::addr_t ret = m_stack_pointer;
340
341 ret -= size;
342 ret -= (ret % byte_alignment);
343
344 if (ret < m_frame_process_address)
345 return LLDB_INVALID_ADDRESS;
346
347 m_stack_pointer = ret;
348 return ret;
349 }
350
Sean Callanan08052af2013-04-17 07:50:58 +0000351 lldb::addr_t MallocPointer ()
352 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000353 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
Sean Callanan08052af2013-04-17 07:50:58 +0000354 }
355
Sean Callanan1582ee62013-04-18 22:06:33 +0000356 lldb::addr_t Malloc (llvm::Type *type)
Sean Callanan08052af2013-04-17 07:50:58 +0000357 {
358 lldb_private::Error alloc_error;
359
Sean Callanan1582ee62013-04-18 22:06:33 +0000360 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
Sean Callanan08052af2013-04-17 07:50:58 +0000361 }
362
Sean Callanan08052af2013-04-17 07:50:58 +0000363 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
364 {
365 size_t length = m_target_data.getTypeStoreSize(type);
366
367 lldb_private::DataBufferHeap buf(length, 0);
368
369 lldb_private::Error read_error;
370
371 m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error);
372
373 if (!read_error.Success())
374 return std::string("<couldn't read data>");
375
376 lldb_private::StreamString ss;
377
378 for (size_t i = 0; i < length; i++)
379 {
380 if ((!(i & 0xf)) && i)
381 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
382 else
383 ss.Printf("%02hhx ", buf.GetBytes()[i]);
384 }
385
386 return ss.GetString();
387 }
388
389 lldb::addr_t ResolveValue (const Value *value, Module &module)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000390 {
391 ValueMap::iterator i = m_values.find(value);
392
393 if (i != m_values.end())
394 return i->second;
395
Sean Callanan1582ee62013-04-18 22:06:33 +0000396 // Fall back and allocate space [allocation type Alloca]
397
398 lldb::addr_t data_address = Malloc(value->getType());
399
400 if (const Constant *constant = dyn_cast<Constant>(value))
401 {
402 if (!ResolveConstant (data_address, constant))
403 {
404 lldb_private::Error free_error;
405 m_memory_map.Free(data_address, free_error);
406 return LLDB_INVALID_ADDRESS;
407 }
408 }
409
410 m_values[value] = data_address;
411 return data_address;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000412 }
413};
414
Sean Callanan175a0d02012-01-24 22:06:48 +0000415static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
Sean Callanan3fa3e652013-05-02 00:33:44 +0000416static const char *unsupported_operand_error = "Interpreter doesn't handle one of the expression's operands";
Greg Claytone01e07b2013-04-18 18:10:51 +0000417//static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
Sean Callanan175a0d02012-01-24 22:06:48 +0000418static const char *interpreter_internal_error = "Interpreter encountered an internal error";
419static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
420static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
421static const char *memory_write_error = "Interpreter couldn't write to memory";
422static const char *memory_read_error = "Interpreter couldn't read from memory";
423static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan44342732013-04-19 08:14:32 +0000424//static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callanan175a0d02012-01-24 22:06:48 +0000425
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000426bool
Sean Callanan44342732013-04-19 08:14:32 +0000427IRInterpreter::CanInterpret (llvm::Module &module,
428 llvm::Function &function,
429 lldb_private::Error &error)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000430{
Greg Clayton5160ce52013-03-27 23:08:40 +0000431 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000432
Sean Callanan44342732013-04-19 08:14:32 +0000433 for (Function::iterator bbi = function.begin(), bbe = function.end();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000434 bbi != bbe;
435 ++bbi)
436 {
437 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
438 ii != ie;
439 ++ii)
440 {
441 switch (ii->getOpcode())
442 {
443 default:
444 {
445 if (log)
446 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
Sean Callanan44342732013-04-19 08:14:32 +0000447 error.SetErrorToGenericError();
448 error.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000449 return false;
450 }
451 case Instruction::Add:
452 case Instruction::Alloca:
453 case Instruction::BitCast:
454 case Instruction::Br:
455 case Instruction::GetElementPtr:
456 break;
457 case Instruction::ICmp:
458 {
459 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
460
461 if (!icmp_inst)
Sean Callanan175a0d02012-01-24 22:06:48 +0000462 {
Sean Callanan44342732013-04-19 08:14:32 +0000463 error.SetErrorToGenericError();
464 error.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000465 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +0000466 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000467
468 switch (icmp_inst->getPredicate())
469 {
470 default:
Sean Callanan44342732013-04-19 08:14:32 +0000471 {
472 if (log)
473 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
474
475 error.SetErrorToGenericError();
476 error.SetErrorString(unsupported_opcode_error);
477 return false;
478 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000479 case CmpInst::ICMP_EQ:
480 case CmpInst::ICMP_NE:
481 case CmpInst::ICMP_UGT:
482 case CmpInst::ICMP_UGE:
483 case CmpInst::ICMP_ULT:
484 case CmpInst::ICMP_ULE:
485 case CmpInst::ICMP_SGT:
486 case CmpInst::ICMP_SGE:
487 case CmpInst::ICMP_SLT:
488 case CmpInst::ICMP_SLE:
489 break;
490 }
491 }
492 break;
Sean Callanan087f4372013-01-09 22:44:41 +0000493 case Instruction::And:
494 case Instruction::AShr:
Sean Callanan80c48c12011-10-21 05:18:02 +0000495 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +0000496 case Instruction::PtrToInt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000497 case Instruction::Load:
Sean Callanan087f4372013-01-09 22:44:41 +0000498 case Instruction::LShr:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000499 case Instruction::Mul:
Sean Callanan087f4372013-01-09 22:44:41 +0000500 case Instruction::Or:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000501 case Instruction::Ret:
502 case Instruction::SDiv:
Sean Callanan087f4372013-01-09 22:44:41 +0000503 case Instruction::Shl:
Sean Callananf466a6e2012-12-21 22:27:55 +0000504 case Instruction::SRem:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000505 case Instruction::Store:
506 case Instruction::Sub:
507 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +0000508 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +0000509 case Instruction::Xor:
Sean Callanan1ef77432012-04-23 17:25:38 +0000510 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000511 break;
512 }
Sean Callanan3fa3e652013-05-02 00:33:44 +0000513
514 for (int oi = 0, oe = ii->getNumOperands();
515 oi != oe;
516 ++oi)
517 {
518 Value *operand = ii->getOperand(oi);
519 Type *operand_type = operand->getType();
520
521 switch (operand_type->getTypeID())
522 {
523 default:
524 break;
525 case Type::VectorTyID:
526 {
527 if (log)
528 log->Printf("Unsupported operand type: %s", PrintType(operand_type).c_str());
529 error.SetErrorString(unsupported_operand_error);
530 return false;
531 }
532 }
533 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000534 }
Sean Callanan3fa3e652013-05-02 00:33:44 +0000535
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000536 }
537
Sean Callanan44342732013-04-19 08:14:32 +0000538 return true;}
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000539
540bool
541IRInterpreter::Interpret (llvm::Module &module,
542 llvm::Function &function,
Sean Callanan1582ee62013-04-18 22:06:33 +0000543 llvm::ArrayRef<lldb::addr_t> args,
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000544 lldb_private::IRMemoryMap &memory_map,
Sean Callanandf565402013-04-27 02:19:33 +0000545 lldb_private::Error &error,
546 lldb::addr_t stack_frame_bottom,
547 lldb::addr_t stack_frame_top)
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000548{
549 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
550
Sean Callanan1582ee62013-04-18 22:06:33 +0000551 if (log)
552 {
553 std::string s;
554 raw_string_ostream oss(s);
555
556 module.print(oss, NULL);
557
558 oss.flush();
559
560 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
561 }
562
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000563 DataLayout data_layout(&module);
564
Sean Callanandf565402013-04-27 02:19:33 +0000565 InterpreterStackFrame frame(data_layout, memory_map, stack_frame_bottom, stack_frame_top);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000566
Sean Callanan1582ee62013-04-18 22:06:33 +0000567 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
568 {
569 error.SetErrorString("Couldn't allocate stack frame");
570 }
571
572 int arg_index = 0;
573
574 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
575 ai != ae;
576 ++ai, ++arg_index)
577 {
578 if (args.size() < arg_index)
579 {
580 error.SetErrorString ("Not enough arguments passed in to function");
581 return false;
582 }
583
584 lldb::addr_t ptr = args[arg_index];
585
586 frame.MakeArgument(ai, ptr);
587 }
588
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000589 uint32_t num_insts = 0;
590
591 frame.Jump(function.begin());
592
593 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
594 {
595 const Instruction *inst = frame.m_ii;
596
597 if (log)
598 log->Printf("Interpreting %s", PrintValue(inst).c_str());
599
600 switch (inst->getOpcode())
601 {
602 default:
603 break;
604 case Instruction::Add:
605 case Instruction::Sub:
606 case Instruction::Mul:
607 case Instruction::SDiv:
608 case Instruction::UDiv:
609 case Instruction::SRem:
610 case Instruction::URem:
611 case Instruction::Shl:
612 case Instruction::LShr:
613 case Instruction::AShr:
614 case Instruction::And:
615 case Instruction::Or:
616 case Instruction::Xor:
617 {
618 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
619
620 if (!bin_op)
621 {
622 if (log)
623 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
624 error.SetErrorToGenericError();
625 error.SetErrorString(interpreter_internal_error);
626 return false;
627 }
628
629 Value *lhs = inst->getOperand(0);
630 Value *rhs = inst->getOperand(1);
631
632 lldb_private::Scalar L;
633 lldb_private::Scalar R;
634
635 if (!frame.EvaluateValue(L, lhs, module))
636 {
637 if (log)
638 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
639 error.SetErrorToGenericError();
640 error.SetErrorString(bad_value_error);
641 return false;
642 }
643
644 if (!frame.EvaluateValue(R, rhs, module))
645 {
646 if (log)
647 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
648 error.SetErrorToGenericError();
649 error.SetErrorString(bad_value_error);
650 return false;
651 }
652
653 lldb_private::Scalar result;
654
655 switch (inst->getOpcode())
656 {
657 default:
658 break;
659 case Instruction::Add:
660 result = L + R;
661 break;
662 case Instruction::Mul:
663 result = L * R;
664 break;
665 case Instruction::Sub:
666 result = L - R;
667 break;
668 case Instruction::SDiv:
Sean Callanan0b342b62013-05-24 20:36:56 +0000669 L.MakeSigned();
670 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000671 result = L / R;
672 break;
673 case Instruction::UDiv:
674 result = L.GetRawBits64(0) / R.GetRawBits64(1);
675 break;
676 case Instruction::SRem:
Sean Callanan0b342b62013-05-24 20:36:56 +0000677 L.MakeSigned();
678 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000679 result = L % R;
680 break;
681 case Instruction::URem:
682 result = L.GetRawBits64(0) % R.GetRawBits64(1);
683 break;
684 case Instruction::Shl:
685 result = L << R;
686 break;
687 case Instruction::AShr:
688 result = L >> R;
689 break;
690 case Instruction::LShr:
691 result = L;
692 result.ShiftRightLogical(R);
693 break;
694 case Instruction::And:
695 result = L & R;
696 break;
697 case Instruction::Or:
698 result = L | R;
699 break;
700 case Instruction::Xor:
701 result = L ^ R;
702 break;
703 }
704
705 frame.AssignValue(inst, result, module);
706
707 if (log)
708 {
709 log->Printf("Interpreted a %s", inst->getOpcodeName());
710 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
711 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
712 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
713 }
714 }
715 break;
716 case Instruction::Alloca:
717 {
718 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
719
720 if (!alloca_inst)
721 {
722 if (log)
723 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
724 error.SetErrorToGenericError();
725 error.SetErrorString(interpreter_internal_error);
726 return false;
727 }
728
729 if (alloca_inst->isArrayAllocation())
730 {
731 if (log)
732 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
733 error.SetErrorToGenericError();
734 error.SetErrorString(unsupported_opcode_error);
735 return false;
736 }
737
738 // The semantics of Alloca are:
739 // Create a region R of virtual memory of type T, backed by a data buffer
740 // Create a region P of virtual memory of type T*, backed by a data buffer
741 // Write the virtual address of R into P
742
743 Type *T = alloca_inst->getAllocatedType();
744 Type *Tptr = alloca_inst->getType();
745
746 lldb::addr_t R = frame.Malloc(T);
747
748 if (R == LLDB_INVALID_ADDRESS)
749 {
750 if (log)
751 log->Printf("Couldn't allocate memory for an AllocaInst");
752 error.SetErrorToGenericError();
753 error.SetErrorString(memory_allocation_error);
754 return false;
755 }
756
757 lldb::addr_t P = frame.Malloc(Tptr);
758
759 if (P == LLDB_INVALID_ADDRESS)
760 {
761 if (log)
762 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
763 error.SetErrorToGenericError();
764 error.SetErrorString(memory_allocation_error);
765 return false;
766 }
767
768 lldb_private::Error write_error;
769
770 memory_map.WritePointerToMemory(P, R, write_error);
771
772 if (!write_error.Success())
773 {
774 if (log)
775 log->Printf("Couldn't write the result pointer for an AllocaInst");
776 error.SetErrorToGenericError();
777 error.SetErrorString(memory_write_error);
778 lldb_private::Error free_error;
779 memory_map.Free(P, free_error);
780 memory_map.Free(R, free_error);
781 return false;
782 }
783
784 frame.m_values[alloca_inst] = P;
785
786 if (log)
787 {
788 log->Printf("Interpreted an AllocaInst");
Matt Kopecef143712013-06-03 18:00:07 +0000789 log->Printf(" R : 0x%" PRIx64, R);
790 log->Printf(" P : 0x%" PRIx64, P);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000791 }
792 }
793 break;
794 case Instruction::BitCast:
795 case Instruction::ZExt:
796 {
797 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
798
799 if (!cast_inst)
800 {
801 if (log)
802 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
803 error.SetErrorToGenericError();
804 error.SetErrorString(interpreter_internal_error);
805 return false;
806 }
807
808 Value *source = cast_inst->getOperand(0);
809
810 lldb_private::Scalar S;
811
812 if (!frame.EvaluateValue(S, source, module))
813 {
814 if (log)
815 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
816 error.SetErrorToGenericError();
817 error.SetErrorString(bad_value_error);
818 return false;
819 }
820
821 frame.AssignValue(inst, S, module);
822 }
823 break;
824 case Instruction::Br:
825 {
826 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
827
828 if (!br_inst)
829 {
830 if (log)
831 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
832 error.SetErrorToGenericError();
833 error.SetErrorString(interpreter_internal_error);
834 return false;
835 }
836
837 if (br_inst->isConditional())
838 {
839 Value *condition = br_inst->getCondition();
840
841 lldb_private::Scalar C;
842
843 if (!frame.EvaluateValue(C, condition, module))
844 {
845 if (log)
846 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
847 error.SetErrorToGenericError();
848 error.SetErrorString(bad_value_error);
849 return false;
850 }
851
852 if (C.GetRawBits64(0))
853 frame.Jump(br_inst->getSuccessor(0));
854 else
855 frame.Jump(br_inst->getSuccessor(1));
856
857 if (log)
858 {
859 log->Printf("Interpreted a BrInst with a condition");
860 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
861 }
862 }
863 else
864 {
865 frame.Jump(br_inst->getSuccessor(0));
866
867 if (log)
868 {
869 log->Printf("Interpreted a BrInst with no condition");
870 }
871 }
872 }
873 continue;
874 case Instruction::GetElementPtr:
875 {
876 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
877
878 if (!gep_inst)
879 {
880 if (log)
881 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
882 error.SetErrorToGenericError();
883 error.SetErrorString(interpreter_internal_error);
884 return false;
885 }
886
887 const Value *pointer_operand = gep_inst->getPointerOperand();
888 Type *pointer_type = pointer_operand->getType();
889
890 lldb_private::Scalar P;
891
892 if (!frame.EvaluateValue(P, pointer_operand, module))
893 {
894 if (log)
895 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
896 error.SetErrorToGenericError();
897 error.SetErrorString(bad_value_error);
898 return false;
899 }
900
901 typedef SmallVector <Value *, 8> IndexVector;
902 typedef IndexVector::iterator IndexIterator;
903
904 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
905 gep_inst->idx_end());
906
907 SmallVector <Value *, 8> const_indices;
908
909 for (IndexIterator ii = indices.begin(), ie = indices.end();
910 ii != ie;
911 ++ii)
912 {
913 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
914
915 if (!constant_index)
916 {
917 lldb_private::Scalar I;
918
919 if (!frame.EvaluateValue(I, *ii, module))
920 {
921 if (log)
922 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
923 error.SetErrorToGenericError();
924 error.SetErrorString(bad_value_error);
925 return false;
926 }
927
928 if (log)
929 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
930
931 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
932 }
933
934 const_indices.push_back(constant_index);
935 }
936
937 uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices);
938
939 lldb_private::Scalar Poffset = P + offset;
940
941 frame.AssignValue(inst, Poffset, module);
942
943 if (log)
944 {
945 log->Printf("Interpreted a GetElementPtrInst");
946 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
947 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
948 }
949 }
950 break;
951 case Instruction::ICmp:
952 {
953 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
954
955 if (!icmp_inst)
956 {
957 if (log)
958 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
959 error.SetErrorToGenericError();
960 error.SetErrorString(interpreter_internal_error);
961 return false;
962 }
963
964 CmpInst::Predicate predicate = icmp_inst->getPredicate();
965
966 Value *lhs = inst->getOperand(0);
967 Value *rhs = inst->getOperand(1);
968
969 lldb_private::Scalar L;
970 lldb_private::Scalar R;
971
972 if (!frame.EvaluateValue(L, lhs, module))
973 {
974 if (log)
975 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
976 error.SetErrorToGenericError();
977 error.SetErrorString(bad_value_error);
978 return false;
979 }
980
981 if (!frame.EvaluateValue(R, rhs, module))
982 {
983 if (log)
984 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
985 error.SetErrorToGenericError();
986 error.SetErrorString(bad_value_error);
987 return false;
988 }
989
990 lldb_private::Scalar result;
991
992 switch (predicate)
993 {
994 default:
995 return false;
996 case CmpInst::ICMP_EQ:
997 result = (L == R);
998 break;
999 case CmpInst::ICMP_NE:
1000 result = (L != R);
1001 break;
1002 case CmpInst::ICMP_UGT:
1003 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1004 break;
1005 case CmpInst::ICMP_UGE:
1006 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1007 break;
1008 case CmpInst::ICMP_ULT:
1009 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1010 break;
1011 case CmpInst::ICMP_ULE:
1012 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1013 break;
1014 case CmpInst::ICMP_SGT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001015 L.MakeSigned();
1016 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001017 result = (L > R);
1018 break;
1019 case CmpInst::ICMP_SGE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001020 L.MakeSigned();
1021 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001022 result = (L >= R);
1023 break;
1024 case CmpInst::ICMP_SLT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001025 L.MakeSigned();
1026 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001027 result = (L < R);
1028 break;
1029 case CmpInst::ICMP_SLE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001030 L.MakeSigned();
1031 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001032 result = (L <= R);
1033 break;
1034 }
1035
1036 frame.AssignValue(inst, result, module);
1037
1038 if (log)
1039 {
1040 log->Printf("Interpreted an ICmpInst");
1041 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1042 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1043 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1044 }
1045 }
1046 break;
1047 case Instruction::IntToPtr:
1048 {
1049 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1050
1051 if (!int_to_ptr_inst)
1052 {
1053 if (log)
1054 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
1055 error.SetErrorToGenericError();
1056 error.SetErrorString(interpreter_internal_error);
1057 return false;
1058 }
1059
1060 Value *src_operand = int_to_ptr_inst->getOperand(0);
1061
1062 lldb_private::Scalar I;
1063
1064 if (!frame.EvaluateValue(I, src_operand, module))
1065 {
1066 if (log)
1067 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1068 error.SetErrorToGenericError();
1069 error.SetErrorString(bad_value_error);
1070 return false;
1071 }
1072
1073 frame.AssignValue(inst, I, module);
1074
1075 if (log)
1076 {
1077 log->Printf("Interpreted an IntToPtr");
1078 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1079 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1080 }
1081 }
1082 break;
1083 case Instruction::PtrToInt:
1084 {
1085 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1086
1087 if (!ptr_to_int_inst)
1088 {
1089 if (log)
1090 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1091 error.SetErrorToGenericError();
1092 error.SetErrorString(interpreter_internal_error);
1093 return false;
1094 }
1095
1096 Value *src_operand = ptr_to_int_inst->getOperand(0);
1097
1098 lldb_private::Scalar I;
1099
1100 if (!frame.EvaluateValue(I, src_operand, module))
1101 {
1102 if (log)
1103 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1104 error.SetErrorToGenericError();
1105 error.SetErrorString(bad_value_error);
1106 return false;
1107 }
1108
1109 frame.AssignValue(inst, I, module);
1110
1111 if (log)
1112 {
1113 log->Printf("Interpreted a PtrToInt");
1114 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1115 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1116 }
1117 }
1118 break;
1119 case Instruction::Load:
1120 {
1121 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1122
1123 if (!load_inst)
1124 {
1125 if (log)
1126 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
1127 error.SetErrorToGenericError();
1128 error.SetErrorString(interpreter_internal_error);
1129 return false;
1130 }
1131
1132 // The semantics of Load are:
1133 // Create a region D that will contain the loaded data
1134 // Resolve the region P containing a pointer
1135 // Dereference P to get the region R that the data should be loaded from
1136 // Transfer a unit of type type(D) from R to D
1137
1138 const Value *pointer_operand = load_inst->getPointerOperand();
1139
1140 Type *pointer_ty = pointer_operand->getType();
1141 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1142 if (!pointer_ptr_ty)
1143 {
1144 if (log)
1145 log->Printf("getPointerOperand()->getType() is not a PointerType");
1146 error.SetErrorToGenericError();
1147 error.SetErrorString(interpreter_internal_error);
1148 return false;
1149 }
1150 Type *target_ty = pointer_ptr_ty->getElementType();
1151
1152 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1153 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1154
1155 if (D == LLDB_INVALID_ADDRESS)
1156 {
1157 if (log)
1158 log->Printf("LoadInst's value doesn't resolve to anything");
1159 error.SetErrorToGenericError();
1160 error.SetErrorString(bad_value_error);
1161 return false;
1162 }
1163
1164 if (P == LLDB_INVALID_ADDRESS)
1165 {
1166 if (log)
1167 log->Printf("LoadInst's pointer doesn't resolve to anything");
1168 error.SetErrorToGenericError();
1169 error.SetErrorString(bad_value_error);
1170 return false;
1171 }
1172
1173 lldb::addr_t R;
1174 lldb_private::Error read_error;
1175 memory_map.ReadPointerFromMemory(&R, P, read_error);
1176
1177 if (!read_error.Success())
1178 {
1179 if (log)
1180 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1181 error.SetErrorToGenericError();
1182 error.SetErrorString(memory_read_error);
1183 return false;
1184 }
1185
1186 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1187 lldb_private::DataBufferHeap buffer(target_size, 0);
1188
1189 read_error.Clear();
1190 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
1191 if (!read_error.Success())
1192 {
1193 if (log)
1194 log->Printf("Couldn't read from a region on behalf of a LoadInst");
1195 error.SetErrorToGenericError();
1196 error.SetErrorString(memory_read_error);
1197 return false;
1198 }
1199
1200 lldb_private::Error write_error;
1201 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1202 if (!write_error.Success())
1203 {
1204 if (log)
1205 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1206 error.SetErrorToGenericError();
1207 error.SetErrorString(memory_read_error);
1208 return false;
1209 }
1210
1211 if (log)
1212 {
1213 log->Printf("Interpreted a LoadInst");
Matt Kopecef143712013-06-03 18:00:07 +00001214 log->Printf(" P : 0x%" PRIx64, P);
1215 log->Printf(" R : 0x%" PRIx64, R);
1216 log->Printf(" D : 0x%" PRIx64, D);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001217 }
1218 }
1219 break;
1220 case Instruction::Ret:
1221 {
1222 return true;
1223 }
1224 case Instruction::Store:
1225 {
1226 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1227
1228 if (!store_inst)
1229 {
1230 if (log)
1231 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
1232 error.SetErrorToGenericError();
1233 error.SetErrorString(interpreter_internal_error);
1234 return false;
1235 }
1236
1237 // The semantics of Store are:
1238 // Resolve the region D containing the data to be stored
1239 // Resolve the region P containing a pointer
1240 // Dereference P to get the region R that the data should be stored in
1241 // Transfer a unit of type type(D) from D to R
1242
1243 const Value *value_operand = store_inst->getValueOperand();
1244 const Value *pointer_operand = store_inst->getPointerOperand();
1245
1246 Type *pointer_ty = pointer_operand->getType();
1247 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1248 if (!pointer_ptr_ty)
1249 return false;
1250 Type *target_ty = pointer_ptr_ty->getElementType();
1251
1252 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1253 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1254
1255 if (D == LLDB_INVALID_ADDRESS)
1256 {
1257 if (log)
1258 log->Printf("StoreInst's value doesn't resolve to anything");
1259 error.SetErrorToGenericError();
1260 error.SetErrorString(bad_value_error);
1261 return false;
1262 }
1263
1264 if (P == LLDB_INVALID_ADDRESS)
1265 {
1266 if (log)
1267 log->Printf("StoreInst's pointer doesn't resolve to anything");
1268 error.SetErrorToGenericError();
1269 error.SetErrorString(bad_value_error);
1270 return false;
1271 }
1272
1273 lldb::addr_t R;
1274 lldb_private::Error read_error;
1275 memory_map.ReadPointerFromMemory(&R, P, read_error);
1276
1277 if (!read_error.Success())
1278 {
1279 if (log)
1280 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1281 error.SetErrorToGenericError();
1282 error.SetErrorString(memory_read_error);
1283 return false;
1284 }
1285
1286 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1287 lldb_private::DataBufferHeap buffer(target_size, 0);
1288
1289 read_error.Clear();
1290 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
1291 if (!read_error.Success())
1292 {
1293 if (log)
1294 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1295 error.SetErrorToGenericError();
1296 error.SetErrorString(memory_read_error);
1297 return false;
1298 }
1299
1300 lldb_private::Error write_error;
1301 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1302 if (!write_error.Success())
1303 {
1304 if (log)
1305 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1306 error.SetErrorToGenericError();
Sean Callanan49630e72013-04-20 02:39:24 +00001307 error.SetErrorString(memory_write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001308 return false;
1309 }
1310
1311 if (log)
1312 {
1313 log->Printf("Interpreted a StoreInst");
Matt Kopecef143712013-06-03 18:00:07 +00001314 log->Printf(" D : 0x%" PRIx64, D);
1315 log->Printf(" P : 0x%" PRIx64, P);
1316 log->Printf(" R : 0x%" PRIx64, R);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001317 }
1318 }
1319 break;
1320 }
1321
1322 ++frame.m_ii;
1323 }
1324
1325 if (num_insts >= 4096)
1326 {
1327 error.SetErrorToGenericError();
1328 error.SetErrorString(infinite_loop_error);
1329 return false;
1330 }
1331
1332 return false;
1333}