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"
17 #include "lldb/Host/Endian.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Support/raw_ostream.h"
31 PrintValue(const Value *value, bool truncate = false)
34 raw_string_ostream rso(s);
38 s.resize(s.length() - 1);
41 while ((offset = s.find('\n')) != s.npos)
43 while (s[0] == ' ' || s[0] == '\t')
50 PrintType(const Type *type, bool truncate = false)
53 raw_string_ostream rso(s);
57 s.resize(s.length() - 1);
61 class InterpreterStackFrame
64 typedef std::map <const Value*, lldb::addr_t> ValueMap;
67 DataLayout &m_target_data;
68 lldb_private::IRMemoryMap &m_memory_map;
69 const BasicBlock *m_bb;
70 BasicBlock::const_iterator m_ii;
71 BasicBlock::const_iterator m_ie;
73 lldb::addr_t m_frame_process_address;
75 lldb::addr_t m_stack_pointer;
77 lldb::ByteOrder m_byte_order;
78 size_t m_addr_byte_size;
80 InterpreterStackFrame (DataLayout &target_data,
81 lldb_private::IRMemoryMap &memory_map,
82 lldb::addr_t stack_frame_bottom,
83 lldb::addr_t stack_frame_top) :
84 m_target_data (target_data),
85 m_memory_map (memory_map)
87 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
88 m_addr_byte_size = (target_data.getPointerSize(0));
90 m_frame_process_address = stack_frame_bottom;
91 m_frame_size = stack_frame_top - stack_frame_bottom;
92 m_stack_pointer = stack_frame_top;
95 ~InterpreterStackFrame ()
99 void Jump (const BasicBlock *bb)
102 m_ii = m_bb->begin();
106 std::string SummarizeValue (const Value *value)
108 lldb_private::StreamString ss;
110 ss.Printf("%s", PrintValue(value).c_str());
112 ValueMap::iterator i = m_values.find(value);
114 if (i != m_values.end())
116 lldb::addr_t addr = i->second;
118 ss.Printf(" 0x%llx", (unsigned long long)addr);
121 return ss.GetString();
124 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
126 size_t type_size = m_target_data.getTypeStoreSize(type);
131 scalar = (uint8_t)u64value;
134 scalar = (uint16_t)u64value;
137 scalar = (uint32_t)u64value;
140 scalar = (uint64_t)u64value;
149 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
151 const Constant *constant = dyn_cast<Constant>(value);
157 if (!ResolveConstantValue(value_apint, constant))
160 return AssignToMatchType(scalar, value_apint.getLimitedValue(), value->getType());
164 lldb::addr_t process_address = ResolveValue(value, module);
165 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
167 lldb_private::DataExtractor value_extractor;
168 lldb_private::Error extract_error;
170 m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error);
172 if (!extract_error.Success())
175 lldb::offset_t offset = 0;
176 if (value_size == 1 || value_size == 2 || value_size == 4 || value_size == 8)
178 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
179 return AssignToMatchType(scalar, u64value, value->getType());
186 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
188 lldb::addr_t process_address = ResolveValue (value, module);
190 if (process_address == LLDB_INVALID_ADDRESS)
193 lldb_private::Scalar cast_scalar;
195 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
198 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
200 lldb_private::DataBufferHeap buf(value_byte_size, 0);
202 lldb_private::Error get_data_error;
204 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
207 lldb_private::Error write_error;
209 m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
211 return write_error.Success();
214 bool ResolveConstantValue (APInt &value, const Constant *constant)
216 switch (constant->getValueID())
220 case Value::ConstantIntVal:
221 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
223 value = constant_int->getValue();
227 case Value::ConstantFPVal:
228 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
230 value = constant_fp->getValueAPF().bitcastToAPInt();
234 case Value::ConstantExprVal:
235 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
237 switch (constant_expr->getOpcode())
241 case Instruction::IntToPtr:
242 case Instruction::PtrToInt:
243 case Instruction::BitCast:
244 return ResolveConstantValue(value, constant_expr->getOperand(0));
245 case Instruction::GetElementPtr:
247 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
248 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
250 Constant *base = dyn_cast<Constant>(*op_cursor);
255 if (!ResolveConstantValue(value, base))
260 if (op_cursor == op_end)
261 return true; // no offset to apply!
263 SmallVector <Value *, 8> indices (op_cursor, op_end);
265 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
267 const bool is_signed = true;
268 value += APInt(value.getBitWidth(), offset, is_signed);
275 case Value::ConstantPointerNullVal:
276 if (isa<ConstantPointerNull>(constant))
278 value = APInt(m_target_data.getPointerSizeInBits(), 0);
286 bool MakeArgument(const Argument *value, uint64_t address)
288 lldb::addr_t data_address = Malloc(value->getType());
290 if (data_address == LLDB_INVALID_ADDRESS)
293 lldb_private::Error write_error;
295 m_memory_map.WritePointerToMemory(data_address, address, write_error);
297 if (!write_error.Success())
299 lldb_private::Error free_error;
300 m_memory_map.Free(data_address, free_error);
304 m_values[value] = data_address;
306 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
310 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str());
311 log->Printf(" Data region : %llx", (unsigned long long)address);
312 log->Printf(" Ref region : %llx", (unsigned long long)data_address);
318 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant)
320 APInt resolved_value;
322 if (!ResolveConstantValue(resolved_value, constant))
325 lldb_private::StreamString buffer (lldb_private::Stream::eBinary,
326 m_memory_map.GetAddressByteSize(),
327 m_memory_map.GetByteOrder());
329 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
331 const uint64_t *raw_data = resolved_value.getRawData();
333 buffer.PutRawBytes(raw_data, constant_size, lldb::endian::InlHostByteOrder());
335 lldb_private::Error write_error;
337 m_memory_map.WriteMemory(process_address, (const uint8_t*)buffer.GetData(), constant_size, write_error);
339 return write_error.Success();
342 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
344 lldb::addr_t ret = m_stack_pointer;
347 ret -= (ret % byte_alignment);
349 if (ret < m_frame_process_address)
350 return LLDB_INVALID_ADDRESS;
352 m_stack_pointer = ret;
356 lldb::addr_t MallocPointer ()
358 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
361 lldb::addr_t Malloc (llvm::Type *type)
363 lldb_private::Error alloc_error;
365 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
368 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
370 size_t length = m_target_data.getTypeStoreSize(type);
372 lldb_private::DataBufferHeap buf(length, 0);
374 lldb_private::Error read_error;
376 m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error);
378 if (!read_error.Success())
379 return std::string("<couldn't read data>");
381 lldb_private::StreamString ss;
383 for (size_t i = 0; i < length; i++)
385 if ((!(i & 0xf)) && i)
386 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
388 ss.Printf("%02hhx ", buf.GetBytes()[i]);
391 return ss.GetString();
394 lldb::addr_t ResolveValue (const Value *value, Module &module)
396 ValueMap::iterator i = m_values.find(value);
398 if (i != m_values.end())
401 // Fall back and allocate space [allocation type Alloca]
403 lldb::addr_t data_address = Malloc(value->getType());
405 if (const Constant *constant = dyn_cast<Constant>(value))
407 if (!ResolveConstant (data_address, constant))
409 lldb_private::Error free_error;
410 m_memory_map.Free(data_address, free_error);
411 return LLDB_INVALID_ADDRESS;
415 m_values[value] = data_address;
420 static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
421 static const char *unsupported_operand_error = "Interpreter doesn't handle one of the expression's operands";
422 //static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
423 static const char *interpreter_internal_error = "Interpreter encountered an internal error";
424 static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
425 static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
426 static const char *memory_write_error = "Interpreter couldn't write to memory";
427 static const char *memory_read_error = "Interpreter couldn't read from memory";
428 static const char *infinite_loop_error = "Interpreter ran for too many cycles";
429 //static const char *bad_result_error = "Result of expression is in bad memory";
432 IRInterpreter::CanInterpret (llvm::Module &module,
433 llvm::Function &function,
434 lldb_private::Error &error)
436 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
438 bool saw_function_with_body = false;
440 for (Module::iterator fi = module.begin(), fe = module.end();
444 if (fi->begin() != fi->end())
446 if (saw_function_with_body)
448 saw_function_with_body = true;
452 for (Function::iterator bbi = function.begin(), bbe = function.end();
456 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
460 switch (ii->getOpcode())
465 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
466 error.SetErrorToGenericError();
467 error.SetErrorString(unsupported_opcode_error);
470 case Instruction::Add:
471 case Instruction::Alloca:
472 case Instruction::BitCast:
473 case Instruction::Br:
474 case Instruction::GetElementPtr:
476 case Instruction::ICmp:
478 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
482 error.SetErrorToGenericError();
483 error.SetErrorString(interpreter_internal_error);
487 switch (icmp_inst->getPredicate())
492 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
494 error.SetErrorToGenericError();
495 error.SetErrorString(unsupported_opcode_error);
498 case CmpInst::ICMP_EQ:
499 case CmpInst::ICMP_NE:
500 case CmpInst::ICMP_UGT:
501 case CmpInst::ICMP_UGE:
502 case CmpInst::ICMP_ULT:
503 case CmpInst::ICMP_ULE:
504 case CmpInst::ICMP_SGT:
505 case CmpInst::ICMP_SGE:
506 case CmpInst::ICMP_SLT:
507 case CmpInst::ICMP_SLE:
512 case Instruction::And:
513 case Instruction::AShr:
514 case Instruction::IntToPtr:
515 case Instruction::PtrToInt:
516 case Instruction::Load:
517 case Instruction::LShr:
518 case Instruction::Mul:
519 case Instruction::Or:
520 case Instruction::Ret:
521 case Instruction::SDiv:
522 case Instruction::SExt:
523 case Instruction::Shl:
524 case Instruction::SRem:
525 case Instruction::Store:
526 case Instruction::Sub:
527 case Instruction::Trunc:
528 case Instruction::UDiv:
529 case Instruction::URem:
530 case Instruction::Xor:
531 case Instruction::ZExt:
535 for (int oi = 0, oe = ii->getNumOperands();
539 Value *operand = ii->getOperand(oi);
540 Type *operand_type = operand->getType();
542 switch (operand_type->getTypeID())
546 case Type::VectorTyID:
549 log->Printf("Unsupported operand type: %s", PrintType(operand_type).c_str());
550 error.SetErrorString(unsupported_operand_error);
562 IRInterpreter::Interpret (llvm::Module &module,
563 llvm::Function &function,
564 llvm::ArrayRef<lldb::addr_t> args,
565 lldb_private::IRMemoryMap &memory_map,
566 lldb_private::Error &error,
567 lldb::addr_t stack_frame_bottom,
568 lldb::addr_t stack_frame_top)
570 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
575 raw_string_ostream oss(s);
577 module.print(oss, NULL);
581 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
584 DataLayout data_layout(&module);
586 InterpreterStackFrame frame(data_layout, memory_map, stack_frame_bottom, stack_frame_top);
588 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
590 error.SetErrorString("Couldn't allocate stack frame");
595 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
599 if (args.size() < arg_index)
601 error.SetErrorString ("Not enough arguments passed in to function");
605 lldb::addr_t ptr = args[arg_index];
607 frame.MakeArgument(ai, ptr);
610 uint32_t num_insts = 0;
612 frame.Jump(function.begin());
614 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
616 const Instruction *inst = frame.m_ii;
619 log->Printf("Interpreting %s", PrintValue(inst).c_str());
621 switch (inst->getOpcode())
625 case Instruction::Add:
626 case Instruction::Sub:
627 case Instruction::Mul:
628 case Instruction::SDiv:
629 case Instruction::UDiv:
630 case Instruction::SRem:
631 case Instruction::URem:
632 case Instruction::Shl:
633 case Instruction::LShr:
634 case Instruction::AShr:
635 case Instruction::And:
636 case Instruction::Or:
637 case Instruction::Xor:
639 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
644 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
645 error.SetErrorToGenericError();
646 error.SetErrorString(interpreter_internal_error);
650 Value *lhs = inst->getOperand(0);
651 Value *rhs = inst->getOperand(1);
653 lldb_private::Scalar L;
654 lldb_private::Scalar R;
656 if (!frame.EvaluateValue(L, lhs, module))
659 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
660 error.SetErrorToGenericError();
661 error.SetErrorString(bad_value_error);
665 if (!frame.EvaluateValue(R, rhs, module))
668 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
669 error.SetErrorToGenericError();
670 error.SetErrorString(bad_value_error);
674 lldb_private::Scalar result;
676 switch (inst->getOpcode())
680 case Instruction::Add:
683 case Instruction::Mul:
686 case Instruction::Sub:
689 case Instruction::SDiv:
694 case Instruction::UDiv:
695 result = L.GetRawBits64(0) / R.GetRawBits64(1);
697 case Instruction::SRem:
702 case Instruction::URem:
703 result = L.GetRawBits64(0) % R.GetRawBits64(1);
705 case Instruction::Shl:
708 case Instruction::AShr:
711 case Instruction::LShr:
713 result.ShiftRightLogical(R);
715 case Instruction::And:
718 case Instruction::Or:
721 case Instruction::Xor:
726 frame.AssignValue(inst, result, module);
730 log->Printf("Interpreted a %s", inst->getOpcodeName());
731 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
732 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
733 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
737 case Instruction::Alloca:
739 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
744 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
745 error.SetErrorToGenericError();
746 error.SetErrorString(interpreter_internal_error);
750 if (alloca_inst->isArrayAllocation())
753 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
754 error.SetErrorToGenericError();
755 error.SetErrorString(unsupported_opcode_error);
759 // The semantics of Alloca are:
760 // Create a region R of virtual memory of type T, backed by a data buffer
761 // Create a region P of virtual memory of type T*, backed by a data buffer
762 // Write the virtual address of R into P
764 Type *T = alloca_inst->getAllocatedType();
765 Type *Tptr = alloca_inst->getType();
767 lldb::addr_t R = frame.Malloc(T);
769 if (R == LLDB_INVALID_ADDRESS)
772 log->Printf("Couldn't allocate memory for an AllocaInst");
773 error.SetErrorToGenericError();
774 error.SetErrorString(memory_allocation_error);
778 lldb::addr_t P = frame.Malloc(Tptr);
780 if (P == LLDB_INVALID_ADDRESS)
783 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
784 error.SetErrorToGenericError();
785 error.SetErrorString(memory_allocation_error);
789 lldb_private::Error write_error;
791 memory_map.WritePointerToMemory(P, R, write_error);
793 if (!write_error.Success())
796 log->Printf("Couldn't write the result pointer for an AllocaInst");
797 error.SetErrorToGenericError();
798 error.SetErrorString(memory_write_error);
799 lldb_private::Error free_error;
800 memory_map.Free(P, free_error);
801 memory_map.Free(R, free_error);
805 frame.m_values[alloca_inst] = P;
809 log->Printf("Interpreted an AllocaInst");
810 log->Printf(" R : 0x%" PRIx64, R);
811 log->Printf(" P : 0x%" PRIx64, P);
815 case Instruction::BitCast:
816 case Instruction::ZExt:
818 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
823 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
824 error.SetErrorToGenericError();
825 error.SetErrorString(interpreter_internal_error);
829 Value *source = cast_inst->getOperand(0);
831 lldb_private::Scalar S;
833 if (!frame.EvaluateValue(S, source, module))
836 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
837 error.SetErrorToGenericError();
838 error.SetErrorString(bad_value_error);
842 frame.AssignValue(inst, S, module);
845 case Instruction::SExt:
847 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
852 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
853 error.SetErrorToGenericError();
854 error.SetErrorString(interpreter_internal_error);
858 Value *source = cast_inst->getOperand(0);
860 lldb_private::Scalar S;
862 if (!frame.EvaluateValue(S, source, module))
865 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
866 error.SetErrorToGenericError();
867 error.SetErrorString(bad_value_error);
873 lldb_private::Scalar S_signextend(S.SLongLong());
875 frame.AssignValue(inst, S_signextend, module);
878 case Instruction::Br:
880 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
885 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
886 error.SetErrorToGenericError();
887 error.SetErrorString(interpreter_internal_error);
891 if (br_inst->isConditional())
893 Value *condition = br_inst->getCondition();
895 lldb_private::Scalar C;
897 if (!frame.EvaluateValue(C, condition, module))
900 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
901 error.SetErrorToGenericError();
902 error.SetErrorString(bad_value_error);
906 if (C.GetRawBits64(0))
907 frame.Jump(br_inst->getSuccessor(0));
909 frame.Jump(br_inst->getSuccessor(1));
913 log->Printf("Interpreted a BrInst with a condition");
914 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
919 frame.Jump(br_inst->getSuccessor(0));
923 log->Printf("Interpreted a BrInst with no condition");
928 case Instruction::GetElementPtr:
930 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
935 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
936 error.SetErrorToGenericError();
937 error.SetErrorString(interpreter_internal_error);
941 const Value *pointer_operand = gep_inst->getPointerOperand();
942 Type *pointer_type = pointer_operand->getType();
944 lldb_private::Scalar P;
946 if (!frame.EvaluateValue(P, pointer_operand, module))
949 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
950 error.SetErrorToGenericError();
951 error.SetErrorString(bad_value_error);
955 typedef SmallVector <Value *, 8> IndexVector;
956 typedef IndexVector::iterator IndexIterator;
958 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
959 gep_inst->idx_end());
961 SmallVector <Value *, 8> const_indices;
963 for (IndexIterator ii = indices.begin(), ie = indices.end();
967 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
971 lldb_private::Scalar I;
973 if (!frame.EvaluateValue(I, *ii, module))
976 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
977 error.SetErrorToGenericError();
978 error.SetErrorString(bad_value_error);
983 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
985 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
988 const_indices.push_back(constant_index);
991 uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices);
993 lldb_private::Scalar Poffset = P + offset;
995 frame.AssignValue(inst, Poffset, module);
999 log->Printf("Interpreted a GetElementPtrInst");
1000 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1001 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1005 case Instruction::ICmp:
1007 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1012 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
1013 error.SetErrorToGenericError();
1014 error.SetErrorString(interpreter_internal_error);
1018 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1020 Value *lhs = inst->getOperand(0);
1021 Value *rhs = inst->getOperand(1);
1023 lldb_private::Scalar L;
1024 lldb_private::Scalar R;
1026 if (!frame.EvaluateValue(L, lhs, module))
1029 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
1030 error.SetErrorToGenericError();
1031 error.SetErrorString(bad_value_error);
1035 if (!frame.EvaluateValue(R, rhs, module))
1038 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
1039 error.SetErrorToGenericError();
1040 error.SetErrorString(bad_value_error);
1044 lldb_private::Scalar result;
1050 case CmpInst::ICMP_EQ:
1053 case CmpInst::ICMP_NE:
1056 case CmpInst::ICMP_UGT:
1057 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1059 case CmpInst::ICMP_UGE:
1060 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1062 case CmpInst::ICMP_ULT:
1063 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1065 case CmpInst::ICMP_ULE:
1066 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1068 case CmpInst::ICMP_SGT:
1073 case CmpInst::ICMP_SGE:
1078 case CmpInst::ICMP_SLT:
1083 case CmpInst::ICMP_SLE:
1090 frame.AssignValue(inst, result, module);
1094 log->Printf("Interpreted an ICmpInst");
1095 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1096 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1097 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1101 case Instruction::IntToPtr:
1103 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1105 if (!int_to_ptr_inst)
1108 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
1109 error.SetErrorToGenericError();
1110 error.SetErrorString(interpreter_internal_error);
1114 Value *src_operand = int_to_ptr_inst->getOperand(0);
1116 lldb_private::Scalar I;
1118 if (!frame.EvaluateValue(I, src_operand, module))
1121 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1122 error.SetErrorToGenericError();
1123 error.SetErrorString(bad_value_error);
1127 frame.AssignValue(inst, I, module);
1131 log->Printf("Interpreted an IntToPtr");
1132 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1133 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1137 case Instruction::PtrToInt:
1139 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1141 if (!ptr_to_int_inst)
1144 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1145 error.SetErrorToGenericError();
1146 error.SetErrorString(interpreter_internal_error);
1150 Value *src_operand = ptr_to_int_inst->getOperand(0);
1152 lldb_private::Scalar I;
1154 if (!frame.EvaluateValue(I, src_operand, module))
1157 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1158 error.SetErrorToGenericError();
1159 error.SetErrorString(bad_value_error);
1163 frame.AssignValue(inst, I, module);
1167 log->Printf("Interpreted a PtrToInt");
1168 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1169 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1173 case Instruction::Trunc:
1175 const TruncInst *trunc_inst = dyn_cast<TruncInst>(inst);
1180 log->Printf("getOpcode() returns Trunc, but instruction is not a TruncInst");
1181 error.SetErrorToGenericError();
1182 error.SetErrorString(interpreter_internal_error);
1186 Value *src_operand = trunc_inst->getOperand(0);
1188 lldb_private::Scalar I;
1190 if (!frame.EvaluateValue(I, src_operand, module))
1193 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1194 error.SetErrorToGenericError();
1195 error.SetErrorString(bad_value_error);
1199 frame.AssignValue(inst, I, module);
1203 log->Printf("Interpreted a Trunc");
1204 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1205 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1209 case Instruction::Load:
1211 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1216 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
1217 error.SetErrorToGenericError();
1218 error.SetErrorString(interpreter_internal_error);
1222 // The semantics of Load are:
1223 // Create a region D that will contain the loaded data
1224 // Resolve the region P containing a pointer
1225 // Dereference P to get the region R that the data should be loaded from
1226 // Transfer a unit of type type(D) from R to D
1228 const Value *pointer_operand = load_inst->getPointerOperand();
1230 Type *pointer_ty = pointer_operand->getType();
1231 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1232 if (!pointer_ptr_ty)
1235 log->Printf("getPointerOperand()->getType() is not a PointerType");
1236 error.SetErrorToGenericError();
1237 error.SetErrorString(interpreter_internal_error);
1240 Type *target_ty = pointer_ptr_ty->getElementType();
1242 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1243 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1245 if (D == LLDB_INVALID_ADDRESS)
1248 log->Printf("LoadInst's value doesn't resolve to anything");
1249 error.SetErrorToGenericError();
1250 error.SetErrorString(bad_value_error);
1254 if (P == LLDB_INVALID_ADDRESS)
1257 log->Printf("LoadInst's pointer doesn't resolve to anything");
1258 error.SetErrorToGenericError();
1259 error.SetErrorString(bad_value_error);
1264 lldb_private::Error read_error;
1265 memory_map.ReadPointerFromMemory(&R, P, read_error);
1267 if (!read_error.Success())
1270 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1271 error.SetErrorToGenericError();
1272 error.SetErrorString(memory_read_error);
1276 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1277 lldb_private::DataBufferHeap buffer(target_size, 0);
1280 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
1281 if (!read_error.Success())
1284 log->Printf("Couldn't read from a region on behalf of a LoadInst");
1285 error.SetErrorToGenericError();
1286 error.SetErrorString(memory_read_error);
1290 lldb_private::Error write_error;
1291 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1292 if (!write_error.Success())
1295 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1296 error.SetErrorToGenericError();
1297 error.SetErrorString(memory_read_error);
1303 log->Printf("Interpreted a LoadInst");
1304 log->Printf(" P : 0x%" PRIx64, P);
1305 log->Printf(" R : 0x%" PRIx64, R);
1306 log->Printf(" D : 0x%" PRIx64, D);
1310 case Instruction::Ret:
1314 case Instruction::Store:
1316 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1321 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
1322 error.SetErrorToGenericError();
1323 error.SetErrorString(interpreter_internal_error);
1327 // The semantics of Store are:
1328 // Resolve the region D containing the data to be stored
1329 // Resolve the region P containing a pointer
1330 // Dereference P to get the region R that the data should be stored in
1331 // Transfer a unit of type type(D) from D to R
1333 const Value *value_operand = store_inst->getValueOperand();
1334 const Value *pointer_operand = store_inst->getPointerOperand();
1336 Type *pointer_ty = pointer_operand->getType();
1337 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1338 if (!pointer_ptr_ty)
1340 Type *target_ty = pointer_ptr_ty->getElementType();
1342 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1343 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1345 if (D == LLDB_INVALID_ADDRESS)
1348 log->Printf("StoreInst's value doesn't resolve to anything");
1349 error.SetErrorToGenericError();
1350 error.SetErrorString(bad_value_error);
1354 if (P == LLDB_INVALID_ADDRESS)
1357 log->Printf("StoreInst's pointer doesn't resolve to anything");
1358 error.SetErrorToGenericError();
1359 error.SetErrorString(bad_value_error);
1364 lldb_private::Error read_error;
1365 memory_map.ReadPointerFromMemory(&R, P, read_error);
1367 if (!read_error.Success())
1370 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1371 error.SetErrorToGenericError();
1372 error.SetErrorString(memory_read_error);
1376 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1377 lldb_private::DataBufferHeap buffer(target_size, 0);
1380 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
1381 if (!read_error.Success())
1384 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1385 error.SetErrorToGenericError();
1386 error.SetErrorString(memory_read_error);
1390 lldb_private::Error write_error;
1391 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1392 if (!write_error.Success())
1395 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1396 error.SetErrorToGenericError();
1397 error.SetErrorString(memory_write_error);
1403 log->Printf("Interpreted a StoreInst");
1404 log->Printf(" D : 0x%" PRIx64, D);
1405 log->Printf(" P : 0x%" PRIx64, P);
1406 log->Printf(" R : 0x%" PRIx64, R);
1415 if (num_insts >= 4096)
1417 error.SetErrorToGenericError();
1418 error.SetErrorString(infinite_loop_error);