Skip to content

Commit c7fc838

Browse files
committed
LLVMSharp 5.0 release.
1 parent 22dcd97 commit c7fc838

File tree

5 files changed

+243
-92
lines changed

5 files changed

+243
-92
lines changed

README.md

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,28 @@
22

33
[![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)
44

5-
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.
5+
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.
66

7-
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.
7+
[**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.
88

99
## Building LLVMSharp
1010

11-
On Linux using Mono:
11+
On Linux using .NET Core:
1212

1313
```bash
14-
$ git clone http://github.com/mjsabby/LLVMSharp
15-
$ cd LLVMSharp
16-
$ chmod +x build.sh
17-
$ ./build.sh /path/to/libLLVM.so /path/llvm/include
14+
$ git clone http://github.com/Microsoft/LLVMSharp
15+
$ cd LLVMSharp/src
16+
$ dotnet build
1817
```
1918

20-
On Windows using Microsoft.NET:
19+
On Windows using .NET Core
2120

22-
**Note:** - you need to run from the Visual Studio Command Prompt of the architecture you want to target.
21+
**Note:** - you need to run these commands from the Visual Studio Developer Command Prompt.
2322

2423
```bash
25-
:> cd c:\path\to\llvm_source\{Release|Debug}\lib
2624
:> git clone http://github.com/mjsabby/LLVMSharp
27-
:> powershell ./LLVMSharp/GenLLVMDLL.ps1
28-
:> build.bat C:\path\llvm.dll C:\path\to\llvm\include
25+
:> cd LLVMSharp\src
26+
:> dotnet build
2927
```
3028

3129
## Features
@@ -52,69 +50,70 @@ The tutorials have been tested to run on Windows and Linux, however the build (u
5250

5351
* Functions are put in a C# class called LLVM and the LLVM prefix is removed from the functions, for example: LLVM.ModuleCreateWithName("LLVMSharpIntro");
5452

55-
* 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);
56-
5753
## Example application
5854

5955
```csharp
60-
using System;
61-
using System.Runtime.InteropServices;
62-
using LLVMSharp;
63-
64-
internal sealed class Program
65-
{
66-
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
67-
public delegate int Add(int a, int b);
56+
using System;
57+
using System.Runtime.InteropServices;
58+
using LLVMSharp;
6859

69-
private static void Main(string[] args)
60+
internal sealed class Program
7061
{
71-
LLVMBool False = new LLVMBool(0);
72-
LLVMModuleRef mod = LLVM.ModuleCreateWithName("LLVMSharpIntro");
62+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
63+
public delegate int Add(int a, int b);
7364

74-
LLVMTypeRef[] param_types = {LLVM.Int32Type(), LLVM.Int32Type()};
75-
LLVMTypeRef ret_type = LLVM.FunctionType(LLVM.Int32Type(), out param_types[0], 2, False);
76-
LLVMValueRef sum = LLVM.AddFunction(mod, "sum", ret_type);
65+
private static void Main(string[] args)
66+
{
67+
LLVMBool Success = new LLVMBool(0);
68+
LLVMModuleRef mod = LLVM.ModuleCreateWithName("LLVMSharpIntro");
7769

78-
LLVMBasicBlockRef entry = LLVM.AppendBasicBlock(sum, "entry");
70+
LLVMTypeRef[] param_types = { LLVM.Int32Type(), LLVM.Int32Type() };
71+
LLVMTypeRef ret_type = LLVM.FunctionType(LLVM.Int32Type(), param_types, false);
72+
LLVMValueRef sum = LLVM.AddFunction(mod, "sum", ret_type);
7973

80-
LLVMBuilderRef builder = LLVM.CreateBuilder();
81-
LLVM.PositionBuilderAtEnd(builder, entry);
82-
LLVMValueRef tmp = LLVM.BuildAdd(builder, LLVM.GetParam(sum, 0), LLVM.GetParam(sum, 1), "tmp");
83-
LLVM.BuildRet(builder, tmp);
74+
LLVMBasicBlockRef entry = LLVM.AppendBasicBlock(sum, "entry");
8475

85-
IntPtr error;
86-
LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMAbortProcessAction, out error);
87-
LLVM.DisposeMessage(error);
76+
LLVMBuilderRef builder = LLVM.CreateBuilder();
77+
LLVM.PositionBuilderAtEnd(builder, entry);
78+
LLVMValueRef tmp = LLVM.BuildAdd(builder, LLVM.GetParam(sum, 0), LLVM.GetParam(sum, 1), "tmp");
79+
LLVM.BuildRet(builder, tmp);
8880

89-
LLVMExecutionEngineRef engine;
81+
if (LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != Success)
82+
{
83+
Console.WriteLine($"Error: {error}");
84+
}
9085

91-
LLVM.LinkInMCJIT();
92-
LLVM.InitializeNativeTarget();
93-
LLVM.InitializeNativeAsmPrinter();
86+
LLVM.LinkInMCJIT();
9487

95-
var options = new LLVMMCJITCompilerOptions();
96-
var optionsSize = (4*sizeof (int)) + IntPtr.Size; // LLVMMCJITCompilerOptions has 4 ints and a pointer
88+
LLVM.InitializeX86TargetMC();
89+
LLVM.InitializeX86Target();
90+
LLVM.InitializeX86TargetInfo();
91+
LLVM.InitializeX86AsmParser();
92+
LLVM.InitializeX86AsmPrinter();
9793

98-
LLVM.InitializeMCJITCompilerOptions(out options, optionsSize);
99-
LLVM.CreateMCJITCompilerForModule(out engine, mod, out options, optionsSize, out error);
94+
LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions { NoFramePointerElim = 1 };
95+
LLVM.InitializeMCJITCompilerOptions(options);
96+
if (LLVM.CreateMCJITCompilerForModule(out var engine, mod, options, out error) != Success)
97+
{
98+
Console.WriteLine($"Error: {error}");
99+
}
100100

101-
var addMethod = (Add) Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(engine, sum), typeof (Add));
102-
int result = addMethod(10, 10);
101+
var addMethod = (Add)Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(engine, sum), typeof(Add));
102+
int result = addMethod(10, 10);
103103

104-
Console.WriteLine("Result of sum is: " + result);
104+
Console.WriteLine("Result of sum is: " + result);
105105

106-
if (LLVM.WriteBitcodeToFile(mod, "sum.bc") != 0)
107-
{
108-
Console.WriteLine("error writing bitcode to file, skipping");
109-
}
106+
if (LLVM.WriteBitcodeToFile(mod, "sum.bc") != 0)
107+
{
108+
Console.WriteLine("error writing bitcode to file, skipping");
109+
}
110110

111-
LLVM.DumpModule(mod);
111+
LLVM.DumpModule(mod);
112112

113-
LLVM.DisposeBuilder(builder);
114-
LLVM.DisposeExecutionEngine(engine);
115-
Console.ReadKey();
113+
LLVM.DisposeBuilder(builder);
114+
LLVM.DisposeExecutionEngine(engine);
115+
}
116116
}
117-
}
118117
````
119118

120119
## Microsoft Open Source Code of Conduct

src/ExecutionEngine.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace LLVMSharp
22
{
33
using System;
4-
using System.Runtime.InteropServices;
4+
using System.Runtime.CompilerServices;
55

66
public sealed class ExecutionEngine : IDisposable
77
{
@@ -11,9 +11,7 @@ public sealed class ExecutionEngine : IDisposable
1111

1212
public static ExecutionEngine Create(Module module)
1313
{
14-
IntPtr error;
15-
LLVMExecutionEngineRef instance;
16-
if (!LLVM.CreateExecutionEngineForModule(out instance, module.instance, out error))
14+
if (!LLVM.CreateExecutionEngineForModule(out var instance, module.instance, out string error))
1715
{
1816
ThrowError(error);
1917
}
@@ -23,23 +21,18 @@ public static ExecutionEngine Create(Module module)
2321

2422
public static ExecutionEngine CreateInterpreter(Module module)
2523
{
26-
IntPtr error;
27-
LLVMExecutionEngineRef instance;
28-
if (LLVM.CreateInterpreterForModule(out instance, module.instance, out error))
24+
if (LLVM.CreateInterpreterForModule(out var instance, module.instance, out string error))
2925
{
3026
ThrowError(error);
3127
}
3228

3329
return new ExecutionEngine(instance);
3430
}
3531

36-
public static ExecutionEngine CreateMCJITCompiler(Module module, LLVMMCJITCompilerOptions options, size_t optionsSize)
32+
public static ExecutionEngine CreateMCJITCompiler(Module module, LLVMMCJITCompilerOptions options)
3733
{
38-
LLVM.InitializeMCJITCompilerOptions(out options, optionsSize);
39-
40-
IntPtr error;
41-
LLVMExecutionEngineRef instance;
42-
if (LLVM.CreateMCJITCompilerForModule(out instance, module.instance, out options, optionsSize, out error))
34+
LLVM.InitializeMCJITCompilerOptions(options);
35+
if (LLVM.CreateMCJITCompilerForModule(out var instance, module.instance, options, out var error))
4336
{
4437
ThrowError(error);
4538
}
@@ -150,11 +143,10 @@ private void Dispose(bool disposing)
150143
this.disposed = true;
151144
}
152145

153-
private static void ThrowError(IntPtr error)
146+
[MethodImpl(MethodImplOptions.NoInlining)]
147+
private static void ThrowError(string error)
154148
{
155-
string errormessage = Marshal.PtrToStringAnsi(error);
156-
LLVM.DisposeMessage(error);
157-
throw new Exception(errormessage);
149+
throw new Exception(error);
158150
}
159151
}
160152
}

0 commit comments

Comments
 (0)