-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
66 lines (51 loc) · 2.35 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Runtime.InteropServices;
using LLVMSharp;
namespace feel_llvm
{
internal sealed class Program
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int Add(int a, int b);
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
LLVMBool Success = new LLVMBool(0);
LLVMModuleRef mod = LLVM.ModuleCreateWithName("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);
LLVMBasicBlockRef entry = LLVM.AppendBasicBlock(sum, "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);
if (LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != Success)
{
Console.WriteLine($"Error: {error}");
}
LLVM.LinkInMCJIT();
LLVM.InitializeX86TargetMC();
LLVM.InitializeX86Target();
LLVM.InitializeX86TargetInfo();
LLVM.InitializeX86AsmParser();
LLVM.InitializeX86AsmPrinter();
LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions { NoFramePointerElim = 1 };
LLVM.InitializeMCJITCompilerOptions(options);
if (LLVM.CreateMCJITCompilerForModule(out var engine, mod, options, out error) != Success)
{
Console.WriteLine($"Error: {error}");
}
var addMethod = (Add)Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(engine, sum), typeof(Add));
int result = addMethod(10, 10);
Console.WriteLine("Result of sum is: " + result);
if (LLVM.WriteBitcodeToFile(mod, "sum.bc") != 0)
{
Console.WriteLine("error writing bitcode to file, skipping");
}
LLVM.DumpModule(mod);
LLVM.DisposeBuilder(builder);
LLVM.DisposeExecutionEngine(engine);
}
}
}