Hi! LLVM has a class, ConstantExpr, that is very handy for compile-time evaluation of const expressions. Unfortunately I cannot find any methods in it that would be helpful in evaluation of expressions similar to this: (uintptr_t)(&(*(TYPE*)0).FIELD), which is basically the implementation of the offsetof(TYPE, FIELD) macro. Specifically, there seem to be no provisions for dereferencing a pointer. Does LLVM have any facilities (that I missed), that would help language front-ends in dealing with this sort of expressions? Obviously, clang does it somehow, but so far I was not able to locate the relevant bit of code. Any pointers would be appreciated! Vadim -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150102/50e79b20/attachment.html>
On Fri, Jan 2, 2015 at 1:58 AM, Vadim Chugunov <vadimcn at gmail.com> wrote:> Hi! > LLVM has a class, ConstantExpr, that is very handy for compile-time > evaluation of const expressions. Unfortunately I cannot find any methods > in it that would be helpful in evaluation of expressions similar to this: > (uintptr_t)(&(*(TYPE*)0).FIELD), which is basically the implementation of > the offsetof(TYPE, FIELD) macro. >I think the closest entity for this sort of thing is LLVM's ConstantExpr::getOffsetOf.> Specifically, there seem to be no provisions for dereferencing a pointer. > > Does LLVM have any facilities (that I missed), that would help language > front-ends in dealing with this sort of expressions? > Obviously, clang does it somehow, but so far I was not able to locate the > relevant bit of code. Any pointers would be appreciated! >Clang handles record types in a very abstract way, it doesn't rely on LLVM IR at any level. An ASTRecordLayout is built for a record type and the ASTRecordLayout::getFieldIndex method is used to determine the offset for a particular field.> > > Vadim > > _______________________________________________ > 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/20150102/01345fbf/attachment.html>
On Fri, Jan 2, 2015 at 2:20 AM, David Majnemer <david.majnemer at gmail.com> wrote:> > > On Fri, Jan 2, 2015 at 1:58 AM, Vadim Chugunov <vadimcn at gmail.com> wrote: > >> Hi! >> LLVM has a class, ConstantExpr, that is very handy for compile-time >> evaluation of const expressions. Unfortunately I cannot find any methods >> in it that would be helpful in evaluation of expressions similar to this: >> (uintptr_t)(&(*(TYPE*)0).FIELD), which is basically the implementation of >> the offsetof(TYPE, FIELD) macro. >> > > I think the closest entity for this sort of thing is LLVM's > ConstantExpr::getOffsetOf. >Certainly, but I think this requires either making `offsetof` a first-class operation in the source language, or handling the folding of <pointer deref>-<field select>-<get-address-of> combo in the front-end.> Specifically, there seem to be no provisions for dereferencing a pointer. >> >> Does LLVM have any facilities (that I missed), that would help language >> front-ends in dealing with this sort of expressions? >> Obviously, clang does it somehow, but so far I was not able to locate the >> relevant bit of code. Any pointers would be appreciated! >> > > Clang handles record types in a very abstract way, it doesn't rely on LLVM > IR at any level. An ASTRecordLayout is built for a record type and the > ASTRecordLayout::getFieldIndex method is used to determine the offset for a > particular field. >So it handles all const expression evaluation in the front-end? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150102/7f2d6688/attachment.html>