Skip to content
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
9 changes: 9 additions & 0 deletions sources/LLVMSharp/Types/ArrayType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System;
using LLVMSharp.Interop;

namespace LLVMSharp;
Expand All @@ -10,5 +11,13 @@ internal ArrayType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMArrayType
{
}

public static ArrayType Get(Type elementType, ulong numElements)
{
ArgumentNullException.ThrowIfNull(elementType);
var context = elementType.Context;
var handle = LLVMTypeRef.CreateArray2(elementType.Handle, numElements);
return context.GetOrCreate<ArrayType>(handle);
}

public ulong NumElements => Handle.ArrayLength2;
}
15 changes: 15 additions & 0 deletions sources/LLVMSharp/Types/FunctionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ internal FunctionType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMFuncti
{
}

public static FunctionType Get(Type returnType, Type[] parameterTypes, bool isVarArg = false)
{
ArgumentNullException.ThrowIfNull(parameterTypes);
return Get(returnType, parameterTypes.AsSpan(), isVarArg);
}

public static FunctionType Get(Type returnType, ReadOnlySpan<Type> parameterTypes, bool isVarArg)
{
ArgumentNullException.ThrowIfNull(returnType);
var context = returnType.Context;
var handles = Type.GetHandles(parameterTypes, context, nameof(parameterTypes));
var handle = LLVMTypeRef.CreateFunction(returnType.Handle, handles, isVarArg);
return context.GetOrCreate<FunctionType>(handle);
}

public bool IsVarArg => Handle.IsFunctionVarArg;

public uint NumParams => Handle.ParamTypesCount;
Expand Down
8 changes: 8 additions & 0 deletions sources/LLVMSharp/Types/PointerType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System;
using LLVMSharp.Interop;

namespace LLVMSharp;
Expand All @@ -11,4 +12,11 @@ internal PointerType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMPointer
}

public uint AddressSpace => Handle.PointerAddressSpace;

public static PointerType Get(LLVMContext context, uint addressSpace = 0)
{
ArgumentNullException.ThrowIfNull(context);
var handle = context.Handle.CreatePointerType(addressSpace);
return context.GetOrCreate<PointerType>(handle);
}
}
24 changes: 15 additions & 9 deletions sources/LLVMSharp/Types/StructType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ public static StructType Create(LLVMContext context, ReadOnlySpan<char> name)
return context.GetOrCreate<StructType>(handle);
}

public static StructType Get(LLVMContext context, Type[] elementTypes, bool packed = false)
{
ArgumentNullException.ThrowIfNull(elementTypes);
return Get(context, elementTypes.AsSpan(), packed);
}

public static StructType Get(LLVMContext context, ReadOnlySpan<Type> elementTypes, bool packed)
{
ArgumentNullException.ThrowIfNull(context);
var handles = Type.GetHandles(elementTypes, context, nameof(elementTypes));
var handle = context.Handle.GetStructType(handles, packed);
return context.GetOrCreate<StructType>(handle);
}

public bool IsOpaque => Handle.IsOpaqueStruct;

public bool IsPacked => Handle.IsPackedStruct;
Expand Down Expand Up @@ -58,15 +72,7 @@ public void SetBody(Type[] elementTypes, bool packed)

public void SetBody(ReadOnlySpan<Type> elementTypes, bool packed)
{
var handles = new LLVMTypeRef[elementTypes.Length];

for (var i = 0; i < handles.Length; i++)
{
var elementType = elementTypes[i];
ArgumentNullException.ThrowIfNull(elementType);
handles[i] = elementType.Handle;
}

var handles = Type.GetHandles(elementTypes, Context, nameof(elementTypes));
Handle.StructSetBody(handles, packed);
}
}
75 changes: 75 additions & 0 deletions sources/LLVMSharp/Types/TargetExtType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System;
using LLVMSharp.Interop;

namespace LLVMSharp;

public sealed class TargetExtType : Type
{
internal TargetExtType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMTargetExtTypeKind)
{
}

public string Name => Handle.TargetExtTypeName;

public uint NumIntParameters => Handle.TargetExtTypeNumIntParams;

public uint NumTypeParameters => Handle.TargetExtTypeNumTypeParams;

public static TargetExtType Get(LLVMContext context, string name)
{
ArgumentNullException.ThrowIfNull(name);
return Get(context, name.AsSpan(), [], []);
}

public static TargetExtType Get(LLVMContext context, string name, Type[] typeParameters, uint[] intParameters)
{
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(typeParameters);
ArgumentNullException.ThrowIfNull(intParameters);
return Get(context, name.AsSpan(), typeParameters.AsSpan(), intParameters.AsSpan());
}

public static TargetExtType Get(LLVMContext context, string name, ReadOnlySpan<Type> typeParameters, ReadOnlySpan<uint> intParameters)
{
ArgumentNullException.ThrowIfNull(name);
return Get(context, name.AsSpan(), typeParameters, intParameters);
}

public static TargetExtType Get(LLVMContext context, ReadOnlySpan<char> name, ReadOnlySpan<Type> typeParameters, ReadOnlySpan<uint> intParameters)
{
ArgumentNullException.ThrowIfNull(context);
var handles = Type.GetHandles(typeParameters, context, nameof(typeParameters));
var handle = context.Handle.CreateTargetExtType(name, handles, intParameters);
return context.GetOrCreate<TargetExtType>(handle);
}

public uint GetIntParameter(uint index) => Handle.GetTargetExtTypeIntParam(index);

public uint[] GetIntParameters()
{
var parameters = new uint[NumIntParameters];

for (var i = 0; i < parameters.Length; i++)
{
parameters[i] = GetIntParameter((uint)i);
}

return parameters;
}

public Type GetTypeParameter(uint index) => Context.GetOrCreate(Handle.GetTargetExtTypeTypeParam(index));

public Type[] GetTypeParameters()
{
var parameters = new Type[NumTypeParameters];

for (var i = 0; i < parameters.Length; i++)
{
parameters[i] = GetTypeParameter((uint)i);
}

return parameters;
}
}
85 changes: 84 additions & 1 deletion sources/LLVMSharp/Types/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ private protected Type(LLVMTypeRef handle, LLVMTypeKind expectedTypeKind1, LLVMT

public bool IsStructTy => Handle.Kind == LLVMTypeKind.LLVMStructTypeKind;

public bool IsTargetExtTy => Handle.Kind == LLVMTypeKind.LLVMTargetExtTypeKind;

public bool IsTokenTy => Handle.Kind == LLVMTypeKind.LLVMTokenTypeKind;

public bool IsVectorTy => Handle.Kind is LLVMTypeKind.LLVMVectorTypeKind or LLVMTypeKind.LLVMScalableVectorTypeKind;
Expand Down Expand Up @@ -107,6 +109,28 @@ public static Type GetHalfTy(LLVMContext c)
return c.GetOrCreate(handle);
}

public static Type? GetPrimitiveType(LLVMContext c, LLVMTypeKind kind)
{
ArgumentNullException.ThrowIfNull(c);

return kind switch
{
LLVMTypeKind.LLVMVoidTypeKind => GetVoidTy(c),
LLVMTypeKind.LLVMHalfTypeKind => GetHalfTy(c),
LLVMTypeKind.LLVMBFloatTypeKind => GetBFloatTy(c),
LLVMTypeKind.LLVMFloatTypeKind => GetFloatTy(c),
LLVMTypeKind.LLVMDoubleTypeKind => GetDoubleTy(c),
LLVMTypeKind.LLVMX86_FP80TypeKind => GetX86_FP80Ty(c),
LLVMTypeKind.LLVMFP128TypeKind => GetFP128Ty(c),
LLVMTypeKind.LLVMPPC_FP128TypeKind => GetPPC_FP128Ty(c),
LLVMTypeKind.LLVMLabelTypeKind => GetLabelTy(c),
LLVMTypeKind.LLVMMetadataTypeKind => GetMetadataTy(c),
LLVMTypeKind.LLVMX86_AMXTypeKind => GetX86_AMXTy(c),
LLVMTypeKind.LLVMTokenTypeKind => GetTokenTy(c),
_ => null,
};
}

public static IntegerType GetInt1Ty(LLVMContext c)
{
ArgumentNullException.ThrowIfNull(c);
Expand Down Expand Up @@ -142,6 +166,20 @@ public static IntegerType GetInt64Ty(LLVMContext c)
return c.GetOrCreate<IntegerType>(handle);
}

public static IntegerType GetInt128Ty(LLVMContext c)
{
ArgumentNullException.ThrowIfNull(c);
var handle = c.Handle.Int128Type;
return c.GetOrCreate<IntegerType>(handle);
}

public static IntegerType GetIntNTy(LLVMContext c, uint n)
{
ArgumentNullException.ThrowIfNull(c);
var handle = c.Handle.GetIntType(n);
return c.GetOrCreate<IntegerType>(handle);
}

public static Type GetFP128Ty(LLVMContext c)
{
ArgumentNullException.ThrowIfNull(c);
Expand All @@ -156,20 +194,45 @@ public static Type GetLabelTy(LLVMContext c)
return c.GetOrCreate(handle);
}

public static Type GetMetadataTy(LLVMContext c)
{
ArgumentNullException.ThrowIfNull(c);
var handle = c.Handle.MetadataType;
return c.GetOrCreate(handle);
}

public static Type GetPPC_FP128Ty(LLVMContext c)
{
ArgumentNullException.ThrowIfNull(c);
var handle = c.Handle.PPCFP128Type;
return c.GetOrCreate(handle);
}

public static Type GetTokenTy(LLVMContext c)
{
ArgumentNullException.ThrowIfNull(c);
var handle = c.Handle.TokenType;
return c.GetOrCreate(handle);
}

public static Type GetVoidTy(LLVMContext c)
{
ArgumentNullException.ThrowIfNull(c);
var handle = c.Handle.VoidType;
return c.GetOrCreate(handle);
}

public static PointerType GetWasm_ExternrefTy(LLVMContext c) => PointerType.Get(c, 10);

public static PointerType GetWasm_FuncrefTy(LLVMContext c) => PointerType.Get(c, 20);

public static Type GetX86_AMXTy(LLVMContext c)
{
ArgumentNullException.ThrowIfNull(c);
var handle = c.Handle.X86AMXType;
return c.GetOrCreate(handle);
}

public static Type GetX86_FP80Ty(LLVMContext c)
{
ArgumentNullException.ThrowIfNull(c);
Expand Down Expand Up @@ -210,7 +273,27 @@ public static Type GetX86_FP80Ty(LLVMContext c)
LLVMTypeKind.LLVMScalableVectorTypeKind => new VectorType(handle),
LLVMTypeKind.LLVMBFloatTypeKind => new Type(handle, LLVMTypeKind.LLVMBFloatTypeKind),
LLVMTypeKind.LLVMX86_AMXTypeKind => new Type(handle, LLVMTypeKind.LLVMX86_AMXTypeKind),
LLVMTypeKind.LLVMTargetExtTypeKind => new Type(handle, LLVMTypeKind.LLVMTargetExtTypeKind),
LLVMTypeKind.LLVMTargetExtTypeKind => new TargetExtType(handle),
_ => new Type(handle, handle.Kind),
};

internal static LLVMTypeRef[] GetHandles(ReadOnlySpan<Type> types, LLVMContext context, string paramName)
{
var handles = new LLVMTypeRef[types.Length];

for (var i = 0; i < handles.Length; i++)
{
var type = types[i];
ArgumentNullException.ThrowIfNull(type, paramName);

if (type.Context != context)
{
throw new ArgumentException("All types must belong to the same context.", paramName);
}

handles[i] = type.Handle;
}

return handles;
}
}
11 changes: 11 additions & 0 deletions sources/LLVMSharp/Types/VectorType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System;
using LLVMSharp.Interop;

namespace LLVMSharp;
Expand All @@ -10,6 +11,16 @@ internal VectorType(LLVMTypeRef handle) : base(handle, LLVMTypeKind.LLVMVectorTy
{
}

public static VectorType Get(Type elementType, uint elementCount, bool scalable = false)
{
ArgumentNullException.ThrowIfNull(elementType);
var context = elementType.Context;
var handle = scalable
? LLVMTypeRef.CreateScalableVector(elementType.Handle, elementCount)
: LLVMTypeRef.CreateVector(elementType.Handle, elementCount);
return context.GetOrCreate<VectorType>(handle);
}

public bool IsScalable => Handle.Kind == LLVMTypeKind.LLVMScalableVectorTypeKind;

public uint NumElements => Handle.VectorSize;
Expand Down
Loading
Loading