[LLVMdev] Size of structs & arrays

Eli Friedman eli.friedman at gmail.com
Wed Feb 22 16:41:46 PST 2012


On Wed, Feb 22, 2012 at 3:54 PM, Fraser Cormack <frasercrmck at gmail.com> wrote:
>
>
> Eli Friedman-2 wrote:
>>
>>
>> Try llvm::Constant::getNullValue().
>>
>>
>
> I'm trying this:
>
> llvm::Constant* one =
> llvm::Constant::getNullValue(llvm::IntegerType::get(mod->getContext(), 64));
> llvm::ConstantInt* two = llvm::ConstantInt::get(mod->getContext(),
> llvm::APInt(32, llvm::StringRef("1"), 10));
>
> std::vector<llvm::Value*> indices;
> indices.push_back(one);  indices.push_back(two);
>
> llvm::Value* size = builder->CreateGEP(struct_ptr, indices);
> llvm::Value* size_int = builder->CreatePtrToInt(size,
> llvm::IntegerType::get(mod->getContext(), 64))
>
> And I'm getting the assertion error "Invalid GetElementPtrInst indices for
> type!"'. 'struct_ptr' is a pointer to the struct I'm trying to size. I've
> also tried using getNullValue on the struct's own type.

Try something more like:

llvm::Constant* base = llvm::Constant::getNullValue(struct_ptr->getType());
llvm::Value* size = builder->CreateConstGEP1_32(base, 1);

-Eli




More information about the llvm-dev mailing list