public void Float64Truncate_Compiled()
    {
        var exports = CompilerTestBase <double> .CreateInstance(
            new LocalGet(0),
            new Float64Truncate(),
            new End());

        foreach (var value in new[] { 1f, -1f, -Math.PI, Math.PI })
        {
            Assert.AreEqual(Math.Truncate(value), exports.Test(value));
        }
    }
Beispiel #2
0
        public void Float32Truncate_Compiled()
        {
            var exports = CompilerTestBase <float> .CreateInstance(
                new LocalGet(0),
                new Float32Truncate(),
                new End());

            foreach (var value in Samples.Single)
            {
                Assert.AreEqual((float)Math.Truncate(value), exports.Test(value));
            }
        }
Beispiel #3
0
        public void Float64Multiply_Compiled()
        {
            var exports = CompilerTestBase <double> .CreateInstance(
                new LocalGet(0),
                new Float64Constant(3),
                new Float64Multiply(),
                new End());

            Assert.AreEqual(0, exports.Test(0));
            Assert.AreEqual(9, exports.Test(3));
            Assert.AreEqual(-6, exports.Test(-2));
        }
    public void Float64Nearest_Compiled()
    {
        var exports = CompilerTestBase <double> .CreateInstance(
            new LocalGet(0),
            new Float64Nearest(),
            new End());

        foreach (var value in new[] { 1f, -1f, -Math.PI, Math.PI })
        {
            Assert.AreEqual(Math.Round(value, MidpointRounding.ToEven), exports.Test(value));
        }
    }
        public void Float32Nearest_Compiled()
        {
            var exports = CompilerTestBase <float> .CreateInstance(
                new LocalGet(0),
                new Float32Nearest(),
                new End());

            foreach (var value in Samples.Single)
            {
                Assert.AreEqual((float)Math.Round(value, MidpointRounding.ToEven), exports.Test(value));
            }
        }
Beispiel #6
0
    public void Float64Negate_Compiled()
    {
        var exports = CompilerTestBase <double> .CreateInstance(
            new LocalGet(0),
            new Float64Negate(),
            new End());

        foreach (var value in Samples.Double)
        {
            Assert.AreEqual(-value, exports.Test(value));
        }
    }
        public void Float64SquareRoot_Compiled()
        {
            var exports = CompilerTestBase <double> .CreateInstance(
                new GetLocal(0),
                new Float64SquareRoot(),
                new End());

            foreach (var value in new[] { 1f, -1f, -Math.PI, Math.PI })
            {
                Assert.AreEqual(Math.Sqrt(value), exports.Test(value));
            }
        }
        public void Int32ShiftLeft_Compiled()
        {
            const int amount = 0xF;

            var exports = CompilerTestBase<int>.CreateInstance(
                new GetLocal(0),
                new Int32Constant(amount),
                new Int32ShiftLeft(),
                new End());

            foreach (var value in new[] { 0x00, 0x01, 0x02, 0x0F, 0xF0, 0xFF, })
                Assert.AreEqual(value << amount, exports.Test(value));
        }
Beispiel #9
0
        public void CompileAndExecuteFunction(CompilerTestBase test, DfirRoot function)
        {
#if LLVM_TEST
            const string     compiledFunctionName = "test";
            LLVMSharp.Module compiledFunction     = test.RunSemanticAnalysisUpToLLVMCodeGeneration(function, compiledFunctionName);
            _context.LoadFunction(compiledFunction);
            _context.ExecuteFunctionTopLevel(compiledFunctionName);
#else
            Function compiledFunction = test.RunSemanticAnalysisUpToCodeGeneration(function);
            _context.LoadFunction(compiledFunction);
            _context.FinalizeLoad();
            _context.ExecuteFunctionTopLevel(compiledFunction.Name);
#endif
        }
Beispiel #10
0
        public void If_Compiled()
        {
            var exports = CompilerTestBase <int> .CreateInstance(
                new LocalGet(0),
                new If(),
                new Int32Constant(3),
                new Return(),
                new End(),
                new Int32Constant(2),
                new End());

            Assert.AreEqual(2, exports.Test(0));
            Assert.AreEqual(3, exports.Test(1));
        }
Beispiel #11
0
    public void SetLocal_Compiled()
    {
        var exports = CompilerTestBase <int> .CreateInstance(
            new LocalGet(0),
            new Int32Constant(1),
            new Int32Add(),
            new LocalSet(0),
            new LocalGet(0),
            new Int32Constant(1),
            new Int32Add(),
            new End());

        Assert.AreEqual(2, exports.Test(0));
        Assert.AreEqual(3, exports.Test(1));
    }
        public void Int64RemainderUnsigned_Compiled()
        {
            const uint divisor = 0xF;

            var exports = CompilerTestBase <long> .CreateInstance(
                new GetLocal(0),
                new Int64Constant(divisor),
                new Int64RemainderUnsigned(),
                new End());

            foreach (var value in new ulong[] { 0x00, 0x0F, 0xF0, 0xFF, })
            {
                Assert.AreEqual(value % divisor, (ulong)exports.Test((long)value));
            }
        }
        public void IInt64CountTrailingZeroes_Compiled()
        {
            var exports = CompilerTestBase <long> .CreateInstance(
                new GetLocal(0),
                new Int64CountTrailingZeroes(),
                new End());

            //Test cases from https://github.com/WebAssembly/spec/blob/f1b89dfaf379060c7e35eb90b7daeb14d4ade3f7/test/core/i64.wast
            Assert.AreEqual(0, exports.Test(-1));
            Assert.AreEqual(64, exports.Test(0));
            Assert.AreEqual(15, exports.Test(0x00008000));
            Assert.AreEqual(16, exports.Test(0x00010000));
            Assert.AreEqual(63, exports.Test(unchecked ((long)0x8000000000000000)));
            Assert.AreEqual(0, exports.Test(0x7fffffffffffffff));
        }
        public void Int64ShiftRightUnsigned_Compiled()
        {
            const int amount = 0xF;

            var exports = CompilerTestBase <long> .CreateInstance(
                new LocalGet(0),
                new Int64Constant(amount),
                new Int64ShiftRightUnsigned(),
                new End());

            foreach (var value in new ulong[] { 0x00, 0x01, 0x02, 0x0F, 0xF0, 0xFF, })
            {
                Assert.AreEqual(value >> amount, (ulong)exports.Test((long)value));
            }
        }
Beispiel #15
0
    public void Else_Compiled_CarriedValue()
    {
        var exports = CompilerTestBase <int> .CreateInstance(
            new LocalGet(0),
            new If(BlockType.Int32),
            new Int32Constant(1),
            new Else(),
            new Int32Constant(2),
            new End(),
            new End());

        Assert.AreEqual(2, exports.Test(0));
        Assert.AreEqual(1, exports.Test(1));
        Assert.AreEqual(1, exports.Test(2));
    }
        public void Int32Or_Compiled()
        {
            const int comparand = 0xF;

            var exports = CompilerTestBase <int> .CreateInstance(
                new GetLocal(0),
                new Int32Constant(comparand),
                new Int32Or(),
                new End());

            foreach (var value in new[] { 0x00, 0x0F, 0xF0, 0xFF, })
            {
                Assert.AreEqual(value | comparand, exports.Test(value));
            }
        }
Beispiel #17
0
        public void BranchIf_Compiled()
        {
            var exports = CompilerTestBase <int> .CreateInstance(
                new Block(BlockType.Empty),
                new LocalGet(0),
                new BranchIf(0),
                new Int32Constant(2),
                new Return(),
                new End(),
                new Int32Constant(1),
                new End());

            Assert.AreEqual(2, exports.Test(0));
            Assert.AreEqual(1, exports.Test(1));
        }
Beispiel #18
0
    public void Int32ShiftRightSigned_Compiled()
    {
        const int amount = 0xF;

        var exports = CompilerTestBase <int> .CreateInstance(
            new LocalGet(0),
            new Int32Constant(amount),
            new Int32ShiftRightSigned(),
            new End());

        foreach (var value in new[] { 0x00, 0x0F, 0xF0, 0xFF, })
        {
            Assert.AreEqual(value >> amount, exports.Test(value));
        }
    }
Beispiel #19
0
        public void Int64DivideSigned_Compiled()
        {
            const int divisor = 2;

            var exports = CompilerTestBase <long> .CreateInstance(
                new GetLocal(0),
                new Int64Constant(divisor),
                new Int64DivideSigned(),
                new End());

            foreach (var value in new long[] { 0, 1, 2, 3, 4, 5, })
            {
                Assert.AreEqual(value / divisor, exports.Test(value));
            }
        }
        public void Int32RemainderSigned_Compiled()
        {
            const int divisor = 0xF;

            var exports = CompilerTestBase <int> .CreateInstance(
                new GetLocal(0),
                new Int32Constant(divisor),
                new Int32RemainderSigned(),
                new End());

            foreach (var value in new[] { 0x00, 0x0F, 0xF0, 0xFF, })
            {
                Assert.AreEqual(value % divisor, exports.Test(value));
            }
        }
Beispiel #21
0
        public void Int32DivideUnsigned_Compiled()
        {
            const uint divisor = 2;

            var exports = CompilerTestBase <int> .CreateInstance(
                new LocalGet(0),
                new Int32Constant(divisor),
                new Int32DivideUnsigned(),
                new End());

            foreach (var value in new uint[] { 0, 1, 2, 3, 4, 5, })
            {
                Assert.AreEqual(value / divisor, (uint)exports.Test((int)value));
            }
        }
        public void Int64Subtract_Compiled()
        {
            const int comparand = 0x8;

            var exports = CompilerTestBase <long> .CreateInstance(
                new LocalGet(0),
                new Int64Constant(comparand),
                new Int64Subtract(),
                new End());

            foreach (var value in new long[] { 0x00, 0x0F, 0xF0, 0xFF, })
            {
                Assert.AreEqual(value - comparand, exports.Test(value));
            }
        }
        public void Int64ExclusiveOr_Compiled()
        {
            const int or = 0xF;

            var exports = CompilerTestBase <long> .CreateInstance(
                new GetLocal(0),
                new Int64Constant(or),
                new Int64ExclusiveOr(),
                new End());

            foreach (var value in new long[] { 0x00, 0x0F, 0xF0, 0xFF, })
            {
                Assert.AreEqual(value ^ or, exports.Test(value));
            }
        }
Beispiel #24
0
        public void Int64And_Compiled()
        {
            const int and = 0xF;

            var exports = CompilerTestBase <long> .CreateInstance(
                new GetLocal(0),
                new Int64Constant(and),
                new Int64And(),
                new End());

            foreach (var value in new long[] { 0x00, 0x0F, 0xF0, 0xFF, })
            {
                Assert.AreEqual(value & and, exports.Test(value));
            }
        }
Beispiel #25
0
        public void Int32CountLeadingZeroes_Compiled()
        {
            var exports = CompilerTestBase <int> .CreateInstance(
                new LocalGet(0),
                new Int32CountLeadingZeroes(),
                new End());

            //Test cases from https://github.com/WebAssembly/spec/blob/f1b89dfaf379060c7e35eb90b7daeb14d4ade3f7/test/core/i32.wast
            Assert.AreEqual(0, exports.Test(unchecked ((int)0xffffffff)));
            Assert.AreEqual(32, exports.Test(0));
            Assert.AreEqual(16, exports.Test(0x00008000));
            Assert.AreEqual(24, exports.Test(0xff));
            Assert.AreEqual(0, exports.Test(unchecked ((int)0x80000000)));
            Assert.AreEqual(31, exports.Test(1));
            Assert.AreEqual(30, exports.Test(2));
            Assert.AreEqual(1, exports.Test(0x7fffffff));
        }
Beispiel #26
0
        public void Int64CountOneBits_Compiled()
        {
            var exports = CompilerTestBase <long> .CreateInstance(
                new GetLocal(0),
                new Int64CountOneBits(),
                new End());

            //Test cases from https://github.com/WebAssembly/spec/blob/f1b89dfaf379060c7e35eb90b7daeb14d4ade3f7/test/core/i64.wast
            Assert.AreEqual(64, exports.Test(-1));
            Assert.AreEqual(0, exports.Test(0));
            Assert.AreEqual(1, exports.Test(0x00008000));
            Assert.AreEqual(4, exports.Test(unchecked ((long)0x8000800080008000)));
            Assert.AreEqual(63, exports.Test(0x7fffffffffffffff));
            Assert.AreEqual(32, exports.Test(unchecked ((long)0xAAAAAAAA55555555)));
            Assert.AreEqual(32, exports.Test(unchecked ((long)0x99999999AAAAAAAA)));
            Assert.AreEqual(48, exports.Test(unchecked ((long)0xDEADBEEFDEADBEEF)));
        }
Beispiel #27
0
        public void Int64ShiftRightUnsigned_Compiled()
        {
            if (!System.Environment.Is64BitProcess)
            {
                Assert.Inconclusive("32-bit .NET doesn't support 64-bit bit shift amounts.");
            }

            const int amount = 0xF;

            var exports = CompilerTestBase <long> .CreateInstance(
                new LocalGet(0),
                new Int64Constant(amount),
                new Int64ShiftRightUnsigned(),
                new End());

            foreach (var value in new ulong[] { 0x00, 0x01, 0x02, 0x0F, 0xF0, 0xFF, })
            {
                Assert.AreEqual(value >> amount, (ulong)exports.Test((long)value));
            }
        }
        public void Int64CountLeadingZeroes_Compiled()
        {
            if (!System.Environment.Is64BitProcess)
            {
                Assert.Inconclusive("32-bit .NET doesn't support the 64-bit bit shifts used by the count leading zeroes helper logic.");
            }

            var exports = CompilerTestBase <long> .CreateInstance(
                new LocalGet(0),
                new Int64CountLeadingZeroes(),
                new End());

            //Test cases from https://github.com/WebAssembly/spec/blob/f1b89dfaf379060c7e35eb90b7daeb14d4ade3f7/test/core/i64.wast
            Assert.AreEqual(0, exports.Test(unchecked ((long)0xffffffffffffffff)));
            Assert.AreEqual(64, exports.Test(0));
            Assert.AreEqual(48, exports.Test(0x00008000));
            Assert.AreEqual(56, exports.Test(0xff));
            Assert.AreEqual(0, exports.Test(unchecked ((long)0x8000000000000000)));
            Assert.AreEqual(63, exports.Test(1));
            Assert.AreEqual(62, exports.Test(2));
            Assert.AreEqual(1, exports.Test(0x7fffffffffffffff));
        }
        public CompileLoadResult CompileAndLoadFunction(CompilerTestBase test, DfirRoot function, DfirRoot[] otherFunctions)
        {
            var calleesIsYielding = new Dictionary <CompilableDefinitionName, bool>();
            var calleesMayPanic   = new Dictionary <CompilableDefinitionName, bool>();

            foreach (DfirRoot otherFunction in otherFunctions)
            {
                FunctionCompileResult otherCompileResult = test.RunSemanticAnalysisUpToLLVMCodeGeneration(
                    otherFunction,
                    FunctionCompileHandler.FunctionLLVMName(otherFunction.CompileSpecification.Name),
                    calleesIsYielding,
                    calleesMayPanic);
                calleesIsYielding[otherFunction.CompileSpecification.Name] = otherCompileResult.IsYielding;
                calleesMayPanic[otherFunction.CompileSpecification.Name]   = otherCompileResult.MayPanic;
                _context.LoadFunction(otherCompileResult.Module);
            }

            const string          compiledFunctionName = "test";
            FunctionCompileResult compileResult        = test.RunSemanticAnalysisUpToLLVMCodeGeneration(function, compiledFunctionName, calleesIsYielding, calleesMayPanic);

            _context.LoadFunction(compileResult.Module);
            return(new CompileLoadResult(compiledFunctionName, compileResult.IsYielding));
        }
 public void CompileAndExecuteFunction(CompilerTestBase test, DfirRoot function, DfirRoot[] otherFunctions)
 {
     ExecuteFunction(CompileAndLoadFunction(test, function, otherFunctions));
 }