[LLVMdev] IR blocks for calling function pointers

Avinash Chiganmi avinash.ca at gmail.com
Mon Mar 23 22:19:10 PDT 2015


Yes. That is exactly what I was looking for. I tried it, and it works.
Thanks, Tim!

On Mon, Mar 23, 2015 at 8:49 PM, Tim Northover <t.p.northover at gmail.com>
wrote:

> Hi,
>
> On 23 March 2015 at 19:04, Avinash Chiganmi <avinash.ca at gmail.com> wrote:
> >   %4 = load void (i64)** @foo_ptr, align 8, !dbg !26
> >   %call = call i32 %4(i32 %3), !dbg !26
> >
> > What LLVM function calls should be used to generate the above IR blocks?
>
> I think the key thing you should notice in the "correct" code is that
> @foo_ptr isn't actually a function. It's a generic global with type
> void(i64)* (which means, as with all other LLVM globals that the
> symbol @foo_ptr has type void(i64)** and you must load it to get the
> actual value of the function pointer). If you look at the module where
> it's defined you'll see something like
>
>     @foo_ptr = global void(i64)* null
>
> instead of a function definition. So you'd get a handle to it via a call
> like
>
>     auto FooFunctionPtr = M.getOrInsertGlobal("foo_ptr",
> PointerType::get(FunctionType::get(IRB.getVoidTy(), FooArgs, false)));
>
> after that (and given that FooFunctionPtr is a Value with type
> "void(i64)**") there's really only one sane set of operations you can
> perform on it:
>
>     auto FooFunction = IRB.getLoad(FooFunctionPtr)
>     IRB.CreateCall(FooFunction, IRB.CreateLoad(LenAlloca));
>
> Does that help?
>
> Cheers.
>
> Tim.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150323/b4f1c653/attachment.html>


More information about the llvm-dev mailing list