686 llvm::ArrayRef<lldb::addr_t> args,
697 raw_string_ostream oss(s);
699 module.print(oss, nullptr);
701 LLDB_LOGF(log,
"Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
705 const DataLayout &data_layout =
module.getDataLayout();
717 for (llvm::Function::arg_iterator ai = function.arg_begin(),
718 ae = function.arg_end();
719 ai != ae; ++ai, ++arg_index) {
720 if (args.size() <=
static_cast<size_t>(arg_index)) {
722 "Not enough arguments passed in to function");
731 frame.
Jump(&function.front());
736 using clock = std::chrono::steady_clock;
739 std::optional<clock::time_point> end_time;
740 if (timeout && timeout->count() > 0)
741 end_time = clock::now() + *timeout;
745 if (end_time && clock::now() >= *end_time) {
753 "Interrupted in IR interpreting.")) {
759 const Instruction *inst = &*frame.
m_ii;
763 switch (inst->getOpcode()) {
767 case Instruction::Add:
768 case Instruction::Sub:
769 case Instruction::Mul:
770 case Instruction::SDiv:
771 case Instruction::UDiv:
772 case Instruction::SRem:
773 case Instruction::URem:
774 case Instruction::Shl:
775 case Instruction::LShr:
776 case Instruction::AShr:
777 case Instruction::And:
778 case Instruction::Or:
779 case Instruction::Xor:
780 case Instruction::FAdd:
781 case Instruction::FSub:
782 case Instruction::FMul:
783 case Instruction::FDiv: {
784 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
789 "getOpcode() returns %s, but instruction is not a BinaryOperator",
790 inst->getOpcodeName());
796 Value *lhs = inst->getOperand(0);
797 Value *rhs = inst->getOperand(1);
816 switch (inst->getOpcode()) {
819 case Instruction::Add:
820 case Instruction::FAdd:
823 case Instruction::Mul:
824 case Instruction::FMul:
827 case Instruction::Sub:
828 case Instruction::FSub:
831 case Instruction::SDiv:
836 case Instruction::UDiv:
841 case Instruction::FDiv:
844 case Instruction::SRem:
849 case Instruction::URem:
854 case Instruction::Shl:
857 case Instruction::AShr:
860 case Instruction::LShr:
864 case Instruction::And:
867 case Instruction::Or:
870 case Instruction::Xor:
878 LLDB_LOGF(log,
"Interpreted a %s", inst->getOpcodeName());
884 case Instruction::Alloca: {
885 const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
887 std::optional<TypeSize> alloca_size =
889 if (!alloca_size || alloca_size->isScalable()) {
890 LLDB_LOGF(log,
"AllocaInsts are not handled if size is not computable");
902 Type *Tptr = alloca_inst->getType();
905 alloca_inst->getAlign().value());
908 LLDB_LOGF(log,
"Couldn't allocate memory for an AllocaInst");
917 "Couldn't allocate the result pointer for an AllocaInst");
927 LLDB_LOGF(log,
"Couldn't write the result pointer for an AllocaInst");
930 execution_unit.
Free(P, free_error);
931 execution_unit.
Free(R, free_error);
938 LLDB_LOGF(log,
"Interpreted an AllocaInst");
943 case Instruction::BitCast:
944 case Instruction::ZExt: {
945 const CastInst *cast_inst = cast<CastInst>(inst);
947 Value *source = cast_inst->getOperand(0);
959 case Instruction::SExt: {
960 const CastInst *cast_inst = cast<CastInst>(inst);
962 Value *source = cast_inst->getOperand(0);
978 case Instruction::Br: {
979 const BranchInst *br_inst = cast<BranchInst>(inst);
981 if (br_inst->isConditional()) {
982 Value *condition = br_inst->getCondition();
993 frame.
Jump(br_inst->getSuccessor(0));
995 frame.
Jump(br_inst->getSuccessor(1));
998 LLDB_LOGF(log,
"Interpreted a BrInst with a condition");
1003 frame.
Jump(br_inst->getSuccessor(0));
1006 LLDB_LOGF(log,
"Interpreted a BrInst with no condition");
1011 case Instruction::PHI: {
1012 const PHINode *phi_inst = cast<PHINode>(inst);
1015 "Encountered PHI node without having jumped from another "
1022 Value *value = phi_inst->getIncomingValueForBlock(frame.
m_prev_bb);
1032 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);
1091 LLDB_LOGF(log,
"Interpreted a GetElementPtrInst");
1097 case Instruction::FCmp:
1098 case Instruction::ICmp: {
1099 const CmpInst *icmp_inst = cast<CmpInst>(inst);
1101 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1103 Value *lhs = inst->getOperand(0);
1104 Value *rhs = inst->getOperand(1);
1123 switch (predicate) {
1126 case CmpInst::ICMP_EQ:
1127 case CmpInst::FCMP_OEQ:
1130 case CmpInst::ICMP_NE:
1131 case CmpInst::FCMP_UNE:
1134 case CmpInst::ICMP_UGT:
1139 case CmpInst::ICMP_UGE:
1144 case CmpInst::FCMP_OGE:
1147 case CmpInst::FCMP_OGT:
1150 case CmpInst::ICMP_ULT:
1155 case CmpInst::FCMP_OLT:
1158 case CmpInst::ICMP_ULE:
1163 case CmpInst::FCMP_OLE:
1166 case CmpInst::ICMP_SGT:
1171 case CmpInst::ICMP_SGE:
1176 case CmpInst::ICMP_SLT:
1181 case CmpInst::ICMP_SLE:
1191 LLDB_LOGF(log,
"Interpreted an ICmpInst");
1197 case Instruction::IntToPtr: {
1198 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1200 Value *src_operand = int_to_ptr_inst->getOperand(0);
1213 LLDB_LOGF(log,
"Interpreted an IntToPtr");
1218 case Instruction::PtrToInt: {
1219 const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
1221 Value *src_operand = ptr_to_int_inst->getOperand(0);
1234 LLDB_LOGF(log,
"Interpreted a PtrToInt");
1239 case Instruction::Trunc: {
1240 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1242 Value *src_operand = trunc_inst->getOperand(0);
1260 case Instruction::Load: {
1261 const LoadInst *load_inst = cast<LoadInst>(inst);
1269 const Value *pointer_operand = load_inst->getPointerOperand();
1275 LLDB_LOGF(log,
"LoadInst's value doesn't resolve to anything");
1281 LLDB_LOGF(log,
"LoadInst's pointer doesn't resolve to anything");
1291 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1296 Type *target_ty = load_inst->getType();
1297 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1304 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a LoadInst");
1313 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a LoadInst");
1319 LLDB_LOGF(log,
"Interpreted a LoadInst");
1325 case Instruction::Ret: {
1328 case Instruction::Store: {
1329 const StoreInst *store_inst = cast<StoreInst>(inst);
1337 const Value *value_operand = store_inst->getValueOperand();
1338 const Value *pointer_operand = store_inst->getPointerOperand();
1344 LLDB_LOGF(log,
"StoreInst's value doesn't resolve to anything");
1350 LLDB_LOGF(log,
"StoreInst's pointer doesn't resolve to anything");
1360 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1365 Type *target_ty = value_operand->getType();
1366 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1373 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a StoreInst");
1382 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a StoreInst");
1388 LLDB_LOGF(log,
"Interpreted a StoreInst");
1394 case Instruction::Call: {
1395 const CallInst *call_inst = cast<CallInst>(inst);
1401 llvm::Type *returnType = call_inst->getType();
1402 if (returnType ==
nullptr) {
1404 "unable to access return type");
1409 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1410 !returnType->isPointerTy()) {
1412 "return type is not supported");
1432 const llvm::Value *val = call_inst->getCalledOperand();
1436 "unable to get address of function");
1444 llvm::FunctionType *prototype = call_inst->getFunctionType();
1447 const int numArgs = call_inst->arg_size();
1451 if (numArgs >= 16) {
1453 "function takes too many arguments");
1459 for (
int i = 0; i < numArgs; i++) {
1461 llvm::Value *arg_op = call_inst->getArgOperand(i);
1462 llvm::Type *arg_ty = arg_op->getType();
1465 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1467 "argument %d must be integer type", i);
1475 "unable to evaluate argument %d", i);
1480 if (arg_ty->isPointerTy()) {
1482 size_t dataSize = 0;
1484 bool Success = execution_unit.
GetAllocSize(addr, dataSize);
1487 "unable to locate host data for transfer to device");
1489 rawArgs[i].
size = dataSize;
1490 rawArgs[i].
data_up.reset(
new uint8_t[dataSize + 1]);
1493 execution_unit.
ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1495 assert(!
error.Fail() &&
1496 "we have failed to read the string from memory");
1499 rawArgs[i].
data_up[dataSize] =
'\0';
1505 rawArgs[i].
size = arg_ty->getIntegerBitWidth() / 8;
1512 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1517 exe_ctx.
GetThreadRef(), funcAddr, *prototype, *returnType, args,
1522 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1524 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1533 process->
RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
1538 "ThreadPlanCallFunctionUsingABI failed");
1545 if (returnType->isVoidTy()) {
1549 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1557 if (vobj ==
nullptr || !retVal) {
1559 "unable to get the return value");