Permalink
Browse files

LLVMSharp 5.0 release.

  • Loading branch information...
mjsabby committed Sep 17, 2017
1 parent 22dcd97 commit c7fc838b400853261545c4e97bb41e95fd62396d
Showing with 243 additions and 92 deletions.
  1. +55 −56 README.md
  2. +9 −17 src/ExecutionEngine.cs
  3. +19 −19 src/Generated.cs
  4. +1 −0 src/LLVMSharp.csproj
  5. +159 −0 src/Overloads.cs
View
111 README.md
@@ -2,30 +2,28 @@
[![Join the chat at https://gitter.im/mjsabby/LLVMSharp](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mjsabby/LLVMSharp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
LLVMSharp are strongly-typed safe LLVM bindings written in C# for .NET and Mono, tested on Linux and Windows. They are auto-generated using [ClangSharp](http://www.clangsharp.org) parsing LLVM-C header files.
LLVMSharp is a multi-platform .NET Standard library for accessing the LLLVM infrastructure. The bindings are auto-generated using [ClangSharp](http://www.clangsharp.org) parsing LLVM-C header files.
If you're on Windows, consider using the [**LLVMSharp 3.8 NuGet Package**](http://www.nuget.org/packages/LLVMSharp/3.8.0) - built from LLVM 3.8 Release.
[**LLVMSharp 5.0 NuGet Package**](http://www.nuget.org/packages/LLVMSharp/5.0.0) for .NET Core 2.0+ (Linux, macOS, Windows) and .NET Framework 4+ - built from the LLVM 5.0.0 Release.
## Building LLVMSharp
On Linux using Mono:
On Linux using .NET Core:
```bash
$ git clone http://github.com/mjsabby/LLVMSharp
$ cd LLVMSharp
$ chmod +x build.sh
$ ./build.sh /path/to/libLLVM.so /path/llvm/include
$ git clone http://github.com/Microsoft/LLVMSharp
$ cd LLVMSharp/src
$ dotnet build
```
On Windows using Microsoft.NET:
On Windows using .NET Core
**Note:** - you need to run from the Visual Studio Command Prompt of the architecture you want to target.
**Note:** - you need to run these commands from the Visual Studio Developer Command Prompt.
```bash
:> cd c:\path\to\llvm_source\{Release|Debug}\lib
:> git clone http://github.com/mjsabby/LLVMSharp
:> powershell ./LLVMSharp/GenLLVMDLL.ps1
:> build.bat C:\path\llvm.dll C:\path\to\llvm\include
:> cd LLVMSharp\src
:> dotnet build
```
## Features
@@ -52,69 +50,70 @@ The tutorials have been tested to run on Windows and Linux, however the build (u
* Functions are put in a C# class called LLVM and the LLVM prefix is removed from the functions, for example: LLVM.ModuleCreateWithName("LLVMSharpIntro");
* For certain functions requiring a pointer to an array, you must pass the array indexed into its first element. If you do not want to pass any element, you can either pass an array with a dummy element, or make a single type and pass it, otherwise you won't be able to compile, for example: LLVM.FunctionType(LLVM.Int32Type(), out typesArr[0], 0, False); is equivalent to LLVM.FunctionType(LLVM.Int32Type(), out type, 0, False);
## Example application
```csharp
using System;
using System.Runtime.InteropServices;
using LLVMSharp;
internal sealed class Program
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int Add(int a, int b);
using System;
using System.Runtime.InteropServices;
using LLVMSharp;
private static void Main(string[] args)
internal sealed class Program
{
LLVMBool False = new LLVMBool(0);
LLVMModuleRef mod = LLVM.ModuleCreateWithName("LLVMSharpIntro");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int Add(int a, int b);
LLVMTypeRef[] param_types = {LLVM.Int32Type(), LLVM.Int32Type()};
LLVMTypeRef ret_type = LLVM.FunctionType(LLVM.Int32Type(), out param_types[0], 2, False);
LLVMValueRef sum = LLVM.AddFunction(mod, "sum", ret_type);
private static void Main(string[] args)
{
LLVMBool Success = new LLVMBool(0);
LLVMModuleRef mod = LLVM.ModuleCreateWithName("LLVMSharpIntro");
LLVMBasicBlockRef entry = LLVM.AppendBasicBlock(sum, "entry");
LLVMTypeRef[] param_types = { LLVM.Int32Type(), LLVM.Int32Type() };
LLVMTypeRef ret_type = LLVM.FunctionType(LLVM.Int32Type(), param_types, false);
LLVMValueRef sum = LLVM.AddFunction(mod, "sum", ret_type);
LLVMBuilderRef builder = LLVM.CreateBuilder();
LLVM.PositionBuilderAtEnd(builder, entry);
LLVMValueRef tmp = LLVM.BuildAdd(builder, LLVM.GetParam(sum, 0), LLVM.GetParam(sum, 1), "tmp");
LLVM.BuildRet(builder, tmp);
LLVMBasicBlockRef entry = LLVM.AppendBasicBlock(sum, "entry");
IntPtr error;
LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMAbortProcessAction, out error);
LLVM.DisposeMessage(error);
LLVMBuilderRef builder = LLVM.CreateBuilder();
LLVM.PositionBuilderAtEnd(builder, entry);
LLVMValueRef tmp = LLVM.BuildAdd(builder, LLVM.GetParam(sum, 0), LLVM.GetParam(sum, 1), "tmp");
LLVM.BuildRet(builder, tmp);
LLVMExecutionEngineRef engine;
if (LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != Success)
{
Console.WriteLine($"Error: {error}");
}
LLVM.LinkInMCJIT();
LLVM.InitializeNativeTarget();
LLVM.InitializeNativeAsmPrinter();
LLVM.LinkInMCJIT();
var options = new LLVMMCJITCompilerOptions();
var optionsSize = (4*sizeof (int)) + IntPtr.Size; // LLVMMCJITCompilerOptions has 4 ints and a pointer
LLVM.InitializeX86TargetMC();
LLVM.InitializeX86Target();
LLVM.InitializeX86TargetInfo();
LLVM.InitializeX86AsmParser();
LLVM.InitializeX86AsmPrinter();
LLVM.InitializeMCJITCompilerOptions(out options, optionsSize);
LLVM.CreateMCJITCompilerForModule(out engine, mod, out options, optionsSize, out error);
LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions { NoFramePointerElim = 1 };
LLVM.InitializeMCJITCompilerOptions(options);
if (LLVM.CreateMCJITCompilerForModule(out var engine, mod, options, out error) != Success)
{
Console.WriteLine($"Error: {error}");
}
var addMethod = (Add) Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(engine, sum), typeof (Add));
int result = addMethod(10, 10);
var addMethod = (Add)Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(engine, sum), typeof(Add));
int result = addMethod(10, 10);
Console.WriteLine("Result of sum is: " + result);
Console.WriteLine("Result of sum is: " + result);
if (LLVM.WriteBitcodeToFile(mod, "sum.bc") != 0)
{
Console.WriteLine("error writing bitcode to file, skipping");
}
if (LLVM.WriteBitcodeToFile(mod, "sum.bc") != 0)
{
Console.WriteLine("error writing bitcode to file, skipping");
}
LLVM.DumpModule(mod);
LLVM.DumpModule(mod);
LLVM.DisposeBuilder(builder);
LLVM.DisposeExecutionEngine(engine);
Console.ReadKey();
LLVM.DisposeBuilder(builder);
LLVM.DisposeExecutionEngine(engine);
}
}
}
````
## Microsoft Open Source Code of Conduct
View
@@ -1,7 +1,7 @@
namespace LLVMSharp
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public sealed class ExecutionEngine : IDisposable
{
@@ -11,9 +11,7 @@ public sealed class ExecutionEngine : IDisposable
public static ExecutionEngine Create(Module module)
{
IntPtr error;
LLVMExecutionEngineRef instance;
if (!LLVM.CreateExecutionEngineForModule(out instance, module.instance, out error))
if (!LLVM.CreateExecutionEngineForModule(out var instance, module.instance, out string error))
{
ThrowError(error);
}
@@ -23,23 +21,18 @@ public static ExecutionEngine Create(Module module)
public static ExecutionEngine CreateInterpreter(Module module)
{
IntPtr error;
LLVMExecutionEngineRef instance;
if (LLVM.CreateInterpreterForModule(out instance, module.instance, out error))
if (LLVM.CreateInterpreterForModule(out var instance, module.instance, out string error))
{
ThrowError(error);
}
return new ExecutionEngine(instance);
}
public static ExecutionEngine CreateMCJITCompiler(Module module, LLVMMCJITCompilerOptions options, size_t optionsSize)
public static ExecutionEngine CreateMCJITCompiler(Module module, LLVMMCJITCompilerOptions options)
{
LLVM.InitializeMCJITCompilerOptions(out options, optionsSize);
IntPtr error;
LLVMExecutionEngineRef instance;
if (LLVM.CreateMCJITCompilerForModule(out instance, module.instance, out options, optionsSize, out error))
LLVM.InitializeMCJITCompilerOptions(options);
if (LLVM.CreateMCJITCompilerForModule(out var instance, module.instance, options, out var error))
{
ThrowError(error);
}
@@ -150,11 +143,10 @@ private void Dispose(bool disposing)
this.disposed = true;
}
private static void ThrowError(IntPtr error)
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowError(string error)
{
string errormessage = Marshal.PtrToStringAnsi(error);
LLVM.DisposeMessage(error);
throw new Exception(errormessage);
throw new Exception(error);
}
}
}
Oops, something went wrong.

0 comments on commit c7fc838

Please sign in to comment.