4
4
5
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.
6
6
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.
8
8
9
9
## Building LLVMSharp
10
10
@@ -24,7 +24,6 @@ On Windows using Microsoft.NET:
24
24
``` bash
25
25
:> cd c:\p ath\t o\l lvm_source\{ Release| Debug}\l ib
26
26
:> git clone http://github.com/mjsabby/LLVMSharp
27
- :> cd LLVMSharp
28
27
:> powershell ./LLVMSharp/GenLLVMDLL.ps1
29
28
:> build.bat C:\p ath\l lvm.dll C:\p ath\t o\l lvm\i nclude
30
29
```
@@ -57,67 +56,63 @@ The tutorials have been tested to run on Windows and Linux, however the build (u
57
56
58
57
## Example application
59
58
60
- Main:
61
-
62
59
``` 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 ;
69
63
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 );
71
68
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 " );
76
73
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 );
80
77
81
- LLVMExecutionEngineRef engine ;
78
+ LLVMBasicBlockRef entry = LLVM . AppendBasicBlock ( sum , " entry " ) ;
82
79
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 );
88
84
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 );
94
88
95
- var options = new LLVMMCJITCompilerOptions ();
96
- var optionsSize = (4 * sizeof (int )) + IntPtr .Size ; // LLVMMCJITCompilerOptions has 4 ints and a pointer
89
+ LLVMExecutionEngineRef engine ;
97
90
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 ();
100
94
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
103
97
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 );
105
100
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 );
110
103
111
- LLVM . DumpModule ( mod );
104
+ Console . WriteLine ( " Result of sum is: " + result );
112
105
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
+ }
117
110
118
- Delegate definition :
111
+ LLVM . DumpModule ( mod );
119
112
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