public void TestInvalidMain() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var mainFunc = new Function( new FunctionDefinition("main", new List<VMType>() { intType }, intType), new List<Instruction>() { new Instruction(OpCodes.LoadInt, 0), new Instruction(OpCodes.Ret) }, new List<VMType>()); try { container.LoadAssembly(Assembly.SingleFunction(mainFunc)); Assert.Fail("Expected invalid main to not pass."); } catch (Exception e) { Assert.AreEqual("Expected the main function to have the signature: 'main() Int'.", e.Message); } } }
public void TestNotEndInReturn() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadInt, 0)); var func = new Function( new FunctionDefinition("main", new List<VMType>(), intType), instructions, new List<VMType>()); container.LoadAssembly(Assembly.SingleFunction(func)); try { container.Execute(); Assert.Fail("Expected without return to not pass."); } catch (VerificationException e) { Assert.AreEqual("0: Functions must end with a return instruction.", e.Message); } } }
public void TestIntDefaultValue() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var funcDef = new FunctionDefinition("main", new List<VMType>(), intType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadLocal, 0)); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function(funcDef, instructions, Enumerable.Repeat(intType, 1).ToList()); container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(0, container.Execute()); } }
public void TestBranchNotEqual() { using (var container = new Win64Container()) { container.LoadAssembly(Assembly.SingleFunction( this.CreateBranchProgram(container, OpCodes.BranchNotEqual, 2, 1))); Assert.AreEqual(1, container.Execute()); } using (var container = new Win64Container()) { container.LoadAssembly(Assembly.SingleFunction( this.CreateBranchProgram(container, OpCodes.BranchNotEqual, 1, 1))); Assert.AreEqual(0, container.Execute()); } }
public void TestMul() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var funcDef = new FunctionDefinition("main", new List<VMType>(), intType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadInt, 2)); instructions.Add(new Instruction(OpCodes.LoadInt, 3)); instructions.Add(new Instruction(OpCodes.MulInt)); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function(funcDef, instructions, new List<VMType>()); container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(6, container.Execute()); } }
public void TestDiv() { using (var container = new Win64Container()) { var floatType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Float); var funcDef = new FunctionDefinition("floatMain", new List<VMType>(), floatType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadFloat, 2.5f)); instructions.Add(new Instruction(OpCodes.LoadFloat, 1.35f)); instructions.Add(new Instruction(OpCodes.DivFloat)); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function(funcDef, instructions, new List<VMType>()); container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(2.5f / 1.35f, ExecuteFloatProgram(container), 1E-4); } }
public void TestFloatDefaultValue() { using (var container = new Win64Container()) { var floatType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Float); var funcDef = new FunctionDefinition("floatMain", new List<VMType>(), floatType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadLocal, 0)); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function(funcDef, instructions, Enumerable.Repeat(floatType, 1).ToList()); container.LoadAssembly(Assembly.SingleFunction(func)); container.VirtualMachine.Compile(); var mainFunc = Marshal.GetDelegateForFunctionPointer<FloatMain>( container.VirtualMachine.Binder.GetFunction("floatMain()").EntryPoint); Assert.AreEqual(0.0f, mainFunc()); } }
public void DefinitionOrder() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var assemblyFunctions = new List<Function>(); Action testFn = () => { var def = new FunctionDefinition("test", new List<VMType>(), intType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadInt, 1)); instructions.Add(new Instruction(OpCodes.LoadInt, 2)); instructions.Add(new Instruction(OpCodes.AddInt)); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function(def, instructions, new List<VMType>()); assemblyFunctions.Add(func); }; Action mainFn = () => { var def = new FunctionDefinition("main", new List<VMType>(), intType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.Call, "test", new List<VMType>())); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function(def, instructions, new List<VMType>()); assemblyFunctions.Add(func); }; mainFn(); testFn(); container.LoadAssembly(new Assembly(assemblyFunctions)); Assert.AreEqual(3, container.Execute()); } }
public void TestAlreadyDefined() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var func1 = new Function( new FunctionDefinition("test", new List<VMType>(), intType), new List<Instruction>() { new Instruction(OpCodes.LoadInt, 0), new Instruction(OpCodes.Ret) }, new List<VMType>()); var func2 = new Function( new FunctionDefinition("test", new List<VMType>(), intType), new List<Instruction>() { new Instruction(OpCodes.LoadInt, 0), new Instruction(OpCodes.Ret) }, new List<VMType>()); var assembly = new Assembly(func1, func2); try { container.LoadAssembly(assembly); Assert.Fail("Expected already defined to not pass."); } catch (Exception e) { Assert.AreEqual("The function 'test() Int' is already defined.", e.Message); } } }
public void TestEmpty() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var func = new Function( new FunctionDefinition("main", new List<VMType>(), intType), new List<Instruction>(), new List<VMType>()); container.LoadAssembly(Assembly.SingleFunction(func)); try { container.Execute(); Assert.Fail("Expected empty functions to not pass."); } catch (VerificationException e) { Assert.AreEqual("0: Empty functions are not allowed.", e.Message); } } }
public void TestRecursive2() { using (var container = new Win64Container()) { var mainFunc = TestProgramGenerator.MainWithIntCall(container, "fib", 11); mainFunc.Optimize = true; var sumFunc = TestProgramGenerator.RecursiveFib(container); sumFunc.Optimize = true; var assembly = new Assembly(mainFunc, sumFunc); container.LoadAssembly(assembly); Assert.AreEqual(89, container.Execute()); } }
public void TestSimple3() { using (var container = new Win64Container()) { var func = TestProgramGenerator.Simple3(container); func.Optimize = true; container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(15, container.Execute()); } }
public void TestNegativeSum() { using (var container = new Win64Container()) { int count = 10; var func = TestProgramGenerator.NegativeSumNoneLoop(container, count); func.Optimize = true; container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(TestProgramGenerator.NegativeSumResult(count), container.Execute()); } }
public void TestProduct() { using (var container = new Win64Container()) { int count = 10; int product = Enumerable.Aggregate(Enumerable.Range(1, count), 1, (total, current) => total * current); var func = TestProgramGenerator.ProductNoneLoop(container, count); func.Optimize = true; container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(product, container.Execute()); } }
public void TestFloatLocals() { using (var container = new Win64Container()) { container.VirtualMachine.Settings["NumIntRegisters"] = 5; var func = TestProgramGenerator.FloatLocals(container); func.Optimize = true; container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(4, TestProgramGenerator.ExecuteFloatProgram(container)); } }
public void TestCallOrder() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var paramsType = Enumerable.Repeat(intType, 2).ToList(); var addFunc = new Function( new FunctionDefinition("sub", paramsType, intType), new List<Instruction>() { new Instruction(OpCodes.LoadArgument, 0), new Instruction(OpCodes.LoadArgument, 1), new Instruction(OpCodes.SubInt), new Instruction(OpCodes.Ret) }, new List<VMType>()) { Optimize = true }; var mainFunc = new Function( new FunctionDefinition("main", new List<VMType>(), intType), new List<Instruction>() { new Instruction(OpCodes.LoadInt, 6), new Instruction(OpCodes.LoadInt, 2), new Instruction(OpCodes.Call, "sub", paramsType.ToList()), new Instruction(OpCodes.Ret) }, new List<VMType>()) { Optimize = true }; container.LoadAssembly(new Assembly(addFunc, mainFunc)); Assert.AreEqual(4, container.Execute()); } }
public void TestFloatAdd3() { using (var container = new Win64Container()) { var floatType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Float); var funcDef = new FunctionDefinition("floatMain", new List<VMType>(), floatType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadFloat, 1f)); instructions.Add(new Instruction(OpCodes.LoadFloat, 2f)); instructions.Add(new Instruction(OpCodes.LoadFloat, 3f)); instructions.Add(new Instruction(OpCodes.LoadFloat, 4f)); instructions.Add(new Instruction(OpCodes.LoadFloat, 5f)); instructions.Add(new Instruction(OpCodes.LoadFloat, 6f)); instructions.Add(new Instruction(OpCodes.AddFloat)); instructions.Add(new Instruction(OpCodes.AddFloat)); instructions.Add(new Instruction(OpCodes.AddFloat)); instructions.Add(new Instruction(OpCodes.AddFloat)); instructions.Add(new Instruction(OpCodes.AddFloat)); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function(funcDef, instructions, new List<VMType>()); func.Optimize = true; container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(1 + 2 + 3 + 4 + 5 + 6, TestProgramGenerator.ExecuteFloatProgram(container), 1E-4); } }
public void TestRecursive2() { using (var container = new Win64Container()) { var assembly = new Assembly( TestProgramGenerator.MainWithIntCall(container, "fib", 11), TestProgramGenerator.RecursiveFib(container)); container.LoadAssembly(assembly); Assert.AreEqual(89, container.Execute()); } }
public void TestLessThanFloat() { using (var container = new Win64Container()) { container.LoadAssembly(Assembly.SingleFunction( this.CreateBranchFloatProgram(container, OpCodes.BranchLessThan, 1, 2))); Assert.AreEqual(1, container.Execute()); } using (var container = new Win64Container()) { container.LoadAssembly(Assembly.SingleFunction( this.CreateBranchFloatProgram(container, OpCodes.BranchLessThan, 1, 1))); Assert.AreEqual(0, container.Execute()); } using (var container = new Win64Container()) { container.LoadAssembly(Assembly.SingleFunction( this.CreateBranchFloatProgram(container, OpCodes.BranchLessThan, 2, 1))); Assert.AreEqual(0, container.Execute()); } }
public void TestCallVoid() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var voidType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Void); var nopFunc = new Function( new FunctionDefinition("nop", new List<VMType>(), voidType), new List<Instruction>() { new Instruction(OpCodes.Ret) }, new List<VMType>()) { Optimize = true }; var mainFunc = new Function( new FunctionDefinition("main", new List<VMType>(), intType), new List<Instruction>() { new Instruction(OpCodes.Call, "nop", new List<VMType>()), new Instruction(OpCodes.LoadInt, 0), new Instruction(OpCodes.Ret) }, new List<VMType>()) { Optimize = true }; container.LoadAssembly(new Assembly(nopFunc, mainFunc)); Assert.AreEqual(0, container.Execute()); } }
public void TestOverload() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var floatType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Float); var func1 = new Function( new FunctionDefinition("test", new List<VMType>() { intType }, intType), new List<Instruction>() { new Instruction(OpCodes.LoadInt, 0), new Instruction(OpCodes.Ret) }, new List<VMType>()); var func2 = new Function( new FunctionDefinition("test", new List<VMType>() { floatType }, floatType), new List<Instruction>() { new Instruction(OpCodes.LoadFloat, 0.0f), new Instruction(OpCodes.Ret) }, new List<VMType>()); var assembly = new Assembly(func1, func2); container.LoadAssembly(assembly); } }
public void TestMixedArguments() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var floatType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Float); var parameters = new List<VMType>() { intType, floatType, intType, floatType, intType, floatType }; container.VirtualMachine.Binder.Define(FunctionDefinition.NewExternal<FuncIntArgIntFloatIntFloatIntFloat>( "add", parameters, intType, MixedAdd)); var def = new FunctionDefinition("main", new List<VMType>(), intType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadInt, 1)); instructions.Add(new Instruction(OpCodes.LoadFloat, 2.0f)); instructions.Add(new Instruction(OpCodes.LoadInt, 3)); instructions.Add(new Instruction(OpCodes.LoadFloat, 4.0f)); instructions.Add(new Instruction(OpCodes.LoadInt, 5)); instructions.Add(new Instruction(OpCodes.LoadFloat, 6.0f)); instructions.Add(new Instruction( OpCodes.Call, "add", parameters)); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function(def, instructions, new List<VMType>()); container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(1 + 2 + 3 + 4 + 5 + 6, container.Execute()); } }
public void TestStackArguments2() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var parameters = Enumerable.Repeat(intType, 8).ToList(); container.VirtualMachine.Binder.Define(FunctionDefinition.NewExternal<FuncIntArgIntIntIntIntIntIntIntInt>( "add", parameters, intType, StackAdd)); var def = new FunctionDefinition("main", new List<VMType>(), intType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadInt, 1)); instructions.Add(new Instruction(OpCodes.LoadInt, 2)); instructions.Add(new Instruction(OpCodes.LoadInt, 3)); instructions.Add(new Instruction(OpCodes.LoadInt, 4)); instructions.Add(new Instruction(OpCodes.LoadInt, 5)); instructions.Add(new Instruction(OpCodes.LoadInt, 6)); instructions.Add(new Instruction(OpCodes.LoadInt, 7)); instructions.Add(new Instruction(OpCodes.LoadInt, 8)); instructions.Add(new Instruction( OpCodes.Call, "add", parameters)); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function(def, instructions, new List<VMType>()); container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, container.Execute()); } }
public void TestSumLocal() { using (var container = new Win64Container()) { int count = 100; var func = TestProgramGenerator.SumNoneLoopLocal(container, count); func.Optimize = true; container.LoadAssembly(Assembly.SingleFunction(func)); Assert.AreEqual((count * (count + 1)) / 2, container.Execute()); } }
public void TestFloatDefaultValue() { using (var container = new Win64Container()) { var floatType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Float); var voidType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Void); var localLoad = new Function( new FunctionDefinition("localLoad", new List<VMType>(), floatType), new List<Instruction>() { new Instruction(OpCodes.LoadLocal, 0), new Instruction(OpCodes.Ret) }, Enumerable.Repeat(floatType, 1).ToList()); localLoad.Optimize = true; var localStore = new Function( new FunctionDefinition("localStore", new List<VMType>(), voidType), new List<Instruction>() { new Instruction(OpCodes.LoadFloat, 5.0f), new Instruction(OpCodes.StoreLocal, 0), new Instruction(OpCodes.Ret) }, Enumerable.Repeat(floatType, 1).ToList()); localStore.Optimize = true; var mainFunc = new Function( new FunctionDefinition("floatMain", new List<VMType>(), floatType), new List<Instruction>() { new Instruction(OpCodes.Call, "localStore", new List<VMType>()), new Instruction(OpCodes.Call, "localLoad", new List<VMType>()), new Instruction(OpCodes.Ret) }, Enumerable.Repeat(floatType, 1).ToList()); mainFunc.Optimize = true; container.LoadAssembly(new Assembly(localLoad, localStore, mainFunc)); container.VirtualMachine.Compile(); var entryPoint = Marshal.GetDelegateForFunctionPointer<FloatMain>( container.VirtualMachine.Binder.GetFunction("floatMain()").EntryPoint); Assert.AreEqual(0.0f, entryPoint()); } }
static void Main(string[] args) { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var floatType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Float); var def = new FunctionDefinition("main", new List<VMType>(), intType); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.LoadFloat, 2.5f)); instructions.Add(new Instruction(OpCodes.LoadFloat, 1.35f)); instructions.Add(new Instruction(OpCodes.AddFloat)); instructions.Add(new Instruction(OpCodes.Call, "std.println", Enumerable.Repeat(floatType, 1).ToList())); instructions.Add(new Instruction(OpCodes.LoadInt, 0)); instructions.Add(new Instruction(OpCodes.Ret)); var assembly = Assembly.SingleFunction(new Function(def, instructions, new List<VMType>())); container.LoadAssembly(assembly); container.VirtualMachine.Compile(); foreach (var function in assembly.Functions) { var disassembler = new Disassembler( container.VirtualMachine.Compiler.GetCompilationData(function), x => new Compiler.Win64.Disassembler(x)); Console.WriteLine(disassembler.Disassemble()); } int returnValue = container.VirtualMachine.GetEntryPoint()(); Console.WriteLine(returnValue); } Console.ReadLine(); }
public void TestRecursive1() { using (var container = new Win64Container()) { var assembly = new Assembly( TestProgramGenerator.MainWithIntCall(container, "sum", 10), TestProgramGenerator.ResursiveSum(container)); container.LoadAssembly(assembly); Assert.AreEqual(55, container.Execute()); } }
public void TestVoidParameter() { using (var container = new Win64Container()) { var voidType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Void); var instructions = new List<Instruction>(); instructions.Add(new Instruction(OpCodes.Ret)); var func = new Function( new FunctionDefinition("test", new List<VMType>() { voidType }, voidType), instructions, new List<VMType>()); container.LoadAssembly(Assembly.SingleFunction(func)); try { container.Execute(); Assert.Fail("Expected void parameter to not pass."); } catch (VerificationException e) { Assert.AreEqual("0: 'Void' is not a valid parameter type.", e.Message); } } }
public void TestNoMain() { using (var container = new Win64Container()) { var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int); var testFunc = new Function( new FunctionDefinition("test", new List<VMType>() { intType }, intType), new List<Instruction>() { new Instruction(OpCodes.LoadInt, 0), new Instruction(OpCodes.Ret) }, new List<VMType>()); container.LoadAssembly(Assembly.SingleFunction(testFunc)); try { container.Execute(); Assert.Fail("Expected no entry point to not pass."); } catch (Exception e) { Assert.AreEqual("There is no entry point defined.", e.Message); } } }