-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a proper managed wrapper for LLVMSharp (#137)
* Adding proper wrappers for LLVMTypeRef * Adding proper wrappers for LLVMValueRef * Adding a proper wrapper for LLVMBuilderRef and LLVMContextRef
- Loading branch information
1 parent
1017748
commit 4298bd7
Showing
128 changed files
with
2,963 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,8 @@ | ||
{ | ||
"profiles": { | ||
"LLVMSharp.UnitTests": { | ||
"commandName": "Project", | ||
"nativeDebugging": true | ||
} | ||
} | ||
} |
This file contains 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,17 @@ | ||
// 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; | ||
|
||
namespace LLVMSharp | ||
{ | ||
public enum AtomicOrdering | ||
{ | ||
NotAtomic = LLVMAtomicOrdering.LLVMAtomicOrderingNotAtomic, | ||
Unordered = LLVMAtomicOrdering.LLVMAtomicOrderingUnordered, | ||
Monotonic = LLVMAtomicOrdering.LLVMAtomicOrderingMonotonic, | ||
Acquire = LLVMAtomicOrdering.LLVMAtomicOrderingAcquire, | ||
Release = LLVMAtomicOrdering.LLVMAtomicOrderingRelease, | ||
AcquireRelease = LLVMAtomicOrdering.LLVMAtomicOrderingAcquireRelease, | ||
SequentiallyConsistent = LLVMAtomicOrdering.LLVMAtomicOrderingSequentiallyConsistent, | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,55 @@ | ||
// 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 unsafe class IRBuilderBase : IEquatable<IRBuilderBase> | ||
{ | ||
private protected IRBuilderBase(LLVMContext C) | ||
{ | ||
Context = C; | ||
Handle = LLVMBuilderRef.Create(C.Handle); | ||
} | ||
|
||
public LLVMContext Context { get; } | ||
|
||
public LLVMBuilderRef Handle { get; } | ||
|
||
public BasicBlock InsertBlock | ||
{ | ||
get | ||
{ | ||
var handle = Handle.InsertBlock; | ||
return Context.GetOrCreate(handle); | ||
} | ||
} | ||
|
||
public static bool operator ==(IRBuilderBase left, IRBuilderBase right) => (left is object) ? ((right is object) && (left.Handle == right.Handle)) : (right is null); | ||
|
||
public static bool operator !=(IRBuilderBase left, IRBuilderBase right) => (left is object) ? ((right is null) || (left.Handle != right.Handle)) : (right is object); | ||
|
||
public void ClearInsertionPoint() => Handle.ClearInsertionPosition(); | ||
|
||
public GlobalVariable CreateGlobalString(string Str, string Name = "") => CreateGlobalString(Str.AsSpan(), Name.AsSpan()); | ||
|
||
public GlobalVariable CreateGlobalString(ReadOnlySpan<char> Str, ReadOnlySpan<char> Name) | ||
{ | ||
var handle = Handle.BuildGlobalString(Str, Name); | ||
return Context.GetOrCreate<GlobalVariable>(handle); | ||
} | ||
|
||
public override bool Equals(object obj) => (obj is IRBuilderBase other) && Equals(other); | ||
|
||
public bool Equals(IRBuilderBase other) => this == other; | ||
|
||
public override int GetHashCode() => Handle.GetHashCode(); | ||
|
||
public void SetInsertPoint(BasicBlock TheBB) => Handle.PositionAtEnd(TheBB.Handle); | ||
|
||
public void SetInstDebugLocation(Instruction I) => Handle.SetInstDebugLocation(I.Handle); | ||
|
||
public override string ToString() => Handle.ToString(); | ||
} | ||
} |
This file contains 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 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,85 @@ | ||
// 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 System.Collections.Generic; | ||
using LLVMSharp.Interop; | ||
|
||
namespace LLVMSharp | ||
{ | ||
public sealed class LLVMContext : IEquatable<LLVMContext> | ||
{ | ||
private readonly Dictionary<LLVMValueRef, WeakReference<Value>> _createdValues = new Dictionary<LLVMValueRef, WeakReference<Value>>(); | ||
private readonly Dictionary<LLVMTypeRef, WeakReference<Type>> _createdTypes = new Dictionary<LLVMTypeRef, WeakReference<Type>>(); | ||
|
||
public LLVMContext() | ||
{ | ||
Handle = LLVMContextRef.Create(); | ||
} | ||
|
||
public LLVMContextRef Handle { get; } | ||
|
||
public static bool operator ==(LLVMContext left, LLVMContext right) => (left is object) ? ((right is object) && (left.Handle == right.Handle)) : (right is null); | ||
|
||
public static bool operator !=(LLVMContext left, LLVMContext right) => (left is object) ? ((right is null) || (left.Handle != right.Handle)) : (right is object); | ||
|
||
public override bool Equals(object obj) => (obj is LLVMContext other) && Equals(other); | ||
|
||
public bool Equals(LLVMContext other) => this == other; | ||
|
||
public override int GetHashCode() => Handle.GetHashCode(); | ||
|
||
public override string ToString() => Handle.ToString(); | ||
|
||
internal BasicBlock GetOrCreate(LLVMBasicBlockRef handle) => GetOrCreate<BasicBlock>(handle.AsValue()); | ||
|
||
internal TType GetOrCreate<TType>(LLVMTypeRef handle) | ||
where TType : Type | ||
{ | ||
WeakReference<Type> typeRef; | ||
|
||
if (handle == null) | ||
{ | ||
return null; | ||
} | ||
else if (!_createdTypes.TryGetValue(handle, out typeRef)) | ||
{ | ||
typeRef = new WeakReference<Type>(null); | ||
_createdTypes.Add(handle, typeRef); | ||
} | ||
|
||
if (!typeRef.TryGetTarget(out Type type)) | ||
{ | ||
type = Type.Create(handle); | ||
typeRef.SetTarget(type); | ||
} | ||
return (TType)type; | ||
} | ||
|
||
internal TValue GetOrCreate<TValue>(LLVMValueRef handle) | ||
where TValue : Value | ||
{ | ||
WeakReference<Value> valueRef; | ||
|
||
if (handle == null) | ||
{ | ||
return null; | ||
} | ||
else if (!_createdValues.TryGetValue(handle, out valueRef)) | ||
{ | ||
valueRef = new WeakReference<Value>(null); | ||
_createdValues.Add(handle, valueRef); | ||
} | ||
|
||
if (!valueRef.TryGetTarget(out Value value)) | ||
{ | ||
value = Value.Create(handle); | ||
valueRef.SetTarget(value); | ||
} | ||
return (TValue)value; | ||
} | ||
|
||
internal Type GetOrCreate(LLVMTypeRef handle) => GetOrCreate<Type>(handle); | ||
|
||
internal Value GetOrCreate(LLVMValueRef handle) => GetOrCreate<Value>(handle); | ||
} | ||
} |
This file contains 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,10 @@ | ||
// 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. | ||
|
||
namespace LLVMSharp | ||
{ | ||
public enum SyncScopeID | ||
{ | ||
SingleThread = 0, | ||
System = 1, | ||
} | ||
} |
This file contains 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,13 @@ | ||
// 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; | ||
|
||
namespace LLVMSharp | ||
{ | ||
public sealed class ArrayType : SequentialType | ||
{ | ||
internal ArrayType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMArrayTypeKind) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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,13 @@ | ||
// 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; | ||
|
||
namespace LLVMSharp | ||
{ | ||
public class CompositeType : Type | ||
{ | ||
private protected CompositeType(LLVMTypeRef handle, LLVMTypeKind expectedTypeKind) : base(handle, expectedTypeKind) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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,13 @@ | ||
// 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; | ||
|
||
namespace LLVMSharp | ||
{ | ||
public sealed class FunctionType : Type | ||
{ | ||
internal FunctionType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMFunctionTypeKind) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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,13 @@ | ||
// 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; | ||
|
||
namespace LLVMSharp | ||
{ | ||
public sealed class IntegerType : Type | ||
{ | ||
internal IntegerType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMIntegerTypeKind) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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,13 @@ | ||
// 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; | ||
|
||
namespace LLVMSharp | ||
{ | ||
public sealed class PointerType : Type | ||
{ | ||
internal PointerType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMPointerTypeKind) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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,13 @@ | ||
// 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; | ||
|
||
namespace LLVMSharp | ||
{ | ||
public class SequentialType : CompositeType | ||
{ | ||
private protected SequentialType(LLVMTypeRef handle, LLVMTypeKind expectedTypeKind) : base(handle, expectedTypeKind) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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,22 @@ | ||
// 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 StructType : CompositeType | ||
{ | ||
internal StructType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMStructTypeKind) | ||
{ | ||
} | ||
|
||
public static StructType Create(LLVMContext Context, string Name) => Create(Context, Name.AsSpan()); | ||
|
||
public static StructType Create(LLVMContext Context, ReadOnlySpan<char> Name) | ||
{ | ||
var handle = Context.Handle.CreateNamedStruct(Name); | ||
return Context.GetOrCreate<StructType>(handle); | ||
} | ||
} | ||
} |
Oops, something went wrong.