On a related topic: The source-level debugging descriptors require you to know up front what the sizeof pointer types are. Is there any hope of the frontend remaining blissfully unaware of platform details? Chris Lattner wrote:> On May 6, 2008, at 11:12 PM, Talin wrote: > > >> For the most part, it appears that writing a front end can be almost >> entirely platform-independent. For example, my front end doesn't know >> how big a pointer is, and for the most part doesn't care. All of the >> platform-specific aspects of compilation have, up to this point, been >> hidden behind the IR. >> > > Nice. > > >> However, where things get tricky is in calling C functions that take a >> size_t as a parameter. Since a size_t is the same size as a pointer, >> it >> may be either 32 or 64 bits depending on the target platform. All of a >> sudden, I can no longer generate 'target-neutral' IR, but instead must >> introduce into the front end knowledge about the target platform. >> > > Right, this is ugliness of C. :( Remember that even the size of int > (and certainly the size of long!) is target specific as well. This > implies that you cannot generate truly portable interfaces to C code > if they take an int. > > >> So my questions are: >> >> 1) Is there a way to declare an integer type in the IR that represents >> "an int the same size as a pointer" without specifying exactly the >> size >> of a pointer? >> > > No. > > >> 2) Assuming the above answer is "no", what's the best way for the >> front >> end to measure the size of a pointer? >> > > It doesn't help for interfacing to C, but you can do things like this: > http://llvm.org/docs/tutorial/LangImpl8.html#offsetofsizeof > > If you want to generate a constant as a Value* in the IR. > > For emitting the right sized integer type, I'm afraid that your front- > end needs to know the size of the integer. > > > If you are willing to accept an ugly hack that works almost always on > common targets, you can just declare the size_t argument *as being a > pointer*, and use inttoptr to pass it. This works because common ABIs > pass integers and pointers the same way. > > -Chris > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >
On Wed, 21 May 2008, Talin wrote:> On a related topic: The source-level debugging descriptors require you > to know up front what the sizeof pointer types are. Is there any hope of > the frontend remaining blissfully unaware of platform details?I really don't know how to do this. The current debug info stuff depends on emitting size info into the IR. At this point, I don't think there is a good way around this. Improvements to the design are welcome of course. -Chris>> Nice. >> >> >>> However, where things get tricky is in calling C functions that take a >>> size_t as a parameter. Since a size_t is the same size as a pointer, >>> it >>> may be either 32 or 64 bits depending on the target platform. All of a >>> sudden, I can no longer generate 'target-neutral' IR, but instead must >>> introduce into the front end knowledge about the target platform. >>> >> >> Right, this is ugliness of C. :( Remember that even the size of int >> (and certainly the size of long!) is target specific as well. This >> implies that you cannot generate truly portable interfaces to C code >> if they take an int. >> >> >>> So my questions are: >>> >>> 1) Is there a way to declare an integer type in the IR that represents >>> "an int the same size as a pointer" without specifying exactly the >>> size >>> of a pointer? >>> >> >> No. >> >> >>> 2) Assuming the above answer is "no", what's the best way for the >>> front >>> end to measure the size of a pointer? >>> >> >> It doesn't help for interfacing to C, but you can do things like this: >> http://llvm.org/docs/tutorial/LangImpl8.html#offsetofsizeof >> >> If you want to generate a constant as a Value* in the IR. >> >> For emitting the right sized integer type, I'm afraid that your front- >> end needs to know the size of the integer. >> >> >> If you are willing to accept an ugly hack that works almost always on >> common targets, you can just declare the size_t argument *as being a >> pointer*, and use inttoptr to pass it. This works because common ABIs >> pass integers and pointers the same way. >> >> -Chris >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> >> > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-Chris -- http://nondot.org/sabre/ http://llvm.org/
Chris Lattner wrote:> On Wed, 21 May 2008, Talin wrote: > >> On a related topic: The source-level debugging descriptors require you >> to know up front what the sizeof pointer types are. Is there any hope of >> the frontend remaining blissfully unaware of platform details? >> > > I really don't know how to do this. The current debug info stuff depends > on emitting size info into the IR. At this point, I don't think there is > a good way around this. Improvements to the design are welcome of course. > > -Chris >As I understand this, this issue and others like it all require a difficult step to be taken, which is to introduce the concept of a constant whose value is not known until code generation time or at least until the compilation target is fully known. These "late bound constants" could then be used to implement "sizeof(type)" and other constants whose value is different on different targets. On the C++ side, you would have ConstantSize(Type) which would be usable anywhere that you could use ConstantInt, except that you can't actually inspect the integer value of the constant or use it in expressions. That part is fairly simple; It gets more complicated if you want to try defining operators such as sizeof(A) + sizeof(B); This requires the IR to support arbitrary expressions, which I don't think you want. But most of the time, you don't want to add the size of A and B, you want sizeof({A, B}), which doesn't require any special syntax other than sizeof itself. So in other words, the frontend sees the "sizeof" constant purely as a symbolic, opaque object, while the code generator simply converts it into a ConstantInt. This makes filling in the dwarf debugging structures relatively easy as long as you have an LLVM type reference to use as a measuring stick. In fact, I'd likely make the hypothetical "DebugBuilder" API such that most of the info was derived from an LLVM type given as a parameter, with just a few additional parameters to specify the things that cannot be determined just from looking at the the LLVM type. -- Talin