<div dir="ltr"><div class="gmail_default" style="font-family:'courier new',monospace">Hi </div><div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default"><span style="font-family:'courier new',monospace">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 </span><span style="font-family:'courier new',monospace">@main_ptr = global i8* bitcast (i32 ()* @main to i8*), align 8</span></div>
<div><br></div><div><div class="gmail_default" style="font-family:'courier new',monospace">but how do i generate i8* bitcast (i32 ()* @main to i8*), align 8 in LLVM IR and use it as initializer to a GlobalVariable.</div>
<div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default" style="font-family:'courier new',monospace">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 ?</div>
<div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default" style="font-family:'courier new',monospace">I am new to LLVM. please help.</div><div class="gmail_default" style="font-family:'courier new',monospace">
<br></div><div class="gmail_default" style="font-family:'courier new',monospace">Thank you,</div><div class="gmail_default" style="font-family:'courier new',monospace">Trent</div><br></div><div class="gmail_default" style="font-family:'courier new',monospace">
<br></div><div class="gmail_default" style="font-family:'courier new',monospace">i.e. </div><div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default" style="font-family:'courier new',monospace">
* C code * </div><div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default"><div class="gmail_default"><font face="courier new, monospace">typedef int (*INVOKE_MAIN)(void);</font></div>
<div class="gmail_default"><font face="courier new, monospace"><br></font></div><div class="gmail_default"><font face="courier new, monospace">int main();</font></div><div class="gmail_default"><font face="courier new, monospace"><br>
</font></div><div class="gmail_default"><font face="courier new, monospace">void *main_ptr = main;</font></div><div class="gmail_default"><font face="courier new, monospace"><br></font></div><div class="gmail_default"><font face="courier new, monospace">int main()</font></div>
<div class="gmail_default"><font face="courier new, monospace">{</font></div><div class="gmail_default"><font face="courier new, monospace"> ((INVOKE_MAIN)(main_ptr))();</font></div><div class="gmail_default"><font face="courier new, monospace"> return 0;</font></div>
<div class="gmail_default"><font face="courier new, monospace">}</font></div><div style="font-family:'courier new',monospace"><br></div><div style="font-family:'courier new',monospace"><br></div><div style="font-family:'courier new',monospace">
*generated LLVM code*</div><div style="font-family:'courier new',monospace"><br></div><div><div><font face="courier new, monospace">@main_ptr = global i8* bitcast (i32 ()* @main to i8*), align 8</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">; Function Attrs: nounwind uwtable</font></div><div><font face="courier new, monospace">define i32 @main() #0 {</font></div><div><font face="courier new, monospace">entry:</font></div>
<div><font face="courier new, monospace"> %retval = alloca i32, align 4</font></div><div><font face="courier new, monospace"> store i32 0, i32* %retval</font></div><div><font face="courier new, monospace"> %0 = load i8** @main_ptr, align 8</font></div>
<div><font face="courier new, monospace"> %1 = bitcast i8* %0 to i32 ()*</font></div><div><font face="courier new, monospace"> %call = call i32 %1()</font></div><div><font face="courier new, monospace"> ret i32 0</font></div>
<div><font face="courier new, monospace">}</font></div><div style="font-family:'courier new',monospace"><br></div></div></div></div>