@@ -4,7 +4,7 @@
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.
If you're on Windows, consider using the [**LLVMSharp 3.6 NuGet Package** ](http://www.nuget.org/packages/LLVMSharp/3.6 .0) - built from LLVM 3.6 Release.
If you're on Windows, consider using the [**LLVMSharp 3.7 NuGet Package** ](http://www.nuget.org/packages/LLVMSharp/3.7 .0) - built from LLVM 3.7 Release.
## Building LLVMSharp
@@ -24,7 +24,6 @@ On Windows using Microsoft.NET:
```bash
:> cd c:\p ath\t o\l lvm_source\{ Release| Debug}\l ib
:> git clone http://github.com/mjsabby/LLVMSharp
:> cd LLVMSharp
:> powershell ./LLVMSharp/GenLLVMDLL.ps1
:> build.bat C:\p ath\l lvm.dll C:\p ath\t o\l lvm\i nclude
```
@@ -57,67 +56,63 @@ The tutorials have been tested to run on Windows and Linux, however the build (u
## Example application
Main:
```csharp
LLVMBool False = new LLVMBool (0 );
LLVMModuleRef mod = LLVM .ModuleCreateWithName (" LLVMSharpIntro" );
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 );
using System ;
using System .Runtime .InteropServices ;
using LLVMSharp ;
LLVMBasicBlockRef entry = LLVM .AppendBasicBlock (sum , " entry" );
internal sealed class Program
{
[UnmanagedFunctionPointer (CallingConvention .Cdecl )]
public delegate int Add (int a , int b );
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 );
private static void Main ( string [] args )
{
LLVMBool False = new LLVMBool ( 0 );
LLVMModuleRef mod = LLVM .ModuleCreateWithName ( " LLVMSharpIntro " );
IntPtr error ;
LLVM .VerifyModule ( mod , LLVMVerifierFailureAction . LLVMAbortProcessAction , out error );
LLVM .DisposeMessage ( error );
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 );
LLVMExecutionEngineRef engine ;
LLVMBasicBlockRef entry = LLVM . AppendBasicBlock ( sum , " entry " ) ;
LLVM .LinkInMCJIT ();
LLVM .InitializeX86Target ();
LLVM .InitializeX86TargetInfo ();
LLVM .InitializeX86TargetMC ();
LLVM .InitializeX86AsmPrinter ();
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 );
var platform = Environment .OSVersion .Platform ;
if (platform == PlatformID .Win32NT ) // On Windows, LLVM currently (3.6) does not support PE/COFF
{
LLVM .SetTarget (mod , Marshal .PtrToStringAnsi (LLVM .GetDefaultTargetTriple ()) + " -elf" );
}
IntPtr error ;
LLVM .VerifyModule (mod , LLVMVerifierFailureAction .LLVMAbortProcessAction , out error );
LLVM .DisposeMessage (error );
var options = new LLVMMCJITCompilerOptions ();
var optionsSize = (4 * sizeof (int )) + IntPtr .Size ; // LLVMMCJITCompilerOptions has 4 ints and a pointer
LLVMExecutionEngineRef engine ;
LLVM .InitializeMCJITCompilerOptions (out options , optionsSize );
LLVM .CreateMCJITCompilerForModule (out engine , mod , out options , optionsSize , out error );
LLVM .LinkInMCJIT ();
LLVM .InitializeNativeTarget ();
LLVM .InitializeNativeAsmPrinter ();
var addMethod = ( Add ) Marshal . GetDelegateForFunctionPointer ( LLVM . GetPointerToGlobal ( engine , sum ), typeof ( Add ) );
int result = addMethod ( 10 , 10 );
var options = new LLVMMCJITCompilerOptions ( );
var optionsSize = ( 4 * sizeof ( int )) + IntPtr . Size ; // LLVMMCJITCompilerOptions has 4 ints and a pointer
Console .WriteLine (" Result of sum is: " + result );
LLVM .InitializeMCJITCompilerOptions (out options , optionsSize );
LLVM .CreateMCJITCompilerForModule (out engine , mod , out options , optionsSize , out error );
if (LLVM .WriteBitcodeToFile (mod , " sum.bc" ) != 0 )
{
Console .WriteLine (" error writing bitcode to file, skipping" );
}
var addMethod = (Add ) Marshal .GetDelegateForFunctionPointer (LLVM .GetPointerToGlobal (engine , sum ), typeof (Add ));
int result = addMethod (10 , 10 );
LLVM . DumpModule ( mod );
Console . WriteLine ( " Result of sum is: " + result );
LLVM .DisposeBuilder ( builder );
LLVM . DisposeExecutionEngine ( engine );
Console .ReadKey ( );
````
if ( LLVM .WriteBitcodeToFile ( mod , " sum.bc " ) != 0 )
{
Console .WriteLine ( " error writing bitcode to file, skipping " );
}
Delegate definition:
LLVM . DumpModule ( mod );
```csharp
[UnmanagedFunctionPointer (CallingConvention .Cdecl )]
public delegate int Add (int a , int b );
```
LLVM .DisposeBuilder (builder );
LLVM .DisposeExecutionEngine (engine );
Console .ReadKey ();
}
}
````