[LLVMdev] Question on using GlobalVariable as function pointer

Duncan Sands baldrick at free.fr
Mon Jan 7 09:19:56 PST 2013


Hi Chia Lun Liu,

On 07/01/13 03:38, ChiaLun wrote:
> Hi everyone,
>
>    I am trying to have an call instruction that calls global variable
>
>
> The first thing I do is
>
> gv = new llvm::GlobalVariable( *(tb->getModule()),
> 		                          fty,
> 		                          false,GlobalValue::InternalLinkage,0,
> 		                          "",0,0,0);
>
> fty : i32 ({ i32, [32 x i32] }*)
>
>
> I get stuck here, the fifth argument is the initializer which is a LLVM
> Constant,
>
> I do not know how to describe a function pointer as a constant.
>
> so currently, I just pass false to it.

If Func is the LLVM object of Function* type that represents the function you
want to call, then you can use it as the initial value.  The Function class
derives from the GlobalValue class, and the GlobalValue class derives from the
Constant class, thus anything of type Function* can be used wherever a Constant*
is required.

Ciao, Duncan.

>
> then I use
>
> myjit->getOrEmitGlobalVariable(gv); // myjit is LLVM ExecutionEngine


>
>
> but, I get an error
>
> LLVM ERROR: Could not resolve external global address
>
> However, when I modify fty to be an integer type or some primitive type,
> there is no error.
>
> Since LLVM demo page is closed, so I am not sure how to do it correctly.
>
>
> Have A Nice Day
>
> Chia Lun Liu
>
>
>
> --
> View this message in context: http://llvm.1065342.n5.nabble.com/Question-on-using-GlobalVariable-as-function-pointer-tp53187.html
> Sent from the LLVM - Dev mailing list archive at Nabble.com.
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>




More information about the llvm-dev mailing list