[LLVMdev] AllocaInst for FunctionType?
Alexander Poddey
alexander.poddey at gmx.net
Thu Jan 15 05:04:48 PST 2015
> As far as I understand, an alloca instruction allocate space for a
> variable. It does not really make sense for it to be used with a function
> type (do you want to store a pointer to a function of this type?).
Yes. if I undertand correctly, alloca allocates space for a variable.
I could then want to allocate space for a function pointer.
I can now see code similar to my example by using c function pointers and
emitting c++API code:
When translating c++ code using function pointers, the emitted llvm for
int (*foo2)(double,float);
looks like
%foo2 = alloca i32 (double, float)*, align 8
and the c++ API code (generated by llc -march=cpp)
std::vector<Type*>FuncTy_49_args;
FuncTy_49_args.push_back(Type::getDoubleTy(mod->getContext()));
FuncTy_49_args.push_back(Type::getFloatTy(mod->getContext()));
FunctionType* FuncTy_49 = FunctionType::get(
/*Result=*/IntegerType::get(mod->getContext(), 32),
/*Params=*/FuncTy_49_args,
/*isVarArg=*/false);
PointerType* PointerTy_48 = PointerType::get(FuncTy_49, 0);
AllocaInst* ptr_foo2 = new AllocaInst(PointerTy_48, "foo2", label_entry_71);
This is what I had in my 1st post. So this seems to be the way to do it.
This vice versa transformation between source, IR and API is quite cool!
Alex
More information about the llvm-dev
mailing list