Esempio n. 1
0
 private Expression GenerateExpression(Expression left, Expression right) {
     Contract.Requires(left != null);
     Contract.Requires(right != null);
     Contract.Requires(left.IsMemoryLocationOrConstant());
     Contract.Requires(right.IsMemoryLocationOrConstant());
     Contract.Ensures(Contract.Result<Expression>() != null);
     return Condition(
         ReductionExpressionGenerator.GenerateOrThrow("GREATEREQUAL", left, right),
         left,
         right
     );
 }
Esempio n. 2
0
 private Expression CreateExpression(Expression input) {
     Contract.Requires(null != input);
     Contract.Requires(input.IsMemoryLocationOrConstant());
     Contract.Ensures(Contract.Result<Expression>() != null);
     var gen = ReductionExpressionGenerator;
     var one = gen.GenerateOrThrow("1", input.Type);
     var log = gen.GenerateOrThrow("LOG",
         gen.GenerateOrThrow("DIVIDE",
             gen.GenerateOrThrow("ADD", one, input),
             gen.GenerateOrThrow("SUBTRACT", one, input)
         )
     );
     var two = gen.GenerateOrThrow("2", input.Type);
     return gen.GenerateOrThrow("DIVIDE", log, two);
 }
Esempio n. 3
0
 private Expression CreateExpression(Expression input) {
     Contract.Requires(input != null);
     Contract.Requires(input.IsMemoryLocationOrConstant());
     Contract.Ensures(Contract.Result<Expression>() != null);
     var gen = ReductionExpressionGenerator;
     return gen.GenerateOrThrow("LOG",
         gen.GenerateOrThrow("ADD",
             gen.GenerateOrThrow("SQUAREROOT",
                 gen.GenerateOrThrow("ADD",
                     gen.GenerateOrThrow("SQUARE", input),
                     gen.GenerateOrThrow("1", input.Type)
                 )
             ),
             input
         )
     );
 }
        private Expression GenerateCompareTo(IExpressionGenerationRequest request, Expression left, Expression right) {
            Contract.Requires(null != request);
            Contract.Requires(null != left);
            Contract.Requires(null != right);
            if (left.IsMemoryLocationOrConstant() && right.IsMemoryLocationOrConstant()) {
                var eq = request.TopLevelGenerator.GenerateOrThrow("EQUAL", left, right);
                var less = request.TopLevelGenerator.GenerateOrThrow("LESS", left, right);
                Contract.Assume(typeof(IComparable<>).GetGenericArguments() != null);
                Contract.Assume(typeof(IComparable<>).GetGenericArguments().Length == 1);
                Contract.Assume(typeof(IComparable<>).IsGenericTypeDefinition());
                var comparableType = typeof(IComparable<>).MakeGenericType(right.Type);

                if (left.Type.ImplementsInterface(comparableType)) {
                    var method = comparableType.GetPublicInstanceInvokableMethod(
                        "CompareTo",
                        new[] {right.Type}
                    );
                    Contract.Assume(method != null);
                    return method.BuildInstanceCallExpression(left, right);
                }

                return Expression.Condition(
                    eq,
                    Expression.Constant(0),
                    Expression.Condition(
                        less,
                        Expression.Constant(-1),
                        Expression.Constant(1)
                    )
                );
            }

            return new BlockExpressionBuilder().AddUsingMemoryLocationsOrConstants(locals => new[] {
                GenerateCompareTo(request, locals[0], locals[1])
            }, left, right).GetExpression();
        }