Skip to content

Commit 4298bd7

Browse files
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
1 parent 1017748 commit 4298bd7

File tree

128 files changed

+2963
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+2963
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"LLVMSharp.UnitTests": {
4+
"commandName": "Project",
5+
"nativeDebugging": true
6+
}
7+
}
8+
}

sources/LLVMSharp/AtomicOrdering.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// 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.
2+
3+
using LLVMSharp.Interop;
4+
5+
namespace LLVMSharp
6+
{
7+
public enum AtomicOrdering
8+
{
9+
NotAtomic = LLVMAtomicOrdering.LLVMAtomicOrderingNotAtomic,
10+
Unordered = LLVMAtomicOrdering.LLVMAtomicOrderingUnordered,
11+
Monotonic = LLVMAtomicOrdering.LLVMAtomicOrderingMonotonic,
12+
Acquire = LLVMAtomicOrdering.LLVMAtomicOrderingAcquire,
13+
Release = LLVMAtomicOrdering.LLVMAtomicOrderingRelease,
14+
AcquireRelease = LLVMAtomicOrdering.LLVMAtomicOrderingAcquireRelease,
15+
SequentiallyConsistent = LLVMAtomicOrdering.LLVMAtomicOrderingSequentiallyConsistent,
16+
}
17+
}

sources/LLVMSharp/IRBuilder.cs

Lines changed: 695 additions & 0 deletions
Large diffs are not rendered by default.

sources/LLVMSharp/IRBuilderBase.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// 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.
2+
3+
using System;
4+
using LLVMSharp.Interop;
5+
6+
namespace LLVMSharp
7+
{
8+
public unsafe class IRBuilderBase : IEquatable<IRBuilderBase>
9+
{
10+
private protected IRBuilderBase(LLVMContext C)
11+
{
12+
Context = C;
13+
Handle = LLVMBuilderRef.Create(C.Handle);
14+
}
15+
16+
public LLVMContext Context { get; }
17+
18+
public LLVMBuilderRef Handle { get; }
19+
20+
public BasicBlock InsertBlock
21+
{
22+
get
23+
{
24+
var handle = Handle.InsertBlock;
25+
return Context.GetOrCreate(handle);
26+
}
27+
}
28+
29+
public static bool operator ==(IRBuilderBase left, IRBuilderBase right) => (left is object) ? ((right is object) && (left.Handle == right.Handle)) : (right is null);
30+
31+
public static bool operator !=(IRBuilderBase left, IRBuilderBase right) => (left is object) ? ((right is null) || (left.Handle != right.Handle)) : (right is object);
32+
33+
public void ClearInsertionPoint() => Handle.ClearInsertionPosition();
34+
35+
public GlobalVariable CreateGlobalString(string Str, string Name = "") => CreateGlobalString(Str.AsSpan(), Name.AsSpan());
36+
37+
public GlobalVariable CreateGlobalString(ReadOnlySpan<char> Str, ReadOnlySpan<char> Name)
38+
{
39+
var handle = Handle.BuildGlobalString(Str, Name);
40+
return Context.GetOrCreate<GlobalVariable>(handle);
41+
}
42+
43+
public override bool Equals(object obj) => (obj is IRBuilderBase other) && Equals(other);
44+
45+
public bool Equals(IRBuilderBase other) => this == other;
46+
47+
public override int GetHashCode() => Handle.GetHashCode();
48+
49+
public void SetInsertPoint(BasicBlock TheBB) => Handle.PositionAtEnd(TheBB.Handle);
50+
51+
public void SetInstDebugLocation(Instruction I) => Handle.SetInstDebugLocation(I.Handle);
52+
53+
public override string ToString() => Handle.ToString();
54+
}
55+
}

sources/LLVMSharp/Interop.Extensions/LLVMValueRef.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,11 @@ public string PrintToString()
960960

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

963+
public void SetAlignment(uint Bytes)
964+
{
965+
Alignment = Bytes;
966+
}
967+
963968
public void SetInstrParamAlignment(uint index, uint align) => LLVM.SetInstrParamAlignment(this, index, align);
964969

965970
public void SetMetadata(uint KindID, LLVMValueRef Node) => LLVM.SetMetadata(this, KindID, Node);

sources/LLVMSharp/LLVMContext.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// 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.
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using LLVMSharp.Interop;
6+
7+
namespace LLVMSharp
8+
{
9+
public sealed class LLVMContext : IEquatable<LLVMContext>
10+
{
11+
private readonly Dictionary<LLVMValueRef, WeakReference<Value>> _createdValues = new Dictionary<LLVMValueRef, WeakReference<Value>>();
12+
private readonly Dictionary<LLVMTypeRef, WeakReference<Type>> _createdTypes = new Dictionary<LLVMTypeRef, WeakReference<Type>>();
13+
14+
public LLVMContext()
15+
{
16+
Handle = LLVMContextRef.Create();
17+
}
18+
19+
public LLVMContextRef Handle { get; }
20+
21+
public static bool operator ==(LLVMContext left, LLVMContext right) => (left is object) ? ((right is object) && (left.Handle == right.Handle)) : (right is null);
22+
23+
public static bool operator !=(LLVMContext left, LLVMContext right) => (left is object) ? ((right is null) || (left.Handle != right.Handle)) : (right is object);
24+
25+
public override bool Equals(object obj) => (obj is LLVMContext other) && Equals(other);
26+
27+
public bool Equals(LLVMContext other) => this == other;
28+
29+
public override int GetHashCode() => Handle.GetHashCode();
30+
31+
public override string ToString() => Handle.ToString();
32+
33+
internal BasicBlock GetOrCreate(LLVMBasicBlockRef handle) => GetOrCreate<BasicBlock>(handle.AsValue());
34+
35+
internal TType GetOrCreate<TType>(LLVMTypeRef handle)
36+
where TType : Type
37+
{
38+
WeakReference<Type> typeRef;
39+
40+
if (handle == null)
41+
{
42+
return null;
43+
}
44+
else if (!_createdTypes.TryGetValue(handle, out typeRef))
45+
{
46+
typeRef = new WeakReference<Type>(null);
47+
_createdTypes.Add(handle, typeRef);
48+
}
49+
50+
if (!typeRef.TryGetTarget(out Type type))
51+
{
52+
type = Type.Create(handle);
53+
typeRef.SetTarget(type);
54+
}
55+
return (TType)type;
56+
}
57+
58+
internal TValue GetOrCreate<TValue>(LLVMValueRef handle)
59+
where TValue : Value
60+
{
61+
WeakReference<Value> valueRef;
62+
63+
if (handle == null)
64+
{
65+
return null;
66+
}
67+
else if (!_createdValues.TryGetValue(handle, out valueRef))
68+
{
69+
valueRef = new WeakReference<Value>(null);
70+
_createdValues.Add(handle, valueRef);
71+
}
72+
73+
if (!valueRef.TryGetTarget(out Value value))
74+
{
75+
value = Value.Create(handle);
76+
valueRef.SetTarget(value);
77+
}
78+
return (TValue)value;
79+
}
80+
81+
internal Type GetOrCreate(LLVMTypeRef handle) => GetOrCreate<Type>(handle);
82+
83+
internal Value GetOrCreate(LLVMValueRef handle) => GetOrCreate<Value>(handle);
84+
}
85+
}

sources/LLVMSharp/SyncScopeID.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// 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.
2+
3+
namespace LLVMSharp
4+
{
5+
public enum SyncScopeID
6+
{
7+
SingleThread = 0,
8+
System = 1,
9+
}
10+
}

sources/LLVMSharp/Types/ArrayType.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 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.
2+
3+
using LLVMSharp.Interop;
4+
5+
namespace LLVMSharp
6+
{
7+
public sealed class ArrayType : SequentialType
8+
{
9+
internal ArrayType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMArrayTypeKind)
10+
{
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 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.
2+
3+
using LLVMSharp.Interop;
4+
5+
namespace LLVMSharp
6+
{
7+
public class CompositeType : Type
8+
{
9+
private protected CompositeType(LLVMTypeRef handle, LLVMTypeKind expectedTypeKind) : base(handle, expectedTypeKind)
10+
{
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 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.
2+
3+
using LLVMSharp.Interop;
4+
5+
namespace LLVMSharp
6+
{
7+
public sealed class FunctionType : Type
8+
{
9+
internal FunctionType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMFunctionTypeKind)
10+
{
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 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.
2+
3+
using LLVMSharp.Interop;
4+
5+
namespace LLVMSharp
6+
{
7+
public sealed class IntegerType : Type
8+
{
9+
internal IntegerType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMIntegerTypeKind)
10+
{
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 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.
2+
3+
using LLVMSharp.Interop;
4+
5+
namespace LLVMSharp
6+
{
7+
public sealed class PointerType : Type
8+
{
9+
internal PointerType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMPointerTypeKind)
10+
{
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 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.
2+
3+
using LLVMSharp.Interop;
4+
5+
namespace LLVMSharp
6+
{
7+
public class SequentialType : CompositeType
8+
{
9+
private protected SequentialType(LLVMTypeRef handle, LLVMTypeKind expectedTypeKind) : base(handle, expectedTypeKind)
10+
{
11+
}
12+
}
13+
}

sources/LLVMSharp/Types/StructType.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// 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.
2+
3+
using System;
4+
using LLVMSharp.Interop;
5+
6+
namespace LLVMSharp
7+
{
8+
public sealed class StructType : CompositeType
9+
{
10+
internal StructType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMStructTypeKind)
11+
{
12+
}
13+
14+
public static StructType Create(LLVMContext Context, string Name) => Create(Context, Name.AsSpan());
15+
16+
public static StructType Create(LLVMContext Context, ReadOnlySpan<char> Name)
17+
{
18+
var handle = Context.Handle.CreateNamedStruct(Name);
19+
return Context.GetOrCreate<StructType>(handle);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)