704 llvm::ArrayRef<lldb::addr_t> args,
715 raw_string_ostream oss(s);
717 module.print(oss, nullptr);
719 LLDB_LOGF(log,
"Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
723 const DataLayout &data_layout =
module.getDataLayout();
735 for (llvm::Function::arg_iterator ai = function.arg_begin(),
736 ae = function.arg_end();
737 ai != ae; ++ai, ++arg_index) {
738 if (args.size() <=
static_cast<size_t>(arg_index)) {
740 "Not enough arguments passed in to function");
749 frame.
Jump(&function.front());
754 using clock = std::chrono::steady_clock;
757 std::optional<clock::time_point> end_time;
758 if (timeout && timeout->count() > 0)
759 end_time = clock::now() + *timeout;
763 if (end_time && clock::now() >= *end_time) {
771 "Interrupted in IR interpreting.")) {
777 const Instruction *inst = &*frame.
m_ii;
781 switch (inst->getOpcode()) {
785 case Instruction::Add:
786 case Instruction::Sub:
787 case Instruction::Mul:
788 case Instruction::SDiv:
789 case Instruction::UDiv:
790 case Instruction::SRem:
791 case Instruction::URem:
792 case Instruction::Shl:
793 case Instruction::LShr:
794 case Instruction::AShr:
795 case Instruction::And:
796 case Instruction::Or:
797 case Instruction::Xor:
798 case Instruction::FAdd:
799 case Instruction::FSub:
800 case Instruction::FMul:
801 case Instruction::FDiv: {
802 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
807 "getOpcode() returns %s, but instruction is not a BinaryOperator",
808 inst->getOpcodeName());
814 Value *lhs = inst->getOperand(0);
815 Value *rhs = inst->getOperand(1);
834 switch (inst->getOpcode()) {
837 case Instruction::Add:
838 case Instruction::FAdd:
841 case Instruction::Mul:
842 case Instruction::FMul:
845 case Instruction::Sub:
846 case Instruction::FSub:
849 case Instruction::SDiv:
854 case Instruction::UDiv:
859 case Instruction::FDiv:
862 case Instruction::SRem:
867 case Instruction::URem:
872 case Instruction::Shl:
875 case Instruction::AShr:
878 case Instruction::LShr:
882 case Instruction::And:
885 case Instruction::Or:
888 case Instruction::Xor:
895 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
900 case Instruction::Alloca: {
901 const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
903 std::optional<TypeSize> alloca_size =
905 if (!alloca_size || alloca_size->isScalable()) {
906 LLDB_LOGF(log,
"AllocaInsts are not handled if size is not computable");
918 Type *Tptr = alloca_inst->getType();
921 alloca_inst->getAlign().value());
924 LLDB_LOGF(log,
"Couldn't allocate memory for an AllocaInst");
933 "Couldn't allocate the result pointer for an AllocaInst");
943 LLDB_LOGF(log,
"Couldn't write the result pointer for an AllocaInst");
946 execution_unit.
Free(P, free_error);
947 execution_unit.
Free(R, free_error);
953 LLDB_LOGF(log,
"Interpreted an AllocaInst");
957 case Instruction::BitCast:
958 case Instruction::ZExt: {
959 const CastInst *cast_inst = cast<CastInst>(inst);
961 Value *source = cast_inst->getOperand(0);
973 case Instruction::SExt: {
974 const CastInst *cast_inst = cast<CastInst>(inst);
976 Value *source = cast_inst->getOperand(0);
992 case Instruction::UncondBr:
993 frame.
Jump(cast<UncondBrInst>(inst)->getSuccessor());
994 LLDB_LOGF(log,
"Interpreted an UncondBrInst");
996 case Instruction::CondBr: {
997 const CondBrInst *br_inst = cast<CondBrInst>(inst);
999 Value *condition = br_inst->getCondition();
1010 frame.
Jump(br_inst->getSuccessor(0));
1012 frame.
Jump(br_inst->getSuccessor(1));
1014 LLDB_LOGF(log,
"Interpreted a CondBrInst");
1018 case Instruction::PHI: {
1019 const PHINode *phi_inst = cast<PHINode>(inst);
1022 "Encountered PHI node without having jumped from another "
1029 Value *value = phi_inst->getIncomingValueForBlock(frame.
m_prev_bb);
1038 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1042 case Instruction::GetElementPtr: {
1043 const GetElementPtrInst *gep_inst = cast<GetElementPtrInst>(inst);
1045 const Value *pointer_operand = gep_inst->getPointerOperand();
1046 Type *src_elem_ty = gep_inst->getSourceElementType();
1057 typedef SmallVector<Value *, 8> IndexVector;
1058 typedef IndexVector::iterator IndexIterator;
1060 SmallVector<Value *, 8> indices(gep_inst->idx_begin(),
1061 gep_inst->idx_end());
1063 SmallVector<Value *, 8> const_indices;
1065 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie;
1067 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1069 if (!constant_index) {
1078 LLDB_LOGF(log,
"Evaluated constant index %s as %llu",
1081 constant_index = cast<ConstantInt>(ConstantInt::get(
1085 const_indices.push_back(constant_index);
1089 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
1095 LLDB_LOGF(log,
"Interpreted a GetElementPtrInst");
1100 case Instruction::FCmp:
1101 case Instruction::ICmp: {
1102 const CmpInst *icmp_inst = cast<CmpInst>(inst);
1104 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1106 Value *lhs = inst->getOperand(0);
1107 Value *rhs = inst->getOperand(1);
1126 switch (predicate) {
1129 case CmpInst::ICMP_EQ:
1130 case CmpInst::FCMP_OEQ:
1133 case CmpInst::ICMP_NE:
1134 case CmpInst::FCMP_UNE:
1137 case CmpInst::ICMP_UGT:
1142 case CmpInst::ICMP_UGE:
1147 case CmpInst::FCMP_OGE:
1150 case CmpInst::FCMP_OGT:
1153 case CmpInst::ICMP_ULT:
1158 case CmpInst::FCMP_OLT:
1161 case CmpInst::ICMP_ULE:
1166 case CmpInst::FCMP_OLE:
1169 case CmpInst::ICMP_SGT:
1174 case CmpInst::ICMP_SGE:
1179 case CmpInst::ICMP_SLT:
1184 case CmpInst::ICMP_SLE:
1193 LLDB_LOGF(log,
"Interpreted an ICmpInst");
1198 case Instruction::IntToPtr: {
1199 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1201 Value *src_operand = int_to_ptr_inst->getOperand(0);
1213 LLDB_LOGF(log,
"Interpreted an IntToPtr");
1217 case Instruction::PtrToInt: {
1218 const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
1220 Value *src_operand = ptr_to_int_inst->getOperand(0);
1232 LLDB_LOGF(log,
"Interpreted a PtrToInt");
1236 case Instruction::Trunc: {
1237 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1239 Value *src_operand = trunc_inst->getOperand(0);
1255 case Instruction::FPToUI:
1256 case Instruction::FPToSI: {
1257 Value *src_operand = inst->getOperand(0);
1266 assert(inst->getType()->isIntegerTy() &&
"Unexpected target type");
1267 llvm::APSInt result(inst->getType()->getIntegerBitWidth(),
1268 inst->getOpcode() ==
1269 Instruction::FPToUI);
1271 "Unexpected source type");
1273 llvm::APFloatBase::opStatus status = S.GetAPFloat().convertToInteger(
1274 result, llvm::APFloat::rmTowardZero, &isExact);
1277 if (status & llvm::APFloatBase::opInvalidOp) {
1279 raw_string_ostream rso(s);
1280 rso <<
"Conversion error: " << S <<
" cannot be converted to ";
1281 if (inst->getOpcode() == Instruction::FPToUI)
1283 rso << *inst->getType();
1291 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1295 case Instruction::UIToFP:
1296 case Instruction::SIToFP:
1297 case Instruction::FPTrunc:
1298 case Instruction::FPExt: {
1299 Value *src_operand = inst->getOperand(0);
1309 Type *result_type = inst->getType();
1311 (result_type->isFloatTy() || result_type->isDoubleTy()) &&
1312 "Unsupported result type; CanInterpret() should have checked that");
1313 if (result_type->isFloatTy())
1319 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1323 case Instruction::Load: {
1324 const LoadInst *load_inst = cast<LoadInst>(inst);
1332 const Value *pointer_operand = load_inst->getPointerOperand();
1338 LLDB_LOGF(log,
"LoadInst's value doesn't resolve to anything");
1344 LLDB_LOGF(log,
"LoadInst's pointer doesn't resolve to anything");
1354 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1359 Type *target_ty = load_inst->getType();
1360 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1367 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a LoadInst");
1376 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a LoadInst");
1381 LLDB_LOGF(log,
"Interpreted a LoadInst");
1386 case Instruction::Ret: {
1389 case Instruction::Store: {
1390 const StoreInst *store_inst = cast<StoreInst>(inst);
1398 const Value *value_operand = store_inst->getValueOperand();
1399 const Value *pointer_operand = store_inst->getPointerOperand();
1405 LLDB_LOGF(log,
"StoreInst's value doesn't resolve to anything");
1411 LLDB_LOGF(log,
"StoreInst's pointer doesn't resolve to anything");
1421 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1426 Type *target_ty = value_operand->getType();
1427 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1434 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a StoreInst");
1443 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a StoreInst");
1448 LLDB_LOGF(log,
"Interpreted a StoreInst");
1453 case Instruction::Call: {
1454 const CallInst *call_inst = cast<CallInst>(inst);
1460 llvm::Type *returnType = call_inst->getType();
1461 if (returnType ==
nullptr) {
1463 "unable to access return type");
1468 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1469 !returnType->isPointerTy()) {
1471 "return type is not supported");
1491 const llvm::Value *val = call_inst->getCalledOperand();
1495 "unable to get address of function");
1503 llvm::FunctionType *prototype = call_inst->getFunctionType();
1506 const int numArgs = call_inst->arg_size();
1510 if (numArgs >= 16) {
1512 "function takes too many arguments");
1518 for (
int i = 0; i < numArgs; i++) {
1520 llvm::Value *arg_op = call_inst->getArgOperand(i);
1521 llvm::Type *arg_ty = arg_op->getType();
1524 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1526 "argument %d must be integer type", i);
1534 "unable to evaluate argument %d", i);
1539 if (arg_ty->isPointerTy()) {
1541 size_t dataSize = 0;
1543 bool Success = execution_unit.
GetAllocSize(addr, dataSize);
1546 "unable to locate host data for transfer to device");
1548 rawArgs[i].
size = dataSize;
1549 rawArgs[i].
data_up.reset(
new uint8_t[dataSize + 1]);
1552 execution_unit.
ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1554 assert(!
error.Fail() &&
1555 "we have failed to read the string from memory");
1558 rawArgs[i].
data_up[dataSize] =
'\0';
1564 rawArgs[i].
size = arg_ty->getIntegerBitWidth() / 8;
1571 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1576 exe_ctx.
GetThreadRef(), funcAddr, *prototype, *returnType, args,
1581 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1583 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1595 process->
RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
1600 "ThreadPlanCallFunctionUsingABI failed");
1607 if (returnType->isVoidTy()) {
1611 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1619 if (vobj ==
nullptr || !retVal) {
1621 "unable to get the return value");