-
Notifications
You must be signed in to change notification settings - Fork 104
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
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dea9131
add methods for LLVMTargetDataRef
yowl 3ff9586
attempt to fix data layout so that it passes on x86
yowl 212633f
add headers, mv file to tests from Tests
yowl 8085b40
add header
yowl 6f9bc5f
address feedback, create StructLayout class to provide struct offset …
yowl d6200c1
Match c++ name
yowl 04c5014
Match c++ name
yowl 2de04f0
line up method names with DataLayout.h
yowl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 CreateStructLayout(StructType structType) | ||
| { | ||
| return new StructLayout(this, structType); | ||
| } | ||
|
|
||
| public ulong TypeSizeInBits(Type type) | ||
yowl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| return Handle.SizeOfTypeInBits(type.Handle); | ||
| } | ||
|
|
||
| public ulong TypeStoreSize(Type type) | ||
| { | ||
| return Handle.StoreSizeOfType(type.Handle); | ||
| } | ||
|
|
||
| public ulong TypeAllocSize(Type type) | ||
| { | ||
| return Handle.ABISizeOfType(type.Handle); | ||
| } | ||
|
|
||
| public uint ABITypeAlignment(Type type) | ||
| { | ||
| return Handle.ABIAlignmentOfType(type.Handle); | ||
| } | ||
|
|
||
| public uint PreferredTypeAlignment(Type type) | ||
| { | ||
| return Handle.PreferredAlignmentOfType(type.Handle); | ||
| } | ||
|
|
||
| public uint PreferredAlignment(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(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.