[LLVMdev] use function address as global variable initializer
Xin Tong
trent.tong at gmail.com
Fri Sep 27 18:47:15 PDT 2013
Hi
I would like to initialize a global variable to the address of a function.
Looking at how clang compiles the C code below, i see @main_ptr = global
i8* bitcast (i32 ()* @main to i8*), align 8
but how do i generate i8* bitcast (i32 ()* @main to i8*), align 8 in LLVM
IR and use it as initializer to a GlobalVariable.
I know i will need to use the bitcast instruction. but as far i as i
understand, every instruction in LLVM needs to be created with an insertion
point. what should be the insertion point of this bitcast instruction.
also, do i just give the LLVM main Function* to the bitcast instruction as
its value ?
I am new to LLVM. please help.
Thank you,
Trent
i.e.
* C code *
typedef int (*INVOKE_MAIN)(void);
int main();
void *main_ptr = main;
int main()
{
((INVOKE_MAIN)(main_ptr))();
return 0;
}
*generated LLVM code*
@main_ptr = global i8* bitcast (i32 ()* @main to i8*), align 8
; Function Attrs: nounwind uwtable
define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
store i32 0, i32* %retval
%0 = load i8** @main_ptr, align 8
%1 = bitcast i8* %0 to i32 ()*
%call = call i32 %1()
ret i32 0
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130928/3859b432/attachment.html>
More information about the llvm-dev
mailing list