[LLVMdev] How to handle size_t in front ends?
sabre at nondot.org
Wed May 7 01:18:03 CDT 2008
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.
> 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,
> 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
> of a pointer?
> 2) Assuming the above answer is "no", what's the best way for the
> end to measure the size of a pointer?
It doesn't help for interfacing to C, but you can do things like this:
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.
More information about the LLVMdev