44
55LLVMSharp 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.
66
7- 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.
7+ 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.
88
99## Building LLVMSharp
1010
@@ -24,7 +24,6 @@ On Windows using Microsoft.NET:
2424``` bash
2525 :> cd c:\p ath\t o\l lvm_source\{ Release| Debug}\l ib
2626 :> git clone http://github.com/mjsabby/LLVMSharp
27- :> cd LLVMSharp
2827 :> powershell ./LLVMSharp/GenLLVMDLL.ps1
2928 :> build.bat C:\p ath\l lvm.dll C:\p ath\t o\l lvm\i nclude
3029```
@@ -57,67 +56,63 @@ The tutorials have been tested to run on Windows and Linux, however the build (u
5756
5857## Example application
5958
60- Main:
61-
6259``` csharp
63- LLVMBool False = new LLVMBool (0 );
64- LLVMModuleRef mod = LLVM .ModuleCreateWithName (" LLVMSharpIntro" );
65-
66- LLVMTypeRef [] param_types = { LLVM .Int32Type (), LLVM .Int32Type () };
67- LLVMTypeRef ret_type = LLVM .FunctionType (LLVM .Int32Type (), out param_types [0 ], 2 , False );
68- LLVMValueRef sum = LLVM .AddFunction (mod , " sum" , ret_type );
60+ using System ;
61+ using System .Runtime .InteropServices ;
62+ using LLVMSharp ;
6963
70- LLVMBasicBlockRef entry = LLVM .AppendBasicBlock (sum , " entry" );
64+ internal sealed class Program
65+ {
66+ [UnmanagedFunctionPointer (CallingConvention .Cdecl )]
67+ public delegate int Add (int a , int b );
7168
72- LLVMBuilderRef builder = LLVM . CreateBuilder ();
73- LLVM . PositionBuilderAtEnd ( builder , entry );
74- LLVMValueRef tmp = LLVM . BuildAdd ( builder , LLVM . GetParam ( sum , 0 ), LLVM . GetParam ( sum , 1 ), " tmp " );
75- LLVM .BuildRet ( builder , tmp );
69+ private static void Main ( string [] args )
70+ {
71+ LLVMBool False = new LLVMBool ( 0 );
72+ LLVMModuleRef mod = LLVM .ModuleCreateWithName ( " LLVMSharpIntro " );
7673
77- IntPtr error ;
78- LLVM .VerifyModule ( mod , LLVMVerifierFailureAction . LLVMAbortProcessAction , out error );
79- LLVM .DisposeMessage ( error );
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 );
8077
81- LLVMExecutionEngineRef engine ;
78+ LLVMBasicBlockRef entry = LLVM . AppendBasicBlock ( sum , " entry " ) ;
8279
83- LLVM .LinkInMCJIT ();
84- LLVM .InitializeX86Target ();
85- LLVM .InitializeX86TargetInfo ();
86- LLVM .InitializeX86TargetMC ();
87- LLVM .InitializeX86AsmPrinter ();
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 );
8884
89- var platform = Environment .OSVersion .Platform ;
90- if (platform == PlatformID .Win32NT ) // On Windows, LLVM currently (3.6) does not support PE/COFF
91- {
92- LLVM .SetTarget (mod , Marshal .PtrToStringAnsi (LLVM .GetDefaultTargetTriple ()) + " -elf" );
93- }
85+ IntPtr error ;
86+ LLVM .VerifyModule (mod , LLVMVerifierFailureAction .LLVMAbortProcessAction , out error );
87+ LLVM .DisposeMessage (error );
9488
95- var options = new LLVMMCJITCompilerOptions ();
96- var optionsSize = (4 * sizeof (int )) + IntPtr .Size ; // LLVMMCJITCompilerOptions has 4 ints and a pointer
89+ LLVMExecutionEngineRef engine ;
9790
98- LLVM .InitializeMCJITCompilerOptions (out options , optionsSize );
99- LLVM .CreateMCJITCompilerForModule (out engine , mod , out options , optionsSize , out error );
91+ LLVM .LinkInMCJIT ();
92+ LLVM .InitializeNativeTarget ();
93+ LLVM .InitializeNativeAsmPrinter ();
10094
101- var addMethod = ( Add ) Marshal . GetDelegateForFunctionPointer ( LLVM . GetPointerToGlobal ( engine , sum ), typeof ( Add ) );
102- int result = addMethod ( 10 , 10 );
95+ var options = new LLVMMCJITCompilerOptions ( );
96+ var optionsSize = ( 4 * sizeof ( int )) + IntPtr . Size ; // LLVMMCJITCompilerOptions has 4 ints and a pointer
10397
104- Console .WriteLine (" Result of sum is: " + result );
98+ LLVM .InitializeMCJITCompilerOptions (out options , optionsSize );
99+ LLVM .CreateMCJITCompilerForModule (out engine , mod , out options , optionsSize , out error );
105100
106- if (LLVM .WriteBitcodeToFile (mod , " sum.bc" ) != 0 )
107- {
108- Console .WriteLine (" error writing bitcode to file, skipping" );
109- }
101+ var addMethod = (Add ) Marshal .GetDelegateForFunctionPointer (LLVM .GetPointerToGlobal (engine , sum ), typeof (Add ));
102+ int result = addMethod (10 , 10 );
110103
111- LLVM . DumpModule ( mod );
104+ Console . WriteLine ( " Result of sum is: " + result );
112105
113- LLVM .DisposeBuilder ( builder );
114- LLVM . DisposeExecutionEngine ( engine );
115- Console .ReadKey ( );
116- ````
106+ if ( LLVM .WriteBitcodeToFile ( mod , " sum.bc " ) != 0 )
107+ {
108+ Console .WriteLine ( " error writing bitcode to file, skipping " );
109+ }
117110
118- Delegate definition :
111+ LLVM . DumpModule ( mod );
119112
120- ```csharp
121- [UnmanagedFunctionPointer (CallingConvention .Cdecl )]
122- public delegate int Add (int a , int b );
123- ```
113+ LLVM .DisposeBuilder (builder );
114+ LLVM .DisposeExecutionEngine (engine );
115+ Console .ReadKey ();
116+ }
117+ }
118+ ````
0 commit comments