Skip to content

Add methods for LLVMTargetDataRef #141

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

Merged
merged 8 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions sources/LLVMSharp/DataLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;
using LLVMSharp.Interop;

namespace LLVMSharp
{
public sealed class DataLayout : IEquatable<DataLayout>
{
public DataLayout(ReadOnlySpan<char> stringRep)
{
Handle = LLVMTargetDataRef.FromStringRepresentation(stringRep);
}

public LLVMTargetDataRef Handle { get; }

public StructLayout GetStructLayout(StructType structType)
{
return new StructLayout(this, structType);
}

public ulong GetTypeSizeInBits(Type type)
{
return Handle.SizeOfTypeInBits(type.Handle);
}

public ulong GetTypeStoreSize(Type type)
{
return Handle.StoreSizeOfType(type.Handle);
}

public ulong GetTypeAllocSize(Type type)
{
return Handle.ABISizeOfType(type.Handle);
}

public uint GetABITypeAlignment(Type type)
{
return Handle.ABIAlignmentOfType(type.Handle);
}

public uint GetPrefTypeAlignment(Type type)
{
return Handle.PreferredAlignmentOfType(type.Handle);
}

public uint GetPreferredAlign(Value value)
{
return Handle.PreferredAlignmentOfGlobal(value.Handle);
}

public static bool operator ==(DataLayout left, DataLayout right) => (left is object) ? ((right is object) && (left.Handle == right.Handle)) : (right is null);

public static bool operator !=(DataLayout left, DataLayout right) => (left is object) ? ((right is null) || (left.Handle != right.Handle)) : (right is object);

public override bool Equals(object obj) => (obj is DataLayout other) && Equals(other);

public bool Equals(DataLayout other) => this == other;

public override int GetHashCode()
{
return Handle.GetHashCode();
}
}
}
50 changes: 50 additions & 0 deletions sources/LLVMSharp/Interop.Extensions/LLVMTargetDataRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public LLVMTargetDataRef(IntPtr handle)
Handle = handle;
}

public static LLVMTargetDataRef FromStringRepresentation(ReadOnlySpan<char> stringRep)
{
return LLVM.CreateTargetData(new MarshaledString(stringRep));
}

public IntPtr Handle;

public static implicit operator LLVMTargetDataRef(LLVMOpaqueTargetData* TargetData)
Expand All @@ -32,5 +37,50 @@ public static implicit operator LLVMTargetDataRef(LLVMOpaqueTargetData* TargetDa
public bool Equals(LLVMTargetDataRef other) => Handle == other.Handle;

public override int GetHashCode() => Handle.GetHashCode();

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

public ulong ElementAtOffset(LLVMTypeRef type, ulong offset)
{
return LLVM.ElementAtOffset(this, type, offset);
}

public ulong SizeOfTypeInBits(LLVMTypeRef type)
{
return LLVM.SizeOfTypeInBits(this, type);
}

public ulong StoreSizeOfType(LLVMTypeRef type)
{
return LLVM.StoreSizeOfType(this, type);
}

public ulong ABISizeOfType(LLVMTypeRef type)
{
return LLVM.ABISizeOfType(this, type);
}

public uint ABIAlignmentOfType(LLVMTypeRef type)
{
return LLVM.ABIAlignmentOfType(this, type);
}

public uint CallFrameAlignmentOfType(LLVMTypeRef type)
{
return LLVM.CallFrameAlignmentOfType(this, type);
}

public uint PreferredAlignmentOfType(LLVMTypeRef type)
{
return LLVM.PreferredAlignmentOfType(this, type);
}

public uint PreferredAlignmentOfGlobal(LLVMValueRef globalVar)
{
return LLVM.PreferredAlignmentOfGlobal(this, globalVar);
}
}
}
41 changes: 41 additions & 0 deletions sources/LLVMSharp/StructLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;

namespace LLVMSharp
{
public sealed class StructLayout : IEquatable<StructLayout>
{
private readonly DataLayout _dataLayout;
private readonly StructType _structType;

internal StructLayout(DataLayout dataLayout, StructType structType)
{
_dataLayout = dataLayout;
_structType = structType;
}

public ulong OffsetOfElement(uint element)
{
return _dataLayout.Handle.OffsetOfElement(_structType.Handle, element);
}

public ulong ElementAtOffset(ulong offset)
{
return _dataLayout.Handle.ElementAtOffset(_structType.Handle, offset);
}

public static bool operator ==(StructLayout left, StructLayout right) => (left is object) ? ((right is object) && (left._dataLayout == right._dataLayout && left._structType == right._structType)) : (right is null);

public static bool operator !=(StructLayout left, StructLayout right) => (left is object) ? ((right is null) || (left._dataLayout != right._dataLayout || left._structType != right._structType)) : (right is object);

public override bool Equals(object obj) => (obj is StructLayout other) && Equals(other);

public bool Equals(StructLayout other) => this == other;

public override int GetHashCode()
{
return _structType.GetHashCode() * 397 ^ _dataLayout.GetHashCode();
}
}
}
71 changes: 71 additions & 0 deletions tests/LLVMSharp.UnitTests/TargetData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using LLVMSharp.Interop;
using NUnit.Framework;

namespace LLVMSharp.UnitTests
{
class TargetData
{
[Test]
public void OffsetTest()
{
LLVMModuleRef m = LLVMModuleRef.CreateWithName("netscripten");
LLVMExecutionEngineRef engineRef = m.CreateExecutionEngine();
LLVMTargetDataRef target = engineRef.TargetData;
LLVMTypeRef testStruct = LLVMTypeRef.CreateStruct(
new[]
{
LLVMTypeRef.Int16,
LLVMTypeRef.Int32
}, true);

Assert.AreEqual(0, target.OffsetOfElement(testStruct, 0));
Assert.AreEqual(2, target.OffsetOfElement(testStruct, 1));

Assert.AreEqual(target.ElementAtOffset(testStruct, 0), 0);
Assert.AreEqual(target.ElementAtOffset(testStruct, 2), 1);
}

[Test]
public void SizeTest()
{
LLVMModuleRef m = LLVMModuleRef.CreateWithName("netscripten");
LLVMExecutionEngineRef engineRef = m.CreateExecutionEngine();
LLVMTargetDataRef target = engineRef.TargetData;
LLVMTypeRef testStruct = LLVMTypeRef.CreateStruct(
new[]
{
LLVMTypeRef.Int16,
LLVMTypeRef.Int32
}, true);

Assert.AreEqual(48, target.SizeOfTypeInBits(testStruct));
Assert.AreEqual(6, target.StoreSizeOfType(testStruct));
Assert.AreEqual(6, target.ABISizeOfType(testStruct));
}

[Test]
public void AlignmentTest()
{
LLVMModuleRef m = LLVMModuleRef.CreateWithName("netscripten");
m.Target = "wasm32-unknown-unknown-wasm";
m.DataLayout = "e-m:e-p:32:32-i64:64-n32:64-S128";
LLVMExecutionEngineRef engineRef = m.CreateExecutionEngine();
LLVMTargetDataRef target = engineRef.TargetData;
LLVMTypeRef testStruct = LLVMTypeRef.CreateStruct(
new[]
{
LLVMTypeRef.Int16,
LLVMTypeRef.Int32
}, true);

Assert.AreEqual(1, target.ABIAlignmentOfType(testStruct));
Assert.AreEqual(1, target.CallFrameAlignmentOfType(testStruct));
Assert.AreEqual(8, target.PreferredAlignmentOfType(testStruct));

LLVMValueRef global = m.AddGlobal(LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0), "someGlobal");
Assert.AreEqual(4, target.PreferredAlignmentOfGlobal(global));
}
}
}