699 llvm::ArrayRef<lldb::addr_t> args,
710 raw_string_ostream oss(s);
712 module.print(oss, nullptr);
714 LLDB_LOGF(log,
"Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
718 const DataLayout &data_layout =
module.getDataLayout();
730 for (llvm::Function::arg_iterator ai = function.arg_begin(),
731 ae = function.arg_end();
732 ai != ae; ++ai, ++arg_index) {
733 if (args.size() <=
static_cast<size_t>(arg_index)) {
735 "Not enough arguments passed in to function");
744 frame.
Jump(&function.front());
749 using clock = std::chrono::steady_clock;
752 std::optional<clock::time_point> end_time;
753 if (timeout && timeout->count() > 0)
754 end_time = clock::now() + *timeout;
758 if (end_time && clock::now() >= *end_time) {
766 "Interrupted in IR interpreting.")) {
772 const Instruction *inst = &*frame.
m_ii;
776 switch (inst->getOpcode()) {
780 case Instruction::Add:
781 case Instruction::Sub:
782 case Instruction::Mul:
783 case Instruction::SDiv:
784 case Instruction::UDiv:
785 case Instruction::SRem:
786 case Instruction::URem:
787 case Instruction::Shl:
788 case Instruction::LShr:
789 case Instruction::AShr:
790 case Instruction::And:
791 case Instruction::Or:
792 case Instruction::Xor:
793 case Instruction::FAdd:
794 case Instruction::FSub:
795 case Instruction::FMul:
796 case Instruction::FDiv: {
797 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
802 "getOpcode() returns %s, but instruction is not a BinaryOperator",
803 inst->getOpcodeName());
809 Value *lhs = inst->getOperand(0);
810 Value *rhs = inst->getOperand(1);
829 switch (inst->getOpcode()) {
832 case Instruction::Add:
833 case Instruction::FAdd:
836 case Instruction::Mul:
837 case Instruction::FMul:
840 case Instruction::Sub:
841 case Instruction::FSub:
844 case Instruction::SDiv:
849 case Instruction::UDiv:
854 case Instruction::FDiv:
857 case Instruction::SRem:
862 case Instruction::URem:
867 case Instruction::Shl:
870 case Instruction::AShr:
873 case Instruction::LShr:
877 case Instruction::And:
880 case Instruction::Or:
883 case Instruction::Xor:
890 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
895 case Instruction::Alloca: {
896 const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
898 std::optional<TypeSize> alloca_size =
900 if (!alloca_size || alloca_size->isScalable()) {
901 LLDB_LOGF(log,
"AllocaInsts are not handled if size is not computable");
913 Type *Tptr = alloca_inst->getType();
916 alloca_inst->getAlign().value());
919 LLDB_LOGF(log,
"Couldn't allocate memory for an AllocaInst");
928 "Couldn't allocate the result pointer for an AllocaInst");
938 LLDB_LOGF(log,
"Couldn't write the result pointer for an AllocaInst");
941 execution_unit.
Free(P, free_error);
942 execution_unit.
Free(R, free_error);
948 LLDB_LOGF(log,
"Interpreted an AllocaInst");
952 case Instruction::BitCast:
953 case Instruction::ZExt: {
954 const CastInst *cast_inst = cast<CastInst>(inst);
956 Value *source = cast_inst->getOperand(0);
968 case Instruction::SExt: {
969 const CastInst *cast_inst = cast<CastInst>(inst);
971 Value *source = cast_inst->getOperand(0);
987 case Instruction::UncondBr:
988 frame.
Jump(cast<UncondBrInst>(inst)->getSuccessor());
989 LLDB_LOGF(log,
"Interpreted an UncondBrInst");
991 case Instruction::CondBr: {
992 const CondBrInst *br_inst = cast<CondBrInst>(inst);
994 Value *condition = br_inst->getCondition();
1005 frame.
Jump(br_inst->getSuccessor(0));
1007 frame.
Jump(br_inst->getSuccessor(1));
1009 LLDB_LOGF(log,
"Interpreted a CondBrInst");
1013 case Instruction::PHI: {
1014 const PHINode *phi_inst = cast<PHINode>(inst);
1017 "Encountered PHI node without having jumped from another "
1024 Value *value = phi_inst->getIncomingValueForBlock(frame.
m_prev_bb);
1033 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1037 case Instruction::GetElementPtr: {
1038 const GetElementPtrInst *gep_inst = cast<GetElementPtrInst>(inst);
1040 const Value *pointer_operand = gep_inst->getPointerOperand();
1041 Type *src_elem_ty = gep_inst->getSourceElementType();
1052 typedef SmallVector<Value *, 8> IndexVector;
1053 typedef IndexVector::iterator IndexIterator;
1055 SmallVector<Value *, 8> indices(gep_inst->idx_begin(),
1056 gep_inst->idx_end());
1058 SmallVector<Value *, 8> const_indices;
1060 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie;
1062 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1064 if (!constant_index) {
1073 LLDB_LOGF(log,
"Evaluated constant index %s as %llu",
1076 constant_index = cast<ConstantInt>(ConstantInt::get(
1080 const_indices.push_back(constant_index);
1084 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
1090 LLDB_LOGF(log,
"Interpreted a GetElementPtrInst");
1095 case Instruction::FCmp:
1096 case Instruction::ICmp: {
1097 const CmpInst *icmp_inst = cast<CmpInst>(inst);
1099 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1101 Value *lhs = inst->getOperand(0);
1102 Value *rhs = inst->getOperand(1);
1121 switch (predicate) {
1124 case CmpInst::ICMP_EQ:
1125 case CmpInst::FCMP_OEQ:
1128 case CmpInst::ICMP_NE:
1129 case CmpInst::FCMP_UNE:
1132 case CmpInst::ICMP_UGT:
1137 case CmpInst::ICMP_UGE:
1142 case CmpInst::FCMP_OGE:
1145 case CmpInst::FCMP_OGT:
1148 case CmpInst::ICMP_ULT:
1153 case CmpInst::FCMP_OLT:
1156 case CmpInst::ICMP_ULE:
1161 case CmpInst::FCMP_OLE:
1164 case CmpInst::ICMP_SGT:
1169 case CmpInst::ICMP_SGE:
1174 case CmpInst::ICMP_SLT:
1179 case CmpInst::ICMP_SLE:
1188 LLDB_LOGF(log,
"Interpreted an ICmpInst");
1193 case Instruction::IntToPtr: {
1194 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1196 Value *src_operand = int_to_ptr_inst->getOperand(0);
1208 LLDB_LOGF(log,
"Interpreted an IntToPtr");
1212 case Instruction::PtrToInt: {
1213 const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
1215 Value *src_operand = ptr_to_int_inst->getOperand(0);
1227 LLDB_LOGF(log,
"Interpreted a PtrToInt");
1231 case Instruction::Trunc: {
1232 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1234 Value *src_operand = trunc_inst->getOperand(0);
1250 case Instruction::FPToUI:
1251 case Instruction::FPToSI: {
1252 Value *src_operand = inst->getOperand(0);
1261 assert(inst->getType()->isIntegerTy() &&
"Unexpected target type");
1262 llvm::APSInt result(inst->getType()->getIntegerBitWidth(),
1263 inst->getOpcode() ==
1264 Instruction::FPToUI);
1266 "Unexpected source type");
1268 llvm::APFloatBase::opStatus status = S.GetAPFloat().convertToInteger(
1269 result, llvm::APFloat::rmTowardZero, &isExact);
1272 if (status & llvm::APFloatBase::opInvalidOp) {
1274 raw_string_ostream rso(s);
1275 rso <<
"Conversion error: " << S <<
" cannot be converted to ";
1276 if (inst->getOpcode() == Instruction::FPToUI)
1278 rso << *inst->getType();
1286 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1290 case Instruction::UIToFP:
1291 case Instruction::SIToFP:
1292 case Instruction::FPTrunc:
1293 case Instruction::FPExt: {
1294 Value *src_operand = inst->getOperand(0);
1304 Type *result_type = inst->getType();
1306 (result_type->isFloatTy() || result_type->isDoubleTy()) &&
1307 "Unsupported result type; CanInterpret() should have checked that");
1308 if (result_type->isFloatTy())
1314 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
1318 case Instruction::Load: {
1319 const LoadInst *load_inst = cast<LoadInst>(inst);
1327 const Value *pointer_operand = load_inst->getPointerOperand();
1333 LLDB_LOGF(log,
"LoadInst's value doesn't resolve to anything");
1339 LLDB_LOGF(log,
"LoadInst's pointer doesn't resolve to anything");
1349 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1354 Type *target_ty = load_inst->getType();
1355 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1362 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a LoadInst");
1371 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a LoadInst");
1376 LLDB_LOGF(log,
"Interpreted a LoadInst");
1381 case Instruction::Ret: {
1384 case Instruction::Store: {
1385 const StoreInst *store_inst = cast<StoreInst>(inst);
1393 const Value *value_operand = store_inst->getValueOperand();
1394 const Value *pointer_operand = store_inst->getPointerOperand();
1400 LLDB_LOGF(log,
"StoreInst's value doesn't resolve to anything");
1406 LLDB_LOGF(log,
"StoreInst's pointer doesn't resolve to anything");
1416 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1421 Type *target_ty = value_operand->getType();
1422 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1429 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a StoreInst");
1438 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a StoreInst");
1443 LLDB_LOGF(log,
"Interpreted a StoreInst");
1448 case Instruction::Call: {
1449 const CallInst *call_inst = cast<CallInst>(inst);
1455 llvm::Type *returnType = call_inst->getType();
1456 if (returnType ==
nullptr) {
1458 "unable to access return type");
1463 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1464 !returnType->isPointerTy()) {
1466 "return type is not supported");
1486 const llvm::Value *val = call_inst->getCalledOperand();
1490 "unable to get address of function");
1498 llvm::FunctionType *prototype = call_inst->getFunctionType();
1501 const int numArgs = call_inst->arg_size();
1505 if (numArgs >= 16) {
1507 "function takes too many arguments");
1513 for (
int i = 0; i < numArgs; i++) {
1515 llvm::Value *arg_op = call_inst->getArgOperand(i);
1516 llvm::Type *arg_ty = arg_op->getType();
1519 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1521 "argument %d must be integer type", i);
1529 "unable to evaluate argument %d", i);
1534 if (arg_ty->isPointerTy()) {
1536 size_t dataSize = 0;
1538 bool Success = execution_unit.
GetAllocSize(addr, dataSize);
1541 "unable to locate host data for transfer to device");
1543 rawArgs[i].
size = dataSize;
1544 rawArgs[i].
data_up.reset(
new uint8_t[dataSize + 1]);
1547 execution_unit.
ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1549 assert(!
error.Fail() &&
1550 "we have failed to read the string from memory");
1553 rawArgs[i].
data_up[dataSize] =
'\0';
1559 rawArgs[i].
size = arg_ty->getIntegerBitWidth() / 8;
1566 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1571 exe_ctx.
GetThreadRef(), funcAddr, *prototype, *returnType, args,
1576 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1578 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1590 process->
RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
1595 "ThreadPlanCallFunctionUsingABI failed");
1602 if (returnType->isVoidTy()) {
1606 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1614 if (vobj ==
nullptr || !retVal) {
1616 "unable to get the return value");