Beispiel #1
0
        public void TestCreateArray()
        {
            using (var container = new Win64Container())
            {
                var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

                var func = new ManagedFunction(
                    new FunctionDefinition("main", new List <BaseType>(), intType),
                    new List <BaseType>(),
                    new List <Instruction>()
                {
                    new Instruction(OpCodes.LoadInt, 10),
                    new Instruction(OpCodes.NewArray, intType.Name),
                    new Instruction(OpCodes.Pop),
                    new Instruction(OpCodes.LoadInt, 0),
                    new Instruction(OpCodes.Return)
                });

                var functions = TestHelpers.SingleFunction(func);

                container.VirtualMachine.LoadFunctionsAsAssembly(functions);
                var result = container.Execute();
                Assert.AreEqual(0, result);
            }
        }
Beispiel #2
0
        public void TestGreaterThanOrEqualFloat()
        {
            using (var container = new Win64Container())
            {
                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(
                                                                     this.CreateBranchFloatProgram(container, OpCodes.BranchGreaterThanOrEqual, 2, 1)));

                Assert.AreEqual(1, container.Execute());
            }

            using (var container = new Win64Container())
            {
                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(
                                                                     this.CreateBranchFloatProgram(container, OpCodes.BranchGreaterThanOrEqual, 1, 1)));

                Assert.AreEqual(1, container.Execute());
            }

            using (var container = new Win64Container())
            {
                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(
                                                                     this.CreateBranchFloatProgram(container, OpCodes.BranchGreaterThanOrEqual, 1, 2)));

                Assert.AreEqual(0, container.Execute());
            }
        }
Beispiel #3
0
        public void TestInvalidMain2()
        {
            using (var container = new Win64Container())
            {
                var voidType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Void);

                var mainFunc = new ManagedFunction(
                    new FunctionDefinition("main", new List <BaseType>()
                {
                }, voidType),
                    new List <BaseType>(),
                    new List <Instruction>()
                {
                    new Instruction(OpCodes.Return)
                });

                try
                {
                    container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.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);
                }
            }
        }
Beispiel #4
0
        public void TestNoMain()
        {
            using (var container = new Win64Container())
            {
                var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

                var testFunc = new ManagedFunction(
                    new FunctionDefinition("test", new List <BaseType>()
                {
                    intType
                }, intType),
                    new List <BaseType>(),
                    new List <Instruction>()
                {
                    new Instruction(OpCodes.LoadInt, 0),
                    new Instruction(OpCodes.Return)
                });

                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.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);
                }
            }
        }
Beispiel #5
0
        public void TestLocals1()
        {
            using (var container = new Win64Container())
            {
                var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);
                var funcDef = new FunctionDefinition("main", new List <BaseType>(), intType);

                var instructions = new List <Instruction>
                {
                    new Instruction(OpCodes.LoadInt, 100),
                    new Instruction(OpCodes.StoreLocal, 0),

                    new Instruction(OpCodes.LoadInt, 200),
                    new Instruction(OpCodes.StoreLocal, 1),

                    new Instruction(OpCodes.LoadInt, 300),
                    new Instruction(OpCodes.StoreLocal, 2),

                    new Instruction(OpCodes.LoadInt, 400),
                    new Instruction(OpCodes.StoreLocal, 3),

                    new Instruction(OpCodes.LoadLocal, 3),
                    new Instruction(OpCodes.Return)
                };
                var func = new ManagedFunction(funcDef, Enumerable.Repeat(intType, 4).ToList(), instructions);
                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(func));
                Assert.AreEqual(400, container.Execute());
            }
        }
        /// <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 ManagedFunction FloatAddFunction(Win64Container container, int numArgs)
        {
            var floatType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Float);

            var parameters = new List <BaseType>();

            for (int i = 0; i < numArgs; i++)
            {
                parameters.Add(floatType);
            }

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

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

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

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

            return(new ManagedFunction(def, new List <BaseType>(), instructions));
        }
Beispiel #7
0
        /// <summary>
        /// Creates a new float branch program
        /// </summary>
        private ManagedFunction CreateBranchFloatProgram(Win64Container container, OpCodes branchInstruction, float value1, float value2)
        {
            var floatType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Float);
            var intType   = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

            var instructions = new List <Instruction>
            {
                new Instruction(OpCodes.LoadFloat, value1),
                new Instruction(OpCodes.LoadFloat, value2),
                new Instruction(branchInstruction, 6),
                new Instruction(OpCodes.LoadInt, 0),
                new Instruction(OpCodes.StoreLocal, 0),
                new Instruction(OpCodes.Branch, 8),
                new Instruction(OpCodes.LoadInt, 1),
                new Instruction(OpCodes.StoreLocal, 0),
                new Instruction(OpCodes.LoadLocal, 0),
                new Instruction(OpCodes.Return)
            };

            return(new ManagedFunction(
                       new FunctionDefinition("main", new List <BaseType>(), intType),
                       new List <BaseType>()
            {
                intType
            },
                       instructions));
        }
Beispiel #8
0
        public void TestStoreField()
        {
            using (var container = new Win64Container())
            {
                var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);
                (var pointType, var pointConstructor) = TestHelpers.DefinePointClass(container.VirtualMachine);

                var func = new ManagedFunction(
                    new FunctionDefinition("main", new List <BaseType>(), intType),
                    new List <BaseType>()
                {
                    pointType
                },
                    new List <Instruction>()
                {
                    new Instruction(OpCodes.NewObject, ".constructor", pointType, new List <BaseType>()),
                    new Instruction(OpCodes.StoreLocal, 0),
                    new Instruction(OpCodes.LoadLocal, 0),
                    new Instruction(OpCodes.LoadInt, 1337),
                    new Instruction(OpCodes.StoreField, "Point::x"),
                    new Instruction(OpCodes.LoadLocal, 0),
                    new Instruction(OpCodes.LoadField, "Point::x"),
                    new Instruction(OpCodes.Return)
                });

                container.VirtualMachine.LoadFunctionsAsAssembly(new List <ManagedFunction>()
                {
                    func,
                    pointConstructor
                });

                var result = container.Execute();
                Assert.AreEqual(1337, result);
            }
        }
        /// <summary>
        /// The max function
        /// </summary>
        public static ManagedFunction Max(Win64Container container)
        {
            var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

            var instructions = new List <Instruction>
            {
                new Instruction(OpCodes.LoadArgument, 0),
                new Instruction(OpCodes.LoadArgument, 1),
                new Instruction(OpCodes.BranchGreaterThan, 6),

                new Instruction(OpCodes.LoadArgument, 1),
                new Instruction(OpCodes.StoreLocal, 0),
                new Instruction(OpCodes.Branch, 9),

                new Instruction(OpCodes.LoadArgument, 0),
                new Instruction(OpCodes.StoreLocal, 0),
                new Instruction(OpCodes.Branch, 9),

                new Instruction(OpCodes.LoadLocal, 0),
                new Instruction(OpCodes.Return)
            };

            return(new ManagedFunction(
                       new FunctionDefinition("max", new List <BaseType>()
            {
                intType, intType
            }, intType),
                       new List <BaseType>()
            {
                intType
            },
                       instructions));
        }
        /// <summary>
        /// Creates a sum function without a loop using locals
        /// </summary>
        public static ManagedFunction SumNoneLoopLocal(Win64Container container, int count)
        {
            var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

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

            var instructions = new List <Instruction>();

            for (int i = 1; i <= count; i++)
            {
                instructions.Add(new Instruction(OpCodes.LoadInt, i));
            }

            for (int i = 0; i < count - 1; i++)
            {
                instructions.Add(new Instruction(OpCodes.AddInt));
            }

            instructions.Add(new Instruction(OpCodes.StoreLocal, 0));
            instructions.Add(new Instruction(OpCodes.LoadLocal, 0));
            instructions.Add(new Instruction(OpCodes.Return));

            return(new ManagedFunction(def, new List <BaseType>()
            {
                intType
            }, instructions));
        }
Beispiel #11
0
        public void TestVoidParameter()
        {
            using (var container = new Win64Container())
            {
                var voidType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Void);

                var instructions = new List <Instruction>
                {
                    new Instruction(OpCodes.Return)
                };
                var func = new ManagedFunction(
                    new FunctionDefinition("test", new List <BaseType>()
                {
                    voidType
                }, voidType),
                    new List <BaseType>(),
                    instructions);

                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.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);
                }
            }
        }
Beispiel #12
0
        public void TestNotEndInReturn()
        {
            using (var container = new Win64Container())
            {
                var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

                var instructions = new List <Instruction>
                {
                    new Instruction(OpCodes.LoadInt, 0)
                };
                var func = new ManagedFunction(
                    new FunctionDefinition("main", new List <BaseType>(), intType),
                    new List <BaseType>(),
                    instructions);

                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(func));

                try
                {
                    container.Execute();
                    Assert.Fail("Expected without return to not pass.");
                }
                catch (VerificationException e)
                {
                    Assert.AreEqual("0: Functions must end with a 'RET' instruction.", e.Message);
                }
            }
        }
        /// <summary>
        /// A function with branches
        /// </summary>
        public static ManagedFunction Branch(Win64Container container)
        {
            var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

            var instructions = new List <Instruction>
            {
                new Instruction(OpCodes.LoadInt, 4),
                new Instruction(OpCodes.LoadInt, 2),
                new Instruction(OpCodes.BranchEqual, 6),

                new Instruction(OpCodes.LoadInt, 5),
                new Instruction(OpCodes.StoreLocal, 0),
                new Instruction(OpCodes.Branch, 8),

                new Instruction(OpCodes.LoadInt, 15),
                new Instruction(OpCodes.StoreLocal, 0),

                new Instruction(OpCodes.LoadLocal, 0),
                new Instruction(OpCodes.Return)
            };

            return(new ManagedFunction(
                       new FunctionDefinition("main", new List <BaseType>()
            {
            }, intType),
                       new List <BaseType>()
            {
                intType
            },
                       instructions));
        }
        /// <summary>
        /// Creates a recursive sum function
        /// </summary>
        public static ManagedFunction ResursiveSum(Win64Container container)
        {
            var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

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

            var instructions = new List <Instruction>
            {
                new Instruction(OpCodes.LoadArgument, 0),
                new Instruction(OpCodes.LoadInt, 0),
                new Instruction(OpCodes.BranchNotEqual, 5),
                new Instruction(OpCodes.LoadInt, 0),
                new Instruction(OpCodes.Return),

                new Instruction(OpCodes.LoadArgument, 0),
                new Instruction(OpCodes.LoadInt, 1),
                new Instruction(OpCodes.SubInt),
                new Instruction(OpCodes.Call, "sum", Enumerable.Repeat(intType, 1).ToList()),

                new Instruction(OpCodes.LoadArgument, 0),
                new Instruction(OpCodes.AddInt),
                new Instruction(OpCodes.Return)
            };

            return(new ManagedFunction(def, new List <BaseType>(), instructions));
        }
        /// <summary>
        /// A simple function without any control flow
        /// </summary>
        public static ManagedFunction Simple3(Win64Container container)
        {
            var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

            var instructions = new List <Instruction>
            {
                new Instruction(OpCodes.LoadInt, 1),
                new Instruction(OpCodes.LoadInt, 2),
                new Instruction(OpCodes.AddInt),
                new Instruction(OpCodes.LoadInt, 3),
                new Instruction(OpCodes.AddInt),
                new Instruction(OpCodes.LoadInt, 4),
                new Instruction(OpCodes.AddInt),
                new Instruction(OpCodes.LoadInt, 5),
                new Instruction(OpCodes.AddInt),
                new Instruction(OpCodes.Return)
            };

            return(new ManagedFunction(
                       new FunctionDefinition("main", new List <BaseType>()
            {
            }, intType),
                       new List <BaseType>(),
                       instructions));
        }
Beispiel #16
0
        /// <summary>
        /// Executes a program that has an entry point that returns a float
        /// </summary>
        private static float ExecuteFloatProgram(Win64Container container, string entryPointName = "floatMain")
        {
            container.VirtualMachine.Compile();
            var entryPoint = container.VirtualMachine.Binder.GetFunction(entryPointName + "()");
            var programPtr = (FloatEntryPoint)Marshal.GetDelegateForFunctionPointer(
                entryPoint.EntryPoint,
                typeof(FloatEntryPoint));

            return(programPtr());
        }
Beispiel #17
0
        public void TestRecursive2()
        {
            using (var container = new Win64Container())
            {
                container.VirtualMachine.LoadFunctionsAsAssembly(new List <ManagedFunction>()
                {
                    TestProgramGenerator.MainWithIntCall(container, "fib", 11),
                    TestProgramGenerator.RecursiveFib(container)
                });

                Assert.AreEqual(89, container.Execute());
            }
        }
        /// <summary>
        /// Creates the main function that invokes the given int function
        /// </summary>
        public static ManagedFunction MainWithIntCall(Win64Container container, string toCall, int n)
        {
            var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);

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

            var instructions = new List <Instruction>
            {
                new Instruction(OpCodes.LoadInt, n),
                new Instruction(OpCodes.Call, toCall, Enumerable.Repeat(intType, 1).ToList()),
                new Instruction(OpCodes.Return)
            };

            return(new ManagedFunction(def, new List <BaseType>(), instructions));
        }
        /// <summary>
        /// Executes a program that has an entry point that returns a float
        /// </summary>
        public static float ExecuteFloatProgram(Win64Container container, string entryPointName = "floatMain", string saveFileName = "")
        {
            container.VirtualMachine.Compile();

            if (saveFileName != "")
            {
                TestHelpers.SaveDisassembledFunctions(container, saveFileName);
            }

            var entryPoint = container.VirtualMachine.Binder.GetFunction(entryPointName + "()");
            var programPtr = (FloatEntryPoint)Marshal.GetDelegateForFunctionPointer(
                entryPoint.EntryPoint,
                typeof(FloatEntryPoint));

            return(programPtr());
        }
Beispiel #20
0
        public void TestArguments()
        {
            for (int i = 1; i <= 16; i++)
            {
                using (var container = new Win64Container())
                {
                    container.VirtualMachine.LoadFunctionsAsAssembly(new List <ManagedFunction>()
                    {
                        TestProgramGenerator.AddMainFunction(container, i),
                        TestProgramGenerator.AddFunction(container, i)
                    });

                    Assert.AreEqual(i * (1 + i) / 2, container.Execute());
                }
            }
        }
Beispiel #21
0
        public void TestInvalidOverload()
        {
            using (var container = new Win64Container())
            {
                var intType   = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);
                var floatType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Float);

                var func1 = new ManagedFunction(
                    new FunctionDefinition("test", new List <BaseType>()
                {
                    intType
                }, intType),
                    new List <BaseType>(),
                    new List <Instruction>()
                {
                    new Instruction(OpCodes.LoadInt, 0),
                    new Instruction(OpCodes.Return)
                });

                var func2 = new ManagedFunction(
                    new FunctionDefinition("test", new List <BaseType>()
                {
                    intType
                }, floatType),
                    new List <BaseType>(),
                    new List <Instruction>()
                {
                    new Instruction(OpCodes.LoadFloat, 0.0f),
                    new Instruction(OpCodes.Return)
                });

                try
                {
                    container.VirtualMachine.LoadFunctionsAsAssembly(new List <ManagedFunction>()
                    {
                        func1, func2
                    });
                    Assert.Fail("Expected invalid overload to not pass.");
                }
                catch (Exception e)
                {
                    Assert.AreEqual("The function 'test(Int) Float' is already defined.", e.Message);
                }
            }
        }
Beispiel #22
0
        /// <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 SharpJIT.Compiler.Win64.Disassembler(x),
                                DisassemblerOptions.NewLineAfterInstruction);

                            writer.WriteLine(disassembler.Disassemble());
                        }
                    }
                }
        }
Beispiel #23
0
        public void TestMul()
        {
            using (var container = new Win64Container())
            {
                var floatType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Float);
                var funcDef   = new FunctionDefinition("floatMain", new List <BaseType>(), floatType);

                var instructions = new List <Instruction>
                {
                    new Instruction(OpCodes.LoadFloat, 2.5f),
                    new Instruction(OpCodes.LoadFloat, 1.35f),
                    new Instruction(OpCodes.MulFloat),
                    new Instruction(OpCodes.Return)
                };
                var func = new ManagedFunction(funcDef, new List <BaseType>(), instructions);
                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(func));
                Assert.AreEqual(2.5f * 1.35f, ExecuteFloatProgram(container), 1E-4);
            }
        }
Beispiel #24
0
        public void TestFloatArguments()
        {
            for (int i = 1; i <= 16; i++)
            {
                using (var container = new Win64Container())
                {
                    container.VirtualMachine.LoadFunctionsAsAssembly(new List <ManagedFunction>()
                    {
                        TestProgramGenerator.FloatAddMainFunction(container, i),
                        TestProgramGenerator.FloatAddFunction(container, i)
                    });
                    container.VirtualMachine.Compile();

                    var funcPtr = Marshal.GetDelegateForFunctionPointer <FloatMain>(
                        container.VirtualMachine.Binder.GetFunction("floatMain()").EntryPoint);
                    Assert.AreEqual(i * (1 + i) / 2, funcPtr());
                }
            }
        }
Beispiel #25
0
        public void TestDiv()
        {
            using (var container = new Win64Container())
            {
                var intType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);
                var funcDef = new FunctionDefinition("main", new List <BaseType>(), intType);

                var instructions = new List <Instruction>
                {
                    new Instruction(OpCodes.LoadInt, 4),
                    new Instruction(OpCodes.LoadInt, 2),
                    new Instruction(OpCodes.DivInt),
                    new Instruction(OpCodes.Return)
                };
                var func = new ManagedFunction(funcDef, new List <BaseType>(), instructions);
                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(func));
                Assert.AreEqual(4 / 2, container.Execute());
            }
        }
Beispiel #26
0
        public void TestDefinitionOrder()
        {
            using (var container = new Win64Container())
            {
                var intType           = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);
                var assemblyFunctions = new List <ManagedFunction>();

                Action testFn = () =>
                {
                    var def = new FunctionDefinition("test", new List <BaseType>(), intType);

                    var instructions = new List <Instruction>
                    {
                        new Instruction(OpCodes.LoadInt, 1),
                        new Instruction(OpCodes.LoadInt, 2),
                        new Instruction(OpCodes.AddInt),
                        new Instruction(OpCodes.Return)
                    };
                    var func = new ManagedFunction(def, new List <BaseType>(), instructions);
                    assemblyFunctions.Add(func);
                };

                Action mainFn = () =>
                {
                    var def = new FunctionDefinition("main", new List <BaseType>(), intType);

                    var instructions = new List <Instruction>
                    {
                        new Instruction(OpCodes.Call, "test", new List <BaseType>()),
                        new Instruction(OpCodes.Return)
                    };

                    var func = new ManagedFunction(def, new List <BaseType>(), instructions);
                    assemblyFunctions.Add(func);
                };

                mainFn();
                testFn();
                container.VirtualMachine.LoadFunctionsAsAssembly(assemblyFunctions);
                Assert.AreEqual(3, container.Execute());
            }
        }
Beispiel #27
0
        public void TestMixedArguments()
        {
            using (var container = new Win64Container())
            {
                var intType    = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);
                var floatType  = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Float);
                var parameters = new List <BaseType>()
                {
                    intType, floatType, intType, floatType, intType, floatType
                };

                container.VirtualMachine.Binder.Define(FunctionDefinition.NewExternal <FuncIntArgIntFloatIntFloatIntFloat>(
                                                           "add",
                                                           parameters,
                                                           intType,
                                                           MixedAdd));

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

                var instructions = 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",
                        parameters),

                    new Instruction(OpCodes.Return)
                };
                var func = new ManagedFunction(def, new List <BaseType>(), instructions);
                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(func));

                Assert.AreEqual(1 + 2 + 3 + 4 + 5 + 6, container.Execute());
            }
        }
Beispiel #28
0
        public void TestFloatDefaultValue()
        {
            using (var container = new Win64Container())
            {
                var floatType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Float);
                var funcDef   = new FunctionDefinition("floatMain", new List <BaseType>(), floatType);

                var instructions = new List <Instruction>
                {
                    new Instruction(OpCodes.LoadLocal, 0),
                    new Instruction(OpCodes.Return)
                };
                var func = new ManagedFunction(funcDef, Enumerable.Repeat(floatType, 1).ToList(), instructions);
                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(func));
                container.VirtualMachine.Compile();
                var mainFunc = Marshal.GetDelegateForFunctionPointer <FloatMain>(
                    container.VirtualMachine.Binder.GetFunction("floatMain()").EntryPoint);

                Assert.AreEqual(0.0f, mainFunc());
            }
        }
Beispiel #29
0
        public void TestStackArguments()
        {
            using (var container = new Win64Container())
            {
                var intType    = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);
                var parameters = Enumerable.Repeat(intType, 9).ToList();

                container.VirtualMachine.Binder.Define(FunctionDefinition.NewExternal <FuncIntArgIntIntIntIntIntIntIntIntInt>(
                                                           "add",
                                                           parameters,
                                                           intType,
                                                           StackAdd));

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

                var instructions = new List <Instruction>
                {
                    new Instruction(OpCodes.LoadInt, 1),
                    new Instruction(OpCodes.LoadInt, 2),
                    new Instruction(OpCodes.LoadInt, 3),
                    new Instruction(OpCodes.LoadInt, 4),
                    new Instruction(OpCodes.LoadInt, 5),
                    new Instruction(OpCodes.LoadInt, 6),
                    new Instruction(OpCodes.LoadInt, 7),
                    new Instruction(OpCodes.LoadInt, 8),
                    new Instruction(OpCodes.LoadInt, 9),

                    new Instruction(
                        OpCodes.Call,
                        "add",
                        parameters),

                    new Instruction(OpCodes.Return)
                };
                var func = new ManagedFunction(def, new List <BaseType>(), instructions);
                container.VirtualMachine.LoadFunctionsAsAssembly(TestHelpers.SingleFunction(func));
                Assert.AreEqual(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, container.Execute());
            }
        }
Beispiel #30
0
        public void TestOverload()
        {
            using (var container = new Win64Container())
            {
                var intType   = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);
                var floatType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Float);

                var func1 = new ManagedFunction(
                    new FunctionDefinition("test", new List <BaseType>()
                {
                    intType
                }, intType),
                    new List <BaseType>(),
                    new List <Instruction>()
                {
                    new Instruction(OpCodes.LoadInt, 0),
                    new Instruction(OpCodes.Return)
                });

                var func2 = new ManagedFunction(
                    new FunctionDefinition("test", new List <BaseType>()
                {
                    floatType
                }, floatType),
                    new List <BaseType>(),
                    new List <Instruction>()
                {
                    new Instruction(OpCodes.LoadFloat, 0.0f),
                    new Instruction(OpCodes.Return)
                });

                container.VirtualMachine.LoadFunctionsAsAssembly(new List <ManagedFunction>()
                {
                    func1, func2
                });
            }
        }