[LLVMdev] Dynamic Creation of a simple program

Misha Brukman brukman at uiuc.edu
Wed Mar 16 14:18:29 PST 2005


On Tue, Mar 15, 2005 at 07:28:58PM -0800, xavier wrote:
> I was doing this:
> 
> ========================
>   BasicBlock *BBlock = new BasicBlock("entry", MyFunc);
> ...
>   Value *Zero = ConstantSInt::get(Type::IntTy, 0);
>   Value *UZero = ConstantUInt::get(Type::UIntTy, 0);
> 
>   MallocInst* mi = new MallocInst( STyStru );
>   mi->setName("tmp.0");
>   BBlock->getInstList().push_back( mi );  
>   GetElementPtrInst *m_Next = new GetElementPtrInst(mi, UZero, Zero, "tmp.3", BBlock );
> ...
> ========================
 
Here's the issue: you flipped the order of the signed/unsigned indices.
The first index is signed, it's for addressing into arrays, the second
is the one addressing into structure elements, and must be unsigned, so
you want to do this:

GetElementPtrInst *m_Next =
  new GetElementPtrInst(mi, Zero, UZero, "tmp.3", BBlock );

The getelementptr docs have a good explanation of this and examples of
what you're trying to do:

http://llvm.cs.uiuc.edu/docs/LangRef.html#i_getelementptr

> But I tried your code instead of the lines above:
> 
> GetElementPtrInst *GEP = 
>   new GetElementPtrInst(mi, // %tmp.0
>                         ConstantSInt::get(Type::IntTy, 0),
>                         ConstantUInt::get(Type::UIntTy, 1),
>                         "tmp.3", BBlock);

My code was trying to parallel what you want your target output to be,
and it did not work because your resulting type is { \2 * }, you forgot
to include the integer as the first element, so '1' is not a valid index
into that structure.  Adding the int type into the struct makes it work
for me.

-- 
Misha Brukman :: http://misha.brukman.net :: http://llvm.cs.uiuc.edu




More information about the llvm-dev mailing list