Skip to content

Commit

Permalink
Add a proper managed wrapper for LLVMSharp (#137)
Browse files Browse the repository at this point in the history
* Adding proper wrappers for LLVMTypeRef

* Adding proper wrappers for LLVMValueRef

* Adding a proper wrapper for LLVMBuilderRef and LLVMContextRef
  • Loading branch information
tannergooding authored Apr 18, 2020
1 parent 1017748 commit 4298bd7
Show file tree
Hide file tree
Showing 128 changed files with 2,963 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Tests/LLVMSharp.UnitTests/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"LLVMSharp.UnitTests": {
"commandName": "Project",
"nativeDebugging": true
}
}
}
17 changes: 17 additions & 0 deletions sources/LLVMSharp/AtomicOrdering.cs
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,
}
}
695 changes: 695 additions & 0 deletions sources/LLVMSharp/IRBuilder.cs

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions sources/LLVMSharp/IRBuilderBase.cs
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();
}
}
5 changes: 5 additions & 0 deletions sources/LLVMSharp/Interop.Extensions/LLVMValueRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,11 @@ public string PrintToString()

public void ReplaceAllUsesWith(LLVMValueRef NewVal) => LLVM.ReplaceAllUsesWith(this, NewVal);

public void SetAlignment(uint Bytes)
{
Alignment = Bytes;
}

public void SetInstrParamAlignment(uint index, uint align) => LLVM.SetInstrParamAlignment(this, index, align);

public void SetMetadata(uint KindID, LLVMValueRef Node) => LLVM.SetMetadata(this, KindID, Node);
Expand Down
85 changes: 85 additions & 0 deletions sources/LLVMSharp/LLVMContext.cs
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);
}
}
10 changes: 10 additions & 0 deletions sources/LLVMSharp/SyncScopeID.cs
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,
}
}
13 changes: 13 additions & 0 deletions sources/LLVMSharp/Types/ArrayType.cs
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)
{
}
}
}
13 changes: 13 additions & 0 deletions sources/LLVMSharp/Types/CompositeType.cs
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)
{
}
}
}
13 changes: 13 additions & 0 deletions sources/LLVMSharp/Types/FunctionType.cs
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)
{
}
}
}
13 changes: 13 additions & 0 deletions sources/LLVMSharp/Types/IntegerType.cs
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)
{
}
}
}
13 changes: 13 additions & 0 deletions sources/LLVMSharp/Types/PointerType.cs
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)
{
}
}
}
13 changes: 13 additions & 0 deletions sources/LLVMSharp/Types/SequentialType.cs
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)
{
}
}
}
22 changes: 22 additions & 0 deletions sources/LLVMSharp/Types/StructType.cs
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);
}
}
}
Loading

0 comments on commit 4298bd7

Please sign in to comment.