Skip to content
Permalink
Browse files

implement missing apis (#112)

* implement missing apis

* follow guidelines for TryEmitToFile, EmitToFile

* call DisposeErrorMessage
  • Loading branch information...
AviAvni authored and tannergooding committed Aug 19, 2019
1 parent 96cd847 commit f986236ebc9ae2b41584ad9b0fad96deadbbbe44
Showing with 55 additions and 0 deletions.
  1. +10 −0 sources/LLVMSharp/Extensions/LLVMTargetRef.cs
  2. +45 −0 sources/LLVMSharp/Wrappers/LLVMTargetMachineRef.cs
@@ -69,5 +69,15 @@ public string Name
public override int GetHashCode() => Pointer.GetHashCode();

public LLVMTargetRef GetNext() => LLVM.GetNextTarget(this);

public LLVMTargetMachineRef CreateTargetMachine(string triple, string cpu, string features, LLVMCodeGenOptLevel level, LLVMRelocMode reloc, LLVMCodeModel codeModel)
{
using (var marshaledTriple = new MarshaledString(triple))
using (var marshaledCPU = new MarshaledString(cpu))
using (var marshaledFeatures = new MarshaledString(features))
{
return LLVM.CreateTargetMachine(this, marshaledTriple, marshaledCPU, marshaledFeatures, level, reloc, codeModel);
}
}
}
}
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;

namespace LLVMSharp
{
@@ -20,5 +21,49 @@ public LLVMTargetMachineRef(IntPtr pointer)
{
return (LLVMOpaqueTargetMachine*)value.Pointer;
}

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 bool TryEmitToFile(LLVMModuleRef module, string fileName, LLVMCodeGenFileType codegen, out string message)
{
using (var marshaledFileName = new MarshaledString(fileName))
{
sbyte* errorMessage;

int result = LLVM.TargetMachineEmitToFile(this, module, marshaledFileName, codegen, &errorMessage);

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

return result == 0;
}
}

public void EmitToFile(LLVMModuleRef module, string fileName, LLVMCodeGenFileType codegen)
{
if (!TryEmitToFile(module, fileName, codegen, out string Error))
{
throw new ExternalException(Error);
}
}
}
}

0 comments on commit f986236

Please sign in to comment.
You can’t perform that action at this time.