[LLVMdev] Dynamic Creation of a simple program

xavier llvmdev at yahoo.com
Tue Mar 15 19:28:58 PST 2005


Hi Misha,

Thanks for your answer

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 );
...
========================

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);
  
Since the malloc the first instruction, I don't want to insert the getelementptr before it, so I
used the basic block instead. It keeps giving me segmentation faults.

If I comment the getelementptr, no such error is printed. With these two:

  MallocInst* mi = new MallocInst( STyStru );
  mi->setName("tmp.0");

This line is correctly generated:

	%tmp.0 = malloc %struct.list		; <%struct.list*>

Probably it's something obvious but I am not familiar enough with the LLVM classes

Thanks for your help!








--- Misha Brukman <brukman at uiuc.edu> wrote:
> 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
> 
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev
> 


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/ 




More information about the llvm-dev mailing list