Skip to content
Permalink
Browse files

Added class-based API. (#85)

  • Loading branch information...
TChatzigiannakis authored and mjsabby committed Dec 4, 2018
1 parent 7ed37c2 commit ae1f56836a0e977045d12ae1c778af70fd770f0b
Showing with 6,057 additions and 20 deletions.
  1. +1 −0 .travis.yml
  2. +8 −2 LLVMSharp.sln
  3. +9 −0 Tests/Delegates.cs
  4. +37 −0 Tests/Examples.cs
  5. +168 −0 Tests/IR.cs
  6. +29 −0 Tests/LLVMSharp.Tests.csproj
  7. +31 −0 Tests/Modules.cs
  8. +36 −0 Tests/Targets.cs
  9. +40 −0 Tests/Types.cs
  10. +34 −0 Tests/Utilities.cs
  11. +13 −0 src/Api/AtomicOrdering.cs
  12. +17 −0 src/Api/AtomicRMWBinOp.cs
  13. +16 −0 src/Api/Attribute.cs
  14. +50 −0 src/Api/CallingConvention.cs
  15. +135 −0 src/Api/Context.cs
  16. +7 −0 src/Api/Core.cs
  17. +9 −0 src/Api/DLLStorageClass.cs
  18. +19 −0 src/Api/DiagnosticInfo.cs
  19. +10 −0 src/Api/DiagnosticSeverity.cs
  20. +126 −0 src/Api/DisasmContext.cs
  21. +182 −0 src/Api/ExecutionEngine.cs
  22. +21 −0 src/Api/FatalErrorHandler.cs
  23. +57 −0 src/Api/GenericValue.cs
  24. +198 −0 src/Api/IRBuilder.cs
  25. +19 −0 src/Api/Initialize.cs
  26. +16 −0 src/Api/IntPredicate.cs
  27. +23 −0 src/Api/Linkage.cs
  28. +86 −0 src/Api/MCJITMemoryManager.cs
  29. +9 −0 src/Api/MDString.cs
  30. +10 −0 src/Api/MDStringAsValue.cs
  31. +106 −0 src/Api/MemoryBuffer.cs
  32. +18 −0 src/Api/Message.cs
  33. +9 −0 src/Api/Metadata.cs
  34. +12 −0 src/Api/MetadataAsValue.cs
  35. +131 −0 src/Api/Module.cs
  36. +46 −0 src/Api/ModuleProvider.cs
  37. +57 −0 src/Api/ObjectFile.cs
  38. +70 −0 src/Api/Opcode.cs
  39. +35 −0 src/Api/OperandList.cs
  40. +118 −0 src/Api/PassManager.cs
  41. +58 −0 src/Api/PassManagerBuilder.cs
  42. +31 −0 src/Api/PassRegistry.cs
  43. +22 −0 src/Api/RealPredicate.cs
  44. +57 −0 src/Api/RelocationIterator.cs
  45. +58 −0 src/Api/SectionIterator.cs
  46. +8 −0 src/Api/Support.cs
  47. +52 −0 src/Api/SymbolIterator.cs
  48. +51 −0 src/Api/Target.cs
  49. +69 −0 src/Api/TargetData.cs
  50. +17 −0 src/Api/TargetInitializers/AArch64TargetInitializer.cs
  51. +17 −0 src/Api/TargetInitializers/ARMTargetInitializer.cs
  52. +15 −0 src/Api/TargetInitializers/HexagonTargetInitializer.cs
  53. +7 −0 src/Api/TargetInitializers/IAsmParser.cs
  54. +7 −0 src/Api/TargetInitializers/IAsmPrinter.cs
  55. +7 −0 src/Api/TargetInitializers/IDisassembler.cs
  56. +7 −0 src/Api/TargetInitializers/ITarget.cs
  57. +7 −0 src/Api/TargetInitializers/ITargetInfo.cs
  58. +7 −0 src/Api/TargetInitializers/ITargetMC.cs
  59. +15 −0 src/Api/TargetInitializers/MSP430TargetInitializer.cs
  60. +17 −0 src/Api/TargetInitializers/MipsTargetInitializer.cs
  61. +15 −0 src/Api/TargetInitializers/NVPTXTargetInitializer.cs
  62. +15 −0 src/Api/TargetInitializers/NativeTargetInitializer.cs
  63. +17 −0 src/Api/TargetInitializers/PowerPCTargetInitializer.cs
  64. +17 −0 src/Api/TargetInitializers/SparcTargetInitializer.cs
  65. +17 −0 src/Api/TargetInitializers/SystemZTargetInitializer.cs
  66. +33 −0 src/Api/TargetInitializers/TargetInitializer.cs
  67. +17 −0 src/Api/TargetInitializers/X86TargetInitializer.cs
  68. +16 −0 src/Api/TargetInitializers/XCoreTargetInitializer.cs
  69. +18 −0 src/Api/TargetLibraryInfo.cs
  70. +68 −0 src/Api/TargetMachine.cs
  71. +11 −0 src/Api/ThreadLocalMode.cs
  72. +7 −0 src/Api/Threading.cs
  73. +112 −0 src/Api/Type.cs
  74. +15 −0 src/Api/Types/Composite/CompositeType.cs
  75. +16 −0 src/Api/Types/Composite/SequentialType.cs
  76. +18 −0 src/Api/Types/Composite/SequentialTypes/ArrayType.cs
  77. +21 −0 src/Api/Types/Composite/SequentialTypes/PointerType.cs
  78. +63 −0 src/Api/Types/Composite/SequentialTypes/VectorType.cs
  79. +78 −0 src/Api/Types/Composite/StructType.cs
  80. +14 −0 src/Api/Types/DoubleType.cs
  81. +14 −0 src/Api/Types/FP128Type.cs
  82. +14 −0 src/Api/Types/FPType.cs
  83. +14 −0 src/Api/Types/FloatType.cs
  84. +26 −0 src/Api/Types/FunctionType.cs
  85. +14 −0 src/Api/Types/HalfType.cs
  86. +8 −0 src/Api/Types/IAggregateType.cs
  87. +19 −0 src/Api/Types/IntegerType.cs
  88. +15 −0 src/Api/Types/LabelType.cs
  89. +10 −0 src/Api/Types/MetadataType.cs
  90. +14 −0 src/Api/Types/PPCFP128Type.cs
  91. +10 −0 src/Api/Types/TokenType.cs
  92. +13 −0 src/Api/Types/VoidType.cs
  93. +14 −0 src/Api/Types/X86FP80Type.cs
  94. +23 −0 src/Api/Types/X86MMXType.cs
  95. +22 −0 src/Api/Use.cs
  96. +109 −0 src/Api/Value.cs
  97. +18 −0 src/Api/Values/Argument.cs
  98. +52 −0 src/Api/Values/BasicBlock.cs
  99. +112 −0 src/Api/Values/Constant.cs
  100. +10 −0 src/Api/Values/Constants/BlockAddress.cs
  101. +10 −0 src/Api/Values/Constants/ConstantAggregateZero.cs
  102. +12 −0 src/Api/Values/Constants/ConstantArray.cs
  103. +18 −0 src/Api/Values/Constants/ConstantDataSequential.cs
  104. +43 −0 src/Api/Values/Constants/ConstantDataSequentials/ConstantDataArray.cs
  105. +10 −0 src/Api/Values/Constants/ConstantDataSequentials/ConstantDataVector.cs
  106. +80 −0 src/Api/Values/Constants/ConstantExpr.cs
  107. +19 −0 src/Api/Values/Constants/ConstantFP.cs
  108. +20 −0 src/Api/Values/Constants/ConstantInt.cs
  109. +12 −0 src/Api/Values/Constants/ConstantPointerNull.cs
  110. +16 −0 src/Api/Values/Constants/ConstantStruct.cs
  111. +12 −0 src/Api/Values/Constants/ConstantVector.cs
  112. +50 −0 src/Api/Values/Constants/GlobalValue.cs
  113. +10 −0 src/Api/Values/Constants/GlobalValues/GlobalAlias.cs
  114. +10 −0 src/Api/Values/Constants/GlobalValues/GlobalObject.cs
  115. +70 −0 src/Api/Values/Constants/GlobalValues/GlobalObjects/Function.cs
  116. +45 −0 src/Api/Values/Constants/GlobalValues/GlobalObjects/GlobalVariable.cs
  117. +12 −0 src/Api/Values/Constants/UndefValue.cs
  118. +98 −0 src/Api/Values/Instruction.cs
  119. +21 −0 src/Api/Values/Instructions/AtomicCmpXchgInst.cs
  120. +10 −0 src/Api/Values/Instructions/AtomicRMWInst.cs
  121. +10 −0 src/Api/Values/Instructions/Binary/AShr.cs
  122. +10 −0 src/Api/Values/Instructions/Binary/Add.cs
  123. +10 −0 src/Api/Values/Instructions/Binary/And.cs
  124. +10 −0 src/Api/Values/Instructions/Binary/FAdd.cs
  125. +10 −0 src/Api/Values/Instructions/Binary/FDiv.cs
  126. +10 −0 src/Api/Values/Instructions/Binary/FMul.cs
  127. +10 −0 src/Api/Values/Instructions/Binary/FNeg.cs
  128. +10 −0 src/Api/Values/Instructions/Binary/FRem.cs
  129. +10 −0 src/Api/Values/Instructions/Binary/FSub.cs
  130. +10 −0 src/Api/Values/Instructions/Binary/LShr.cs
  131. +10 −0 src/Api/Values/Instructions/Binary/Mul.cs
  132. +10 −0 src/Api/Values/Instructions/Binary/Neg.cs
  133. +10 −0 src/Api/Values/Instructions/Binary/Not.cs
  134. +10 −0 src/Api/Values/Instructions/Binary/Or.cs
  135. +10 −0 src/Api/Values/Instructions/Binary/SDiv.cs
  136. +10 −0 src/Api/Values/Instructions/Binary/SRem.cs
  137. +10 −0 src/Api/Values/Instructions/Binary/Shl.cs
  138. +10 −0 src/Api/Values/Instructions/Binary/Sub.cs
  139. +10 −0 src/Api/Values/Instructions/Binary/UDiv.cs
  140. +10 −0 src/Api/Values/Instructions/Binary/URem.cs
  141. +10 −0 src/Api/Values/Instructions/Binary/Xor.cs
  142. +58 −0 src/Api/Values/Instructions/BinaryOperator.cs
  143. +10 −0 src/Api/Values/Instructions/Call/IntrinsicInst.cs
  144. +10 −0 src/Api/Values/Instructions/Call/Intrinsics/DbgInfoIntrinsic.cs
  145. +10 −0 src/Api/Values/Instructions/Call/Intrinsics/DbgInfoIntrinsics/DbgDeclareInst.cs
  146. +10 −0 src/Api/Values/Instructions/Call/Intrinsics/DbgInfoIntrinsics/DbgValueInst.cs
  147. +10 −0 src/Api/Values/Instructions/Call/Intrinsics/MemIntrinsic.cs
  148. +10 −0 src/Api/Values/Instructions/Call/Intrinsics/MemIntrinsics/MemSetInst.cs
  149. +10 −0 src/Api/Values/Instructions/Call/Intrinsics/MemIntrinsics/MemTransferInst.cs
  150. +10 −0 src/Api/Values/Instructions/Call/Intrinsics/MemIntrinsics/MemTransferInsts/MemCpyInst.cs
  151. +10 −0 src/Api/Values/Instructions/Call/Intrinsics/MemIntrinsics/MemTransferInsts/MemMoveInst.cs
  152. +22 −0 src/Api/Values/Instructions/CallInst.cs
  153. +12 −0 src/Api/Values/Instructions/Cmp/FCmpInst.cs
  154. +12 −0 src/Api/Values/Instructions/Cmp/ICmpInst.cs
  155. +10 −0 src/Api/Values/Instructions/CmpInst.cs
  156. +10 −0 src/Api/Values/Instructions/ExtractElementInst.cs
  157. +10 −0 src/Api/Values/Instructions/FenceInst.cs
  158. +10 −0 src/Api/Values/Instructions/GetElementPtrInst.cs
  159. +7 −0 src/Api/Values/Instructions/IMemoryAccessInstruction.cs
  160. +10 −0 src/Api/Values/Instructions/InsertElementInst.cs
  161. +10 −0 src/Api/Values/Instructions/InsertValueInst.cs
  162. +18 −0 src/Api/Values/Instructions/LandingPadInst.cs
  163. +58 −0 src/Api/Values/Instructions/PHINode.cs
  164. +10 −0 src/Api/Values/Instructions/SelectInst.cs
  165. +10 −0 src/Api/Values/Instructions/ShuffleVectorInst.cs
  166. +16 −0 src/Api/Values/Instructions/StoreInst.cs
  167. +19 −0 src/Api/Values/Instructions/Terminator/BranchInst.cs
  168. +12 −0 src/Api/Values/Instructions/Terminator/IndirectBrInst.cs
  169. +10 −0 src/Api/Values/Instructions/Terminator/InvokeInst.cs
  170. +10 −0 src/Api/Values/Instructions/Terminator/ResumeInst.cs
  171. +10 −0 src/Api/Values/Instructions/Terminator/ReturnInst.cs
  172. +16 −0 src/Api/Values/Instructions/Terminator/SwitchInst.cs
  173. +10 −0 src/Api/Values/Instructions/Terminator/UnreachableInst.cs
  174. +14 −0 src/Api/Values/Instructions/TerminatorInst.cs
  175. +16 −0 src/Api/Values/Instructions/Unary/AllocaInst.cs
  176. +10 −0 src/Api/Values/Instructions/Unary/CastInst.cs
  177. +10 −0 src/Api/Values/Instructions/Unary/Casts/AddrSpaceCastInst.cs
  178. +10 −0 src/Api/Values/Instructions/Unary/Casts/BitCastInst.cs
  179. +10 −0 src/Api/Values/Instructions/Unary/Casts/FPExtInst.cs
  180. +10 −0 src/Api/Values/Instructions/Unary/Casts/FPToSIInst.cs
  181. +10 −0 src/Api/Values/Instructions/Unary/Casts/FPToUIInst.cs
  182. +10 −0 src/Api/Values/Instructions/Unary/Casts/FPTruncInst.cs
  183. +10 −0 src/Api/Values/Instructions/Unary/Casts/IntToPtrInst.cs
  184. +10 −0 src/Api/Values/Instructions/Unary/Casts/PtrToIntInst.cs
  185. +10 −0 src/Api/Values/Instructions/Unary/Casts/SExtInst.cs
  186. +10 −0 src/Api/Values/Instructions/Unary/Casts/SIToFPInst.cs
  187. +10 −0 src/Api/Values/Instructions/Unary/Casts/TruncInst.cs
  188. +10 −0 src/Api/Values/Instructions/Unary/Casts/UIToFPInst.cs
  189. +10 −0 src/Api/Values/Instructions/Unary/Casts/ZExtInst.cs
  190. +10 −0 src/Api/Values/Instructions/Unary/ExtractValueInst.cs
  191. +22 −0 src/Api/Values/Instructions/Unary/LoadInst.cs
  192. +10 −0 src/Api/Values/Instructions/Unary/VAArgInst.cs
  193. +10 −0 src/Api/Values/Instructions/UnaryInstruction.cs
  194. +9 −0 src/Api/Visibility.cs
  195. +18 −0 src/LLVMAttributeRef.cs
  196. +12 −1 src/LLVMBasicBlockRef.cs
  197. +13 −0 src/LLVMBool.cs
  198. +18 −0 src/LLVMBuilderRef.cs
  199. +12 −1 src/LLVMContextRef.cs
  200. +18 −0 src/LLVMDiagnosticInfoRef.cs
  201. +18 −0 src/LLVMDisasmContextRef.cs
  202. +17 −0 src/LLVMExecutionEngineRef.cs
  203. +18 −0 src/LLVMGenericValueRef.cs
  204. +16 −0 src/LLVMMCJITCompilerOptions.cs
  205. +18 −0 src/LLVMMCJITMemoryManagerRef.cs
  206. +18 −0 src/LLVMMemoryBufferRef.cs
  207. +18 −0 src/LLVMModuleProviderRef.cs
  208. +17 −0 src/LLVMModuleRef.cs
  209. +18 −0 src/LLVMObjectFileRef.cs
  210. +18 −0 src/LLVMPassManagerBuilderRef.cs
  211. +17 −0 src/LLVMPassManagerRef.cs
  212. +18 −0 src/LLVMPassRegistryRef.cs
  213. +18 −0 src/LLVMRelocationIteratorRef.cs
  214. +18 −0 src/LLVMSectionIteratorRef.cs
  215. +18 −0 src/LLVMSymbolIteratorRef.cs
  216. +18 −0 src/LLVMTargetDataRef.cs
  217. +18 −0 src/LLVMTargetLibraryInfoRef.cs
  218. +18 −0 src/LLVMTargetMachineRef.cs
  219. +18 −0 src/LLVMTargetRef.cs
  220. +13 −2 src/LLVMTypeRef.cs
  221. +18 −0 src/LLVMUseRef.cs
  222. +13 −2 src/LLVMValueRef.cs
  223. +25 −12 src/{Overloads.cs → Overloads.Convenience.cs}
  224. +23 −0 src/Overloads.Interop.cs
  225. +113 −0 src/Utilities/ConversionUtilities.cs
  226. +10 −0 src/Utilities/IDisposableWrapper.cs
  227. +11 −0 src/Utilities/IHandle.cs
  228. +8 −0 src/Utilities/IWrapper.cs
  229. +34 −0 src/Utilities/TextUtilities.cs
  230. +48 −0 src/Utilities/TypeUtilities.cs
  231. +39 −0 src/llvm_lto_t.cs
  232. +39 −0 src/lto_bool_t.cs
  233. +39 −0 src/lto_code_gen_t.cs
  234. +39 −0 src/lto_module_t.cs
  235. +12 −0 src/size_t.cs
@@ -10,3 +10,4 @@ cache:
script:
- dotnet restore
- dotnet build
- dotnet test Tests/LLVMSharp.Tests.csproj
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.15
VisualStudioVersion = 15.0.26730.16
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LLVMSharp", "src\LLVMSharp.csproj", "{3EA66A6D-9DC3-46FF-AA7F-D0BB5BF0BA73}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLVMSharp", "src\LLVMSharp.csproj", "{3EA66A6D-9DC3-46FF-AA7F-D0BB5BF0BA73}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LLVMSharp.Tests", "Tests\LLVMSharp.Tests.csproj", "{40C8C114-092F-4263-9685-F1A4A8E1CAD8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
{3EA66A6D-9DC3-46FF-AA7F-D0BB5BF0BA73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EA66A6D-9DC3-46FF-AA7F-D0BB5BF0BA73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EA66A6D-9DC3-46FF-AA7F-D0BB5BF0BA73}.Release|Any CPU.Build.0 = Release|Any CPU
{40C8C114-092F-4263-9685-F1A4A8E1CAD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40C8C114-092F-4263-9685-F1A4A8E1CAD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40C8C114-092F-4263-9685-F1A4A8E1CAD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40C8C114-092F-4263-9685-F1A4A8E1CAD8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -0,0 +1,9 @@
namespace Tests
{
using System.Runtime.InteropServices;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int Int32Delegate();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate long Int64Delegate();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int Int32Int32Int32Delegate(int a, int b);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate byte Int32Int32Int8Delegate(int a, int b);
}
@@ -0,0 +1,37 @@
namespace Tests
{
using LLVMSharp.API;
using System.Runtime.InteropServices;
using NUnit.Framework;

public class Examples
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int BinaryInt32Operation(int op1, int op2);

[Test]
public void Intro()
{
using(var module = Module.Create("LLVMSharpIntro"))
{
var def = module.AddFunction(
Type.Int32, "sum", new[] { Type.Int32, Type.Int32 }, (f, b) =>
{
var p1 = f.Parameters[0];
var p2 = f.Parameters[1];
var add = b.CreateAdd(p1, p2);
var ret = b.CreateRet(add);
});
module.Verify();

Initialize.X86.All();
using (var engine = ExecutionEngine.CreateMCJITCompilerForModule(module))
{
var function = engine.GetDelegate<BinaryInt32Operation>(def);
var result = function(2, 2);
Assert.AreEqual(4, result);
}
}
}
}
}
@@ -0,0 +1,168 @@
namespace Tests
{
using LLVMSharp.API;
using LLVMSharp.API.Types.Composite;
using LLVMSharp.API.Values.Constants;
using NUnit.Framework;

public class IR
{
[Test]
public void AddsSigned()
{
var op1 = 0;
var op2 = 0;
using (var module = Module.Create("test_add"))
{
var def = module.AddFunction(
Type.Int32, "add", new[] { Type.Int32, Type.Int32 }, (f, b) =>
{
var p1 = f.Parameters[0];
var p2 = f.Parameters[1];
var add = b.CreateAdd(p1, p2);
var ret = b.CreateRet(add);
});
module.Verify();

Initialize.X86.All();
using (var engine = ExecutionEngine.CreateMCJITCompilerForModule(module))
{
var func = engine.GetDelegate<Int32Int32Int32Delegate>(def);
var result = op1 + op2;
Assert.AreEqual(result, func(op1, op2));
}
}
}

[Test]
public void ShiftsRight([Range(0, 256)] int op1, [Range(0, 8)] int op2)
{
using (var module = Module.Create("test_lshift"))
{
var def = module.AddFunction(
Type.Int32, "lshift", new[] { Type.Int32, Type.Int32 }, (f, b) =>
{
var p1 = f.Parameters[0];
var p2 = f.Parameters[1];
var shift = b.CreateLShr(p1, p2);
var ret = b.CreateRet(shift);
});
module.Verify();

Initialize.X86.All();
using (var engine = ExecutionEngine.CreateMCJITCompilerForModule(module))
{
var func = engine.GetDelegate<Int32Int32Int32Delegate>(def);
var result = op1 >> op2;
Assert.AreEqual(result, func(op1, op2));
}
}
}

[Test]
public void ComparesGreaterThan([Range(0, 10)] int op1, [Range(0, 10)] int op2)
{
using (var module = Module.Create("test_greaterthan"))
{
var def = module.AddFunction(
Type.Int1, "greaterthan", new[] { Type.Int32, Type.Int32 }, (f, b) =>
{
var p1 = f.Parameters[0];
var p2 = f.Parameters[1];
var cmp = b.CreateICmp(IntPredicate.SGT, p1, p2);
var r = b.CreateBitCast(cmp, f.FunctionType.ReturnType);
var ret = b.CreateRet(r);
});
module.Verify();

Initialize.X86.All();
using (var engine = ExecutionEngine.CreateMCJITCompilerForModule(module))
{
var func = engine.GetDelegate<Int32Int32Int8Delegate>(def);
var result = op1 > op2 ? 1 : 0;
Assert.AreEqual(result, func(op1, op2));
}
}
}

[Test]
public void CallsFunction([Range(0, 10)] int op1, [Range(0, 10)] int op2)
{
using (var module = Module.Create("test_call"))
{
var defAdd = module.AddFunction(
Type.Int32, "add", new[] { Type.Int32, Type.Int32 }, (f, b) =>
{
var p1 = f.Parameters[0];
var p2 = f.Parameters[1];
var add = b.CreateAdd(p1, p2);
var ret = b.CreateRet(add);
});
var defEntry = module.AddFunction(
Type.Int32, "entry", new[] { Type.Int32, Type.Int32 }, (f, b) =>
{
var p1 = f.Parameters[0];
var p2 = f.Parameters[1];
var call = b.CreateCall(defAdd, p1, p2);
var ret = b.CreateRet(call);
});
module.Verify();

Initialize.X86.All();
using (var engine = ExecutionEngine.CreateMCJITCompilerForModule(module))
{
var func = engine.GetDelegate<Int32Int32Int32Delegate>(defEntry);
var result = op1 + op2;
Assert.AreEqual(result, func(op1, op2));
}
}
}

[Test]
public void ReturnsConstant([Range(0, 10)] int input)
{
var uInput = (uint)input;
using (var module = Module.Create("test_constant"))
{
var def = module.AddFunction(
Type.Int32, "constant", new Type[0], (f, b) =>
{
var value = ConstantInt.Get(Type.Int32, uInput);
var ret = b.CreateRet(value);
});
module.Verify();

Initialize.X86.All();
using (var engine = ExecutionEngine.CreateMCJITCompilerForModule(module))
{
var func = engine.GetDelegate<Int32Delegate>(def);
Assert.AreEqual(input, func());
}
}
}

[Test]
public void ReturnsSizeOf()
{
using(var module = Module.Create("test_sizeof"))
{
var str = StructType.Create(new[] { Type.Int32, Type.Int32 }, true);
var def = module.AddFunction(
Type.Int32, "structure", new Type[0], (f, b) =>
{
var sz = ConstantExpr.GetSizeOf(str);
var sz32 = b.CreateBitCast(sz, Type.Int32);
var ret = b.CreateRet(sz32);
});
module.Verify();

Initialize.X86.All();
using(var engine = ExecutionEngine.CreateMCJITCompilerForModule(module))
{
var func = engine.GetDelegate<Int32Delegate>(def);
Assert.AreEqual(8, func());
}
}
}
}
}
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="libLLVM" Version="5.0.1" />
<PackageReference Include="nunit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\LLVMSharp.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

</Project>
@@ -0,0 +1,31 @@
namespace Tests
{
using LLVMSharp.API;
using NUnit;
using NUnit.Framework;

public class Modules
{
[Test]
public void SetsAndGetsDataLayout()
{
using(var module = Module.Create("test"))
{
const string ExampleDataLayout = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128";
module.DataLayout = ExampleDataLayout;
Assert.AreEqual(ExampleDataLayout, module.DataLayout);
}
}

[Test]
public void SetsAndGetsTarget()
{
using(var module = Module.Create("test"))
{
const string ExampleTarget = "x86_64-pc-windows-msvc";
module.Target = ExampleTarget;
Assert.AreEqual(ExampleTarget, module.Target);
}
}
}
}
@@ -0,0 +1,36 @@
namespace Tests
{
using LLVMSharp.API;
using LLVMSharp.API.TargetInitializers;
using NUnit.Framework;
using System.Linq;

public class Targets
{
[Test]
public void InitializeX86Targets() => this.InitializeTargets(Initialize.X86, new[] { "x86" });

[Test]
public void InitializeARMTargets() => this.InitializeTargets(Initialize.ARM, new[] { "arm" });

private void InitializeTargets(TargetInitializer init, string[] expectedTargets)
{
init.All();
foreach (var u in Target.Targets)
{
u.EnsurePropertiesWork();
}
foreach (var t in expectedTargets)
{
Assert.IsTrue(Target.Targets.Any(x => x.Name == t));
}
}

[Test]
public void DefaultTargetTriple()
{
var str = Target.DefaultTriple;
Assert.Greater(str.Length, 0);
}
}
}
@@ -0,0 +1,40 @@
namespace Tests
{
using LLVMSharp.API;
using NUnit.Framework;
using System.Collections.Generic;

public class Types
{
[Test]
public void IntSizes([Values(1, 8, 16, 32, 64)] int width)
{
var uWidth = (uint)width;
var t = Context.Global.IntType(uWidth);
Assert.AreEqual(uWidth, t.BitWidth);
}

[Test]
public void FloatingTypes()
{
var dic = new Dictionary<Type, bool>
{
{ Context.Global.VoidType, false },
{ Context.Global.Int32Type, false },
{ Context.Global.X86MMXType, false },
{ Context.Global.LabelType, false },

{ Context.Global.HalfType, true },
{ Context.Global.FloatType, true },
{ Context.Global.DoubleType, true },
{ Context.Global.FP128Type, true },
{ Context.Global.X86FP80Type, true },
{ Context.Global.PPCFP128Type, true },
};
foreach (var p in dic.Keys)
{
Assert.AreEqual(dic[p], p.IsFloatingPoint);
}
}
}
}
Oops, something went wrong.

0 comments on commit ae1f568

Please sign in to comment.
You can’t perform that action at this time.