[LLVMdev] Size of structs & arrays

Eli Friedman
Wed, 22 Feb 2012 22:02:46 -0000

On Wed, Feb 22, 2012 at 1:56 PM, Fraser Cormack <frasercrmck at gmail.com>
wrote:
>
> I'm trying to work out the size of a struct so that I can pass this to
the
> 'llvm.memcpy' intrinsic. It's easy to find out how I'm
supposed to do this,
> as I keep seeing the following:
>
> %Size = getelementptr %T* null, int 1
> %SizeI = cast %T* %Size to uint
>
> But I'm unsure how I actually do this in the C++ API. Is the
'null' here a
> llvm::ConstantPointerNull? Any help here would be much appreciated.

Try llvm::Constant::getNullValue().

> Additionally, I was wondering whether I can use this method to find out the
> size of variable-length arrays, such as [0 x i32], for example? I've
tried
> doing this and am obviously doing something wrong.
>
> %Size = getelementptr [0 x i32]* %1, i32* null, i32 1
> %SizeI = ptrtoint i32* %Size to i64
>
> This gives entirely the wrong result, and I'm not surprised, as it
doesn't
> look correct. Should I instead be implementing these arrays with a size
> associated with them? Something like { i32, [0 x i32]}, for instance?

Yes; [0 x i32] isn't really a variable-length array, just a
placeholder for an array of unknown size.  You have to track the size
yourself.

-Eli