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 if (alloca_inst->isArrayAllocation()) {
889 "AllocaInsts are not handled if isArrayAllocation() is true");
901 Type *T = alloca_inst->getAllocatedType();
902 Type *Tptr = alloca_inst->getType();
907 LLDB_LOGF(log,
"Couldn't allocate memory for an AllocaInst");
916 "Couldn't allocate the result pointer for an AllocaInst");
926 LLDB_LOGF(log,
"Couldn't write the result pointer for an AllocaInst");
929 execution_unit.
Free(P, free_error);
930 execution_unit.
Free(R, free_error);
937 LLDB_LOGF(log,
"Interpreted an AllocaInst");
942 case Instruction::BitCast:
943 case Instruction::ZExt: {
944 const CastInst *cast_inst = cast<CastInst>(inst);
946 Value *source = cast_inst->getOperand(0);
958 case Instruction::SExt: {
959 const CastInst *cast_inst = cast<CastInst>(inst);
961 Value *source = cast_inst->getOperand(0);
977 case Instruction::Br: {
978 const BranchInst *br_inst = cast<BranchInst>(inst);
980 if (br_inst->isConditional()) {
981 Value *condition = br_inst->getCondition();
992 frame.
Jump(br_inst->getSuccessor(0));
994 frame.
Jump(br_inst->getSuccessor(1));
997 LLDB_LOGF(log,
"Interpreted a BrInst with a condition");
1002 frame.
Jump(br_inst->getSuccessor(0));
1005 LLDB_LOGF(log,
"Interpreted a BrInst with no condition");
1010 case Instruction::PHI: {
1011 const PHINode *phi_inst = cast<PHINode>(inst);
1014 "Encountered PHI node without having jumped from another "
1021 Value *value = phi_inst->getIncomingValueForBlock(frame.
m_prev_bb);
1031 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);
1090 LLDB_LOGF(log,
"Interpreted a GetElementPtrInst");
1096 case Instruction::FCmp:
1097 case Instruction::ICmp: {
1098 const CmpInst *icmp_inst = cast<CmpInst>(inst);
1100 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1102 Value *lhs = inst->getOperand(0);
1103 Value *rhs = inst->getOperand(1);
1122 switch (predicate) {
1125 case CmpInst::ICMP_EQ:
1126 case CmpInst::FCMP_OEQ:
1129 case CmpInst::ICMP_NE:
1130 case CmpInst::FCMP_UNE:
1133 case CmpInst::ICMP_UGT:
1138 case CmpInst::ICMP_UGE:
1143 case CmpInst::FCMP_OGE:
1146 case CmpInst::FCMP_OGT:
1149 case CmpInst::ICMP_ULT:
1154 case CmpInst::FCMP_OLT:
1157 case CmpInst::ICMP_ULE:
1162 case CmpInst::FCMP_OLE:
1165 case CmpInst::ICMP_SGT:
1170 case CmpInst::ICMP_SGE:
1175 case CmpInst::ICMP_SLT:
1180 case CmpInst::ICMP_SLE:
1190 LLDB_LOGF(log,
"Interpreted an ICmpInst");
1196 case Instruction::IntToPtr: {
1197 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1199 Value *src_operand = int_to_ptr_inst->getOperand(0);
1212 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);
1233 LLDB_LOGF(log,
"Interpreted a PtrToInt");
1238 case Instruction::Trunc: {
1239 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1241 Value *src_operand = trunc_inst->getOperand(0);
1259 case Instruction::Load: {
1260 const LoadInst *load_inst = cast<LoadInst>(inst);
1268 const Value *pointer_operand = load_inst->getPointerOperand();
1274 LLDB_LOGF(log,
"LoadInst's value doesn't resolve to anything");
1280 LLDB_LOGF(log,
"LoadInst's pointer doesn't resolve to anything");
1290 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1295 Type *target_ty = load_inst->getType();
1296 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1303 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a LoadInst");
1312 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a LoadInst");
1318 LLDB_LOGF(log,
"Interpreted a LoadInst");
1324 case Instruction::Ret: {
1327 case Instruction::Store: {
1328 const StoreInst *store_inst = cast<StoreInst>(inst);
1336 const Value *value_operand = store_inst->getValueOperand();
1337 const Value *pointer_operand = store_inst->getPointerOperand();
1343 LLDB_LOGF(log,
"StoreInst's value doesn't resolve to anything");
1349 LLDB_LOGF(log,
"StoreInst's pointer doesn't resolve to anything");
1359 LLDB_LOGF(log,
"Couldn't read the address to be loaded for a LoadInst");
1364 Type *target_ty = value_operand->getType();
1365 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1372 LLDB_LOGF(log,
"Couldn't read from a region on behalf of a StoreInst");
1381 LLDB_LOGF(log,
"Couldn't write to a region on behalf of a StoreInst");
1387 LLDB_LOGF(log,
"Interpreted a StoreInst");
1393 case Instruction::Call: {
1394 const CallInst *call_inst = cast<CallInst>(inst);
1400 llvm::Type *returnType = call_inst->getType();
1401 if (returnType ==
nullptr) {
1403 "unable to access return type");
1408 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1409 !returnType->isPointerTy()) {
1411 "return type is not supported");
1431 const llvm::Value *val = call_inst->getCalledOperand();
1435 "unable to get address of function");
1443 llvm::FunctionType *prototype = call_inst->getFunctionType();
1446 const int numArgs = call_inst->arg_size();
1450 if (numArgs >= 16) {
1452 "function takes too many arguments");
1458 for (
int i = 0; i < numArgs; i++) {
1460 llvm::Value *arg_op = call_inst->getArgOperand(i);
1461 llvm::Type *arg_ty = arg_op->getType();
1464 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1466 "argument %d must be integer type", i);
1474 "unable to evaluate argument %d", i);
1479 if (arg_ty->isPointerTy()) {
1481 size_t dataSize = 0;
1483 bool Success = execution_unit.
GetAllocSize(addr, dataSize);
1486 "unable to locate host data for transfer to device");
1488 rawArgs[i].
size = dataSize;
1489 rawArgs[i].
data_up.reset(
new uint8_t[dataSize + 1]);
1492 execution_unit.
ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1494 assert(!
error.Fail() &&
1495 "we have failed to read the string from memory");
1498 rawArgs[i].
data_up[dataSize] =
'\0';
1504 rawArgs[i].
size = arg_ty->getIntegerBitWidth() / 8;
1511 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1516 exe_ctx.
GetThreadRef(), funcAddr, *prototype, *returnType, args,
1521 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1523 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1532 process->
RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
1537 "ThreadPlanCallFunctionUsingABI failed");
1544 if (returnType->isVoidTy()) {
1548 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1556 if (vobj ==
nullptr || !retVal) {
1558 "unable to get the return value");