701 llvm::ArrayRef<lldb::addr_t> args,
712 raw_string_ostream oss(s);
714 module.print(oss, nullptr);
716 LLDB_LOGF(log,
"Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
720 const DataLayout &data_layout =
module.getDataLayout();
732 for (llvm::Function::arg_iterator ai = function.arg_begin(),
733 ae = function.arg_end();
734 ai != ae; ++ai, ++arg_index) {
735 if (args.size() <=
static_cast<size_t>(arg_index)) {
737 "Not enough arguments passed in to function");
746 frame.
Jump(&function.front());
751 using clock = std::chrono::steady_clock;
754 std::optional<clock::time_point> end_time;
755 if (timeout && timeout->count() > 0)
756 end_time = clock::now() + *timeout;
760 if (end_time && clock::now() >= *end_time) {
768 "Interrupted in IR interpreting.")) {
774 const Instruction *inst = &*frame.
m_ii;
778 switch (inst->getOpcode()) {
782 case Instruction::Add:
783 case Instruction::Sub:
784 case Instruction::Mul:
785 case Instruction::SDiv:
786 case Instruction::UDiv:
787 case Instruction::SRem:
788 case Instruction::URem:
789 case Instruction::Shl:
790 case Instruction::LShr:
791 case Instruction::AShr:
792 case Instruction::And:
793 case Instruction::Or:
794 case Instruction::Xor:
795 case Instruction::FAdd:
796 case Instruction::FSub:
797 case Instruction::FMul:
798 case Instruction::FDiv: {
799 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
804 "getOpcode() returns %s, but instruction is not a BinaryOperator",
805 inst->getOpcodeName());
811 Value *lhs = inst->getOperand(0);
812 Value *rhs = inst->getOperand(1);
831 switch (inst->getOpcode()) {
834 case Instruction::Add:
835 case Instruction::FAdd:
838 case Instruction::Mul:
839 case Instruction::FMul:
842 case Instruction::Sub:
843 case Instruction::FSub:
846 case Instruction::SDiv:
851 case Instruction::UDiv:
856 case Instruction::FDiv:
859 case Instruction::SRem:
864 case Instruction::URem:
869 case Instruction::Shl:
872 case Instruction::AShr:
875 case Instruction::LShr:
879 case Instruction::And:
882 case Instruction::Or:
885 case Instruction::Xor:
893 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
899 case Instruction::Alloca: {
900 const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
902 std::optional<TypeSize> alloca_size =
904 if (!alloca_size || alloca_size->isScalable()) {
905 LLDB_LOGF(log,
"AllocaInsts are not handled if size is not computable");
917 Type *Tptr = alloca_inst->getType();
920 alloca_inst->getAlign().value());
923 LLDB_LOGF(log,
"Couldn't allocate memory for an AllocaInst");
932 "Couldn't allocate the result pointer for an AllocaInst");
942 LLDB_LOGF(log,
"Couldn't write the result pointer for an AllocaInst");
945 execution_unit.
Free(P, free_error);
946 execution_unit.
Free(R, free_error);
953 LLDB_LOGF(log,
"Interpreted an AllocaInst");
958 case Instruction::BitCast:
959 case Instruction::ZExt: {
960 const CastInst *cast_inst = cast<CastInst>(inst);
962 Value *source = cast_inst->getOperand(0);
974 case Instruction::SExt: {
975 const CastInst *cast_inst = cast<CastInst>(inst);
977 Value *source = cast_inst->getOperand(0);
993 case Instruction::UncondBr:
994 frame.
Jump(cast<UncondBrInst>(inst)->getSuccessor());
996 LLDB_LOGF(log,
"Interpreted an UncondBrInst");
999 case Instruction::CondBr: {
1000 const CondBrInst *br_inst = cast<CondBrInst>(inst);
1002 Value *condition = br_inst->getCondition();
1013 frame.
Jump(br_inst->getSuccessor(0));
1015 frame.
Jump(br_inst->getSuccessor(1));
1018 LLDB_LOGF(log,
"Interpreted a CondBrInst");
1023 case Instruction::PHI: {
1024 const PHINode *phi_inst = cast<PHINode>(inst);
1027 "Encountered PHI node without having jumped from another "
1034 Value *value = phi_inst->getIncomingValueForBlock(frame.
m_prev_bb);
1044 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1049 case Instruction::GetElementPtr: {
1050 const GetElementPtrInst *gep_inst = cast<GetElementPtrInst>(inst);
1052 const Value *pointer_operand = gep_inst->getPointerOperand();
1053 Type *src_elem_ty = gep_inst->getSourceElementType();
1064 typedef SmallVector<Value *, 8> IndexVector;
1065 typedef IndexVector::iterator IndexIterator;
1067 SmallVector<Value *, 8> indices(gep_inst->idx_begin(),
1068 gep_inst->idx_end());
1070 SmallVector<Value *, 8> const_indices;
1072 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie;
1074 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1076 if (!constant_index) {
1085 LLDB_LOGF(log,
"Evaluated constant index %s as %llu",
1088 constant_index = cast<ConstantInt>(ConstantInt::get(
1092 const_indices.push_back(constant_index);
1096 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
1103 LLDB_LOGF(log,
"Interpreted a GetElementPtrInst");
1109 case Instruction::FCmp:
1110 case Instruction::ICmp: {
1111 const CmpInst *icmp_inst = cast<CmpInst>(inst);
1113 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1115 Value *lhs = inst->getOperand(0);
1116 Value *rhs = inst->getOperand(1);
1135 switch (predicate) {
1138 case CmpInst::ICMP_EQ:
1139 case CmpInst::FCMP_OEQ:
1142 case CmpInst::ICMP_NE:
1143 case CmpInst::FCMP_UNE:
1146 case CmpInst::ICMP_UGT:
1151 case CmpInst::ICMP_UGE:
1156 case CmpInst::FCMP_OGE:
1159 case CmpInst::FCMP_OGT:
1162 case CmpInst::ICMP_ULT:
1167 case CmpInst::FCMP_OLT:
1170 case CmpInst::ICMP_ULE:
1175 case CmpInst::FCMP_OLE:
1178 case CmpInst::ICMP_SGT:
1183 case CmpInst::ICMP_SGE:
1188 case CmpInst::ICMP_SLT:
1193 case CmpInst::ICMP_SLE:
1203 LLDB_LOGF(log,
"Interpreted an ICmpInst");
1209 case Instruction::IntToPtr: {
1210 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1212 Value *src_operand = int_to_ptr_inst->getOperand(0);
1225 LLDB_LOGF(log,
"Interpreted an IntToPtr");
1230 case Instruction::PtrToInt: {
1231 const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
1233 Value *src_operand = ptr_to_int_inst->getOperand(0);
1246 LLDB_LOGF(log,
"Interpreted a PtrToInt");
1251 case Instruction::Trunc: {
1252 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1254 Value *src_operand = trunc_inst->getOperand(0);
1272 case Instruction::FPToUI:
1273 case Instruction::FPToSI: {
1274 Value *src_operand = inst->getOperand(0);
1283 assert(inst->getType()->isIntegerTy() &&
"Unexpected target type");
1284 llvm::APSInt result(inst->getType()->getIntegerBitWidth(),
1285 inst->getOpcode() ==
1286 Instruction::FPToUI);
1288 "Unexpected source type");
1290 llvm::APFloatBase::opStatus status = S.GetAPFloat().convertToInteger(
1291 result, llvm::APFloat::rmTowardZero, &isExact);
1294 if (status & llvm::APFloatBase::opInvalidOp) {
1296 raw_string_ostream rso(s);
1297 rso <<
"Conversion error: " << S <<
" cannot be converted to ";
1298 if (inst->getOpcode() == Instruction::FPToUI)
1300 rso << *inst->getType();
1309 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1314 case Instruction::UIToFP:
1315 case Instruction::SIToFP:
1316 case Instruction::FPTrunc:
1317 case Instruction::FPExt: {
1318 Value *src_operand = inst->getOperand(0);
1328 Type *result_type = inst->getType();
1330 (result_type->isFloatTy() || result_type->isDoubleTy()) &&
1331 "Unsupported result type; CanInterpret() should have checked that");
1332 if (result_type->isFloatTy())
1339 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1344 case Instruction::Load: {
1345 const LoadInst *load_inst = cast<LoadInst>(inst);
1353 const Value *pointer_operand = load_inst->getPointerOperand();
1359 LLDB_LOGF(log,
"LoadInst's value doesn't resolve to anything");
1365 LLDB_LOGF(log,
"LoadInst's pointer doesn't resolve to anything");
1375 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1380 Type *target_ty = load_inst->getType();
1381 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1388 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a LoadInst");
1397 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a LoadInst");
1403 LLDB_LOGF(log,
"Interpreted a LoadInst");
1409 case Instruction::Ret: {
1412 case Instruction::Store: {
1413 const StoreInst *store_inst = cast<StoreInst>(inst);
1421 const Value *value_operand = store_inst->getValueOperand();
1422 const Value *pointer_operand = store_inst->getPointerOperand();
1428 LLDB_LOGF(log,
"StoreInst's value doesn't resolve to anything");
1434 LLDB_LOGF(log,
"StoreInst's pointer doesn't resolve to anything");
1444 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1449 Type *target_ty = value_operand->getType();
1450 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1457 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a StoreInst");
1466 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a StoreInst");
1472 LLDB_LOGF(log,
"Interpreted a StoreInst");
1478 case Instruction::Call: {
1479 const CallInst *call_inst = cast<CallInst>(inst);
1485 llvm::Type *returnType = call_inst->getType();
1486 if (returnType ==
nullptr) {
1488 "unable to access return type");
1493 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1494 !returnType->isPointerTy()) {
1496 "return type is not supported");
1516 const llvm::Value *val = call_inst->getCalledOperand();
1520 "unable to get address of function");
1528 llvm::FunctionType *prototype = call_inst->getFunctionType();
1531 const int numArgs = call_inst->arg_size();
1535 if (numArgs >= 16) {
1537 "function takes too many arguments");
1543 for (
int i = 0; i < numArgs; i++) {
1545 llvm::Value *arg_op = call_inst->getArgOperand(i);
1546 llvm::Type *arg_ty = arg_op->getType();
1549 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1551 "argument %d must be integer type", i);
1559 "unable to evaluate argument %d", i);
1564 if (arg_ty->isPointerTy()) {
1566 size_t dataSize = 0;
1568 bool Success = execution_unit.
GetAllocSize(addr, dataSize);
1571 "unable to locate host data for transfer to device");
1573 rawArgs[i].
size = dataSize;
1574 rawArgs[i].
data_up.reset(
new uint8_t[dataSize + 1]);
1577 execution_unit.
ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1579 assert(!
error.Fail() &&
1580 "we have failed to read the string from memory");
1583 rawArgs[i].
data_up[dataSize] =
'\0';
1589 rawArgs[i].
size = arg_ty->getIntegerBitWidth() / 8;
1596 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1601 exe_ctx.
GetThreadRef(), funcAddr, *prototype, *returnType, args,
1606 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1608 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1617 process->
RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
1622 "ThreadPlanCallFunctionUsingABI failed");
1629 if (returnType->isVoidTy()) {
1633 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1641 if (vobj ==
nullptr || !retVal) {
1643 "unable to get the return value");