Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The tutorials have been tested to run on Windows and Linux, however the build (u
```csharp
using System;
using System.Runtime.InteropServices;
using LLVMSharp;
using LLVMSharp.Interop;

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

private static void Main(string[] args)
{
LLVMBool Success = new LLVMBool(0);
LLVMModuleRef mod = LLVM.ModuleCreateWithName("LLVMSharpIntro");
LLVMModuleRef mod = LLVMModuleRef.CreateWithName("LLVMSharpIntro");

LLVMTypeRef[] param_types = { LLVM.Int32Type(), LLVM.Int32Type() };
LLVMTypeRef ret_type = LLVM.FunctionType(LLVM.Int32Type(), param_types, false);
LLVMValueRef sum = LLVM.AddFunction(mod, "sum", ret_type);
LLVMTypeRef[] param_types = new LLVMTypeRef[2] { LLVMTypeRef.Int32, LLVMTypeRef.Int32 };
LLVMTypeRef ret_type = LLVMTypeRef.CreateFunction(LLVMTypeRef.Int32, param_types);
LLVMValueRef sum = mod.AddFunction("sum", ret_type);

LLVMBasicBlockRef entry = LLVM.AppendBasicBlock(sum, "entry");
LLVMBasicBlockRef entry = sum.AppendBasicBlock("entry");

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);
LLVMBuilderRef builder = LLVMBuilderRef.Create(mod.Context);
builder.PositionAtEnd(entry);
LLVMValueRef tmp = builder.BuildAdd(sum.Params[0], sum.Params[1], "tmp");
builder.BuildRet(tmp);

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

LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions { NoFramePointerElim = 1 };
LLVM.InitializeMCJITCompilerOptions(options);
if (LLVM.CreateMCJITCompilerForModule(out var engine, mod, options, out error) != Success)
if (!mod.TryCreateMCJITCompiler(out var engine, ref options, out error))
{
Console.WriteLine($"Error: {error}");
}

var addMethod = (Add)Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(engine, sum), typeof(Add));
var addMethod = (Add)Marshal.GetDelegateForFunctionPointer(engine.GetPointerToGlobal(sum), typeof(Add));
int result = addMethod(10, 10);

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

if (LLVM.WriteBitcodeToFile(mod, "sum.bc") != 0)
if (mod.WriteBitcodeToFile("sum.bc") != 0)
{
Console.WriteLine("error writing bitcode to file, skipping");
}

LLVM.DumpModule(mod);

LLVM.DisposeBuilder(builder);
LLVM.DisposeExecutionEngine(engine);
mod.Dump();
builder.Dispose();
engine.Dispose();
}
}
````
Expand Down