blob: 082e886188d608b371d1fdfea07fbaf39dc5fe38 [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"
Sean Callanan46fc0062013-09-17 18:26:42 +000017#include "lldb/Host/Endian.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000018
Chandler Carruth1e157582013-01-02 12:20:07 +000019#include "llvm/IR/Constants.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000020#include "llvm/IR/DataLayout.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000021#include "llvm/IR/Function.h"
22#include "llvm/IR/Instructions.h"
Sean Callanan576a4372014-03-25 19:33:15 +000023#include "llvm/IR/Intrinsics.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000024#include "llvm/IR/Module.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000025#include "llvm/Support/raw_ostream.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000026
27#include <map>
28
29using namespace llvm;
30
Sean Callanan3bfdaa22011-09-15 02:13:07 +000031static std::string
32PrintValue(const Value *value, bool truncate = false)
33{
34 std::string s;
35 raw_string_ostream rso(s);
36 value->print(rso);
37 rso.flush();
38 if (truncate)
39 s.resize(s.length() - 1);
40
41 size_t offset;
42 while ((offset = s.find('\n')) != s.npos)
43 s.erase(offset, 1);
44 while (s[0] == ' ' || s[0] == '\t')
45 s.erase(0, 1);
46
47 return s;
48}
49
50static std::string
51PrintType(const Type *type, bool truncate = false)
52{
53 std::string s;
54 raw_string_ostream rso(s);
55 type->print(rso);
56 rso.flush();
57 if (truncate)
58 s.resize(s.length() - 1);
59 return s;
60}
61
Sean Callanan576a4372014-03-25 19:33:15 +000062static bool
63CanIgnoreCall (const CallInst *call)
64{
65 const llvm::Function *called_function = call->getCalledFunction();
66
67 if (!called_function)
68 return false;
69
70 if (called_function->isIntrinsic())
71 {
72 switch (called_function->getIntrinsicID())
73 {
74 default:
75 break;
76 case llvm::Intrinsic::dbg_declare:
77 case llvm::Intrinsic::dbg_value:
78 return true;
79 }
80 }
81
82 return false;
83}
84
Sean Callanan3bfdaa22011-09-15 02:13:07 +000085class InterpreterStackFrame
86{
87public:
Sean Callanan08052af2013-04-17 07:50:58 +000088 typedef std::map <const Value*, lldb::addr_t> ValueMap;
89
Sean Callanan3bfdaa22011-09-15 02:13:07 +000090 ValueMap m_values;
Micah Villmow8468dbe2012-10-08 16:28:57 +000091 DataLayout &m_target_data;
Sean Callanan179b5482013-04-16 23:49:09 +000092 lldb_private::IRMemoryMap &m_memory_map;
Sean Callanan3bfdaa22011-09-15 02:13:07 +000093 const BasicBlock *m_bb;
94 BasicBlock::const_iterator m_ii;
95 BasicBlock::const_iterator m_ie;
96
Sean Callanan1582ee62013-04-18 22:06:33 +000097 lldb::addr_t m_frame_process_address;
98 size_t m_frame_size;
99 lldb::addr_t m_stack_pointer;
100
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000101 lldb::ByteOrder m_byte_order;
102 size_t m_addr_byte_size;
103
Micah Villmow8468dbe2012-10-08 16:28:57 +0000104 InterpreterStackFrame (DataLayout &target_data,
Sean Callanandf565402013-04-27 02:19:33 +0000105 lldb_private::IRMemoryMap &memory_map,
106 lldb::addr_t stack_frame_bottom,
107 lldb::addr_t stack_frame_top) :
Daniel Dunbara08823f2011-10-31 22:50:49 +0000108 m_target_data (target_data),
Sean Callanan179b5482013-04-16 23:49:09 +0000109 m_memory_map (memory_map)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000110 {
111 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Sean Callanan95769bf2012-10-11 22:00:52 +0000112 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanandf565402013-04-27 02:19:33 +0000113
114 m_frame_process_address = stack_frame_bottom;
115 m_frame_size = stack_frame_top - stack_frame_bottom;
116 m_stack_pointer = stack_frame_top;
Sean Callanan1582ee62013-04-18 22:06:33 +0000117 }
118
119 ~InterpreterStackFrame ()
120 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000121 }
122
123 void Jump (const BasicBlock *bb)
124 {
125 m_bb = bb;
126 m_ii = m_bb->begin();
127 m_ie = m_bb->end();
128 }
129
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000130 std::string SummarizeValue (const Value *value)
131 {
132 lldb_private::StreamString ss;
133
134 ss.Printf("%s", PrintValue(value).c_str());
135
136 ValueMap::iterator i = m_values.find(value);
137
138 if (i != m_values.end())
139 {
Sean Callanan08052af2013-04-17 07:50:58 +0000140 lldb::addr_t addr = i->second;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000141
Sean Callanan08052af2013-04-17 07:50:58 +0000142 ss.Printf(" 0x%llx", (unsigned long long)addr);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000143 }
144
145 return ss.GetString();
146 }
147
148 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
149 {
150 size_t type_size = m_target_data.getTypeStoreSize(type);
151
152 switch (type_size)
153 {
154 case 1:
155 scalar = (uint8_t)u64value;
156 break;
157 case 2:
158 scalar = (uint16_t)u64value;
159 break;
160 case 4:
161 scalar = (uint32_t)u64value;
162 break;
163 case 8:
164 scalar = (uint64_t)u64value;
165 break;
166 default:
167 return false;
168 }
169
170 return true;
171 }
172
173 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
174 {
175 const Constant *constant = dyn_cast<Constant>(value);
176
177 if (constant)
178 {
Sean Callanan415422c2013-06-05 22:07:06 +0000179 APInt value_apint;
180
181 if (!ResolveConstantValue(value_apint, constant))
182 return false;
183
184 return AssignToMatchType(scalar, value_apint.getLimitedValue(), value->getType());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000185 }
186 else
187 {
Sean Callanan08052af2013-04-17 07:50:58 +0000188 lldb::addr_t process_address = ResolveValue(value, module);
189 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
190
191 lldb_private::DataExtractor value_extractor;
192 lldb_private::Error extract_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000193
Sean Callanan08052af2013-04-17 07:50:58 +0000194 m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error);
195
196 if (!extract_error.Success())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000197 return false;
198
Greg Claytonc7bece562013-01-25 18:06:21 +0000199 lldb::offset_t offset = 0;
Sean Callanan544053e2013-06-06 21:14:35 +0000200 if (value_size == 1 || value_size == 2 || value_size == 4 || value_size == 8)
Greg Clayton78e44bd2013-04-25 00:57:05 +0000201 {
202 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
203 return AssignToMatchType(scalar, u64value, value->getType());
204 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000205 }
206
207 return false;
208 }
209
210 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
211 {
Sean Callanan08052af2013-04-17 07:50:58 +0000212 lldb::addr_t process_address = ResolveValue (value, module);
213
214 if (process_address == LLDB_INVALID_ADDRESS)
215 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000216
217 lldb_private::Scalar cast_scalar;
218
219 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
220 return false;
Sean Callanan08052af2013-04-17 07:50:58 +0000221
222 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000223
Sean Callanan08052af2013-04-17 07:50:58 +0000224 lldb_private::DataBufferHeap buf(value_byte_size, 0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000225
Sean Callanan08052af2013-04-17 07:50:58 +0000226 lldb_private::Error get_data_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000227
Sean Callanan08052af2013-04-17 07:50:58 +0000228 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000229 return false;
230
Sean Callanan08052af2013-04-17 07:50:58 +0000231 lldb_private::Error write_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000232
Sean Callanan08052af2013-04-17 07:50:58 +0000233 m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
Sean Callananc8675502013-02-15 23:07:52 +0000234
Sean Callanan08052af2013-04-17 07:50:58 +0000235 return write_error.Success();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000236 }
237
Sean Callanan94a9a392012-02-08 01:27:49 +0000238 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000239 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000240 switch (constant->getValueID())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000241 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000242 default:
243 break;
244 case Value::ConstantIntVal:
245 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
Sean Callanan80c48c12011-10-21 05:18:02 +0000246 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000247 value = constant_int->getValue();
248 return true;
249 }
250 break;
251 case Value::ConstantFPVal:
252 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
253 {
254 value = constant_fp->getValueAPF().bitcastToAPInt();
255 return true;
256 }
257 break;
258 case Value::ConstantExprVal:
259 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
260 {
261 switch (constant_expr->getOpcode())
Sean Callanan94a9a392012-02-08 01:27:49 +0000262 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000263 default:
Sean Callanan94a9a392012-02-08 01:27:49 +0000264 return false;
Sean Callanan1582ee62013-04-18 22:06:33 +0000265 case Instruction::IntToPtr:
266 case Instruction::PtrToInt:
267 case Instruction::BitCast:
268 return ResolveConstantValue(value, constant_expr->getOperand(0));
269 case Instruction::GetElementPtr:
270 {
271 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
272 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
273
274 Constant *base = dyn_cast<Constant>(*op_cursor);
275
276 if (!base)
277 return false;
278
279 if (!ResolveConstantValue(value, base))
280 return false;
281
282 op_cursor++;
283
284 if (op_cursor == op_end)
285 return true; // no offset to apply!
286
287 SmallVector <Value *, 8> indices (op_cursor, op_end);
288
289 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
290
291 const bool is_signed = true;
292 value += APInt(value.getBitWidth(), offset, is_signed);
293
294 return true;
295 }
Sean Callanan94a9a392012-02-08 01:27:49 +0000296 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000297 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000298 break;
299 case Value::ConstantPointerNullVal:
300 if (isa<ConstantPointerNull>(constant))
301 {
302 value = APInt(m_target_data.getPointerSizeInBits(), 0);
303 return true;
304 }
305 break;
306 }
307 return false;
308 }
309
310 bool MakeArgument(const Argument *value, uint64_t address)
311 {
312 lldb::addr_t data_address = Malloc(value->getType());
313
314 if (data_address == LLDB_INVALID_ADDRESS)
315 return false;
316
317 lldb_private::Error write_error;
318
319 m_memory_map.WritePointerToMemory(data_address, address, write_error);
320
321 if (!write_error.Success())
322 {
323 lldb_private::Error free_error;
324 m_memory_map.Free(data_address, free_error);
325 return false;
Sean Callanan80c48c12011-10-21 05:18:02 +0000326 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000327
Sean Callanan1582ee62013-04-18 22:06:33 +0000328 m_values[value] = data_address;
329
330 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
331
332 if (log)
333 {
334 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str());
335 log->Printf(" Data region : %llx", (unsigned long long)address);
336 log->Printf(" Ref region : %llx", (unsigned long long)data_address);
337 }
338
339 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000340 }
341
Sean Callanan08052af2013-04-17 07:50:58 +0000342 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant)
Sean Callanan94a9a392012-02-08 01:27:49 +0000343 {
344 APInt resolved_value;
345
346 if (!ResolveConstantValue(resolved_value, constant))
347 return false;
348
Sean Callanan46fc0062013-09-17 18:26:42 +0000349 lldb_private::StreamString buffer (lldb_private::Stream::eBinary,
350 m_memory_map.GetAddressByteSize(),
351 m_memory_map.GetByteOrder());
352
Sean Callanan94a9a392012-02-08 01:27:49 +0000353 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
Sean Callanan94a9a392012-02-08 01:27:49 +0000354
Sean Callanan46fc0062013-09-17 18:26:42 +0000355 const uint64_t *raw_data = resolved_value.getRawData();
356
357 buffer.PutRawBytes(raw_data, constant_size, lldb::endian::InlHostByteOrder());
358
Sean Callanan08052af2013-04-17 07:50:58 +0000359 lldb_private::Error write_error;
360
Sean Callanan46fc0062013-09-17 18:26:42 +0000361 m_memory_map.WriteMemory(process_address, (const uint8_t*)buffer.GetData(), constant_size, write_error);
Sean Callanan08052af2013-04-17 07:50:58 +0000362
363 return write_error.Success();
364 }
365
Sean Callanan1582ee62013-04-18 22:06:33 +0000366 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
367 {
368 lldb::addr_t ret = m_stack_pointer;
369
370 ret -= size;
371 ret -= (ret % byte_alignment);
372
373 if (ret < m_frame_process_address)
374 return LLDB_INVALID_ADDRESS;
375
376 m_stack_pointer = ret;
377 return ret;
378 }
379
Sean Callanan08052af2013-04-17 07:50:58 +0000380 lldb::addr_t MallocPointer ()
381 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000382 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
Sean Callanan08052af2013-04-17 07:50:58 +0000383 }
384
Sean Callanan1582ee62013-04-18 22:06:33 +0000385 lldb::addr_t Malloc (llvm::Type *type)
Sean Callanan08052af2013-04-17 07:50:58 +0000386 {
387 lldb_private::Error alloc_error;
388
Sean Callanan1582ee62013-04-18 22:06:33 +0000389 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
Sean Callanan08052af2013-04-17 07:50:58 +0000390 }
391
Sean Callanan08052af2013-04-17 07:50:58 +0000392 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
393 {
394 size_t length = m_target_data.getTypeStoreSize(type);
395
396 lldb_private::DataBufferHeap buf(length, 0);
397
398 lldb_private::Error read_error;
399
400 m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error);
401
402 if (!read_error.Success())
403 return std::string("<couldn't read data>");
404
405 lldb_private::StreamString ss;
406
407 for (size_t i = 0; i < length; i++)
408 {
409 if ((!(i & 0xf)) && i)
410 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
411 else
412 ss.Printf("%02hhx ", buf.GetBytes()[i]);
413 }
414
415 return ss.GetString();
416 }
417
418 lldb::addr_t ResolveValue (const Value *value, Module &module)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000419 {
420 ValueMap::iterator i = m_values.find(value);
421
422 if (i != m_values.end())
423 return i->second;
424
Sean Callanan1582ee62013-04-18 22:06:33 +0000425 // Fall back and allocate space [allocation type Alloca]
426
427 lldb::addr_t data_address = Malloc(value->getType());
428
429 if (const Constant *constant = dyn_cast<Constant>(value))
430 {
431 if (!ResolveConstant (data_address, constant))
432 {
433 lldb_private::Error free_error;
434 m_memory_map.Free(data_address, free_error);
435 return LLDB_INVALID_ADDRESS;
436 }
437 }
438
439 m_values[value] = data_address;
440 return data_address;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000441 }
442};
443
Sean Callanan175a0d02012-01-24 22:06:48 +0000444static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
Sean Callanan3fa3e652013-05-02 00:33:44 +0000445static const char *unsupported_operand_error = "Interpreter doesn't handle one of the expression's operands";
Greg Claytone01e07b2013-04-18 18:10:51 +0000446//static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
Sean Callanan175a0d02012-01-24 22:06:48 +0000447static const char *interpreter_internal_error = "Interpreter encountered an internal error";
448static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
449static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
450static const char *memory_write_error = "Interpreter couldn't write to memory";
451static const char *memory_read_error = "Interpreter couldn't read from memory";
452static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan44342732013-04-19 08:14:32 +0000453//static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callanan175a0d02012-01-24 22:06:48 +0000454
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000455bool
Sean Callanan44342732013-04-19 08:14:32 +0000456IRInterpreter::CanInterpret (llvm::Module &module,
457 llvm::Function &function,
458 lldb_private::Error &error)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000459{
Greg Clayton5160ce52013-03-27 23:08:40 +0000460 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000461
Sean Callanan85fc8762013-06-27 01:59:51 +0000462 bool saw_function_with_body = false;
463
464 for (Module::iterator fi = module.begin(), fe = module.end();
465 fi != fe;
466 ++fi)
467 {
468 if (fi->begin() != fi->end())
469 {
470 if (saw_function_with_body)
471 return false;
472 saw_function_with_body = true;
473 }
474 }
475
Sean Callanan44342732013-04-19 08:14:32 +0000476 for (Function::iterator bbi = function.begin(), bbe = function.end();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000477 bbi != bbe;
478 ++bbi)
479 {
480 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
481 ii != ie;
482 ++ii)
483 {
484 switch (ii->getOpcode())
485 {
486 default:
487 {
488 if (log)
489 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
Sean Callanan44342732013-04-19 08:14:32 +0000490 error.SetErrorToGenericError();
491 error.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000492 return false;
493 }
494 case Instruction::Add:
495 case Instruction::Alloca:
496 case Instruction::BitCast:
497 case Instruction::Br:
Sean Callanan576a4372014-03-25 19:33:15 +0000498 break;
499 case Instruction::Call:
500 {
501 CallInst *call_inst = dyn_cast<CallInst>(ii);
502
503 if (!call_inst)
504 {
505 error.SetErrorToGenericError();
506 error.SetErrorString(interpreter_internal_error);
507 return false;
508 }
509
510 if (!CanIgnoreCall(call_inst))
511 {
512 if (log)
513 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
514 error.SetErrorToGenericError();
515 error.SetErrorString(unsupported_opcode_error);
516 return false;
517 }
518 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000519 case Instruction::GetElementPtr:
520 break;
521 case Instruction::ICmp:
522 {
523 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
524
525 if (!icmp_inst)
Sean Callanan175a0d02012-01-24 22:06:48 +0000526 {
Sean Callanan44342732013-04-19 08:14:32 +0000527 error.SetErrorToGenericError();
528 error.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000529 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +0000530 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000531
532 switch (icmp_inst->getPredicate())
533 {
534 default:
Sean Callanan44342732013-04-19 08:14:32 +0000535 {
536 if (log)
537 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
538
539 error.SetErrorToGenericError();
540 error.SetErrorString(unsupported_opcode_error);
541 return false;
542 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000543 case CmpInst::ICMP_EQ:
544 case CmpInst::ICMP_NE:
545 case CmpInst::ICMP_UGT:
546 case CmpInst::ICMP_UGE:
547 case CmpInst::ICMP_ULT:
548 case CmpInst::ICMP_ULE:
549 case CmpInst::ICMP_SGT:
550 case CmpInst::ICMP_SGE:
551 case CmpInst::ICMP_SLT:
552 case CmpInst::ICMP_SLE:
553 break;
554 }
555 }
556 break;
Sean Callanan087f4372013-01-09 22:44:41 +0000557 case Instruction::And:
558 case Instruction::AShr:
Sean Callanan80c48c12011-10-21 05:18:02 +0000559 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +0000560 case Instruction::PtrToInt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000561 case Instruction::Load:
Sean Callanan087f4372013-01-09 22:44:41 +0000562 case Instruction::LShr:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000563 case Instruction::Mul:
Sean Callanan087f4372013-01-09 22:44:41 +0000564 case Instruction::Or:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000565 case Instruction::Ret:
566 case Instruction::SDiv:
Sean Callanan415422c2013-06-05 22:07:06 +0000567 case Instruction::SExt:
Sean Callanan087f4372013-01-09 22:44:41 +0000568 case Instruction::Shl:
Sean Callananf466a6e2012-12-21 22:27:55 +0000569 case Instruction::SRem:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000570 case Instruction::Store:
571 case Instruction::Sub:
Sean Callanan8c46bac2013-10-11 19:45:00 +0000572 case Instruction::Trunc:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000573 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +0000574 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +0000575 case Instruction::Xor:
Sean Callanan1ef77432012-04-23 17:25:38 +0000576 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000577 break;
578 }
Sean Callanan3fa3e652013-05-02 00:33:44 +0000579
580 for (int oi = 0, oe = ii->getNumOperands();
581 oi != oe;
582 ++oi)
583 {
584 Value *operand = ii->getOperand(oi);
585 Type *operand_type = operand->getType();
586
587 switch (operand_type->getTypeID())
588 {
589 default:
590 break;
591 case Type::VectorTyID:
592 {
593 if (log)
594 log->Printf("Unsupported operand type: %s", PrintType(operand_type).c_str());
595 error.SetErrorString(unsupported_operand_error);
596 return false;
597 }
598 }
599 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000600 }
Sean Callanan3fa3e652013-05-02 00:33:44 +0000601
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000602 }
603
Sean Callanan44342732013-04-19 08:14:32 +0000604 return true;}
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000605
606bool
607IRInterpreter::Interpret (llvm::Module &module,
608 llvm::Function &function,
Sean Callanan1582ee62013-04-18 22:06:33 +0000609 llvm::ArrayRef<lldb::addr_t> args,
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000610 lldb_private::IRMemoryMap &memory_map,
Sean Callanandf565402013-04-27 02:19:33 +0000611 lldb_private::Error &error,
612 lldb::addr_t stack_frame_bottom,
613 lldb::addr_t stack_frame_top)
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000614{
615 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
616
Sean Callanan1582ee62013-04-18 22:06:33 +0000617 if (log)
618 {
619 std::string s;
620 raw_string_ostream oss(s);
621
622 module.print(oss, NULL);
623
624 oss.flush();
625
626 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
627 }
628
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000629 DataLayout data_layout(&module);
630
Sean Callanandf565402013-04-27 02:19:33 +0000631 InterpreterStackFrame frame(data_layout, memory_map, stack_frame_bottom, stack_frame_top);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000632
Sean Callanan1582ee62013-04-18 22:06:33 +0000633 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
634 {
635 error.SetErrorString("Couldn't allocate stack frame");
636 }
637
638 int arg_index = 0;
639
640 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
641 ai != ae;
642 ++ai, ++arg_index)
643 {
644 if (args.size() < arg_index)
645 {
646 error.SetErrorString ("Not enough arguments passed in to function");
647 return false;
648 }
649
650 lldb::addr_t ptr = args[arg_index];
651
652 frame.MakeArgument(ai, ptr);
653 }
654
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000655 uint32_t num_insts = 0;
656
657 frame.Jump(function.begin());
658
659 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
660 {
661 const Instruction *inst = frame.m_ii;
662
663 if (log)
664 log->Printf("Interpreting %s", PrintValue(inst).c_str());
665
666 switch (inst->getOpcode())
667 {
668 default:
669 break;
Sean Callanan576a4372014-03-25 19:33:15 +0000670 case Instruction::Call:
671 {
672 const CallInst *call_inst = dyn_cast<CallInst>(inst);
673
674 if (!call_inst)
675 {
676 if (log)
677 log->Printf("getOpcode() returns %s, but instruction is not a CallInst", inst->getOpcodeName());
678 error.SetErrorToGenericError();
679 error.SetErrorString(interpreter_internal_error);
680 return false;
681 }
682
683 if (!CanIgnoreCall(call_inst))
684 {
685 if (log)
686 log->Printf("The interpreter shouldn't have accepted %s", PrintValue(call_inst).c_str());
687 error.SetErrorToGenericError();
688 error.SetErrorString(interpreter_internal_error);
689 return false;
690 }
691 }
692 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000693 case Instruction::Add:
694 case Instruction::Sub:
695 case Instruction::Mul:
696 case Instruction::SDiv:
697 case Instruction::UDiv:
698 case Instruction::SRem:
699 case Instruction::URem:
700 case Instruction::Shl:
701 case Instruction::LShr:
702 case Instruction::AShr:
703 case Instruction::And:
704 case Instruction::Or:
705 case Instruction::Xor:
706 {
707 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
708
709 if (!bin_op)
710 {
711 if (log)
712 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
713 error.SetErrorToGenericError();
714 error.SetErrorString(interpreter_internal_error);
715 return false;
716 }
717
718 Value *lhs = inst->getOperand(0);
719 Value *rhs = inst->getOperand(1);
720
721 lldb_private::Scalar L;
722 lldb_private::Scalar R;
723
724 if (!frame.EvaluateValue(L, lhs, module))
725 {
726 if (log)
727 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
728 error.SetErrorToGenericError();
729 error.SetErrorString(bad_value_error);
730 return false;
731 }
732
733 if (!frame.EvaluateValue(R, rhs, module))
734 {
735 if (log)
736 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
737 error.SetErrorToGenericError();
738 error.SetErrorString(bad_value_error);
739 return false;
740 }
741
742 lldb_private::Scalar result;
743
744 switch (inst->getOpcode())
745 {
746 default:
747 break;
748 case Instruction::Add:
749 result = L + R;
750 break;
751 case Instruction::Mul:
752 result = L * R;
753 break;
754 case Instruction::Sub:
755 result = L - R;
756 break;
757 case Instruction::SDiv:
Sean Callanan0b342b62013-05-24 20:36:56 +0000758 L.MakeSigned();
759 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000760 result = L / R;
761 break;
762 case Instruction::UDiv:
763 result = L.GetRawBits64(0) / R.GetRawBits64(1);
764 break;
765 case Instruction::SRem:
Sean Callanan0b342b62013-05-24 20:36:56 +0000766 L.MakeSigned();
767 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000768 result = L % R;
769 break;
770 case Instruction::URem:
771 result = L.GetRawBits64(0) % R.GetRawBits64(1);
772 break;
773 case Instruction::Shl:
774 result = L << R;
775 break;
776 case Instruction::AShr:
777 result = L >> R;
778 break;
779 case Instruction::LShr:
780 result = L;
781 result.ShiftRightLogical(R);
782 break;
783 case Instruction::And:
784 result = L & R;
785 break;
786 case Instruction::Or:
787 result = L | R;
788 break;
789 case Instruction::Xor:
790 result = L ^ R;
791 break;
792 }
793
794 frame.AssignValue(inst, result, module);
795
796 if (log)
797 {
798 log->Printf("Interpreted a %s", inst->getOpcodeName());
799 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
800 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
801 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
802 }
803 }
804 break;
805 case Instruction::Alloca:
806 {
807 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
808
809 if (!alloca_inst)
810 {
811 if (log)
812 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
813 error.SetErrorToGenericError();
814 error.SetErrorString(interpreter_internal_error);
815 return false;
816 }
817
818 if (alloca_inst->isArrayAllocation())
819 {
820 if (log)
821 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
822 error.SetErrorToGenericError();
823 error.SetErrorString(unsupported_opcode_error);
824 return false;
825 }
826
827 // The semantics of Alloca are:
828 // Create a region R of virtual memory of type T, backed by a data buffer
829 // Create a region P of virtual memory of type T*, backed by a data buffer
830 // Write the virtual address of R into P
831
832 Type *T = alloca_inst->getAllocatedType();
833 Type *Tptr = alloca_inst->getType();
834
835 lldb::addr_t R = frame.Malloc(T);
836
837 if (R == LLDB_INVALID_ADDRESS)
838 {
839 if (log)
840 log->Printf("Couldn't allocate memory for an AllocaInst");
841 error.SetErrorToGenericError();
842 error.SetErrorString(memory_allocation_error);
843 return false;
844 }
845
846 lldb::addr_t P = frame.Malloc(Tptr);
847
848 if (P == LLDB_INVALID_ADDRESS)
849 {
850 if (log)
851 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
852 error.SetErrorToGenericError();
853 error.SetErrorString(memory_allocation_error);
854 return false;
855 }
856
857 lldb_private::Error write_error;
858
859 memory_map.WritePointerToMemory(P, R, write_error);
860
861 if (!write_error.Success())
862 {
863 if (log)
864 log->Printf("Couldn't write the result pointer for an AllocaInst");
865 error.SetErrorToGenericError();
866 error.SetErrorString(memory_write_error);
867 lldb_private::Error free_error;
868 memory_map.Free(P, free_error);
869 memory_map.Free(R, free_error);
870 return false;
871 }
872
873 frame.m_values[alloca_inst] = P;
874
875 if (log)
876 {
877 log->Printf("Interpreted an AllocaInst");
Matt Kopecef143712013-06-03 18:00:07 +0000878 log->Printf(" R : 0x%" PRIx64, R);
879 log->Printf(" P : 0x%" PRIx64, P);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000880 }
881 }
882 break;
883 case Instruction::BitCast:
884 case Instruction::ZExt:
885 {
886 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
887
888 if (!cast_inst)
889 {
890 if (log)
891 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
892 error.SetErrorToGenericError();
893 error.SetErrorString(interpreter_internal_error);
894 return false;
895 }
896
897 Value *source = cast_inst->getOperand(0);
898
899 lldb_private::Scalar S;
900
901 if (!frame.EvaluateValue(S, source, module))
902 {
903 if (log)
904 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
905 error.SetErrorToGenericError();
906 error.SetErrorString(bad_value_error);
907 return false;
908 }
909
910 frame.AssignValue(inst, S, module);
911 }
912 break;
Sean Callanan415422c2013-06-05 22:07:06 +0000913 case Instruction::SExt:
914 {
915 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
916
917 if (!cast_inst)
918 {
919 if (log)
920 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
921 error.SetErrorToGenericError();
922 error.SetErrorString(interpreter_internal_error);
923 return false;
924 }
925
926 Value *source = cast_inst->getOperand(0);
927
928 lldb_private::Scalar S;
929
930 if (!frame.EvaluateValue(S, source, module))
931 {
932 if (log)
933 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
934 error.SetErrorToGenericError();
935 error.SetErrorString(bad_value_error);
936 return false;
937 }
938
939 S.MakeSigned();
940
941 lldb_private::Scalar S_signextend(S.SLongLong());
942
943 frame.AssignValue(inst, S_signextend, module);
944 }
945 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000946 case Instruction::Br:
947 {
948 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
949
950 if (!br_inst)
951 {
952 if (log)
953 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
954 error.SetErrorToGenericError();
955 error.SetErrorString(interpreter_internal_error);
956 return false;
957 }
958
959 if (br_inst->isConditional())
960 {
961 Value *condition = br_inst->getCondition();
962
963 lldb_private::Scalar C;
964
965 if (!frame.EvaluateValue(C, condition, module))
966 {
967 if (log)
968 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
969 error.SetErrorToGenericError();
970 error.SetErrorString(bad_value_error);
971 return false;
972 }
973
974 if (C.GetRawBits64(0))
975 frame.Jump(br_inst->getSuccessor(0));
976 else
977 frame.Jump(br_inst->getSuccessor(1));
978
979 if (log)
980 {
981 log->Printf("Interpreted a BrInst with a condition");
982 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
983 }
984 }
985 else
986 {
987 frame.Jump(br_inst->getSuccessor(0));
988
989 if (log)
990 {
991 log->Printf("Interpreted a BrInst with no condition");
992 }
993 }
994 }
995 continue;
996 case Instruction::GetElementPtr:
997 {
998 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
999
1000 if (!gep_inst)
1001 {
1002 if (log)
1003 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
1004 error.SetErrorToGenericError();
1005 error.SetErrorString(interpreter_internal_error);
1006 return false;
1007 }
1008
1009 const Value *pointer_operand = gep_inst->getPointerOperand();
1010 Type *pointer_type = pointer_operand->getType();
1011
1012 lldb_private::Scalar P;
1013
1014 if (!frame.EvaluateValue(P, pointer_operand, module))
1015 {
1016 if (log)
1017 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1018 error.SetErrorToGenericError();
1019 error.SetErrorString(bad_value_error);
1020 return false;
1021 }
1022
1023 typedef SmallVector <Value *, 8> IndexVector;
1024 typedef IndexVector::iterator IndexIterator;
1025
1026 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1027 gep_inst->idx_end());
1028
1029 SmallVector <Value *, 8> const_indices;
1030
1031 for (IndexIterator ii = indices.begin(), ie = indices.end();
1032 ii != ie;
1033 ++ii)
1034 {
1035 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1036
1037 if (!constant_index)
1038 {
1039 lldb_private::Scalar I;
1040
1041 if (!frame.EvaluateValue(I, *ii, module))
1042 {
1043 if (log)
1044 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
1045 error.SetErrorToGenericError();
1046 error.SetErrorString(bad_value_error);
1047 return false;
1048 }
1049
1050 if (log)
1051 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
1052
1053 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1054 }
1055
1056 const_indices.push_back(constant_index);
1057 }
1058
1059 uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices);
1060
1061 lldb_private::Scalar Poffset = P + offset;
1062
1063 frame.AssignValue(inst, Poffset, module);
1064
1065 if (log)
1066 {
1067 log->Printf("Interpreted a GetElementPtrInst");
1068 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1069 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1070 }
1071 }
1072 break;
1073 case Instruction::ICmp:
1074 {
1075 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1076
1077 if (!icmp_inst)
1078 {
1079 if (log)
1080 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
1081 error.SetErrorToGenericError();
1082 error.SetErrorString(interpreter_internal_error);
1083 return false;
1084 }
1085
1086 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1087
1088 Value *lhs = inst->getOperand(0);
1089 Value *rhs = inst->getOperand(1);
1090
1091 lldb_private::Scalar L;
1092 lldb_private::Scalar R;
1093
1094 if (!frame.EvaluateValue(L, lhs, module))
1095 {
1096 if (log)
1097 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
1098 error.SetErrorToGenericError();
1099 error.SetErrorString(bad_value_error);
1100 return false;
1101 }
1102
1103 if (!frame.EvaluateValue(R, rhs, module))
1104 {
1105 if (log)
1106 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
1107 error.SetErrorToGenericError();
1108 error.SetErrorString(bad_value_error);
1109 return false;
1110 }
1111
1112 lldb_private::Scalar result;
1113
1114 switch (predicate)
1115 {
1116 default:
1117 return false;
1118 case CmpInst::ICMP_EQ:
1119 result = (L == R);
1120 break;
1121 case CmpInst::ICMP_NE:
1122 result = (L != R);
1123 break;
1124 case CmpInst::ICMP_UGT:
1125 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1126 break;
1127 case CmpInst::ICMP_UGE:
1128 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1129 break;
1130 case CmpInst::ICMP_ULT:
1131 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1132 break;
1133 case CmpInst::ICMP_ULE:
1134 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1135 break;
1136 case CmpInst::ICMP_SGT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001137 L.MakeSigned();
1138 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001139 result = (L > R);
1140 break;
1141 case CmpInst::ICMP_SGE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001142 L.MakeSigned();
1143 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001144 result = (L >= R);
1145 break;
1146 case CmpInst::ICMP_SLT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001147 L.MakeSigned();
1148 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001149 result = (L < R);
1150 break;
1151 case CmpInst::ICMP_SLE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001152 L.MakeSigned();
1153 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001154 result = (L <= R);
1155 break;
1156 }
1157
1158 frame.AssignValue(inst, result, module);
1159
1160 if (log)
1161 {
1162 log->Printf("Interpreted an ICmpInst");
1163 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1164 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1165 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1166 }
1167 }
1168 break;
1169 case Instruction::IntToPtr:
1170 {
1171 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1172
1173 if (!int_to_ptr_inst)
1174 {
1175 if (log)
1176 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
1177 error.SetErrorToGenericError();
1178 error.SetErrorString(interpreter_internal_error);
1179 return false;
1180 }
1181
1182 Value *src_operand = int_to_ptr_inst->getOperand(0);
1183
1184 lldb_private::Scalar I;
1185
1186 if (!frame.EvaluateValue(I, src_operand, module))
1187 {
1188 if (log)
1189 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1190 error.SetErrorToGenericError();
1191 error.SetErrorString(bad_value_error);
1192 return false;
1193 }
1194
1195 frame.AssignValue(inst, I, module);
1196
1197 if (log)
1198 {
1199 log->Printf("Interpreted an IntToPtr");
1200 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1201 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1202 }
1203 }
1204 break;
1205 case Instruction::PtrToInt:
1206 {
1207 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1208
1209 if (!ptr_to_int_inst)
1210 {
1211 if (log)
1212 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1213 error.SetErrorToGenericError();
1214 error.SetErrorString(interpreter_internal_error);
1215 return false;
1216 }
1217
1218 Value *src_operand = ptr_to_int_inst->getOperand(0);
1219
1220 lldb_private::Scalar I;
1221
1222 if (!frame.EvaluateValue(I, src_operand, module))
1223 {
1224 if (log)
1225 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1226 error.SetErrorToGenericError();
1227 error.SetErrorString(bad_value_error);
1228 return false;
1229 }
1230
1231 frame.AssignValue(inst, I, module);
1232
1233 if (log)
1234 {
1235 log->Printf("Interpreted a PtrToInt");
1236 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1237 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1238 }
1239 }
1240 break;
Sean Callanan8c46bac2013-10-11 19:45:00 +00001241 case Instruction::Trunc:
1242 {
1243 const TruncInst *trunc_inst = dyn_cast<TruncInst>(inst);
1244
1245 if (!trunc_inst)
1246 {
1247 if (log)
1248 log->Printf("getOpcode() returns Trunc, but instruction is not a TruncInst");
1249 error.SetErrorToGenericError();
1250 error.SetErrorString(interpreter_internal_error);
1251 return false;
1252 }
1253
1254 Value *src_operand = trunc_inst->getOperand(0);
1255
1256 lldb_private::Scalar I;
1257
1258 if (!frame.EvaluateValue(I, src_operand, module))
1259 {
1260 if (log)
1261 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1262 error.SetErrorToGenericError();
1263 error.SetErrorString(bad_value_error);
1264 return false;
1265 }
1266
1267 frame.AssignValue(inst, I, module);
1268
1269 if (log)
1270 {
1271 log->Printf("Interpreted a Trunc");
1272 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1273 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1274 }
1275 }
1276 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001277 case Instruction::Load:
1278 {
1279 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1280
1281 if (!load_inst)
1282 {
1283 if (log)
1284 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
1285 error.SetErrorToGenericError();
1286 error.SetErrorString(interpreter_internal_error);
1287 return false;
1288 }
1289
1290 // The semantics of Load are:
1291 // Create a region D that will contain the loaded data
1292 // Resolve the region P containing a pointer
1293 // Dereference P to get the region R that the data should be loaded from
1294 // Transfer a unit of type type(D) from R to D
1295
1296 const Value *pointer_operand = load_inst->getPointerOperand();
1297
1298 Type *pointer_ty = pointer_operand->getType();
1299 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1300 if (!pointer_ptr_ty)
1301 {
1302 if (log)
1303 log->Printf("getPointerOperand()->getType() is not a PointerType");
1304 error.SetErrorToGenericError();
1305 error.SetErrorString(interpreter_internal_error);
1306 return false;
1307 }
1308 Type *target_ty = pointer_ptr_ty->getElementType();
1309
1310 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1311 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1312
1313 if (D == LLDB_INVALID_ADDRESS)
1314 {
1315 if (log)
1316 log->Printf("LoadInst's value doesn't resolve to anything");
1317 error.SetErrorToGenericError();
1318 error.SetErrorString(bad_value_error);
1319 return false;
1320 }
1321
1322 if (P == LLDB_INVALID_ADDRESS)
1323 {
1324 if (log)
1325 log->Printf("LoadInst's pointer doesn't resolve to anything");
1326 error.SetErrorToGenericError();
1327 error.SetErrorString(bad_value_error);
1328 return false;
1329 }
1330
1331 lldb::addr_t R;
1332 lldb_private::Error read_error;
1333 memory_map.ReadPointerFromMemory(&R, P, read_error);
1334
1335 if (!read_error.Success())
1336 {
1337 if (log)
1338 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1339 error.SetErrorToGenericError();
1340 error.SetErrorString(memory_read_error);
1341 return false;
1342 }
1343
1344 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1345 lldb_private::DataBufferHeap buffer(target_size, 0);
1346
1347 read_error.Clear();
1348 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
1349 if (!read_error.Success())
1350 {
1351 if (log)
1352 log->Printf("Couldn't read from a region on behalf of a LoadInst");
1353 error.SetErrorToGenericError();
1354 error.SetErrorString(memory_read_error);
1355 return false;
1356 }
1357
1358 lldb_private::Error write_error;
1359 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1360 if (!write_error.Success())
1361 {
1362 if (log)
1363 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1364 error.SetErrorToGenericError();
1365 error.SetErrorString(memory_read_error);
1366 return false;
1367 }
1368
1369 if (log)
1370 {
1371 log->Printf("Interpreted a LoadInst");
Matt Kopecef143712013-06-03 18:00:07 +00001372 log->Printf(" P : 0x%" PRIx64, P);
1373 log->Printf(" R : 0x%" PRIx64, R);
1374 log->Printf(" D : 0x%" PRIx64, D);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001375 }
1376 }
1377 break;
1378 case Instruction::Ret:
1379 {
1380 return true;
1381 }
1382 case Instruction::Store:
1383 {
1384 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1385
1386 if (!store_inst)
1387 {
1388 if (log)
1389 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
1390 error.SetErrorToGenericError();
1391 error.SetErrorString(interpreter_internal_error);
1392 return false;
1393 }
1394
1395 // The semantics of Store are:
1396 // Resolve the region D containing the data to be stored
1397 // Resolve the region P containing a pointer
1398 // Dereference P to get the region R that the data should be stored in
1399 // Transfer a unit of type type(D) from D to R
1400
1401 const Value *value_operand = store_inst->getValueOperand();
1402 const Value *pointer_operand = store_inst->getPointerOperand();
1403
1404 Type *pointer_ty = pointer_operand->getType();
1405 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1406 if (!pointer_ptr_ty)
1407 return false;
1408 Type *target_ty = pointer_ptr_ty->getElementType();
1409
1410 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1411 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1412
1413 if (D == LLDB_INVALID_ADDRESS)
1414 {
1415 if (log)
1416 log->Printf("StoreInst's value doesn't resolve to anything");
1417 error.SetErrorToGenericError();
1418 error.SetErrorString(bad_value_error);
1419 return false;
1420 }
1421
1422 if (P == LLDB_INVALID_ADDRESS)
1423 {
1424 if (log)
1425 log->Printf("StoreInst's pointer doesn't resolve to anything");
1426 error.SetErrorToGenericError();
1427 error.SetErrorString(bad_value_error);
1428 return false;
1429 }
1430
1431 lldb::addr_t R;
1432 lldb_private::Error read_error;
1433 memory_map.ReadPointerFromMemory(&R, P, read_error);
1434
1435 if (!read_error.Success())
1436 {
1437 if (log)
1438 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1439 error.SetErrorToGenericError();
1440 error.SetErrorString(memory_read_error);
1441 return false;
1442 }
1443
1444 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1445 lldb_private::DataBufferHeap buffer(target_size, 0);
1446
1447 read_error.Clear();
1448 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
1449 if (!read_error.Success())
1450 {
1451 if (log)
1452 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1453 error.SetErrorToGenericError();
1454 error.SetErrorString(memory_read_error);
1455 return false;
1456 }
1457
1458 lldb_private::Error write_error;
1459 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1460 if (!write_error.Success())
1461 {
1462 if (log)
1463 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1464 error.SetErrorToGenericError();
Sean Callanan49630e72013-04-20 02:39:24 +00001465 error.SetErrorString(memory_write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001466 return false;
1467 }
1468
1469 if (log)
1470 {
1471 log->Printf("Interpreted a StoreInst");
Matt Kopecef143712013-06-03 18:00:07 +00001472 log->Printf(" D : 0x%" PRIx64, D);
1473 log->Printf(" P : 0x%" PRIx64, P);
1474 log->Printf(" R : 0x%" PRIx64, R);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001475 }
1476 }
1477 break;
1478 }
1479
1480 ++frame.m_ii;
1481 }
1482
1483 if (num_insts >= 4096)
1484 {
1485 error.SetErrorToGenericError();
1486 error.SetErrorString(infinite_loop_error);
1487 return false;
1488 }
1489
1490 return false;
1491}