Skip to content

DataLayout class implementation #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yowl opened this issue May 28, 2020 · 2 comments
Open

DataLayout class implementation #140

yowl opened this issue May 28, 2020 · 2 comments

Comments

@yowl
Copy link
Contributor

yowl commented May 28, 2020

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 ?

@tannergooding
Copy link
Member

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.

@yowl
Copy link
Contributor Author

yowl commented May 28, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants