[LLVMdev] How to get a pointer to a constant struct?

Duncan Sands baldrick at free.fr
Sat Apr 13 09:31:15 PDT 2013


Hi Callum, if you want to take the address of a constant, then you need to
allocate some memory to hold the constant.  This can be done as follows:
create a global variable (with the type T of your constant), use your constant
as the initial value, and mark the global as being constant (read-only).  You
may want to give it private linkage so it isn't visible from outside of your
compile unit.  The resulting GlobalVariable API object then has type T* and
is the pointer you are looking for.

Ciao, Duncan.

> I'm trying to create a constant (named) struct and store a pointer to it in
> another struct using the C Api (and the llvm-fs bindings). As a contrived
> example, this is what I want to do:
>
> %A = type { %VTable* }
> %VTable = type { void (%A*)*, void (%A*)* }
>
> define void @main(%A* %a) {
> entry:
>    %0 = getelementptr inbounds %A* %a, i32 0, i32 0 ; [type=%VTable**]
>    %2 = somehow make a pointer to: { void(%A*)* null, void (%A*)* @main }
>    store %VTable* %2, %VTable** %1
>    ret void
> }
>
> How would I do the "somehow make a pointer to" part? I currently make the
> constant named struct by using LLVMConstNamedStruct(passing in %VTable, [null,
> @main]) and have tried using LLVMConstGEP with [0] as the indices on this but
> both times I get an assertion error saying:
>
> "Assertion failed: getOperand(0)->getType() ==
> cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be a
> pointer to Val type!", file I
> nstructions.cpp, line 1056"
>
> Cheers,
> Callum Rogers
>
> (I am using LLVM 3.1 btw)
> _______________________________________________
> 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