[LLVMdev] creating function pointers

Chris Lattner sabre at nondot.org
Thu Dec 16 22:37:00 PST 2004


On Thu, 16 Dec 2004, Jai Vasanth wrote:

> How do I create function pointers in LLVM? Is it possible to assign a
> function to a function pointer?

This is really easy with LLVM: a Function *is* a function pointer.  Thus 
the following is perfectly legal:

%FP = global void ()* null

implementation

void %func() {
         store void ()* %func, void ()** %FP
         call void %caller( void ()* %func )
         ret void
}

void %caller(void ()* %fp2) {
         call void %fp2( )
         ret void
}

... etc.  You can even play fun games that you can't in C (at least 
without casting).  For example, you can have a function return a pointer 
to itself:

%FPTy = type %FPTy* ()

implementation

%FPTy* %func() {
         ret %FPTy* %func
}

-Chris

-- 
http://nondot.org/sabre/
http://llvm.cs.uiuc.edu/




More information about the llvm-dev mailing list