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
13 changes: 1 addition & 12 deletions sources/LLVMSharp/Interop.Extensions/LLVMTargetMachineRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,7 @@ public LLVMTargetMachineRef(IntPtr handle)

public static bool operator !=(LLVMTargetMachineRef left, LLVMTargetMachineRef right) => !(left == right);

public string CreateTargetDataLayout()
{
var pDataLayout = LLVM.CreateTargetDataLayout(this);

if (pDataLayout is null)
{
return string.Empty;
}

var span = new ReadOnlySpan<byte>(pDataLayout, int.MaxValue);
return span.Slice(0, span.IndexOf((byte)'\0')).AsString();
}
public LLVMTargetDataRef CreateTargetDataLayout() => LLVM.CreateTargetDataLayout(this);

public void EmitToFile(LLVMModuleRef module, string fileName, LLVMCodeGenFileType codegen) => EmitToFile(module, fileName.AsSpan(), codegen);

Expand Down
36 changes: 36 additions & 0 deletions sources/LLVMSharp/Interop.Extensions/LLVMTargetRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace LLVMSharp.Interop
{
Expand Down Expand Up @@ -30,6 +31,41 @@ public static string DefaultTriple
}
}

public static bool TryGetTargetFromTriple(ReadOnlySpan<char> triple, out LLVMTargetRef outTarget, out string outError)
{
using var marshaledTriple = new MarshaledString(triple);

fixed (LLVMTargetRef* pOutTarget = &outTarget)
{
sbyte* pError;
var result = LLVM.GetTargetFromTriple(marshaledTriple, (LLVMTarget**)pOutTarget, &pError);

if (pError is null)
{
outError = string.Empty;
}
else
{
var span = new ReadOnlySpan<byte>(pError, int.MaxValue);
outError = span.Slice(0, span.IndexOf((byte)'\0')).AsString();
}

return result == 0;
}
}

public static LLVMTargetRef GetTargetFromTriple(string triple) => GetTargetFromTriple(triple.AsSpan());

public static LLVMTargetRef GetTargetFromTriple(ReadOnlySpan<char> triple)
{
if (!TryGetTargetFromTriple(triple, out LLVMTargetRef target, out string message))
{
throw new ExternalException(message);
}

return target;
}

public static LLVMTargetRef First => LLVM.GetFirstTarget();

public static IEnumerable<LLVMTargetRef> Targets
Expand Down
24 changes: 24 additions & 0 deletions tests/LLVMSharp.UnitTests/TargetData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,29 @@ public void AlignmentTest()
LLVMValueRef global = m.AddGlobal(LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0), "someGlobal");
Assert.AreEqual(4, target.PreferredAlignmentOfGlobal(global));
}

private LLVMTargetDataRef TargetDataFromTriple(string triple)
{
LLVMTargetRef target = LLVMTargetRef.GetTargetFromTriple(triple);
LLVMTargetMachineRef targetMachine = target.CreateTargetMachine(triple, "", "",
LLVMCodeGenOptLevel.LLVMCodeGenLevelDefault, LLVMRelocMode.LLVMRelocDefault,
LLVMCodeModel.LLVMCodeModelDefault);
return targetMachine.CreateTargetDataLayout();
}

[Test]
public void MachineTest()
{
LLVM.InitializeX86TargetInfo();
LLVM.InitializeX86Target();
LLVM.InitializeX86TargetMC();

LLVMTypeRef pointerType = LLVMTypeRef.CreatePointer(LLVMTypeRef.Int32, 0);
LLVMTargetDataRef x86 = TargetDataFromTriple("i386-unknown-unknown");
LLVMTargetDataRef x86_64 = TargetDataFromTriple("amd64-unknown-unknown");

Assert.AreEqual(4, x86.ABISizeOfType(pointerType));
Assert.AreEqual(8, x86_64.ABISizeOfType(pointerType));
}
}
}