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);
                }
            }
        }
        /// <summary>
        /// Creates the fibonacci function
        /// </summary>
        private static Function CreateFibFunction(Win64Container container, bool optimize = false)
        {
            var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int);

            var def = new FunctionDefinition("fib", Enumerable.Repeat(intType, 1).ToList(), intType);

            var instructions = new List<Instruction>();
            instructions.Add(new Instruction(OpCodes.LoadArgument, 0));
            instructions.Add(new Instruction(OpCodes.LoadInt, 1));
            instructions.Add(new Instruction(OpCodes.BranchGreaterThan, 5));
            instructions.Add(new Instruction(OpCodes.LoadArgument, 0));
            instructions.Add(new Instruction(OpCodes.Ret));

            instructions.Add(new Instruction(OpCodes.LoadArgument, 0));
            instructions.Add(new Instruction(OpCodes.LoadInt, 2));
            instructions.Add(new Instruction(OpCodes.SubInt));
            instructions.Add(new Instruction(OpCodes.Call, "fib", Enumerable.Repeat(intType, 1).ToList()));

            instructions.Add(new Instruction(OpCodes.LoadArgument, 0));
            instructions.Add(new Instruction(OpCodes.LoadInt, 1));
            instructions.Add(new Instruction(OpCodes.SubInt));
            instructions.Add(new Instruction(OpCodes.Call, "fib", Enumerable.Repeat(intType, 1).ToList()));

            instructions.Add(new Instruction(OpCodes.AddInt));
            instructions.Add(new Instruction(OpCodes.Ret));

            return new Function(def, instructions, new List<VMType>())
            {
                Optimize = optimize
            };
        }
        /// <summary>
        /// Creates a add function with that takes the given amount of arguments
        /// </summary>
        /// <param name="container">The container</param>
        /// <param name="numArgs">The number of arguments</param>
        public static Function AddFunction(Win64Container container, int numArgs)
        {
            var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int);

            var parameters = new List<VMType>();
            for (int i = 0; i < numArgs; i++)
            {
                parameters.Add(intType);
            }

            var def = new FunctionDefinition("add", parameters, intType);

            var instructions = new List<Instruction>();
            instructions.Add(new Instruction(OpCodes.LoadArgument, 0));

            for (int i = 1; i < numArgs; i++)
            {
                instructions.Add(new Instruction(OpCodes.LoadArgument, i));
                instructions.Add(new Instruction(OpCodes.AddInt));
            }

            instructions.Add(new Instruction(OpCodes.Ret));

            return new Function(def, instructions, new List<VMType>());
        }
        /// <summary>
        /// Creates a loop call add function
        /// </summary>
        private static Function CreateLoopCallAdd(Win64Container container, int count, bool optimize = false)
        {
            var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int);

            var def = new FunctionDefinition("main", new List<VMType>(), intType);

            var instructions = new List<Instruction>();

            instructions.Add(new Instruction(OpCodes.LoadInt, count));
            instructions.Add(new Instruction(OpCodes.StoreLocal, 0));

            instructions.Add(new Instruction(OpCodes.LoadInt, 1));
            instructions.Add(new Instruction(OpCodes.LoadLocal, 1));
            instructions.Add(new Instruction(OpCodes.Call, "add", Enumerable.Repeat(intType, 2).ToList()));
            instructions.Add(new Instruction(OpCodes.StoreLocal, 1));

            instructions.Add(new Instruction(OpCodes.LoadLocal, 0));
            instructions.Add(new Instruction(OpCodes.LoadInt, 1));
            instructions.Add(new Instruction(OpCodes.SubInt));
            instructions.Add(new Instruction(OpCodes.StoreLocal, 0));
            instructions.Add(new Instruction(OpCodes.LoadLocal, 0));

            instructions.Add(new Instruction(OpCodes.LoadInt, 0));
            instructions.Add(new Instruction(OpCodes.BranchGreaterThan, 2));

            instructions.Add(new Instruction(OpCodes.LoadLocal, 1));
            instructions.Add(new Instruction(OpCodes.Ret));

            return new Function(def, instructions, new List<VMType>() { intType, intType })
            {
                Optimize = optimize
            };
        }
        public void TestLocals()
        {
            using (var container = new Win64Container())
            {
                var func = TestProgramGenerator.Locals(container);

                container.VirtualMachine.Verifier.VerifiyFunction(func);

                var virtualInstructions = VirtualRegisterIR.Create(container.VirtualMachine, func);
                var virtualControlFlowGraph = VirtualControlFlowGraph.FromBasicBlocks(
                    VirtualBasicBlock.CreateBasicBlocks(new ReadOnlyCollection<VirtualInstruction>(virtualInstructions)));

                var livenessIntervals = LivenessAnalysis.ComputeLiveness(virtualControlFlowGraph);

                Assert.AreEqual(3, livenessIntervals.Count);

                Assert.AreEqual(0, livenessIntervals[0].VirtualRegister.Number);
                Assert.AreEqual(0, livenessIntervals[0].Start);
                Assert.AreEqual(5, livenessIntervals[0].End);

                Assert.AreEqual(1, livenessIntervals[1].VirtualRegister.Number);
                Assert.AreEqual(1, livenessIntervals[1].Start);
                Assert.AreEqual(1, livenessIntervals[1].End);

                Assert.AreEqual(2, livenessIntervals[2].VirtualRegister.Number);
                Assert.AreEqual(3, livenessIntervals[2].Start);
                Assert.AreEqual(4, livenessIntervals[2].End);
            }
        }
        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 TestBranchFunction()
        {
            using (var container = new Win64Container())
            {
                var func = TestProgramGenerator.Branch(container);
                var instructions = func.Instructions;

                container.VirtualMachine.Verifier.VerifiyFunction(func);

                var basicBlocks = BasicBlock.CreateBasicBlocks(func);

                //Check that all the instrutions are in exactly one block
                Assert.AreEqual(instructions.Count, basicBlocks.Aggregate(0, (total, current) => total + current.Instructions.Count));
                Assert.AreEqual(4, basicBlocks.Count);

                Assert.AreEqual(0, basicBlocks[0].StartOffset);
                Assert.AreEqual(3, basicBlocks[0].Instructions.Count);

                Assert.AreEqual(3, basicBlocks[1].StartOffset);
                Assert.AreEqual(3, basicBlocks[1].Instructions.Count);

                Assert.AreEqual(6, basicBlocks[2].StartOffset);
                Assert.AreEqual(2, basicBlocks[2].Instructions.Count);

                Assert.AreEqual(8, basicBlocks[3].StartOffset);
                Assert.AreEqual(2, basicBlocks[3].Instructions.Count);
            }
        }
        public void TestMax()
        {
            using (var container = new Win64Container())
            {
                var func = TestProgramGenerator.Max(container);

                container.VirtualMachine.Verifier.VerifiyFunction(func);

                var basicBlocks = BasicBlock.CreateBasicBlocks(func);
                var controlGraph = ControlFlowGraph.FromBasicBlocks(basicBlocks);

                Assert.AreEqual(3, controlGraph.NeighborLists.Count);

                Assert.AreEqual(2, controlGraph.NeighborLists[basicBlocks[0]].Count);
                Assert.AreEqual(true, controlGraph.NeighborLists[basicBlocks[0]].SetEquals(new HashSet<ControlFlowEdge>()
                {
                    new ControlFlowEdge(basicBlocks[0], basicBlocks[1]),
                    new ControlFlowEdge(basicBlocks[0], basicBlocks[2]),
                }));

                Assert.AreEqual(1, controlGraph.NeighborLists[basicBlocks[1]].Count);
                Assert.AreEqual(true, controlGraph.NeighborLists[basicBlocks[1]].SetEquals(new HashSet<ControlFlowEdge>()
                    {
                        new ControlFlowEdge(basicBlocks[1], basicBlocks[3])
                    }));

                Assert.AreEqual(1, controlGraph.NeighborLists[basicBlocks[2]].Count);
                Assert.AreEqual(true, controlGraph.NeighborLists[basicBlocks[2]].SetEquals(new HashSet<ControlFlowEdge>()
                    {
                        new ControlFlowEdge(basicBlocks[2], basicBlocks[3])
                    }));
            }
        }
        public void TestLocals1()
        {
            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, 100));
                instructions.Add(new Instruction(OpCodes.StoreLocal, 0));

                instructions.Add(new Instruction(OpCodes.LoadInt, 200));
                instructions.Add(new Instruction(OpCodes.StoreLocal, 1));

                instructions.Add(new Instruction(OpCodes.LoadInt, 300));
                instructions.Add(new Instruction(OpCodes.StoreLocal, 2));

                instructions.Add(new Instruction(OpCodes.LoadInt, 400));
                instructions.Add(new Instruction(OpCodes.StoreLocal, 3));

                instructions.Add(new Instruction(OpCodes.LoadLocal, 3));
                instructions.Add(new Instruction(OpCodes.Ret));

                var func = new Function(funcDef, instructions, Enumerable.Repeat(intType, 4).ToList());
                container.LoadAssembly(Assembly.SingleFunction(func));
                Assert.AreEqual(400, container.Execute());
            }
        }
        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 TestArguments()
        {
            for (int i = 16; i <= 16; i++)
            {
                using (var container = new Win64Container())
                {
                    var assembly = new Assembly(
                        TestProgramGenerator.AddMainFunction(container, i),
                        TestProgramGenerator.AddFunction(container, i));

                    container.VirtualMachine.LoadAssembly(assembly);
                    Assert.AreEqual(i * (1 + i) / 2, 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 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.MulFloat));
                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);
            }
        }
示例#14
0
        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 TestSimple2()
        {
            using (var container = new Win64Container())
            {
                var func = TestProgramGenerator.Simple2(container);

                container.VirtualMachine.Verifier.VerifiyFunction(func);

                var virtualInstructions = VirtualRegisterIR.Create(container.VirtualMachine, func);
                var virtualControlFlowGraph = VirtualControlFlowGraph.FromBasicBlocks(
                    VirtualBasicBlock.CreateBasicBlocks(new ReadOnlyCollection<VirtualInstruction>(virtualInstructions)));

                var livenessIntervals = LivenessAnalysis.ComputeLiveness(virtualControlFlowGraph);
                var registerAllocation = LinearScanRegisterAllocation.Allocate(livenessIntervals, 2);

                Assert.AreEqual(2, registerAllocation.NumAllocatedRegisters);
                Assert.AreEqual(1, registerAllocation.NumSpilledRegisters);
            }
        }
        public void TestCall()
        {
            for (int i = 1; i <= 16; i++)
            {
                using (var container = new Win64Container())
                {
                    var mainFunc = TestProgramGenerator.AddMainFunction(container, i);
                    mainFunc.Optimize = true;

                    var addFunc = TestProgramGenerator.AddFunction(container, i);
                    addFunc.Optimize = true;

                    var assembly = new Assembly(mainFunc, addFunc);

                    container.VirtualMachine.LoadAssembly(assembly);
                    Assert.AreEqual(i * (1 + i) / 2, container.Execute());
                }
            }
        }
        /// <summary>
        /// Saves the disassembled functions to a file for the given container
        /// </summary>
        /// <param name="container">The container</param>
        /// <param name="fileName">The name of the file</param>
        public static void SaveDisassembledFunctions(Win64Container container, string fileName)
        {
            using (var fileStream = new FileStream(fileName, FileMode.Create))
            using (var writer = new StreamWriter(fileStream))
            {
                foreach (var assembly in container.VirtualMachine.LoadedAssemblies)
                {
                    foreach (var function in assembly.Functions)
                    {
                        var disassembler = new Disassembler(
                            container.VirtualMachine.Compiler.GetCompilationData(function),
                            x => new XONEVirtualMachine.Compiler.Win64.Disassembler(x),
                            DisassemblerOptions.NewLineAfterInstruction);

                        writer.WriteLine(disassembler.Disassemble());
                    }
                }
            }
        }
        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 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());
            }
        }
示例#21
0
        /// <summary>
        /// Creates an add function with that takes the given amount of arguments
        /// </summary>
        private static Function CreateAddFunction(Win64Container container, int numArgs, bool optimize = false)
        {
            var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int);

            var def = new FunctionDefinition("add", Enumerable.Repeat(intType, numArgs).ToList(), intType);

            var instructions = new List<Instruction>();
            instructions.Add(new Instruction(OpCodes.LoadArgument, 0));

            for (int i = 1; i < numArgs; i++)
            {
                instructions.Add(new Instruction(OpCodes.LoadArgument, i));
                instructions.Add(new Instruction(OpCodes.AddInt));
            }

            instructions.Add(new Instruction(OpCodes.Ret));

            return new Function(def, instructions, new List<VMType>())
            {
                Optimize = optimize
            };
        }
        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 TestSimpleFunction()
        {
            using (var container = new Win64Container())
            {
                var func = TestProgramGenerator.Simple(container);
                container.VirtualMachine.Verifier.VerifiyFunction(func);

                var basicBlocks = BasicBlock.CreateBasicBlocks(func);
                Assert.AreEqual(1, basicBlocks.Count);
                Assert.AreEqual(4, basicBlocks[0].Instructions.Count);
            }
        }
        public void TestMixed3()
        {
            using (var container = new Win64Container())
            {
                var intType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Int);
                var floatType = container.VirtualMachine.TypeProvider.GetPrimitiveType(PrimitiveTypes.Float);

                container.VirtualMachine.Binder.Define(FunctionDefinition.NewExternal<FuncFloatArgInt>(
                    "intToFloat",
                    new List<VMType>() { intType },
                    floatType,
                    FloatToInt));

                var convIntToFloat = new Instruction(OpCodes.Call, "intToFloat", new List<VMType>() { intType });
                var paramTypes = new List<VMType>() { intType, floatType, intType, floatType, intType, floatType };

                var addFunc = new Function(
                    new FunctionDefinition("add", paramTypes, floatType),
                    new List<Instruction>()
                    {
                        new Instruction(OpCodes.LoadArgument, 0),
                        convIntToFloat,
                        new Instruction(OpCodes.LoadArgument, 1),
                        new Instruction(OpCodes.LoadArgument, 2),
                        convIntToFloat,
                        new Instruction(OpCodes.LoadArgument, 3),
                        new Instruction(OpCodes.LoadArgument, 4),
                        convIntToFloat,
                        new Instruction(OpCodes.LoadArgument, 5),
                        new Instruction(OpCodes.AddFloat),
                        new Instruction(OpCodes.AddFloat),
                        new Instruction(OpCodes.AddFloat),
                        new Instruction(OpCodes.AddFloat),
                        new Instruction(OpCodes.AddFloat),
                        new Instruction(OpCodes.Ret)
                    },
                    new List<VMType>());
                addFunc.Optimize = true;

                var mainFunc = new Function(
                    new FunctionDefinition("floatMain", new List<VMType>(), floatType),
                    new List<Instruction>()
                    {
                        new Instruction(OpCodes.LoadInt, 1),
                        new Instruction(OpCodes.LoadFloat, 2.0f),
                        new Instruction(OpCodes.LoadInt, 3),
                        new Instruction(OpCodes.LoadFloat, 4.0f),
                        new Instruction(OpCodes.LoadInt, 5),
                        new Instruction(OpCodes.LoadFloat, 6.0f),
                        new Instruction(OpCodes.Call, "add", paramTypes),
                        new Instruction(OpCodes.Ret)
                    },
                    new List<VMType>());
                mainFunc.Optimize = true;

                var assembly = new Assembly(mainFunc, addFunc);

                container.VirtualMachine.LoadAssembly(assembly);
                Assert.AreEqual(1 + 2 + 3 + 4 + 5 + 6, TestProgramGenerator.ExecuteFloatProgram(container));
            }
        }
 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 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 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 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 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());
     }
 }