[LLVMdev] creating and inserting a function with variable arguments

Óscar Fuentes ofv at wanadoo.es
Tue Apr 16 10:41:02 PDT 2013


Akshay Jain <jivan.molu at gmail.com> writes:

> The code that I have written to get the function type is:
>
> const std::vector<Type *> ParamTys;

You are declaring a constant vector...

> ParamTys.push_back(IntegerType::getInt32Ty(Context));

... and then you try to modify it by pushing back an element. Remove the
`const' from the std::vector declaration.

> ParamTys.push_back(IntegerType::getInt32Ty(Context));
> ParamTys.push_back(IntegerType::getInt32Ty(Context));
> ParamTys.push_back(PointerType::get(Type::getVoidTy(Context), 0));

There is no "pointer to void" in the LLVM type system. Use a pointer to
byte:

ParamTys.push_back(PointerType::get(Type::getInt8Ty(Context), 0));

> ParamTys.push_back(PointerType::get(Type::getVoidTy(Context), 0));

Same.

> FunctionType *ftype = FunctionType::get(Type::getVoidTy(Context), ParamTys,
> true);

This looks good.

> And the errors are:

[snip]

> It may be a silly mistake, but I am quite new to c and c++ coding
> especially in llvm. So I will be very grateful if you can help out.

Using LLVM without a solid background in C++ is a tough task.

You don't need to know about template metaprogramming, but an
understanding of classes (and the C++ type system in general) and some
familiarity with the idioms of the C++ Standard Library is essential.

A good method for familiarizing yourself with the LLVM API is to feed
clang with some C or C++ source code that uses the feature you are
interested in and then use llc for obtaining the LLVM C++ code that
generates the corresponding LLVM intermediate representation. For this
specific case:

/* foo.c */
void foo(int, int, void*, void*, ...) {
  return;
}
/* end of foo.c */

clang -emit-llvm -c foo.c -o foo.ll
llc -march=cpp foo.ll -o foo.ll.cpp

Now look into foo.ll.cpp




More information about the llvm-dev mailing list