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. Any hint appreciated! Alex
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>
Hi David, Ok, got it, thank you! When extending the struct to something including doubles, the API code uses const_ptr_xy_indices.push_back(const_int64_yz). Can I tell (programmatically) from the Type* if I should use a 32 or 64 bit int as ConstantInt* - is it the sizeof the Type* pointer? Is there a 'shortcut' to programmatically accessing a globals sub-field? Lets say that I know that the sequence would be '0', '1', '3'. Can I circumvent creating the Constant* vector, calling something like getGetElementPtr on a global using the integer values? Another question would be if I could use the Constant * retrieved from Constant* const_ptr_110 = ConstantExpr::getGetElementPtr(gvar_struct_foo, const_ptr_110_indices); just like I do when using Instruction results as operands to other instructions; something like: Value* gv=cast<Value>(const_ptr_110); MyInst->setOperand(2, gv); // replace 3rd operand by global's data Thank you! Alexander David Blaikie wrote:> 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 >>