[LLVMdev] size of const_ptr for array index

Mehdi Amini
Mon, 16 Mar 2015 19:47:44 -0000

Hi,


> On Mar 16, 2015, at 12:44 PM, Alexander Poddey <alexander.poddey at
gmx.net> wrote:
>
> Hi all,
>
> when e.g. accessing a global variable residing in a structure, the c++API
> code looks like:
>
> std::vector<Constant*> const_ptr_183_indices;
> const_ptr_183_indices.push_back(const_int32_172);
> const_ptr_183_indices.push_back(const_int32_184);
> const_ptr_183_indices.push_back(const_int64_175);
> Constant* const_ptr_183 = ConstantExpr::getGetElementPtr(gvar_struct_foo,
> const_ptr_183_indices);
>
>
> the struct could be
>
> struct stest {
>    double age;
>   double darray[3];
> }
>
> The indices for accessing 'age' would be
> 0 (32bit)
> 0 (32bit).
>
> For an element of the darray, one however needs
> 0 (32bit)
> 1 (32bit)
> 0-2 (64bit)
>
> The same for indices of vectors.
>
> The question:
> Can I at runtime determine (how?) which size I have to use per case?
> I made some tests but could not figure it out.

I believe you have to check the pointer size in the DataLayout.

Mehdi



>
> So for now (at my architecture) it seems hardcoding 32 bit as default, and
> 64bit for indices (of arrays and vectors) works. But this is not portable.
>
> Thanks
> Alexander