LLVMSharp icon indicating copy to clipboard operation
LLVMSharp copied to clipboard

DataLayout class implementation

Open yowl opened this issue 5 years ago • 2 comments

I'm a little confused about how this is implemented in LLVMSharp. It's possible to set the DataLayout string, but the function getDataLayout that returns an object with methods as per https://stackoverflow.com/questions/32299166/accessing-struct-members-and-arrays-of-structs-from-llvm-ir is not there and so I thought I'd try to add it. But the methods already seem to be used for getting and setting the layout string

        [DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLVMGetDataLayout", ExactSpelling = true)]
        [return: NativeTypeName("const char *")]
        public static extern sbyte* GetDataLayout([NativeTypeName("LLVMModuleRef")] LLVMOpaqueModule* M);

        [DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLVMSetDataLayout", ExactSpelling = true)]
        public static extern void SetDataLayout([NativeTypeName("LLVMModuleRef")] LLVMOpaqueModule* M, [NativeTypeName("const char *")] sbyte* DataLayoutStr);

So how would one go about adding support for the DataLayout class http://llvm.org/doxygen/classllvm_1_1DataLayout.html ?

yowl avatar May 28 '20 15:05 yowl

llvm::DataLayout is the C++ API while LLVMGetDataLayout and related functions are part of the C API. Given we bind over the C API, we can only loosely reconstruct the C++ API and we have currently only done that for a limited subset of types.

In order to expose more closely mirroring the C++ API, there would need to be a Module class which wraps the LLVMModuleRef and which exposes a DataLayoutStr property that internally calls GetDataLayoutStr and SetDataLayoutStr.

  • Noting GetDataLayout and GetDataLayoutStr are the "same" but the former is deprecated according to the docs

Likewise the various llvm::DataLayout APIs could be more accurately exposed by wrapping the LLVMTargetDataRef and exposing the appropriate underlying functions.

tannergooding avatar May 28 '20 17:05 tannergooding

Ok, but I'd have to add the methods to LLVMTargetDataRef anyway so what is the wrapper giving, e.g.

in LLVMTargetDataRef

        public ulong OffsetOfElement(LLVMTypeRef type, uint element)
        {
            return LLVM.OffsetOfElement(this, type, element);
        }

and then a wrapper:

    public class LLVMTargetData
    {
        private readonly LLVMTargetDataRef _llvmTargetDataRef;

        public LLVMTargetData(LLVMTargetDataRef llvmTargetDataRef)
        {
            _llvmTargetDataRef = llvmTargetDataRef;
        }

        public ulong OffsetOfElement(LLVMTypeRef type, uint element)
        {
            return _llvmTargetDataRef.OffsetOfElement(type, element);
        }
    }

Maybe it would be easier if I just fill it all out and create a PR and you can comment there?

yowl avatar May 28 '20 23:05 yowl