forked from dotnet/LLVMSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIR.cs
More file actions
186 lines (154 loc) · 6.54 KB
/
IR.cs
File metadata and controls
186 lines (154 loc) · 6.54 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
using System;
using LLVMSharp.Interop;
using NUnit.Framework;
using NUnit.Framework.Internal;
namespace LLVMSharp.UnitTests;
[Platform(Exclude = "32-bit")]
public class IR
{
[Test]
public void AddsSigned()
{
var op1 = 0;
var op2 = 0;
using var module = LLVMModuleRef.CreateWithName("test_add");
(_, var def) = module.AddFunction(
LLVMTypeRef.Int32, "add", [LLVMTypeRef.Int32, LLVMTypeRef.Int32], (f, b) =>
{
var p1 = f.GetParam(0);
var p2 = f.GetParam(1);
var add = b.BuildAdd(p1, p2);
var ret = b.BuildRet(add);
});
module.Verify(LLVMVerifierFailureAction.LLVMPrintMessageAction);
_ = LLVM.InitializeNativeTarget();
_ = LLVM.InitializeNativeAsmParser();
_ = LLVM.InitializeNativeAsmPrinter();
using var engine = module.CreateMCJITCompiler();
var func = engine.GetPointerToGlobal<Int32Int32Int32Delegate>(def);
var result = op1 + op2;
Assert.That(func(op1, op2), Is.EqualTo(result));
engine.FreeMachineCodeForFunction(def);
_ = engine.RemoveModule(module);
}
[Test]
public void ShiftsRight([Range(0, 256)] int op1, [Range(0, 8)] int op2)
{
using var module = LLVMModuleRef.CreateWithName("test_lshift");
(_, var def) = module.AddFunction(
LLVMTypeRef.Int32, "lshift", [LLVMTypeRef.Int32, LLVMTypeRef.Int32], (f, b) =>
{
var p1 = f.GetParam(0);
var p2 = f.GetParam(1);
var shift = b.BuildLShr(p1, p2);
var ret = b.BuildRet(shift);
});
module.Verify(LLVMVerifierFailureAction.LLVMPrintMessageAction);
_ = LLVM.InitializeNativeTarget();
_ = LLVM.InitializeNativeAsmParser();
_ = LLVM.InitializeNativeAsmPrinter();
using var engine = module.CreateMCJITCompiler();
var func = engine.GetPointerToGlobal<Int32Int32Int32Delegate>(def);
var result = op1 >> op2;
Assert.That(func(op1, op2), Is.EqualTo(result));
engine.FreeMachineCodeForFunction(def);
_ = engine.RemoveModule(module);
}
[Test]
public void ComparesGreaterThan([Range(0, 10)] int op1, [Range(0, 10)] int op2)
{
using var module = LLVMModuleRef.CreateWithName("test_greaterthan");
(_, var def) = module.AddFunction(
LLVMTypeRef.Int1, "greaterthan", [LLVMTypeRef.Int32, LLVMTypeRef.Int32], (f, b) =>
{
var p1 = f.GetParam(0);
var p2 = f.GetParam(1);
var cmp = b.BuildICmp(LLVMIntPredicate.LLVMIntSGT, p1, p2);
var ret = b.BuildRet(cmp);
});
module.Verify(LLVMVerifierFailureAction.LLVMPrintMessageAction);
_ = LLVM.InitializeNativeTarget();
_ = LLVM.InitializeNativeAsmParser();
_ = LLVM.InitializeNativeAsmPrinter();
using var engine = module.CreateMCJITCompiler();
var func = engine.GetPointerToGlobal<Int32Int32Int8Delegate>(def);
var result = op1 > op2 ? 1 : 0;
Assert.That(func(op1, op2), Is.EqualTo(result));
engine.FreeMachineCodeForFunction(def);
_ = engine.RemoveModule(module);
}
[Test]
public void CallsFunction([Range(0, 10)] int op1, [Range(0, 10)] int op2)
{
using var module = LLVMModuleRef.CreateWithName("test_call");
(var addType, var addDef) = module.AddFunction(
LLVMTypeRef.Int32, "add", [LLVMTypeRef.Int32, LLVMTypeRef.Int32], (f, b) =>
{
var p1 = f.GetParam(0);
var p2 = f.GetParam(1);
var add = b.BuildAdd(p1, p2);
var ret = b.BuildRet(add);
});
(_, var entryDef) = module.AddFunction(
LLVMTypeRef.Int32, "entry", [LLVMTypeRef.Int32, LLVMTypeRef.Int32], (f, b) =>
{
var call = b.BuildCall2(addType, addDef, f.GetParams(), "");
var ret = b.BuildRet(call);
});
module.Verify(LLVMVerifierFailureAction.LLVMPrintMessageAction);
_ = LLVM.InitializeNativeTarget();
_ = LLVM.InitializeNativeAsmParser();
_ = LLVM.InitializeNativeAsmPrinter();
using var engine = module.CreateMCJITCompiler();
var func = engine.GetPointerToGlobal<Int32Int32Int32Delegate>(entryDef);
var result = op1 + op2;
Assert.That(func(op1, op2), Is.EqualTo(result));
engine.FreeMachineCodeForFunction(entryDef);
engine.FreeMachineCodeForFunction(addDef);
_ = engine.RemoveModule(module);
}
[Test]
public void ReturnsConstant([Range(0, 10)] int input)
{
var uInput = (uint)input;
using var module = LLVMModuleRef.CreateWithName("test_constant");
(_, var def) = module.AddFunction(
LLVMTypeRef.Int32, "constant", [], (f, b) =>
{
var value = LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, uInput);
var ret = b.BuildRet(value);
});
module.Verify(LLVMVerifierFailureAction.LLVMPrintMessageAction);
_ = LLVM.InitializeNativeTarget();
_ = LLVM.InitializeNativeAsmParser();
_ = LLVM.InitializeNativeAsmPrinter();
using var engine = module.CreateMCJITCompiler();
var func = engine.GetPointerToGlobal<Int32Delegate>(def);
Assert.That(func(), Is.EqualTo(input));
engine.FreeMachineCodeForFunction(def);
_ = engine.RemoveModule(module);
}
[Test]
public void ReturnsSizeOf()
{
using var module = LLVMModuleRef.CreateWithName("test_sizeof");
var str = LLVMTypeRef.CreateStruct(new[] { LLVMTypeRef.Int32, LLVMTypeRef.Int32 }, true);
(_, var def) = module.AddFunction(
LLVMTypeRef.Int32, "structure", [], (f, b) =>
{
var sz = str.SizeOf;
var sz32 = b.BuildIntCast(sz, LLVMTypeRef.Int32);
var ret = b.BuildRet(sz32);
});
module.Verify(LLVMVerifierFailureAction.LLVMPrintMessageAction);
_ = LLVM.InitializeNativeTarget();
_ = LLVM.InitializeNativeAsmParser();
_ = LLVM.InitializeNativeAsmPrinter();
using var engine = module.CreateMCJITCompiler();
var func = engine.GetPointerToGlobal<Int32Delegate>(def);
Assert.That(func(), Is.EqualTo(8));
engine.FreeMachineCodeForFunction(def);
_ = engine.RemoveModule(module);
}
}