1 //===-- IRInterpreter.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/DataExtractor.h"
11 #include "lldb/Core/Error.h"
12 #include "lldb/Core/Log.h"
13 #include "lldb/Core/Scalar.h"
14 #include "lldb/Core/StreamString.h"
15 #include "lldb/Expression/IRMemoryMap.h"
16 #include "lldb/Expression/IRInterpreter.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/raw_ostream.h"
30 PrintValue(const Value *value, bool truncate = false)
33 raw_string_ostream rso(s);
37 s.resize(s.length() - 1);
40 while ((offset = s.find('\n')) != s.npos)
42 while (s[0] == ' ' || s[0] == '\t')
49 PrintType(const Type *type, bool truncate = false)
52 raw_string_ostream rso(s);
56 s.resize(s.length() - 1);
60 class InterpreterStackFrame
63 typedef std::map <const Value*, lldb::addr_t> ValueMap;
66 DataLayout &m_target_data;
67 lldb_private::IRMemoryMap &m_memory_map;
68 const BasicBlock *m_bb;
69 BasicBlock::const_iterator m_ii;
70 BasicBlock::const_iterator m_ie;
72 lldb::addr_t m_frame_process_address;
74 lldb::addr_t m_stack_pointer;
76 lldb::ByteOrder m_byte_order;
77 size_t m_addr_byte_size;
79 InterpreterStackFrame (DataLayout &target_data,
80 lldb_private::IRMemoryMap &memory_map,
81 lldb::addr_t stack_frame_bottom,
82 lldb::addr_t stack_frame_top) :
83 m_target_data (target_data),
84 m_memory_map (memory_map)
86 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
87 m_addr_byte_size = (target_data.getPointerSize(0));
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;
94 ~InterpreterStackFrame ()
98 void Jump (const BasicBlock *bb)
101 m_ii = m_bb->begin();
105 std::string SummarizeValue (const Value *value)
107 lldb_private::StreamString ss;
109 ss.Printf("%s", PrintValue(value).c_str());
111 ValueMap::iterator i = m_values.find(value);
113 if (i != m_values.end())
115 lldb::addr_t addr = i->second;
117 ss.Printf(" 0x%llx", (unsigned long long)addr);
120 return ss.GetString();
123 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
125 size_t type_size = m_target_data.getTypeStoreSize(type);
130 scalar = (uint8_t)u64value;
133 scalar = (uint16_t)u64value;
136 scalar = (uint32_t)u64value;
139 scalar = (uint64_t)u64value;
148 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
150 const Constant *constant = dyn_cast<Constant>(value);
156 if (!ResolveConstantValue(value_apint, constant))
159 return AssignToMatchType(scalar, value_apint.getLimitedValue(), value->getType());
163 lldb::addr_t process_address = ResolveValue(value, module);
164 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
166 lldb_private::DataExtractor value_extractor;
167 lldb_private::Error extract_error;
169 m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error);
171 if (!extract_error.Success())
174 lldb::offset_t offset = 0;
175 if (value_size == 1 || value_size == 2 || value_size == 4 || value_size == 8)
177 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
178 return AssignToMatchType(scalar, u64value, value->getType());
185 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
187 lldb::addr_t process_address = ResolveValue (value, module);
189 if (process_address == LLDB_INVALID_ADDRESS)
192 lldb_private::Scalar cast_scalar;
194 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
197 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
199 lldb_private::DataBufferHeap buf(value_byte_size, 0);
201 lldb_private::Error get_data_error;
203 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
206 lldb_private::Error write_error;
208 m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
210 return write_error.Success();
213 bool ResolveConstantValue (APInt &value, const Constant *constant)
215 switch (constant->getValueID())
219 case Value::ConstantIntVal:
220 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
222 value = constant_int->getValue();
226 case Value::ConstantFPVal:
227 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
229 value = constant_fp->getValueAPF().bitcastToAPInt();
233 case Value::ConstantExprVal:
234 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
236 switch (constant_expr->getOpcode())
240 case Instruction::IntToPtr:
241 case Instruction::PtrToInt:
242 case Instruction::BitCast:
243 return ResolveConstantValue(value, constant_expr->getOperand(0));
244 case Instruction::GetElementPtr:
246 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
247 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
249 Constant *base = dyn_cast<Constant>(*op_cursor);
254 if (!ResolveConstantValue(value, base))
259 if (op_cursor == op_end)
260 return true; // no offset to apply!
262 SmallVector <Value *, 8> indices (op_cursor, op_end);
264 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
266 const bool is_signed = true;
267 value += APInt(value.getBitWidth(), offset, is_signed);
274 case Value::ConstantPointerNullVal:
275 if (isa<ConstantPointerNull>(constant))
277 value = APInt(m_target_data.getPointerSizeInBits(), 0);
285 bool MakeArgument(const Argument *value, uint64_t address)
287 lldb::addr_t data_address = Malloc(value->getType());
289 if (data_address == LLDB_INVALID_ADDRESS)
292 lldb_private::Error write_error;
294 m_memory_map.WritePointerToMemory(data_address, address, write_error);
296 if (!write_error.Success())
298 lldb_private::Error free_error;
299 m_memory_map.Free(data_address, free_error);
303 m_values[value] = data_address;
305 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
309 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str());
310 log->Printf(" Data region : %llx", (unsigned long long)address);
311 log->Printf(" Ref region : %llx", (unsigned long long)data_address);
317 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant)
319 APInt resolved_value;
321 if (!ResolveConstantValue(resolved_value, constant))
324 const uint64_t *raw_data = resolved_value.getRawData();
326 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
328 lldb_private::Error write_error;
330 m_memory_map.WriteMemory(process_address, (uint8_t*)raw_data, constant_size, write_error);
332 return write_error.Success();
335 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
337 lldb::addr_t ret = m_stack_pointer;
340 ret -= (ret % byte_alignment);
342 if (ret < m_frame_process_address)
343 return LLDB_INVALID_ADDRESS;
345 m_stack_pointer = ret;
349 lldb::addr_t MallocPointer ()
351 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
354 lldb::addr_t Malloc (llvm::Type *type)
356 lldb_private::Error alloc_error;
358 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
361 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
363 size_t length = m_target_data.getTypeStoreSize(type);
365 lldb_private::DataBufferHeap buf(length, 0);
367 lldb_private::Error read_error;
369 m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error);
371 if (!read_error.Success())
372 return std::string("<couldn't read data>");
374 lldb_private::StreamString ss;
376 for (size_t i = 0; i < length; i++)
378 if ((!(i & 0xf)) && i)
379 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
381 ss.Printf("%02hhx ", buf.GetBytes()[i]);
384 return ss.GetString();
387 lldb::addr_t ResolveValue (const Value *value, Module &module)
389 ValueMap::iterator i = m_values.find(value);
391 if (i != m_values.end())
394 // Fall back and allocate space [allocation type Alloca]
396 lldb::addr_t data_address = Malloc(value->getType());
398 if (const Constant *constant = dyn_cast<Constant>(value))
400 if (!ResolveConstant (data_address, constant))
402 lldb_private::Error free_error;
403 m_memory_map.Free(data_address, free_error);
404 return LLDB_INVALID_ADDRESS;
408 m_values[value] = data_address;
413 static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
414 static const char *unsupported_operand_error = "Interpreter doesn't handle one of the expression's operands";
415 //static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
416 static const char *interpreter_internal_error = "Interpreter encountered an internal error";
417 static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
418 static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
419 static const char *memory_write_error = "Interpreter couldn't write to memory";
420 static const char *memory_read_error = "Interpreter couldn't read from memory";
421 static const char *infinite_loop_error = "Interpreter ran for too many cycles";
422 //static const char *bad_result_error = "Result of expression is in bad memory";
425 IRInterpreter::CanInterpret (llvm::Module &module,
426 llvm::Function &function,
427 lldb_private::Error &error)
429 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
431 bool saw_function_with_body = false;
433 for (Module::iterator fi = module.begin(), fe = module.end();
437 if (fi->begin() != fi->end())
439 if (saw_function_with_body)
441 saw_function_with_body = true;
445 for (Function::iterator bbi = function.begin(), bbe = function.end();
449 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
453 switch (ii->getOpcode())
458 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
459 error.SetErrorToGenericError();
460 error.SetErrorString(unsupported_opcode_error);
463 case Instruction::Add:
464 case Instruction::Alloca:
465 case Instruction::BitCast:
466 case Instruction::Br:
467 case Instruction::GetElementPtr:
469 case Instruction::ICmp:
471 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
475 error.SetErrorToGenericError();
476 error.SetErrorString(interpreter_internal_error);
480 switch (icmp_inst->getPredicate())
485 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
487 error.SetErrorToGenericError();
488 error.SetErrorString(unsupported_opcode_error);
491 case CmpInst::ICMP_EQ:
492 case CmpInst::ICMP_NE:
493 case CmpInst::ICMP_UGT:
494 case CmpInst::ICMP_UGE:
495 case CmpInst::ICMP_ULT:
496 case CmpInst::ICMP_ULE:
497 case CmpInst::ICMP_SGT:
498 case CmpInst::ICMP_SGE:
499 case CmpInst::ICMP_SLT:
500 case CmpInst::ICMP_SLE:
505 case Instruction::And:
506 case Instruction::AShr:
507 case Instruction::IntToPtr:
508 case Instruction::PtrToInt:
509 case Instruction::Load:
510 case Instruction::LShr:
511 case Instruction::Mul:
512 case Instruction::Or:
513 case Instruction::Ret:
514 case Instruction::SDiv:
515 case Instruction::SExt:
516 case Instruction::Shl:
517 case Instruction::SRem:
518 case Instruction::Store:
519 case Instruction::Sub:
520 case Instruction::UDiv:
521 case Instruction::URem:
522 case Instruction::Xor:
523 case Instruction::ZExt:
527 for (int oi = 0, oe = ii->getNumOperands();
531 Value *operand = ii->getOperand(oi);
532 Type *operand_type = operand->getType();
534 switch (operand_type->getTypeID())
538 case Type::VectorTyID:
541 log->Printf("Unsupported operand type: %s", PrintType(operand_type).c_str());
542 error.SetErrorString(unsupported_operand_error);
554 IRInterpreter::Interpret (llvm::Module &module,
555 llvm::Function &function,
556 llvm::ArrayRef<lldb::addr_t> args,
557 lldb_private::IRMemoryMap &memory_map,
558 lldb_private::Error &error,
559 lldb::addr_t stack_frame_bottom,
560 lldb::addr_t stack_frame_top)
562 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
567 raw_string_ostream oss(s);
569 module.print(oss, NULL);
573 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
576 DataLayout data_layout(&module);
578 InterpreterStackFrame frame(data_layout, memory_map, stack_frame_bottom, stack_frame_top);
580 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
582 error.SetErrorString("Couldn't allocate stack frame");
587 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
591 if (args.size() < arg_index)
593 error.SetErrorString ("Not enough arguments passed in to function");
597 lldb::addr_t ptr = args[arg_index];
599 frame.MakeArgument(ai, ptr);
602 uint32_t num_insts = 0;
604 frame.Jump(function.begin());
606 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
608 const Instruction *inst = frame.m_ii;
611 log->Printf("Interpreting %s", PrintValue(inst).c_str());
613 switch (inst->getOpcode())
617 case Instruction::Add:
618 case Instruction::Sub:
619 case Instruction::Mul:
620 case Instruction::SDiv:
621 case Instruction::UDiv:
622 case Instruction::SRem:
623 case Instruction::URem:
624 case Instruction::Shl:
625 case Instruction::LShr:
626 case Instruction::AShr:
627 case Instruction::And:
628 case Instruction::Or:
629 case Instruction::Xor:
631 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
636 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
637 error.SetErrorToGenericError();
638 error.SetErrorString(interpreter_internal_error);
642 Value *lhs = inst->getOperand(0);
643 Value *rhs = inst->getOperand(1);
645 lldb_private::Scalar L;
646 lldb_private::Scalar R;
648 if (!frame.EvaluateValue(L, lhs, module))
651 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
652 error.SetErrorToGenericError();
653 error.SetErrorString(bad_value_error);
657 if (!frame.EvaluateValue(R, rhs, module))
660 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
661 error.SetErrorToGenericError();
662 error.SetErrorString(bad_value_error);
666 lldb_private::Scalar result;
668 switch (inst->getOpcode())
672 case Instruction::Add:
675 case Instruction::Mul:
678 case Instruction::Sub:
681 case Instruction::SDiv:
686 case Instruction::UDiv:
687 result = L.GetRawBits64(0) / R.GetRawBits64(1);
689 case Instruction::SRem:
694 case Instruction::URem:
695 result = L.GetRawBits64(0) % R.GetRawBits64(1);
697 case Instruction::Shl:
700 case Instruction::AShr:
703 case Instruction::LShr:
705 result.ShiftRightLogical(R);
707 case Instruction::And:
710 case Instruction::Or:
713 case Instruction::Xor:
718 frame.AssignValue(inst, result, module);
722 log->Printf("Interpreted a %s", inst->getOpcodeName());
723 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
724 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
725 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
729 case Instruction::Alloca:
731 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
736 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
737 error.SetErrorToGenericError();
738 error.SetErrorString(interpreter_internal_error);
742 if (alloca_inst->isArrayAllocation())
745 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
746 error.SetErrorToGenericError();
747 error.SetErrorString(unsupported_opcode_error);
751 // The semantics of Alloca are:
752 // Create a region R of virtual memory of type T, backed by a data buffer
753 // Create a region P of virtual memory of type T*, backed by a data buffer
754 // Write the virtual address of R into P
756 Type *T = alloca_inst->getAllocatedType();
757 Type *Tptr = alloca_inst->getType();
759 lldb::addr_t R = frame.Malloc(T);
761 if (R == LLDB_INVALID_ADDRESS)
764 log->Printf("Couldn't allocate memory for an AllocaInst");
765 error.SetErrorToGenericError();
766 error.SetErrorString(memory_allocation_error);
770 lldb::addr_t P = frame.Malloc(Tptr);
772 if (P == LLDB_INVALID_ADDRESS)
775 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
776 error.SetErrorToGenericError();
777 error.SetErrorString(memory_allocation_error);
781 lldb_private::Error write_error;
783 memory_map.WritePointerToMemory(P, R, write_error);
785 if (!write_error.Success())
788 log->Printf("Couldn't write the result pointer for an AllocaInst");
789 error.SetErrorToGenericError();
790 error.SetErrorString(memory_write_error);
791 lldb_private::Error free_error;
792 memory_map.Free(P, free_error);
793 memory_map.Free(R, free_error);
797 frame.m_values[alloca_inst] = P;
801 log->Printf("Interpreted an AllocaInst");
802 log->Printf(" R : 0x%" PRIx64, R);
803 log->Printf(" P : 0x%" PRIx64, P);
807 case Instruction::BitCast:
808 case Instruction::ZExt:
810 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
815 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
816 error.SetErrorToGenericError();
817 error.SetErrorString(interpreter_internal_error);
821 Value *source = cast_inst->getOperand(0);
823 lldb_private::Scalar S;
825 if (!frame.EvaluateValue(S, source, module))
828 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
829 error.SetErrorToGenericError();
830 error.SetErrorString(bad_value_error);
834 frame.AssignValue(inst, S, module);
837 case Instruction::SExt:
839 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
844 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
845 error.SetErrorToGenericError();
846 error.SetErrorString(interpreter_internal_error);
850 Value *source = cast_inst->getOperand(0);
852 lldb_private::Scalar S;
854 if (!frame.EvaluateValue(S, source, module))
857 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
858 error.SetErrorToGenericError();
859 error.SetErrorString(bad_value_error);
865 lldb_private::Scalar S_signextend(S.SLongLong());
867 frame.AssignValue(inst, S_signextend, module);
870 case Instruction::Br:
872 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
877 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
878 error.SetErrorToGenericError();
879 error.SetErrorString(interpreter_internal_error);
883 if (br_inst->isConditional())
885 Value *condition = br_inst->getCondition();
887 lldb_private::Scalar C;
889 if (!frame.EvaluateValue(C, condition, module))
892 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
893 error.SetErrorToGenericError();
894 error.SetErrorString(bad_value_error);
898 if (C.GetRawBits64(0))
899 frame.Jump(br_inst->getSuccessor(0));
901 frame.Jump(br_inst->getSuccessor(1));
905 log->Printf("Interpreted a BrInst with a condition");
906 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
911 frame.Jump(br_inst->getSuccessor(0));
915 log->Printf("Interpreted a BrInst with no condition");
920 case Instruction::GetElementPtr:
922 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
927 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
928 error.SetErrorToGenericError();
929 error.SetErrorString(interpreter_internal_error);
933 const Value *pointer_operand = gep_inst->getPointerOperand();
934 Type *pointer_type = pointer_operand->getType();
936 lldb_private::Scalar P;
938 if (!frame.EvaluateValue(P, pointer_operand, module))
941 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
942 error.SetErrorToGenericError();
943 error.SetErrorString(bad_value_error);
947 typedef SmallVector <Value *, 8> IndexVector;
948 typedef IndexVector::iterator IndexIterator;
950 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
951 gep_inst->idx_end());
953 SmallVector <Value *, 8> const_indices;
955 for (IndexIterator ii = indices.begin(), ie = indices.end();
959 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
963 lldb_private::Scalar I;
965 if (!frame.EvaluateValue(I, *ii, module))
968 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
969 error.SetErrorToGenericError();
970 error.SetErrorString(bad_value_error);
975 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
977 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
980 const_indices.push_back(constant_index);
983 uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices);
985 lldb_private::Scalar Poffset = P + offset;
987 frame.AssignValue(inst, Poffset, module);
991 log->Printf("Interpreted a GetElementPtrInst");
992 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
993 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
997 case Instruction::ICmp:
999 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1004 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
1005 error.SetErrorToGenericError();
1006 error.SetErrorString(interpreter_internal_error);
1010 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1012 Value *lhs = inst->getOperand(0);
1013 Value *rhs = inst->getOperand(1);
1015 lldb_private::Scalar L;
1016 lldb_private::Scalar R;
1018 if (!frame.EvaluateValue(L, lhs, module))
1021 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
1022 error.SetErrorToGenericError();
1023 error.SetErrorString(bad_value_error);
1027 if (!frame.EvaluateValue(R, rhs, module))
1030 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
1031 error.SetErrorToGenericError();
1032 error.SetErrorString(bad_value_error);
1036 lldb_private::Scalar result;
1042 case CmpInst::ICMP_EQ:
1045 case CmpInst::ICMP_NE:
1048 case CmpInst::ICMP_UGT:
1049 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1051 case CmpInst::ICMP_UGE:
1052 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1054 case CmpInst::ICMP_ULT:
1055 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1057 case CmpInst::ICMP_ULE:
1058 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1060 case CmpInst::ICMP_SGT:
1065 case CmpInst::ICMP_SGE:
1070 case CmpInst::ICMP_SLT:
1075 case CmpInst::ICMP_SLE:
1082 frame.AssignValue(inst, result, module);
1086 log->Printf("Interpreted an ICmpInst");
1087 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1088 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1089 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1093 case Instruction::IntToPtr:
1095 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1097 if (!int_to_ptr_inst)
1100 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
1101 error.SetErrorToGenericError();
1102 error.SetErrorString(interpreter_internal_error);
1106 Value *src_operand = int_to_ptr_inst->getOperand(0);
1108 lldb_private::Scalar I;
1110 if (!frame.EvaluateValue(I, src_operand, module))
1113 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1114 error.SetErrorToGenericError();
1115 error.SetErrorString(bad_value_error);
1119 frame.AssignValue(inst, I, module);
1123 log->Printf("Interpreted an IntToPtr");
1124 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1125 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1129 case Instruction::PtrToInt:
1131 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1133 if (!ptr_to_int_inst)
1136 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1137 error.SetErrorToGenericError();
1138 error.SetErrorString(interpreter_internal_error);
1142 Value *src_operand = ptr_to_int_inst->getOperand(0);
1144 lldb_private::Scalar I;
1146 if (!frame.EvaluateValue(I, src_operand, module))
1149 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1150 error.SetErrorToGenericError();
1151 error.SetErrorString(bad_value_error);
1155 frame.AssignValue(inst, I, module);
1159 log->Printf("Interpreted a PtrToInt");
1160 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1161 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1165 case Instruction::Load:
1167 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1172 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
1173 error.SetErrorToGenericError();
1174 error.SetErrorString(interpreter_internal_error);
1178 // The semantics of Load are:
1179 // Create a region D that will contain the loaded data
1180 // Resolve the region P containing a pointer
1181 // Dereference P to get the region R that the data should be loaded from
1182 // Transfer a unit of type type(D) from R to D
1184 const Value *pointer_operand = load_inst->getPointerOperand();
1186 Type *pointer_ty = pointer_operand->getType();
1187 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1188 if (!pointer_ptr_ty)
1191 log->Printf("getPointerOperand()->getType() is not a PointerType");
1192 error.SetErrorToGenericError();
1193 error.SetErrorString(interpreter_internal_error);
1196 Type *target_ty = pointer_ptr_ty->getElementType();
1198 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1199 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1201 if (D == LLDB_INVALID_ADDRESS)
1204 log->Printf("LoadInst's value doesn't resolve to anything");
1205 error.SetErrorToGenericError();
1206 error.SetErrorString(bad_value_error);
1210 if (P == LLDB_INVALID_ADDRESS)
1213 log->Printf("LoadInst's pointer doesn't resolve to anything");
1214 error.SetErrorToGenericError();
1215 error.SetErrorString(bad_value_error);
1220 lldb_private::Error read_error;
1221 memory_map.ReadPointerFromMemory(&R, P, read_error);
1223 if (!read_error.Success())
1226 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1227 error.SetErrorToGenericError();
1228 error.SetErrorString(memory_read_error);
1232 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1233 lldb_private::DataBufferHeap buffer(target_size, 0);
1236 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
1237 if (!read_error.Success())
1240 log->Printf("Couldn't read from a region on behalf of a LoadInst");
1241 error.SetErrorToGenericError();
1242 error.SetErrorString(memory_read_error);
1246 lldb_private::Error write_error;
1247 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1248 if (!write_error.Success())
1251 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1252 error.SetErrorToGenericError();
1253 error.SetErrorString(memory_read_error);
1259 log->Printf("Interpreted a LoadInst");
1260 log->Printf(" P : 0x%" PRIx64, P);
1261 log->Printf(" R : 0x%" PRIx64, R);
1262 log->Printf(" D : 0x%" PRIx64, D);
1266 case Instruction::Ret:
1270 case Instruction::Store:
1272 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1277 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
1278 error.SetErrorToGenericError();
1279 error.SetErrorString(interpreter_internal_error);
1283 // The semantics of Store are:
1284 // Resolve the region D containing the data to be stored
1285 // Resolve the region P containing a pointer
1286 // Dereference P to get the region R that the data should be stored in
1287 // Transfer a unit of type type(D) from D to R
1289 const Value *value_operand = store_inst->getValueOperand();
1290 const Value *pointer_operand = store_inst->getPointerOperand();
1292 Type *pointer_ty = pointer_operand->getType();
1293 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1294 if (!pointer_ptr_ty)
1296 Type *target_ty = pointer_ptr_ty->getElementType();
1298 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1299 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1301 if (D == LLDB_INVALID_ADDRESS)
1304 log->Printf("StoreInst's value doesn't resolve to anything");
1305 error.SetErrorToGenericError();
1306 error.SetErrorString(bad_value_error);
1310 if (P == LLDB_INVALID_ADDRESS)
1313 log->Printf("StoreInst's pointer doesn't resolve to anything");
1314 error.SetErrorToGenericError();
1315 error.SetErrorString(bad_value_error);
1320 lldb_private::Error read_error;
1321 memory_map.ReadPointerFromMemory(&R, P, read_error);
1323 if (!read_error.Success())
1326 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1327 error.SetErrorToGenericError();
1328 error.SetErrorString(memory_read_error);
1332 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1333 lldb_private::DataBufferHeap buffer(target_size, 0);
1336 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
1337 if (!read_error.Success())
1340 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1341 error.SetErrorToGenericError();
1342 error.SetErrorString(memory_read_error);
1346 lldb_private::Error write_error;
1347 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1348 if (!write_error.Success())
1351 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1352 error.SetErrorToGenericError();
1353 error.SetErrorString(memory_write_error);
1359 log->Printf("Interpreted a StoreInst");
1360 log->Printf(" D : 0x%" PRIx64, D);
1361 log->Printf(" P : 0x%" PRIx64, P);
1362 log->Printf(" R : 0x%" PRIx64, R);
1371 if (num_insts >= 4096)
1373 error.SetErrorToGenericError();
1374 error.SetErrorString(infinite_loop_error);