[cfe-dev] Best way of implement a fat pointer for C

Nicholas Krause via cfe-dev cfe-dev at lists.llvm.org
Mon Jan 6 19:28:42 PST 2020



On 1/6/20 9:51 PM, Jie Zhou via cfe-dev wrote:
> Dear All,
>
> I’m working on a project that extends C. I’m adding a new type of pointer
> that is a fat pointer. It has some metadata about the pointed object 
> besides
> the starting address of the object. Currently I implemented this 
> pointer as
> an llvm:StructType. In llvm::Type generation function
> /llvm::Type *CodeGenTypes::ConvertType(QualType T)/
> in the case for /clang::Type::Pointer/, instead of creating an 
> llvm::PointerType
> I create an llvm::StructType type for this new type of pointer. And I 
> added some
> helper code in llvm::StructType and in multiple places I added code to 
> trick
> the compiler to believe sometimes a struct is actually a pointer. 
> Until now
> it compile test programs fine with -O0 but I got lots of assertion 
> failures when
> compiling with -O1 or -O2 majorly because of the confusion of type 
> mismatch.
>
> LLVM assumes that a PointerType is essentially an Integer (32 or 64 
> bit depending
> on the architecture), and since this is quite a fundamental 
> assumption, I started
> to question whether my way of implementing the fat pointer is feasible.
> I thought about adding a new llvm type that inherits both llvm:PointerType
> and llvm:StructType; but I’m not sure if this is the correct path. It 
> looks like
> this demands substantial changes to the compiler including adding code
> for bitcode generation. Can you give me some advice on how to implement
> a fat pointer in llvm?
>
> Thanks,
> - Jie
Jie,

I'm no expert but LLVM does do function pointers as a pointer type with 
extra data in IR.
I just checked and it's mentioned in Global Variables and Function 
Addresses of the LLVM
IR manual. Maybe exploring how  that is implemented as it seems similar 
would be a place
to start. The data would be different i.e. no function arguments as its 
a data object but the
ideas should be similar in that its a pointer with more meta information 
than a normal pointer.

Maybe that helps,

Nick
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200106/c572c300/attachment-0001.html>


More information about the cfe-dev mailing list