Exemple #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);
            }
        }
Exemple #2
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);
                }
            }
        }
Exemple #3
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());
            }
        }
Exemple #4
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);
                }
            }
        }
Exemple #5
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);
            }
        }
        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);
                }
            }
        }
        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());
            }
        }
Exemple #8
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());
            }
        }
Exemple #9
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());
                }
            }
        }
Exemple #10
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());
            }
        }
Exemple #11
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());
            }
        }
Exemple #12
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());
            }
        }
Exemple #13
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());
            }
        }
Exemple #14
0
        public void TestEmpty()
        {
            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>());

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

                var classDef = new SharpJIT.Loader.Data.Class(
                    "Point",
                    new List <SharpJIT.Loader.Data.Field>()
                {
                    new SharpJIT.Loader.Data.Field("x", intType.Name, SharpJIT.Core.Objects.AccessModifier.Public),
                    new SharpJIT.Loader.Data.Field("y", intType.Name, SharpJIT.Core.Objects.AccessModifier.Public)
                });

                var pointConstructor = new SharpJIT.Loader.Data.Function(
                    ".constructor",
                    new List <string>(),
                    voidType.Name,
                    "Point",
                    true,
                    new List <string>(),
                    new List <SharpJIT.Loader.Data.Instruction>()
                {
                    new SharpJIT.Loader.Data.Instruction(OpCodes.Return)
                });

                var pointAddFunction = new SharpJIT.Loader.Data.Function(
                    "add",
                    new List <string>(),
                    intType.Name,
                    "Point",
                    false,
                    new List <string>(),
                    new List <SharpJIT.Loader.Data.Instruction>()
                {
                    new SharpJIT.Loader.Data.Instruction(OpCodes.LoadArgument, 0),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.LoadField, "Point::x"),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.LoadArgument, 0),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.LoadField, "Point::y"),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.AddInt),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.Return)
                });

                var mainFunction = new SharpJIT.Loader.Data.Function(
                    "main",
                    new List <string>(),
                    intType.Name,
                    new List <string>()
                {
                    "Ref.Point"
                },
                    new List <SharpJIT.Loader.Data.Instruction>()
                {
                    new SharpJIT.Loader.Data.Instruction(OpCodes.NewObject, ".constructor", "Point", new List <string>()),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.StoreLocal, 0),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.LoadLocal, 0),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.LoadInt, 4711),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.StoreField, "Point::x"),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.LoadLocal, 0),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.LoadInt, 1337),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.StoreField, "Point::y"),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.LoadLocal, 0),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.CallInstance, "add", "Point", new List <string>()),
                    new SharpJIT.Loader.Data.Instruction(OpCodes.Return)
                });

                var assembly = new SharpJIT.Loader.Data.Assembly(
                    "test",
                    new List <SharpJIT.Loader.Data.Class>()
                {
                    classDef
                },
                    new List <SharpJIT.Loader.Data.Function>()
                {
                    pointConstructor, pointAddFunction, mainFunction
                });

                container.VirtualMachine.LoadAssembly(assembly);
                var result = container.Execute();
                Assert.AreEqual(1337 + 4711, result);
            }
        }
Exemple #16
0
        public void TestConstructor()
        {
            using (var container = new Win64Container())
            {
                var intType  = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Int);
                var voidType = container.VirtualMachine.TypeProvider.FindPrimitiveType(PrimitiveTypes.Void);

                var pointMetadata = new ClassMetadata("Point");
                pointMetadata.DefineField(new FieldDefinition("x", intType, AccessModifier.Public));
                pointMetadata.DefineField(new FieldDefinition("y", intType, AccessModifier.Public));
                pointMetadata.CreateFields();

                container.VirtualMachine.ClassMetadataProvider.Add(pointMetadata);

                var pointType = container.VirtualMachine.TypeProvider.FindClassType("Point");

                var pointConstructor = new ManagedFunction(
                    new FunctionDefinition(".constructor", new List <BaseType>()
                {
                    intType, intType
                }, voidType, pointType, true),
                    new List <BaseType>(),
                    new List <Instruction>()
                {
                    new Instruction(OpCodes.LoadArgument, 0),
                    new Instruction(OpCodes.LoadArgument, 1),
                    new Instruction(OpCodes.StoreField, "Point::x"),
                    new Instruction(OpCodes.LoadArgument, 0),
                    new Instruction(OpCodes.LoadArgument, 2),
                    new Instruction(OpCodes.StoreField, "Point::y"),
                    new Instruction(OpCodes.Return)
                });

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

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

                var result = container.Execute();
                Assert.AreEqual(1337 + 4711, result);
            }
        }