Dynamic Creation of a simple program

Thanks for the information

I am trying to use one of your examples for recursive data structures:

Thanks for the information
I am trying to use one of your examples for recursive data structures:

=========================
PATypeHolder StructTy = OpaqueType::get();
std::vector<const Type*> Elts;
Elts.push_back(PointerType::get(StructTy));
Elts.push_back(PointerType::get(Type::SByteTy));
StructType *NewSTy = StructType::get(Elts);

// At this point, NewSTy = "{ opaque*, sbyte* }", tell VMCore that
// the struct and the opaque type are actually the same.
cast<OpaqueType>(StructTy.get())->refineAbstractTypeTo(NewSTy);

// NewSTy is potentially invalidated, but StructTy (a PATypeHolder) is
// kept up-to-date.
NewSTy = StructTy.get();

It gives this error in the last line:

        invalid conversion from `llvm::Type*' to `llvm::StructType*'
How can I create such a recursive DS?

Replace the last line with:

   NewSTy = cast<StructType>(StructTy.get());

-Chris

Hi,

Given these C instructions:

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,