[LLVMdev] cannot understand global c++API code

David Blaikie dblaikie at gmail.com
Sat Mar 7 09:03:24 PST 2015


On Sat, Mar 7, 2015 at 8:37 AM, Alexander Poddey <alexander.poddey at gmx.net>
wrote:

> Hi all,
>
> translating the following c code to llvm c++Api code, I can not understand
> the result.
> Perhaps someone could explain it to me.
>
> c code
> ---------------------------------
>
>  struct stest {
>     int   age;
>     float weight;
>  } foo={44,67.2};
>
>
>
> int main() {
>
> foo.weight=68.2;
>
> ...
> ----------------------------------------
>
> API code
>
> // this is clear
> ConstantFP* const_float_102 = ConstantFP::get(mod->getContext(),
> APFloat(6.820000e+01f));
>
>
> ConstantInt* const_int32_98 = ConstantInt::get(mod->getContext(), APInt(32,
> StringRef("0"), 10));
> ConstantInt* const_int32_99 = ConstantInt::get(mod->getContext(), APInt(32,
> StringRef("1"), 10));
>
>
> std::vector<Constant*> const_ptr_103_indices;
> const_ptr_103_indices.push_back(const_int32_98);
> const_ptr_103_indices.push_back(const_int32_99);
> Constant* const_ptr_103 = ConstantExpr::getGetElementPtr(gvar_struct_foo,
> const_ptr_103_indices);
>
> //the store inst is in principle clear
> StoreInst* void_119 = new StoreInst(const_float_102, const_ptr_103, false,
> label_entry_113);
> void_119->setAlignment(4);
> ---------------------------------------------
>
> comment:
> I don't get whats the meaning of the two const_int32 (98 & 99 above) and
> why
> the const_ptr_103 should relate the foo.weight field of the stest struct.
>

http://llvm.org/docs/GetElementPtr.html

They're indexes into the structure (well, imagine that the pointer to the
struct is a pointer to an array of structs of length 1 - so the first index
is 0, to access the first (and only) element of that array, and the second
index is 1 to access the second element of the structure)


>
>
> Any hint appreciated!
>
> Alex
>
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150307/2ba523b6/attachment.html>


More information about the llvm-dev mailing list