698 llvm::ArrayRef<lldb::addr_t> args,
709 raw_string_ostream oss(s);
711 module.print(oss, nullptr);
713 LLDB_LOGF(log,
"Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
717 const DataLayout &data_layout =
module.getDataLayout();
729 for (llvm::Function::arg_iterator ai = function.arg_begin(),
730 ae = function.arg_end();
731 ai != ae; ++ai, ++arg_index) {
732 if (args.size() <=
static_cast<size_t>(arg_index)) {
734 "Not enough arguments passed in to function");
743 frame.
Jump(&function.front());
748 using clock = std::chrono::steady_clock;
751 std::optional<clock::time_point> end_time;
752 if (timeout && timeout->count() > 0)
753 end_time = clock::now() + *timeout;
757 if (end_time && clock::now() >= *end_time) {
765 "Interrupted in IR interpreting.")) {
771 const Instruction *inst = &*frame.
m_ii;
775 switch (inst->getOpcode()) {
779 case Instruction::Add:
780 case Instruction::Sub:
781 case Instruction::Mul:
782 case Instruction::SDiv:
783 case Instruction::UDiv:
784 case Instruction::SRem:
785 case Instruction::URem:
786 case Instruction::Shl:
787 case Instruction::LShr:
788 case Instruction::AShr:
789 case Instruction::And:
790 case Instruction::Or:
791 case Instruction::Xor:
792 case Instruction::FAdd:
793 case Instruction::FSub:
794 case Instruction::FMul:
795 case Instruction::FDiv: {
796 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
801 "getOpcode() returns %s, but instruction is not a BinaryOperator",
802 inst->getOpcodeName());
808 Value *lhs = inst->getOperand(0);
809 Value *rhs = inst->getOperand(1);
828 switch (inst->getOpcode()) {
831 case Instruction::Add:
832 case Instruction::FAdd:
835 case Instruction::Mul:
836 case Instruction::FMul:
839 case Instruction::Sub:
840 case Instruction::FSub:
843 case Instruction::SDiv:
848 case Instruction::UDiv:
853 case Instruction::FDiv:
856 case Instruction::SRem:
861 case Instruction::URem:
866 case Instruction::Shl:
869 case Instruction::AShr:
872 case Instruction::LShr:
876 case Instruction::And:
879 case Instruction::Or:
882 case Instruction::Xor:
889 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
894 case Instruction::Alloca: {
895 const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
897 std::optional<TypeSize> alloca_size =
899 if (!alloca_size || alloca_size->isScalable()) {
900 LLDB_LOGF(log,
"AllocaInsts are not handled if size is not computable");
912 Type *Tptr = alloca_inst->getType();
915 alloca_inst->getAlign().value());
918 LLDB_LOGF(log,
"Couldn't allocate memory for an AllocaInst");
927 "Couldn't allocate the result pointer for an AllocaInst");
937 LLDB_LOGF(log,
"Couldn't write the result pointer for an AllocaInst");
940 execution_unit.
Free(P, free_error);
941 execution_unit.
Free(R, free_error);
947 LLDB_LOGF(log,
"Interpreted an AllocaInst");
951 case Instruction::BitCast:
952 case Instruction::ZExt: {
953 const CastInst *cast_inst = cast<CastInst>(inst);
955 Value *source = cast_inst->getOperand(0);
967 case Instruction::SExt: {
968 const CastInst *cast_inst = cast<CastInst>(inst);
970 Value *source = cast_inst->getOperand(0);
986 case Instruction::UncondBr:
987 frame.
Jump(cast<UncondBrInst>(inst)->getSuccessor());
988 LLDB_LOGF(log,
"Interpreted an UncondBrInst");
990 case Instruction::CondBr: {
991 const CondBrInst *br_inst = cast<CondBrInst>(inst);
993 Value *condition = br_inst->getCondition();
1004 frame.
Jump(br_inst->getSuccessor(0));
1006 frame.
Jump(br_inst->getSuccessor(1));
1008 LLDB_LOGF(log,
"Interpreted a CondBrInst");
1012 case Instruction::PHI: {
1013 const PHINode *phi_inst = cast<PHINode>(inst);
1016 "Encountered PHI node without having jumped from another "
1023 Value *value = phi_inst->getIncomingValueForBlock(frame.
m_prev_bb);
1032 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1036 case Instruction::GetElementPtr: {
1037 const GetElementPtrInst *gep_inst = cast<GetElementPtrInst>(inst);
1039 const Value *pointer_operand = gep_inst->getPointerOperand();
1040 Type *src_elem_ty = gep_inst->getSourceElementType();
1051 typedef SmallVector<Value *, 8> IndexVector;
1052 typedef IndexVector::iterator IndexIterator;
1054 SmallVector<Value *, 8> indices(gep_inst->idx_begin(),
1055 gep_inst->idx_end());
1057 SmallVector<Value *, 8> const_indices;
1059 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie;
1061 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1063 if (!constant_index) {
1072 LLDB_LOGF(log,
"Evaluated constant index %s as %llu",
1075 constant_index = cast<ConstantInt>(ConstantInt::get(
1079 const_indices.push_back(constant_index);
1083 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
1089 LLDB_LOGF(log,
"Interpreted a GetElementPtrInst");
1094 case Instruction::FCmp:
1095 case Instruction::ICmp: {
1096 const CmpInst *icmp_inst = cast<CmpInst>(inst);
1098 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1100 Value *lhs = inst->getOperand(0);
1101 Value *rhs = inst->getOperand(1);
1120 switch (predicate) {
1123 case CmpInst::ICMP_EQ:
1124 case CmpInst::FCMP_OEQ:
1127 case CmpInst::ICMP_NE:
1128 case CmpInst::FCMP_UNE:
1131 case CmpInst::ICMP_UGT:
1136 case CmpInst::ICMP_UGE:
1141 case CmpInst::FCMP_OGE:
1144 case CmpInst::FCMP_OGT:
1147 case CmpInst::ICMP_ULT:
1152 case CmpInst::FCMP_OLT:
1155 case CmpInst::ICMP_ULE:
1160 case CmpInst::FCMP_OLE:
1163 case CmpInst::ICMP_SGT:
1168 case CmpInst::ICMP_SGE:
1173 case CmpInst::ICMP_SLT:
1178 case CmpInst::ICMP_SLE:
1187 LLDB_LOGF(log,
"Interpreted an ICmpInst");
1192 case Instruction::IntToPtr: {
1193 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1195 Value *src_operand = int_to_ptr_inst->getOperand(0);
1207 LLDB_LOGF(log,
"Interpreted an IntToPtr");
1211 case Instruction::PtrToInt: {
1212 const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
1214 Value *src_operand = ptr_to_int_inst->getOperand(0);
1226 LLDB_LOGF(log,
"Interpreted a PtrToInt");
1230 case Instruction::Trunc: {
1231 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1233 Value *src_operand = trunc_inst->getOperand(0);
1249 case Instruction::FPToUI:
1250 case Instruction::FPToSI: {
1251 Value *src_operand = inst->getOperand(0);
1260 assert(inst->getType()->isIntegerTy() &&
"Unexpected target type");
1261 llvm::APSInt result(inst->getType()->getIntegerBitWidth(),
1262 inst->getOpcode() ==
1263 Instruction::FPToUI);
1265 "Unexpected source type");
1267 llvm::APFloatBase::opStatus status = S.GetAPFloat().convertToInteger(
1268 result, llvm::APFloat::rmTowardZero, &isExact);
1271 if (status & llvm::APFloatBase::opInvalidOp) {
1273 raw_string_ostream rso(s);
1274 rso <<
"Conversion error: " << S <<
" cannot be converted to ";
1275 if (inst->getOpcode() == Instruction::FPToUI)
1277 rso << *inst->getType();
1285 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1289 case Instruction::UIToFP:
1290 case Instruction::SIToFP:
1291 case Instruction::FPTrunc:
1292 case Instruction::FPExt: {
1293 Value *src_operand = inst->getOperand(0);
1303 Type *result_type = inst->getType();
1305 (result_type->isFloatTy() || result_type->isDoubleTy()) &&
1306 "Unsupported result type; CanInterpret() should have checked that");
1307 if (result_type->isFloatTy())
1313 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1317 case Instruction::Load: {
1318 const LoadInst *load_inst = cast<LoadInst>(inst);
1326 const Value *pointer_operand = load_inst->getPointerOperand();
1332 LLDB_LOGF(log,
"LoadInst's value doesn't resolve to anything");
1338 LLDB_LOGF(log,
"LoadInst's pointer doesn't resolve to anything");
1348 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1353 Type *target_ty = load_inst->getType();
1354 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1361 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a LoadInst");
1370 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a LoadInst");
1375 LLDB_LOGF(log,
"Interpreted a LoadInst");
1380 case Instruction::Ret: {
1383 case Instruction::Store: {
1384 const StoreInst *store_inst = cast<StoreInst>(inst);
1392 const Value *value_operand = store_inst->getValueOperand();
1393 const Value *pointer_operand = store_inst->getPointerOperand();
1399 LLDB_LOGF(log,
"StoreInst's value doesn't resolve to anything");
1405 LLDB_LOGF(log,
"StoreInst's pointer doesn't resolve to anything");
1415 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1420 Type *target_ty = value_operand->getType();
1421 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1428 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a StoreInst");
1437 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a StoreInst");
1442 LLDB_LOGF(log,
"Interpreted a StoreInst");
1447 case Instruction::Call: {
1448 const CallInst *call_inst = cast<CallInst>(inst);
1454 llvm::Type *returnType = call_inst->getType();
1455 if (returnType ==
nullptr) {
1457 "unable to access return type");
1462 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1463 !returnType->isPointerTy()) {
1465 "return type is not supported");
1485 const llvm::Value *val = call_inst->getCalledOperand();
1489 "unable to get address of function");
1497 llvm::FunctionType *prototype = call_inst->getFunctionType();
1500 const int numArgs = call_inst->arg_size();
1504 if (numArgs >= 16) {
1506 "function takes too many arguments");
1512 for (
int i = 0; i < numArgs; i++) {
1514 llvm::Value *arg_op = call_inst->getArgOperand(i);
1515 llvm::Type *arg_ty = arg_op->getType();
1518 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1520 "argument %d must be integer type", i);
1528 "unable to evaluate argument %d", i);
1533 if (arg_ty->isPointerTy()) {
1535 size_t dataSize = 0;
1537 bool Success = execution_unit.
GetAllocSize(addr, dataSize);
1540 "unable to locate host data for transfer to device");
1542 rawArgs[i].
size = dataSize;
1543 rawArgs[i].
data_up.reset(
new uint8_t[dataSize + 1]);
1546 execution_unit.
ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1548 assert(!
error.Fail() &&
1549 "we have failed to read the string from memory");
1552 rawArgs[i].
data_up[dataSize] =
'\0';
1558 rawArgs[i].
size = arg_ty->getIntegerBitWidth() / 8;
1565 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1570 exe_ctx.
GetThreadRef(), funcAddr, *prototype, *returnType, args,
1575 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1577 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1586 process->
RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
1591 "ThreadPlanCallFunctionUsingABI failed");
1598 if (returnType->isVoidTy()) {
1602 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1610 if (vobj ==
nullptr || !retVal) {
1612 "unable to get the return value");