AllocaInst for FunctionType?

Hi,
I'm trying to get my head around c++ - IR - c++ API and getting used
tramform manual information to code.

The manual states alloca is defined for <type>. FunstionType is a type, so
alloca for functionType should be possible? Not?

If we have a valid Module *m

we can get an allocate instruction allocating space for a non-argumented
function as follows:

AllocaInst* pa2 = new AllocaInst( FunctionType::get(
            Type::getVoidTy(m->getContext()), false )
   , 1, "myName");

but how about nonvar-argumented functions? E.g. I32,I16

std::vector<Type*>FuncTy_args;
FuncTy_args.push_back(IntegerType::get(mod->getContext(), 32));
FuncTy_args.push_back(IntegerType::get(mod->getContext(), 16));

AllocaInst* pa3 = new AllocaInst( FunctionType::get(
    Type::getVoidTy(m->getContext()), FuncTy_args ,false )
    , 1, "MyName");

does this make sense?
I guess this might not be correct, because we explicitely introduce
FuncTy_args, where we only would like to know the 'necessary memory
footprint' of it, for alloca to be able to allocate it, not?

Alex

Hi,
I'm trying to get my head around c++ - IR - c++ API and getting used
tramform manual information to code.

The manual states alloca is defined for <type>. FunstionType is a type, so
alloca for functionType should be possible? Not?

I may not understand what you try to achieve because it does not make much sense to me :slight_smile:

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?).

Maybe this would help: http://llvm.org/docs/tutorial/LangImpl7.html
(and in any case you can check how clang is using alloca to emit llvm IR)

OTH

Mehdi

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