Skip to content

Commit dedccf8

Browse files
authored
Updated README example to use OOP api (#168)
1 parent cd43d99 commit dedccf8

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

README.md

+17-20
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ The tutorials have been tested to run on Windows and Linux, however the build (u
6464
```csharp
6565
using System;
6666
using System.Runtime.InteropServices;
67-
using LLVMSharp;
67+
using LLVMSharp.Interop;
6868

6969
internal sealed class Program
7070
{
@@ -73,21 +73,20 @@ The tutorials have been tested to run on Windows and Linux, however the build (u
7373

7474
private static void Main(string[] args)
7575
{
76-
LLVMBool Success = new LLVMBool(0);
77-
LLVMModuleRef mod = LLVM.ModuleCreateWithName("LLVMSharpIntro");
76+
LLVMModuleRef mod = LLVMModuleRef.CreateWithName("LLVMSharpIntro");
7877

79-
LLVMTypeRef[] param_types = { LLVM.Int32Type(), LLVM.Int32Type() };
80-
LLVMTypeRef ret_type = LLVM.FunctionType(LLVM.Int32Type(), param_types, false);
81-
LLVMValueRef sum = LLVM.AddFunction(mod, "sum", ret_type);
78+
LLVMTypeRef[] param_types = new LLVMTypeRef[2] { LLVMTypeRef.Int32, LLVMTypeRef.Int32 };
79+
LLVMTypeRef ret_type = LLVMTypeRef.CreateFunction(LLVMTypeRef.Int32, param_types);
80+
LLVMValueRef sum = mod.AddFunction("sum", ret_type);
8281

83-
LLVMBasicBlockRef entry = LLVM.AppendBasicBlock(sum, "entry");
82+
LLVMBasicBlockRef entry = sum.AppendBasicBlock("entry");
8483

85-
LLVMBuilderRef builder = LLVM.CreateBuilder();
86-
LLVM.PositionBuilderAtEnd(builder, entry);
87-
LLVMValueRef tmp = LLVM.BuildAdd(builder, LLVM.GetParam(sum, 0), LLVM.GetParam(sum, 1), "tmp");
88-
LLVM.BuildRet(builder, tmp);
84+
LLVMBuilderRef builder = LLVMBuilderRef.Create(mod.Context);
85+
builder.PositionAtEnd(entry);
86+
LLVMValueRef tmp = builder.BuildAdd(sum.Params[0], sum.Params[1], "tmp");
87+
builder.BuildRet(tmp);
8988

90-
if (LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != Success)
89+
if (!mod.TryVerify(LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error))
9190
{
9291
Console.WriteLine($"Error: {error}");
9392
}
@@ -101,26 +100,24 @@ The tutorials have been tested to run on Windows and Linux, however the build (u
101100
LLVM.InitializeX86AsmPrinter();
102101

103102
LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions { NoFramePointerElim = 1 };
104-
LLVM.InitializeMCJITCompilerOptions(options);
105-
if (LLVM.CreateMCJITCompilerForModule(out var engine, mod, options, out error) != Success)
103+
if (!mod.TryCreateMCJITCompiler(out var engine, ref options, out error))
106104
{
107105
Console.WriteLine($"Error: {error}");
108106
}
109107

110-
var addMethod = (Add)Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(engine, sum), typeof(Add));
108+
var addMethod = (Add)Marshal.GetDelegateForFunctionPointer(engine.GetPointerToGlobal(sum), typeof(Add));
111109
int result = addMethod(10, 10);
112110

113111
Console.WriteLine("Result of sum is: " + result);
114112

115-
if (LLVM.WriteBitcodeToFile(mod, "sum.bc") != 0)
113+
if (mod.WriteBitcodeToFile("sum.bc") != 0)
116114
{
117115
Console.WriteLine("error writing bitcode to file, skipping");
118116
}
119117

120-
LLVM.DumpModule(mod);
121-
122-
LLVM.DisposeBuilder(builder);
123-
LLVM.DisposeExecutionEngine(engine);
118+
mod.Dump();
119+
builder.Dispose();
120+
engine.Dispose();
124121
}
125122
}
126123
````

0 commit comments

Comments
 (0)