Generating function pointers in LLVM
I am writing my first LLVM sample. I am trying to build a small LLVM module consisting of a function which takes in a name of a function and returns a pointer to it. The problem is I don't know how to generate function pointers in LLVM. I have got a Function
object by calling getDeclaration(...)
. Is there some way I can get a poi开发者_开发问答nter to it?
Function is a GlobalValue, so it is the pointer by itself. In the meantime, you can use LLVM's C++ backend to generate the C++ API calls which will recreate the IR you're feeding to llc.
For example, feed the following code into http://llvm.org/demo:
void foo(int (*bar)(int));
int factorial(int X);
int main(int argc, char **argv) {
foo(factorial);
}
Make sure you have the "Show LLVM C++ API code" checkbox selected and you'll see the corresponding LLVM IR and the C++ API calls which will recreate it.
精彩评论