From 0ef159888e8a7aedad9364abb0aef8bc911c087f Mon Sep 17 00:00:00 2001 From: Mohammad Rahhal Date: Tue, 8 Aug 2023 13:12:32 +0200 Subject: [PATCH 1/2] Add AttributeKind enum --- sources/LLVMSharp/Attribute.cs | 101 +++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/sources/LLVMSharp/Attribute.cs b/sources/LLVMSharp/Attribute.cs index 74f029b..33d7c96 100644 --- a/sources/LLVMSharp/Attribute.cs +++ b/sources/LLVMSharp/Attribute.cs @@ -21,3 +21,104 @@ public sealed class Attribute : IEquatable public override string ToString() => Handle.ToString(); } + +public enum AttributeKind +{ +#pragma warning disable CA1069 // Enums values should not be duplicated + None, + + FirstEnumAttr = 1, + AllocAlign = 1, + AllocatedPointer = 2, + AlwaysInline = 3, + Builtin = 4, + Cold = 5, + Convergent = 6, + DisableSanitizerInstrumentation = 7, + FnRetThunkExtern = 8, + Hot = 9, + ImmArg = 10, + InReg = 11, + InlineHint = 12, + JumpTable = 13, + MinSize = 14, + MustProgress = 15, + Naked = 16, + Nest = 17, + NoAlias = 18, + NoBuiltin = 19, + NoCallback = 20, + NoCapture = 21, + NoCfCheck = 22, + NoDuplicate = 23, + NoFree = 24, + NoImplicitFloat = 25, + NoInline = 26, + NoMerge = 27, + NoProfile = 28, + NoRecurse = 29, + NoRedZone = 30, + NoReturn = 31, + NoSanitizeBounds = 32, + NoSanitizeCoverage = 33, + NoSync = 34, + NoUndef = 35, + NoUnwind = 36, + NonLazyBind = 37, + NonNull = 38, + NullPointerIsValid = 39, + OptForFuzzing = 40, + OptimizeForSize = 41, + OptimizeNone = 42, + PresplitCoroutine = 43, + ReadNone = 44, + ReadOnly = 45, + Returned = 46, + ReturnsTwice = 47, + SExt = 48, + SafeStack = 49, + SanitizeAddress = 50, + SanitizeHWAddress = 51, + SanitizeMemTag = 52, + SanitizeMemory = 53, + SanitizeThread = 54, + ShadowCallStack = 55, + SkipProfile = 56, + Speculatable = 57, + SpeculativeLoadHardening = 58, + StackProtect = 59, + StackProtectReq = 60, + StackProtectStrong = 61, + StrictFP = 62, + SwiftAsync = 63, + SwiftError = 64, + SwiftSelf = 65, + WillReturn = 66, + WriteOnly = 67, + ZExt = 68, + LastEnumAttr = 68, + FirstTypeAttr = 69, + ByRef = 69, + ByVal = 70, + ElementType = 71, + InAlloca = 72, + Preallocated = 73, + StructRet = 74, + LastTypeAttr = 74, + FirstIntAttr = 75, + Alignment = 75, + AllocKind = 76, + AllocSize = 77, + Dereferenceable = 78, + DereferenceableOrNull = 79, + Memory = 80, + StackAlignment = 81, + UWTable = 82, + VScaleRange = 83, + LastIntAttr = 83, + + EndAttrKinds, + EmptyKey, + TombstoneKey, +#pragma warning restore CA1069 // Enums values should not be duplicated +} From 58ed9bab1ec42cce0b0f90bb53387ddc7e173ebe Mon Sep 17 00:00:00 2001 From: Mohammad Rahhal Date: Tue, 8 Aug 2023 13:15:00 +0200 Subject: [PATCH 2/2] add missing bindings related to attributes --- .../Extensions/LLVMAttributeRef.cs | 4 ++++ .../LLVMSharp.Interop/Extensions/LLVMContextRef.cs | 5 +++++ .../LLVMSharp.Interop/Extensions/LLVMValueRef.cs | 5 +++++ tests/LLVMSharp.UnitTests/Functions.cs | 13 +++++++++++++ 4 files changed, 27 insertions(+) diff --git a/sources/LLVMSharp.Interop/Extensions/LLVMAttributeRef.cs b/sources/LLVMSharp.Interop/Extensions/LLVMAttributeRef.cs index 3596f64..07d167e 100644 --- a/sources/LLVMSharp.Interop/Extensions/LLVMAttributeRef.cs +++ b/sources/LLVMSharp.Interop/Extensions/LLVMAttributeRef.cs @@ -13,6 +13,10 @@ public LLVMAttributeRef(IntPtr handle) Handle = handle; } + public readonly uint Kind => LLVM.GetEnumAttributeKind(this); + + public readonly ulong Value => LLVM.GetEnumAttributeValue(this); + public static implicit operator LLVMAttributeRef(LLVMOpaqueAttributeRef* value) => new LLVMAttributeRef((IntPtr)value); public static implicit operator LLVMOpaqueAttributeRef*(LLVMAttributeRef value) => (LLVMOpaqueAttributeRef*)value.Handle; diff --git a/sources/LLVMSharp.Interop/Extensions/LLVMContextRef.cs b/sources/LLVMSharp.Interop/Extensions/LLVMContextRef.cs index 0e8e149..a8904bb 100644 --- a/sources/LLVMSharp.Interop/Extensions/LLVMContextRef.cs +++ b/sources/LLVMSharp.Interop/Extensions/LLVMContextRef.cs @@ -86,6 +86,11 @@ public LLVMTypeRef CreateNamedStruct(ReadOnlySpan Name) return LLVM.StructCreateNamed(this, marshaledName); } + public LLVMAttributeRef CreateEnumAttribute(uint KindId, ulong Val) + { + return LLVM.CreateEnumAttribute(this, KindId, Val); + } + public void Dispose() { if (Handle != IntPtr.Zero) diff --git a/sources/LLVMSharp.Interop/Extensions/LLVMValueRef.cs b/sources/LLVMSharp.Interop/Extensions/LLVMValueRef.cs index 8565acd..1c8fbf1 100644 --- a/sources/LLVMSharp.Interop/Extensions/LLVMValueRef.cs +++ b/sources/LLVMSharp.Interop/Extensions/LLVMValueRef.cs @@ -953,6 +953,11 @@ public string GetAsString(out UIntPtr Length) } } + public void AddAttributeAtIndex(LLVMAttributeIndex Idx, LLVMAttributeRef A) + { + LLVM.AddAttributeAtIndex(this, Idx, A); + } + public LLVMAttributeRef[] GetAttributesAtIndex(LLVMAttributeIndex Idx) { var Attrs = new LLVMAttributeRef[GetAttributeCountAtIndex(Idx)]; diff --git a/tests/LLVMSharp.UnitTests/Functions.cs b/tests/LLVMSharp.UnitTests/Functions.cs index 0607ea9..7bea004 100644 --- a/tests/LLVMSharp.UnitTests/Functions.cs +++ b/tests/LLVMSharp.UnitTests/Functions.cs @@ -15,4 +15,17 @@ public void ParamTypesRoundtrip() var functionType = LLVMTypeRef.CreateFunction(returnType, parameterTypes); Assert.AreEqual(parameterTypes, functionType.ParamTypes); } + + [Test] + public void AddsAttributeAtIndex() + { + var module = LLVMModuleRef.CreateWithName("Test Module"); + var functionType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Int8, new[] { LLVMTypeRef.Double }); + var functionValue = module.AddFunction("test", functionType); + var attr = module.Context.CreateEnumAttribute((uint)AttributeKind.ByVal, default); + functionValue.AddAttributeAtIndex((LLVMAttributeIndex)1, attr); + + var attrs = functionValue.GetAttributesAtIndex((LLVMAttributeIndex)1); + Assert.AreEqual(attrs[0].Kind, (uint)AttributeKind.ByVal); + } }