[LLVMdev] Dynamic Creation of a simple program

Misha Brukman brukman at uiuc.edu
Tue Mar 15 18:29:50 PST 2005


On Tue, Mar 15, 2005 at 05:59:06PM -0800, xavier wrote:
> Given these C instructions:
> ==============================
> struct stru { struct stru  *Next; }; 
> struct list  *NewStru = malloc ( sizeof ( struct stru ) );
> struct list  *tmp.3;
> tmp.3 = NewStru->Next;
> ==============================
> LLVM generates something like this:
> %tmp.0 = malloc %struct.stru		; <%struct.stru*>
> %tmp.3 = getelementptr %struct.stru* %tmp.0, int 0, uint 1		; <%struct.stru**>
[snip] 
> How can I do that?

The way you create it parallels the printed instruction quite well: 

Value *V = ...;       // %tmp.0
Instruction *I = ...; // where you want to insert the getelementptr
GetElementPtrInst *GEP = 
  new GetElementPtrInst(V, // %tmp.0
                        ConstantSInt::get(Type::IntTy, 0),
                        ConstantUInt::get(Type::UIntTy, 1),
                        "tmp.3", I);

There are several GetElementPtrInst constructors, some allow you to pass
in a std::vector of indices, some take the indices as arguments
directly.

Hope that helps,
-- 
Misha Brukman :: http://misha.brukman.net :: http://llvm.cs.uiuc.edu




More information about the llvm-dev mailing list