Skip to content

Commit f5b61b9

Browse files
authored
Expose GetTargetFromTriple from LLVMTargetRef (#153)
1 parent 9d2f9a1 commit f5b61b9

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

sources/LLVMSharp/Interop.Extensions/LLVMTargetMachineRef.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,7 @@ public LLVMTargetMachineRef(IntPtr handle)
2222

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

25-
public string CreateTargetDataLayout()
26-
{
27-
var pDataLayout = LLVM.CreateTargetDataLayout(this);
28-
29-
if (pDataLayout is null)
30-
{
31-
return string.Empty;
32-
}
33-
34-
var span = new ReadOnlySpan<byte>(pDataLayout, int.MaxValue);
35-
return span.Slice(0, span.IndexOf((byte)'\0')).AsString();
36-
}
25+
public LLVMTargetDataRef CreateTargetDataLayout() => LLVM.CreateTargetDataLayout(this);
3726

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

sources/LLVMSharp/Interop.Extensions/LLVMTargetRef.cs

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Runtime.InteropServices;
56

67
namespace LLVMSharp.Interop
78
{
@@ -30,6 +31,41 @@ public static string DefaultTriple
3031
}
3132
}
3233

34+
public static bool TryGetTargetFromTriple(ReadOnlySpan<char> triple, out LLVMTargetRef outTarget, out string outError)
35+
{
36+
using var marshaledTriple = new MarshaledString(triple);
37+
38+
fixed (LLVMTargetRef* pOutTarget = &outTarget)
39+
{
40+
sbyte* pError;
41+
var result = LLVM.GetTargetFromTriple(marshaledTriple, (LLVMTarget**)pOutTarget, &pError);
42+
43+
if (pError is null)
44+
{
45+
outError = string.Empty;
46+
}
47+
else
48+
{
49+
var span = new ReadOnlySpan<byte>(pError, int.MaxValue);
50+
outError = span.Slice(0, span.IndexOf((byte)'\0')).AsString();
51+
}
52+
53+
return result == 0;
54+
}
55+
}
56+
57+
public static LLVMTargetRef GetTargetFromTriple(string triple) => GetTargetFromTriple(triple.AsSpan());
58+
59+
public static LLVMTargetRef GetTargetFromTriple(ReadOnlySpan<char> triple)
60+
{
61+
if (!TryGetTargetFromTriple(triple, out LLVMTargetRef target, out string message))
62+
{
63+
throw new ExternalException(message);
64+
}
65+
66+
return target;
67+
}
68+
3369
public static LLVMTargetRef First => LLVM.GetFirstTarget();
3470

3571
public static IEnumerable<LLVMTargetRef> Targets

tests/LLVMSharp.UnitTests/TargetData.cs

+24
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,29 @@ public void AlignmentTest()
6767
LLVMValueRef global = m.AddGlobal(LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0), "someGlobal");
6868
Assert.AreEqual(4, target.PreferredAlignmentOfGlobal(global));
6969
}
70+
71+
private LLVMTargetDataRef TargetDataFromTriple(string triple)
72+
{
73+
LLVMTargetRef target = LLVMTargetRef.GetTargetFromTriple(triple);
74+
LLVMTargetMachineRef targetMachine = target.CreateTargetMachine(triple, "", "",
75+
LLVMCodeGenOptLevel.LLVMCodeGenLevelDefault, LLVMRelocMode.LLVMRelocDefault,
76+
LLVMCodeModel.LLVMCodeModelDefault);
77+
return targetMachine.CreateTargetDataLayout();
78+
}
79+
80+
[Test]
81+
public void MachineTest()
82+
{
83+
LLVM.InitializeX86TargetInfo();
84+
LLVM.InitializeX86Target();
85+
LLVM.InitializeX86TargetMC();
86+
87+
LLVMTypeRef pointerType = LLVMTypeRef.CreatePointer(LLVMTypeRef.Int32, 0);
88+
LLVMTargetDataRef x86 = TargetDataFromTriple("i386-unknown-unknown");
89+
LLVMTargetDataRef x86_64 = TargetDataFromTriple("amd64-unknown-unknown");
90+
91+
Assert.AreEqual(4, x86.ABISizeOfType(pointerType));
92+
Assert.AreEqual(8, x86_64.ABISizeOfType(pointerType));
93+
}
7094
}
7195
}

0 commit comments

Comments
 (0)