Esempio n. 1
0
        public void Bool_non_constant_unary_expression_compiled_successfully(string expressionString, Opcode expectedOp)
        {
            var expressionSyntax = ParseExpression(expressionString);
            var method           = new CompiledMethod("Test::Method");
            var builder          = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics      = new TestingDiagnosticSink();
            var variableMap      = new ScopedVariableMap();

            variableMap.PushScope();
            variableMap.TryAddVariable("a", method.AddLocal(SimpleType.Bool, LocalFlags.None));

            var localIndex = ExpressionCompiler.TryCompileExpression(expressionSyntax,
                                                                     SimpleType.Bool, method, builder, new TestingResolver(variableMap), diagnostics);

            Assert.That(diagnostics.Diagnostics, Is.Empty);
            Assert.That(localIndex, Is.EqualTo(1));
            Assert.That(method.Values[localIndex].Type, Is.EqualTo(SimpleType.Bool));
            Assert.That(builder.Instructions, Has.Exactly(1).Items);

            var instruction = builder.Instructions[0];

            Assert.That(instruction.Operation, Is.EqualTo(expectedOp));
            Assert.That(instruction.Left, Is.EqualTo(0));
            Assert.That(instruction.Destination, Is.EqualTo(1));
        }
Esempio n. 2
0
        public void Integer_literal_that_is_too_large_fails()
        {
            var position     = new TextPosition(10, 3, 4);
            var syntax       = new IntegerLiteralSyntax((ulong)int.MaxValue + 1, position);
            var method       = new CompiledMethod("Test::Method");
            var builder      = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var nameResolver = new TestingResolver(new ScopedVariableMap());
            var diagnostics  = new TestingDiagnosticSink();

            var localIndex = ExpressionCompiler.TryCompileExpression(
                syntax, SimpleType.Int32, method, builder, nameResolver, diagnostics);

            Assert.That(localIndex, Is.EqualTo(-1));
            diagnostics.AssertDiagnosticAt(DiagnosticCode.TypeMismatch, position)
            .WithActual("uint32").WithExpected("int32");
        }
Esempio n. 3
0
        public void Constant_comparison_compiled_successfully(string expressionString, bool expectedValue)
        {
            var expressionSyntax = ParseExpression(expressionString);
            var method           = new CompiledMethod("Test::Method");
            var builder          = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics      = new TestingDiagnosticSink();
            var nameResolver     = new TestingResolver(new ScopedVariableMap());

            var localIndex = ExpressionCompiler.TryCompileExpression(expressionSyntax,
                                                                     SimpleType.Bool, method, builder, nameResolver, diagnostics);

            Assert.That(diagnostics.Diagnostics, Is.Empty);
            Assert.That(localIndex, Is.EqualTo(0));
            Assert.That(method.Values[localIndex].Type, Is.EqualTo(SimpleType.Bool));
            AssertSingleLoad(builder, localIndex, expectedValue);
        }
Esempio n. 4
0
        public void Variable_reference_must_refer_to_existent_variable()
        {
            var position    = new TextPosition(10, 3, 4);
            var syntax      = new IdentifierSyntax("a", position);
            var method      = new CompiledMethod("Test::Method");
            var builder     = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics = new TestingDiagnosticSink();
            var variableMap = new ScopedVariableMap();

            variableMap.PushScope();

            var localIndex = ExpressionCompiler.TryCompileExpression(
                syntax, SimpleType.Int32, method, builder, new TestingResolver(variableMap), diagnostics);

            Assert.That(localIndex, Is.EqualTo(-1));
            diagnostics.AssertDiagnosticAt(DiagnosticCode.VariableNotFound, position).WithActual("a");
        }
Esempio n. 5
0
        public void Int32_division_by_zero_in_constant_expression_fails(BinaryOperation op)
        {
            var position = new TextPosition(10, 3, 4);
            var syntax   = new BinaryExpressionSyntax(op,
                                                      new IntegerLiteralSyntax(2ul, default),
                                                      new IntegerLiteralSyntax(0, default),
                                                      position);
            var method       = new CompiledMethod("Test::Method");
            var builder      = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics  = new TestingDiagnosticSink();
            var nameResolver = new TestingResolver(new ScopedVariableMap());

            var localIndex = ExpressionCompiler.TryCompileExpression(
                syntax, SimpleType.Int32, method, builder, nameResolver, diagnostics);

            Assert.That(localIndex, Is.EqualTo(-1));
            diagnostics.AssertDiagnosticAt(DiagnosticCode.DivisionByConstantZero, position);
        }
Esempio n. 6
0
        public void Operator_not_defined_for_type_in_non_constant(string expressionString, string operatorName, string typeName)
        {
            var expressionSyntax = ParseExpression(expressionString);
            var method           = new CompiledMethod("Test::Method");
            var builder          = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics      = new TestingDiagnosticSink();
            var variableMap      = new ScopedVariableMap();

            variableMap.PushScope();
            variableMap.TryAddVariable("b", method.AddLocal(SimpleType.Bool, LocalFlags.None));
            variableMap.TryAddVariable("i", method.AddLocal(SimpleType.Int32, LocalFlags.None));

            var localIndex = ExpressionCompiler.TryCompileExpression(
                expressionSyntax, SimpleType.Void, method, builder, new TestingResolver(variableMap), diagnostics);

            Assert.That(localIndex, Is.EqualTo(-1));
            diagnostics.AssertDiagnosticAt(DiagnosticCode.OperatorNotDefined, expressionSyntax.Position)
            .WithActual(operatorName).WithExpected(typeName);
        }
Esempio n. 7
0
        public void Variable_reference_returns_local_index_of_variable()
        {
            var syntax      = new IdentifierSyntax("a", default);
            var method      = new CompiledMethod("Test::Method");
            var builder     = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics = new TestingDiagnosticSink();

            var variableMap = new ScopedVariableMap();

            variableMap.PushScope();
            variableMap.TryAddVariable("a", 0);
            method.AddLocal(SimpleType.Bool, LocalFlags.None);

            var localIndex = ExpressionCompiler.TryCompileExpression(
                syntax, SimpleType.Bool, method, builder, new TestingResolver(variableMap), diagnostics);

            Assert.That(localIndex, Is.EqualTo(0));
            Assert.That(diagnostics.Diagnostics, Is.Empty);
        }
Esempio n. 8
0
        public void Boolean_literal_stored_in_bool_succeeds()
        {
            var syntax       = new BooleanLiteralSyntax(true, default);
            var method       = new CompiledMethod("Test::Method");
            var builder      = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var nameResolver = new TestingResolver(new ScopedVariableMap());
            var diagnostics  = new TestingDiagnosticSink();

            var localIndex = ExpressionCompiler.TryCompileExpression(
                syntax, SimpleType.Bool, method, builder, nameResolver, diagnostics);

            Assert.That(localIndex, Is.EqualTo(0));
            Assert.That(diagnostics.Diagnostics, Is.Empty);
            Assert.That(method.Values, Has.Exactly(1).Items);

            var local = method.Values[0];

            Assert.That(local.Type, Is.EqualTo(SimpleType.Bool));
            AssertSingleLoad(builder, localIndex, true);
        }
Esempio n. 9
0
        public void Variable_reference_must_have_correct_type()
        {
            var position    = new TextPosition(10, 3, 4);
            var syntax      = new IdentifierSyntax("a", position);
            var method      = new CompiledMethod("Test::Method");
            var builder     = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics = new TestingDiagnosticSink();

            var variableMap = new ScopedVariableMap();

            variableMap.PushScope();
            variableMap.TryAddVariable("a", 0);
            method.AddLocal(SimpleType.Bool, LocalFlags.None);

            var localIndex = ExpressionCompiler.TryCompileExpression(
                syntax, SimpleType.Int32, method, builder, new TestingResolver(variableMap), diagnostics);

            Assert.That(localIndex, Is.EqualTo(-1));
            diagnostics.AssertDiagnosticAt(DiagnosticCode.TypeMismatch, position)
            .WithActual("bool").WithExpected("int32");
        }
Esempio n. 10
0
        public void Non_constant_inequality_compiled_successfully()
        {
            var expressionSyntax = ParseExpression("a != b");
            var method           = new CompiledMethod("Test::Method");
            var builder          = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics      = new TestingDiagnosticSink();
            var variableMap      = new ScopedVariableMap();

            variableMap.PushScope();
            variableMap.TryAddVariable("a", method.AddLocal(SimpleType.Int32, LocalFlags.None));
            variableMap.TryAddVariable("b", method.AddLocal(SimpleType.Int32, LocalFlags.None));

            var localIndex = ExpressionCompiler.TryCompileExpression(expressionSyntax,
                                                                     SimpleType.Bool, method, builder, new TestingResolver(variableMap), diagnostics);

            Assert.That(diagnostics.Diagnostics, Is.Empty);
            Assert.That(localIndex, Is.EqualTo(3));
            Assert.That(method.Values[localIndex].Type, Is.EqualTo(SimpleType.Bool));
            Assert.That(builder.Instructions, Has.Exactly(2).Items);

            // a != b is compiled as !(a == b)
            Assert.That(builder.Instructions[0], Is.EqualTo(new Instruction(Opcode.Equal, 0, 1, 2)));
            Assert.That(builder.Instructions[1], Is.EqualTo(new Instruction(Opcode.BitwiseNot, 2, 0, 3)));
        }