IfThen() публичный статический Метод

Creates a ConditionalExpression.
public static IfThen ( Expression test, Expression ifTrue ) : ConditionalExpression
test Expression An to set the property equal to.
ifTrue Expression An to set the property equal to.
Результат ConditionalExpression
Пример #1
0
        protected override object VisitFor(For F)
        {
            target.PushScope();

            // Generate the loop header code.
            Visit(F.Init);

            string name = target.LabelName();

            LinqExprs.LabelTarget begin = LinqExpr.Label("for_" + name + "_begin");
            LinqExprs.LabelTarget end   = LinqExpr.Label("for_" + name + "_end");
            loops.Push(new Loop(LinqExpr.Goto(end), LinqExpr.Goto(begin)));

            // Check the condition, exit if necessary.
            target.Add(LinqExpr.Label(begin));
            target.Add(LinqExpr.IfThen(LinqExpr.Not(target.Compile(F.Condition)), LinqExpr.Goto(end)));

            // Generate the body code.
            Visit(F.Body);

            // Generate the step code.
            Visit(F.Step);
            target.Add(LinqExpr.Goto(begin));

            // Exit point.
            target.Add(LinqExpr.Label(end));

            loops.Pop();
            target.PopScope();

            return(null);
        }
Пример #2
0
        public void IfThen()
        {
            var param    = LinqExpression.Parameter(typeof(bool));
            var expected = LinqExpression.IfThen(param, param);

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    a :IfThen ;
    :conditionTest _:param ;
    :conditionIfTrue _:param ;
.

_:param
    :parameterType [
        :typeName ""System.Boolean"" ;
    ] ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
Пример #3
0
        public void If(
            LinqExpr Condition,
            Action True,
            Action False)
        {
            string name = LabelName();

            LinqExprs.LabelTarget _else = LinqExpr.Label("if_" + name + "_else");
            LinqExprs.LabelTarget end   = LinqExpr.Label("if_" + name + "_end");

            // Check the condition, exit if necessary.
            code.Add(LinqExpr.IfThen(LinqExpr.Not(Condition), LinqExpr.Goto(_else)));

            // Execute true code.
            PushScope();
            True();
            PopScope();
            code.Add(LinqExpr.Goto(end));

            // Execute false code.
            code.Add(LinqExpr.Label(_else));
            PushScope();
            False();
            PopScope();

            // Exit point.
            code.Add(LinqExpr.Label(end));
        }
Пример #4
0
        private Exp CheckIfZero(CompilerState state, Exp expression)
        {
            if (state == null)
            {
                throw new Exception();
            }
            if (expression == null)
            {
                throw new Exception();
            }

            if (expression.Type == typeof(int))
            {
                var constructor = typeof(Exception).GetConstructor(new Type[] { typeof(string) });
                return(Exp.Block(
                           Exp.IfThen(Exp.Equal(expression, Exp.Constant(0)),
                                      Exp.Throw(Exp.New(constructor, Exp.Constant("Division by zero")))),
                           expression));
            }

            if (expression.Type == typeof(float))
            {
                var constructor = typeof(Exception).GetConstructor(new Type[] { typeof(string) });
                return(Exp.Block(
                           Exp.IfThen(Exp.Equal(expression, Exp.Constant(0.0f)),
                                      Exp.Throw(Exp.New(constructor, Exp.Constant("Division by zero")))),
                           expression));
            }
            return(expression);
        }
        public void IfThen()
        {
            var param      = LinqExpression.Parameter(typeof(bool));
            var expression = LinqExpression.IfThen(param, param);

            ShouldRoundrip(expression);
        }
Пример #6
0
        protected override object VisitDoWhile(DoWhile W)
        {
            string name = target.LabelName();

            LinqExprs.LabelTarget begin = LinqExpr.Label("do_" + name + "_begin");
            LinqExprs.LabelTarget end   = LinqExpr.Label("do_" + name + "_end");
            loops.Push(new Loop(LinqExpr.Goto(end), LinqExpr.Goto(begin)));

            target.PushScope();

            // Check the condition, exit if necessary.
            target.Add(LinqExpr.Label(begin));

            // Generate the body target.
            Visit(W.Body);

            // Loop.
            target.Add(LinqExpr.IfThen(target.Compile(W.Condition), LinqExpr.Goto(begin)));

            // Exit label.
            target.Add(LinqExpr.Label(end));

            loops.Pop();
            target.PopScope();
            return(null);
        }
Пример #7
0
        protected override object VisitIf(If I)
        {
            string name = target.LabelName();

            LinqExprs.LabelTarget _else = LinqExpr.Label("if_" + name + "_else");
            LinqExprs.LabelTarget end   = LinqExpr.Label("if_" + name + "_end");

            // Check the condition, exit if necessary.
            target.Add(LinqExpr.IfThen(LinqExpr.Not(target.Compile(I.Condition)), LinqExpr.Goto(_else)));

            // Execute true code.
            target.PushScope();
            Visit(I.True);
            target.PopScope();
            target.Add(LinqExpr.Goto(end));

            // Execute false code.
            target.Add(LinqExpr.Label(_else));
            target.PushScope();
            Visit(I.False);
            target.PopScope();

            // Exit point.
            target.Add(LinqExpr.Label(end));
            return(null);
        }
Пример #8
0
        // Pseudocode:
        //string input =>
        //{
        //    R result = 0;
        //    for (int i = input.Length - 1; i >= 0; i--)
        //    {
        //        result <<= 6;
        //        var m = _invMap[input[i]];
        //        if (m == 0xff)
        //            return default(ConversionResult<R>);
        //        result += m;
        //    }
        //    return new ConversionResult<R>(result);
        //}
        private LambdaExpression fromLambda(Type to)
        {
            var stringthis = typeof(string).GetTypeInfo().DeclaredProperties.First(p => p.GetIndexParameters().Length == 1 && p.GetIndexParameters()[0].ParameterType == typeof(int));
            var input      = Ex.Parameter(typeof(string), "input");
            var result     = Ex.Parameter(to, "result");
            var i          = Ex.Parameter(typeof(int), "i");
            var m          = Ex.Parameter(typeof(byte), "m");
            var loopstart  = Ex.Label("loopstart");
            var end        = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            var loop       = Ex.Block(
                Ex.Label(loopstart),
                Ex.IfThen(Ex.MakeBinary(ExpressionType.LessThan, i, Ex.Constant(0)),
                          Ex.Goto(end, Result(to, result))),
                Ex.LeftShiftAssign(result, Ex.Constant(6)),
                Ex.Assign(m, Ex.ArrayIndex(Ex.Constant(_invMap), Ex.Convert(Ex.MakeIndex(input, stringthis, new[] { i }), typeof(int)))),
                Ex.IfThen(Ex.MakeBinary(ExpressionType.Equal, m, Ex.Constant((byte)0xff)),
                          Ex.Goto(end, NoResult(to))),
                Ex.AddAssign(result, Ex.Convert(m, result.Type)),
                Ex.PostDecrementAssign(i),
                Ex.Goto(loopstart));
            var block = Ex.Block(new[] { result, i, m },
                                 Ex.Assign(result, Ex.Convert(Ex.Constant(0), to)),
                                 Ex.Assign(i, Ex.MakeBinary(ExpressionType.Subtract, Ex.Property(input, nameof(string.Length)), Ex.Constant(1))),
                                 loop,
                                 Ex.Label(end, NoResult(to)));

            return(Ex.Lambda(block, input));
        }
Пример #9
0
        /// <summary>
        /// Generate a for loop with the given code generator functions.
        /// </summary>
        /// <param name="Init"></param>
        /// <param name="Condition"></param>
        /// <param name="Step"></param>
        /// <param name="Body"></param>
        public void For(
            Action Init,
            LinqExpr Condition,
            Action Step,
            Action <LinqExpr, LinqExpr> Body)
        {
            PushScope();

            // Generate the loop header code.
            Init();

            string name = LabelName();

            LinqExprs.LabelTarget begin = LinqExpr.Label("for_" + name + "_begin");
            LinqExprs.LabelTarget end   = LinqExpr.Label("for_" + name + "_end");

            // Check the condition, exit if necessary.
            code.Add(LinqExpr.Label(begin));
            code.Add(LinqExpr.IfThen(LinqExpr.Not(Condition), LinqExpr.Goto(end)));

            // Generate the body code.
            Body(LinqExpr.Goto(end), LinqExpr.Goto(begin));

            // Generate the step code.
            Step();
            code.Add(LinqExpr.Goto(begin));

            // Exit point.
            code.Add(LinqExpr.Label(end));

            PopScope();
        }
        public LambdaExpression CreateLambda(Type from, Type to)
        {
            var toParameters = to.GetTypeInfo().GenericTypeArguments;
            var tupa         = toParameters.Length;
            var input        = Ex.Parameter(from, "input");
            var converters   = toParameters.Select(p => Ref.GetLambda(typeof(string), p)).ToArray();
            var res          = toParameters.Select(p => Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(p))).ToArray();
            var end          = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            var indexer      = typeof(string[]).GetTypeInfo().GetDeclaredProperty("Item");

            var split      = Ex.Parameter(typeof(string[]), "split");
            var conversion = Ex.Block(converters.Select((c, i) =>
                                                        Ex.Block(
                                                            Ex.Assign(res[i],
                                                                      c.ApplyTo(Ex.MakeIndex(split, indexer, new[] { Ex.MakeBinary(Et.Add, Ex.Constant(i), Ex.MakeBinary(Et.Subtract, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa))) }))),
                                                            Ex.IfThen(Ex.Not(Ex.Property(res[i], nameof(IConversionResult.IsSuccessful))),
                                                                      Ex.Goto(end, NoResult(to))))));
            var block = Ex.Block(new[] { split },
                                 Ex.Assign(split, Ex.Call(input, nameof(string.Split), Type.EmptyTypes, _separator)),
                                 Ex.Condition(Ex.MakeBinary(Et.LessThan, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa)),
                                              NoResult(to),
                                              Ex.Block(res,
                                                       Ex.IfThen(Ex.MakeBinary(Et.GreaterThan, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa)),
                                                                 Ex.Assign(Ex.ArrayAccess(split, Ex.MakeBinary(Et.Subtract, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa))),
                                                                           Ex.Call(typeof(string), nameof(string.Join), Type.EmptyTypes, _separatorString,
                                                                                   Ex.Call(typeof(Enumerable), nameof(Enumerable.Take), new[] { typeof(string) }, split,
                                                                                           Ex.MakeBinary(Et.Add, Ex.Constant(1), Ex.MakeBinary(Et.Subtract, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa))))))),
                                                       conversion,
                                                       Ex.Label(end, Result(to, Ex.Call(Creator(to), res.Select(r => Ex.Property(r, nameof(IConversionResult.Result)))))))));
            var lambda = Ex.Lambda(block, input);

            return(lambda);
        }
Пример #11
0
        private LambdaExpression toDictLambda(Type from, Type to)
        {
            var valueType = recordCreator.GetValueType(to);
            var recType   = typeof(IRecord <>).MakeGenericType(valueType);
            var set       = recType.GetTypeInfo().GetDeclaredMethod(nameof(IRecord <object> .SetValue));

            var input      = Ex.Parameter(from, "input");
            var tmp        = Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(valueType), "tmp");
            var res        = Ex.Parameter(typeof(IDictionary <,>).MakeGenericType(typeof(string), valueType), "res");
            var rec        = Ex.Parameter(recType, "rec");
            var getters    = GetReadablePropertiesForType(from);
            var converters = getters.Select(g => Ref.GetLambda(g.PropertyType, valueType));

            var end   = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to));
            var block = Ex.Block(new[] { tmp, res, rec },
                                 Ex.Assign(res, Ex.New(GetParameterlessConstructor(to))),
                                 Ex.Assign(rec, recordCreator.Creator(to).ApplyTo(res)),
                                 Ex.IfThen(Ex.MakeBinary(ExpressionType.Equal, rec, Ex.Default(rec.Type)), Ex.Goto(end, NoResult(to))),
                                 Ex.Block(getters.Zip(converters, (g, c) => new { g, c })
                                          .Select(x =>
                                                  Ex.Block(
                                                      Ex.Assign(tmp, x.c.ApplyTo(Ex.Property(input, x.g))),
                                                      Ex.IfThenElse(Ex.Property(tmp, nameof(IConversionResult.IsSuccessful)),
                                                                    Ex.Call(rec, set, Ex.Constant(x.g.Name), Ex.Property(tmp, nameof(IConversionResult.Result))),
                                                                    Ex.Goto(end, NoResult(to)))))),
                                 Ex.Label(end, Result(to, Ex.Convert(res, to))));

            return(Ex.Lambda(block, input));
        }
Пример #12
0
        /// <summary>
        /// Generate a while loop with the given code generator functions.
        /// </summary>
        /// <param name="Condition"></param>
        /// <param name="Body"></param>
        public void While(
            LinqExpr Condition,
            Action <LinqExpr, LinqExpr> Body)
        {
            string name = LabelName();

            LinqExprs.LabelTarget begin = LinqExpr.Label("while_" + name + "_begin");
            LinqExprs.LabelTarget end   = LinqExpr.Label("while_" + name + "_end");

            PushScope();

            // Check the condition, exit if necessary.
            code.Add(LinqExpr.Label(begin));
            code.Add(LinqExpr.IfThen(LinqExpr.Not(Condition), LinqExpr.Goto(end)));

            // Generate the body code.
            Body(LinqExpr.Goto(end), LinqExpr.Goto(begin));

            // Loop.
            code.Add(LinqExpr.Goto(begin));

            // Exit label.
            code.Add(LinqExpr.Label(end));

            PopScope();
        }
Пример #13
0
 /// <summary>
 /// Wrap a position equation around a cylinder.
 /// </summary>
 /// <param name="radius">Radius of the cylinder</param>
 /// <param name="ang0">Starting angle offset (radians) on the cylinder. 0 = z-axis</param>
 /// <param name="maxWrap">Maximum angle value (radians) of the wrap. After this, the function will continue along the tangent. Starting offset not included. Absolute value tested.</param>
 /// <param name="axisOff">Offset angle (radians) of the axis of the cylinder from the y-axis</param>
 /// <param name="position">Position equation</param>
 /// <returns></returns>
 public static tv3 CylinderWrap(efloat radius, efloat ang0, efloat maxWrap, efloat axisOff, ev2 position) =>
 EEx.Resolve(radius, ang0, axisOff, position, (r, a0, axis, _v2) => {
     var cs    = new TExV2();
     var xyd   = new TExV2();
     var v2    = new TExV2(_v2);
     var v3    = new TExV3();
     var a     = ExUtils.VFloat();
     var aRem  = ExUtils.VFloat();
     var aMax  = ExUtils.VFloat();
     var a_cs  = new TExV2();
     var a0_cs = new TExV2();
     return(Ex.Block(new[] { v3, cs, xyd, a, aRem, aMax, a_cs, a0_cs },
                     aMax.Is(maxWrap),
                     cs.Is(CosSin(axis)),
                     xyd.Is(ConvertBasis(v2, cs)),
                     a.Is(xyd.x.Div(r)),
                     aRem.Is(E0),
                     Ex.IfThen(Abs(a).GT(aMax), Ex.Block(
                                   Ex.IfThen(a.LT0(), MulAssign(aMax, EN1)),
                                   aRem.Is(a.Sub(aMax)),
                                   a.Is(aMax)
                                   )),
                     a0_cs.Is(CosSin(a0)),
                     a_cs.Is(CosSin(a.Add(a0))),
                     xyd.x.Is(r.Mul(a_cs.y.Sub(a0_cs.y).Add(aRem.Mul(a_cs.x)))),
                     v3.Is(TP3(DeconvertBasis(xyd, cs))),
                     v3.z.Is(r.Mul(a_cs.x.Sub(a0_cs.x).Sub(aRem.Mul(a_cs.y)))),
                     v3
                     ));
 });
Пример #14
0
 public static ExTP LNearestEnemy() => b => {
     var loc = new TExV2();
     return(Ex.Block(new ParameterExpression[] { loc },
                     Ex.IfThen(Ex.Not(Enemy.findNearest.Of(b.loc, loc)),
                               loc.Is(Ex.Constant(new Vector2(0f, 50f)))
                               ),
                     loc
                     ));
 };
Пример #15
0
 private static Ex enumeratorConversion(Type to, Ex enumerator, Type eType, LambdaExpression[] converters, Ex[] res, System.Linq.Expressions.LabelTarget end)
 {
     return(Ex.Block(res.Zip(converters, (r, con) =>
                             Ex.Block(
                                 Ex.IfThenElse(
                                     enumerator.Type.GetTypeInfo().GetDeclaredMethod(nameof(IEnumerator.MoveNext)) == null
                         ? Ex.Call(Ex.Convert(enumerator, typeof(IEnumerator)), nameof(IEnumerator.MoveNext), Type.EmptyTypes)
                         : Ex.Call(enumerator, enumerator.Type.GetTypeInfo().GetDeclaredMethod(nameof(IEnumerator.MoveNext))),
                                     Ex.Assign(r, con.ApplyTo(Ex.Property(enumerator, nameof(IEnumerator.Current)))),
                                     Ex.Goto(end, NoResult(to))),
                                 Ex.IfThen(Ex.Not(Ex.Property(r, nameof(IConversionResult.IsSuccessful))),
                                           Ex.Goto(end, NoResult(to)))))));
 }
Пример #16
0
        public static Ex DictSafeGet(this Ex dict, Ex key, string err)
        {
            return(Ex.Block(
                       Ex.IfThen(Ex.Not(ExUtils.DictContains(dict, key)), Ex.Throw(Ex.Constant(new Exception(err)))),
                       dict.DictGet(key)
                       ));

            /*
             * return Ex.Condition(ExUtils.DictContains<K, V>(dict, key), dict.DictGet(key), Ex.Block(
             *  Ex.Throw(Ex.Constant(new Exception(err))),
             *  dict.DictGet(key)
             * ));*/
        }
Пример #17
0
        public void ReplaceMember()
        {
            var x  = V <Vector2>("x");
            var y  = VF("y");
            var ex = Ex.Block(new[] { x }, Ex.Assign(x, ExC(new Vector2(2f, 3f))), Ex.Field(x, "x").Add(ExC(6f)));

            AreEqual("((x=(2.0, 3.0));\n(x.x+6);)", ex.Debug());
            AreEqual("((x=(2.0, 3.0));\n8;)", ex.FlatDebug());
            AreEqual(Compile(ex.Flatten()), 8f);
            ex = Ex.Block(new[] { x }, Ex.Assign(x, ExC(new Vector2(2f, 3f))), Ex.IfThen(y.GT(E1), x.Is(ExC(Vector2.right))), Ex.Add(Ex.Field(x, "x"), ExC(6f)));
            AreEqual("((x=(2.0, 3.0));\nif(y>1){(x=(1.0, 0.0))}else{};\n(x.x+6);)", ex.FlatDebug());
            ex = Ex.Block(new[] { x }, x.Is(ExC(new Vector2(2f, 3f))), Ex.IfThen(y.GT(E1), Ex.Field(x, "x").Is(ExC(6f))), Ex.Add(Ex.Field(x, "x"), ExC(6f)));
            AreEqual("((x=(2.0, 3.0));\nif(y>1){(x.x=6)}else{};\n(x.x+6);)", ex.FlatDebug());
        }
Пример #18
0
        private LambdaExpression constructionLambda(Type from, Type to)
        {
            var valueType = recordCreator.GetValueType(from);
            var recType   = typeof(IRecord <>).MakeGenericType(valueType);
            var tryGet    = recType.GetTypeInfo().GetDeclaredMethod(nameof(IRecord <object> .TryGetValue));

            var input = Ex.Parameter(from, "input");
            var rec   = Ex.Parameter(recType, "rec");
            var ctor  = GetConstructorForType(to);
            var tmp   = Ex.Parameter(valueType, "tmp");
            var pars  = ctor.GetParameters().Select((p, i) => new
            {
                Converter = Ref.GetLambda(valueType, p.ParameterType),
                Var       = Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(p.ParameterType), p.Name),
                Type      = p.ParameterType,
                Optional  = p.GetCustomAttributes <OptionalAttribute>().Any(),
                Default   = p.HasDefaultValue ? p.DefaultValue : p.ParameterType.GetTypeInfo().IsValueType ? Activator.CreateInstance(p.ParameterType) : null
            }).ToArray();
            var end   = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            var block = Ex.Block(new[] { rec }.Concat(pars.Select(x => x.Var)),
                                 Ex.Assign(rec, recordCreator.Creator(from).ApplyTo(input)),
                                 Ex.Block(new[] { tmp }, pars
                                          .Select(x =>
                                                  x.Optional
                        ? Ex.Block(
                                                      Ex.IfThenElse(
                                                          Ex.MakeBinary(ExpressionType.OrElse,
                                                                        Ex.Call(rec, tryGet, Ex.Constant(x.Var.Name), tmp),
                                                                        Ex.Call(rec, tryGet, Ex.Constant(ToPascalCase(x.Var.Name)), tmp)),
                                                          Ex.Block(
                                                              Ex.Assign(x.Var, x.Converter.ApplyTo(tmp)),
                                                              Ex.IfThen(Ex.Not(Ex.Property(x.Var, nameof(IConversionResult.IsSuccessful))),
                                                                        Ex.Goto(end, NoResult(to)))),
                                                          Ex.Assign(x.Var, Result(x.Type, Ex.Constant(x.Default, x.Type)))))
                        : Ex.Block(
                                                      Ex.IfThen(Ex.Not(Ex.Call(rec, tryGet, Ex.Constant(x.Var.Name), tmp)),
                                                                Ex.IfThen(Ex.Not(Ex.Call(rec, tryGet, Ex.Constant(ToPascalCase(x.Var.Name)), tmp)),
                                                                          Ex.Goto(end, NoResult(to)))),
                                                      Ex.Assign(x.Var, x.Converter.ApplyTo(tmp)),
                                                      Ex.IfThen(Ex.Not(Ex.Property(x.Var, nameof(IConversionResult.IsSuccessful))),
                                                                Ex.Goto(end, NoResult(to)))))),
                                 Ex.Label(end, Result(to, Ex.New(ctor, pars.Select(p => Ex.Property(p.Var, nameof(IConversionResult.Result)))))));

            return(Ex.Lambda(block, input));
        }
Пример #19
0
        // Pseudocode:
        //I input =>
        //{
        //  var i = (UI)input;
        //  var result = new char[base64sizeof(UI)];
        //  for (int j = 0; j == 0 || i > 0; j++)
        //  {
        //      result[j] = _mapChar[i & 0x3f];
        //      i >>= 6;
        //  }
        //}
        private LambdaExpression toLambda(Type from)
        {
            var input     = Ex.Parameter(from, "input");
            var result    = Ex.Parameter(typeof(char[]), "result");
            var i         = workingType(from) == from ? input : Ex.Parameter(workingType(from), "i");
            var j         = Ex.Parameter(typeof(int), "j");
            var loopstart = Ex.Label("loopstart");
            var loopend   = Ex.Label("loopend");

            var loop = Ex.Block(
                Ex.Label(loopstart),
                Ex.IfThen(Ex.MakeBinary(ExpressionType.AndAlso,
                                        Ex.MakeBinary(ExpressionType.GreaterThan, j, Ex.Constant(0)),
                                        i.Type == typeof(BigInteger)
                        ? (Ex)Ex.Call(i, nameof(BigInteger.Equals), Type.EmptyTypes, Ex.Constant(BigInteger.Zero))
                        : Ex.MakeBinary(ExpressionType.Equal, i, Ex.Convert(Ex.Constant(0), i.Type))),
                          Ex.Goto(loopend)),
                Ex.Assign(
                    Ex.ArrayAccess(result, j),
                    Ex.ArrayIndex(Ex.Constant(_mapChars),
                                  Ex.Convert(Ex.MakeBinary(ExpressionType.And, i, Ex.Convert(Ex.Constant(0x3f), i.Type)), typeof(int)))),
                Ex.RightShiftAssign(i, Ex.Constant(6)),
                Ex.PostIncrementAssign(j),
                Ex.Goto(loopstart));
            var ret = Result(typeof(string),
                             Ex.New(typeof(string).GetTypeInfo().DeclaredConstructors
                                    .Select(c => new { c, p = c.GetParameters() })
                                    .First(c => c.p.Length == 3 && c.p[0].ParameterType == typeof(char[]) && c.p[1].ParameterType == typeof(int) && c.p[2].ParameterType == typeof(int)).c,
                                    result, Ex.Constant(0), j));
            var block = Ex.Block(Ex.Assign(j, Ex.Constant(0)),
                                 Ex.Assign(result, Ex.NewArrayBounds(typeof(char), Ex.Constant(charbound(from)))),
                                 loop,
                                 Ex.Label(loopend),
                                 ret);

            block = input == i
                ? Ex.Block(new[] { j, result },
                           block)
                : Ex.Block(new[] { i, j, result },
                           Ex.Assign(i, Ex.Convert(input, i.Type)),
                           block);

            return(Ex.Lambda(block, input));
        }
Пример #20
0
            /// <summary>
            /// Creates an Entry based on the converter functions passed as a parameter.
            /// </summary>
            public static Entry Make <T, U>(IEnumerable <Tuple <LambdaExpression, Delegate> > fs)
            {
                var subs      = fs.Select(t => Tuple.Create(t.Item1, (Func <T, ConversionResult <U> >)t.Item2)).ToArray();
                var functions = subs.Select(f => f.Item2).ToArray();
                var specific  = functions.Length == 0
                    ? _ => new ConversionResult <U>()
                    : functions.Length == 1
                        ? functions[0]
                        : new Func <T, ConversionResult <U> >(i =>
                {
                    ConversionResult <U> res;
                    for (int j = 0; j < functions.Length; j++)
                    {
                        res = functions[j](i);
                        if (res.IsSuccessful)
                        {
                            return(res);
                        }
                    }
                    return(new ConversionResult <U>());
                });
                var generic = new Func <object, IConversionResult>(i => specific((T)i));
                var input   = Ex.Parameter(typeof(T), "input");
                var result  = Ex.Parameter(typeof(ConversionResult <U>), "result");
                var end     = Ex.Label(typeof(ConversionResult <U>), "end");
                var lambda  = subs.Length == 0
                    ? (Expression <Func <T, ConversionResult <U> > >)(_ => default(ConversionResult <U>))
                    : subs.Length == 1
                        ? subs[0].Item1 ?? funcToLambda(subs[0].Item2)
                        : Ex.Lambda(Ex.Block(new[] { result },
                                             Ex.Block(subs.Select(sub =>
                                                                  Ex.Block(
                                                                      Ex.Assign(result, (sub.Item1 ?? funcToLambda(sub.Item2)).ApplyTo(input)),
                                                                      Ex.IfThen(
                                                                          Ex.Property(result, nameof(IConversionResult.IsSuccessful)),
                                                                          Ex.Goto(end, result))))),
                                             Ex.Label(end, NoResult(typeof(U)))), input);

                return(new Entry(generic, specific, lambda));

                LambdaExpression funcToLambda(Func <T, ConversionResult <U> > f)
                => (Expression <Func <T, ConversionResult <U> > >)(inp => f(inp));
            }
Пример #21
0
        private LambdaExpression createForCollection(Type from, Type to)
        {
            var cType = (from i in @from.GetTypeInfo().ImplementedInterfaces.Concat(new[] { @from })
                         where i.GetTypeInfo().IsInterface &&
                         i.GenericTypeArguments.Length == 1 &&
                         i.GetGenericTypeDefinition() == typeof(IReadOnlyCollection <>)
                         select i.GenericTypeArguments[0]).SingleOrDefault();

            if (cType == null)
            {
                return(null);
            }
            var toParameters  = to.GetTypeInfo().GenericTypeArguments;
            var input         = Ex.Parameter(from, "input");
            var getEnumerator = getGetEnumeratorMethod(from);
            var enumerator    = Ex.Parameter(getEnumerator.ReturnType, "enumerator");
            var eType         = getEnumerator.ReturnType.GetTypeInfo().GetDeclaredProperty(nameof(IEnumerator.Current)).PropertyType;
            var converters    = toParameters.Select(p => Ref.GetLambda(eType, p)).ToArray();

            var res        = toParameters.Select(p => Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(p))).ToArray();
            var end        = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            Ex  conversion = enumeratorConversion(to, enumerator, eType, converters, res, end);
            var block      = Ex.Block(res.Concat(new[] { enumerator }),
                                      Ex.IfThen(
                                          Ex.MakeBinary(Et.LessThan,
                                                        Ex.Property(input, typeof(IReadOnlyCollection <>).MakeGenericType(cType).GetTypeInfo()
                                                                    .GetDeclaredProperty(nameof(IReadOnlyCollection <object> .Count))),
                                                        Ex.Constant(TupleArity(to))),
                                          Ex.Goto(end, NoResult(to))),
                                      Ex.Assign(enumerator, Ex.Call(input, getEnumerator)),
                                      typeof(IDisposable).GetTypeInfo().IsAssignableFrom(enumerator.Type.GetTypeInfo())
                    ? (Ex)Ex.TryFinally(conversion,
                                        Ex.Call(enumerator, typeof(IDisposable).GetTypeInfo().GetDeclaredMethod(nameof(IDisposable.Dispose))))
                    : conversion,
                                      Ex.Label(end, Result(to, Ex.Call(Creator(to), res.Select(r => Ex.Property(r, nameof(IConversionResult.Result)))))));

            var lambda = Ex.Lambda(block, input);

            return(lambda);
        }
Пример #22
0
        private LambdaExpression createForList(Type from, Type to)
        {
            var cType = (from i in @from.GetTypeInfo().ImplementedInterfaces.Concat(new[] { @from })
                         where i.GetTypeInfo().IsInterface &&
                         i.GenericTypeArguments.Length == 1 &&
                         i.GetGenericTypeDefinition() == typeof(IReadOnlyList <>)
                         select i.GenericTypeArguments[0]).SingleOrDefault();

            if (cType == null)
            {
                return(null);
            }
            var indexer      = typeof(IReadOnlyList <>).MakeGenericType(cType).GetTypeInfo().GetDeclaredProperty("Item");
            var count        = typeof(IReadOnlyCollection <>).MakeGenericType(cType).GetTypeInfo().GetDeclaredProperty(nameof(IReadOnlyCollection <object> .Count));
            var toParameters = to.GetTypeInfo().GenericTypeArguments;
            var input        = Ex.Parameter(from, "input");
            var converters   = toParameters.Select(p => Ref.GetLambda(cType, p)).ToArray();
            var res          = toParameters.Select(p => Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(p))).ToArray();
            var end          = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");

            var conversion = Ex.Block(converters.Select((c, i) =>
                                                        Ex.Block(
                                                            Ex.Assign(res[i],
                                                                      c.ApplyTo(Ex.MakeIndex(input, indexer, new[] { Ex.Constant(i) }))),
                                                            Ex.IfThen(Ex.Not(Ex.Property(res[i], nameof(IConversionResult.IsSuccessful))),
                                                                      Ex.Goto(end, NoResult(to))))));
            var block = Ex.Block(res,
                                 Ex.IfThen(
                                     Ex.MakeBinary(Et.LessThan,
                                                   Ex.Property(input, count),
                                                   Ex.Constant(toParameters.Length)),
                                     Ex.Goto(end, NoResult(to))),
                                 conversion,
                                 Ex.Label(end, Result(to, Ex.Call(Creator(to), res.Select(r => Ex.Property(r, nameof(IConversionResult.Result)))))));
            var lambda = Ex.Lambda(block, input);

            return(lambda);
        }
Пример #23
0
        private LambdaExpression setterLambda(Type from, Type to)
        {
            var valueType = recordCreator.GetValueType(from);
            var recType   = typeof(IRecord <>).MakeGenericType(valueType);
            var tryGet    = recType.GetTypeInfo().GetDeclaredMethod(nameof(IRecord <object> .TryGetValue));

            var input   = Ex.Parameter(from, "input");
            var rec     = Ex.Parameter(recType, "rec");
            var tmp     = Ex.Parameter(valueType, "tmp");
            var ctor    = GetParameterlessConstructor(to);
            var setters = GetWritablePropertiesForType(to);
            var res     = Ex.Parameter(to, "res");
            var pars    = setters.Select((p, i) => new
            {
                Converter = Ref.GetLambda(valueType, p.PropertyType),
                Var       = Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(p.PropertyType), p.Name)
            }).ToArray();
            var end   = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            var block = Ex.Block(new[] { tmp, res, rec }.Concat(pars.Select(x => x.Var)),
                                 Ex.Assign(res, Ex.New(ctor)),
                                 Ex.Assign(rec, recordCreator.Creator(from).ApplyTo(input)),
                                 Ex.Block(pars
                                          .Select(x =>
                                                  Ex.Block(
                                                      Ex.IfThen(
                                                          Ex.MakeBinary(ExpressionType.OrElse,
                                                                        Ex.Call(rec, tryGet, Ex.Constant(x.Var.Name), tmp),
                                                                        Ex.Call(rec, tryGet, Ex.Constant(ToCamelCase(x.Var.Name)), tmp)),
                                                          Ex.Block(
                                                              Ex.Assign(x.Var, x.Converter.ApplyTo(tmp)),
                                                              Ex.IfThenElse(Ex.Property(x.Var, nameof(IConversionResult.IsSuccessful)),
                                                                            Ex.Assign(Ex.Property(res, x.Var.Name), Ex.Property(x.Var, nameof(IConversionResult.Result))),
                                                                            Ex.Goto(end, NoResult(to)))))))),
                                 Ex.Label(end, Result(to, res)));

            return(Ex.Lambda(block, input));
        }
        public LambdaExpression CreateLambda(Type from, Type to)
        {
            var input          = Ex.Parameter(from, "input");
            var eType          = to.GetElementType();
            var res            = Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(eType).MakeArrayType(), "res");
            var end            = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            var fromParameters = from.GetTypeInfo().GenericTypeArguments;
            var converters     = fromParameters.Select(t => new { Lambda = Ref.GetLambda(t, eType), Input = t }).ToArray();

            var block = Ex.Block(new[] { res },
                                 Ex.Assign(res, Ex.NewArrayBounds(typeof(ConversionResult <>).MakeGenericType(eType), Ex.Constant(fromParameters.Length))),
                                 Ex.Block(converters.Select((con, i) =>
                                                            Ex.Block(
                                                                Ex.Assign(Ex.ArrayAccess(res, Ex.Constant(i)), con.Lambda.ApplyTo(Ex.PropertyOrField(input, $"Item{i + 1}"))),
                                                                Ex.IfThen(Ex.Not(Ex.Property(Ex.ArrayIndex(res, Ex.Constant(i)), nameof(IConversionResult.IsSuccessful))),
                                                                          Ex.Goto(end, NoResult(to)))))),
                                 Ex.Label(end, Result(to,
                                                      Ex.NewArrayInit(eType,
                                                                      Enumerable.Range(0, fromParameters.Length)
                                                                      .Select(idx => Ex.Property(Ex.ArrayIndex(res, Ex.Constant(idx)), nameof(IConversionResult.Result)))))));
            var lambda = Ex.Lambda(block, input);

            return(lambda);
        }
        private static TDelegate Simdize <T, TDelegate>(LambdaExpression expr)
            where T : unmanaged
            where TDelegate : Delegate
        {
            if (_cache.TryGetValue(expr, out var value))
            {
                return((TDelegate)value);
            }

            var simdVisitor = new SimdVisitor <T>(expr);
            var simdBody    = simdVisitor.Visit(expr.Body);

            var i   = Expr.Parameter(typeof(int), "i");
            var len = Expr.Parameter(typeof(int), "len");

            var xMemories    = expr.Parameters.Select(p => Expr.Parameter(typeof(ReadOnlyMemory <T>), p.Name)).ToArray();
            var resultMemory = Expr.Parameter(typeof(Memory <T>), "result");

            var xSpans     = xMemories.Select(p => Expr.Variable(typeof(ReadOnlySpan <T>), $"{p.Name}Span")).ToArray();
            var resultSpan = Expr.Variable(typeof(Span <T>), "resultSpan");

            var xVecSpans     = xMemories.Select(p => Expr.Variable(typeof(ReadOnlySpan <Vector <T> >), $"{p.Name}VecSpan")).ToArray();
            var resultVecSpan = Expr.Variable(typeof(Span <Vector <T> >), "resultVecSpan");

            var xSpanGetters    = xSpans.Select(p => Expr.Call(null, MemberTable._ReadOnlySpan <T> .GetItem, p, i)).ToArray();
            var xVecSpanGetters = xVecSpans.Select(p => Expr.Call(null, MemberTable._ReadOnlySpan <Vector <T> > .GetItem, p, i)).ToArray();

            var exprCall = new LambdaArgsVisitor(expr.Parameters.Zip(xSpanGetters).ToDictionary(tpl => tpl.Item1, tpl => (Expr)tpl.Item2)).Visit(expr.Body);
            var simdCall = new LambdaArgsVisitor(simdVisitor.NewArguments.Zip(xVecSpanGetters).ToDictionary(tpl => tpl.Item1, tpl => (Expr)tpl.Item2)).Visit(simdBody);

            var resultSpanSetter    = Expr.Call(null, MemberTable._Span <T> .SetItem, resultSpan, i, exprCall);
            var resultVecSpanSetter = Expr.Call(null, MemberTable._Span <Vector <T> > .SetItem, resultVecSpan, i, simdCall);

            var parameters = xMemories.Concat(new[] { resultMemory });
            var variables  = xSpans.Concat(xVecSpans).Concat(new[] { i, len, resultSpan, resultVecSpan });

            var block = new List <Expr>();

            //  xSpan = xMemory.Span;
            block.AddRange(xMemories.Zip(xSpans, (memory, span) =>
                                         Expr.Assign(
                                             span,
                                             Expr.Property(memory, MemberTable._ReadOnlyMemory <T> .Span)
                                             )
                                         ));

            //  xVecSpan = MemoryMarshal.Cast<T, Vector<T>>(xSpan);
            block.AddRange(xSpans.Zip(xVecSpans, (span, vSpan) =>
                                      Expr.Assign(
                                          vSpan,
                                          Expr.Call(null, MemberTable._MemoryMarshal.Cast <T, Vector <T> > .ForReadOnlySpan, span)
                                          )
                                      ));

            //  resultSpan = resultMemory.Span;
            block.Add(
                Expr.Assign(
                    resultSpan,
                    Expr.Property(resultMemory, MemberTable._Memory <T> .Span)
                    )
                );

            //  resultVecSpan = MemoryMarshal.Cast<T, Vector<T>>(resultSpan);
            block.Add(
                Expr.Assign(
                    resultVecSpan,
                    Expr.Call(null, MemberTable._MemoryMarshal.Cast <T, Vector <T> > .ForSpan, resultSpan)
                    )
                );

            //  for(i = 0, len = resultVecSpan.Length & ~0b1111; i < len; )
            //  {
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x0
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x1
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x2
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x3
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x4
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x5
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x6
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x7
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x8
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x9
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0xA
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0xB
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0xC
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0xD
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0xE
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0xF
            //  }
            block.Add(
                ExpressionEx.For(
                    Expr.Block(
                        Expr.Assign(i, Expr.Constant(0)),
                        Expr.Assign(
                            len,
                            Expr.And(Expr.Property(resultVecSpan, MemberTable._Span <Vector <T> > .Length), Expr.Constant(~0b1111))
                            )
                        ),
                    Expr.LessThan(i, len),
                    Expr.Empty(),
                    Expr.Block(
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x0
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x1
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x2
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x3
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x4
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x5
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x6
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x7
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x8
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x9
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0xA
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0xB
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0xC
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0xD
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0xE
                        resultVecSpanSetter, Expr.PreIncrementAssign(i)   // 0xF
                        )
                    )
                );

            //  if(i < (resultVecSpan.Length & ~0b111))
            //  {
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x0
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x1
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x2
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x3
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x4
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x5
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x6
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x7
            //  }
            block.Add(
                Expr.IfThen(
                    Expr.LessThan(
                        i,
                        Expr.And(Expr.Property(resultVecSpan, MemberTable._Span <Vector <T> > .Length), Expr.Constant(~0b111))
                        ),
                    Expr.Block(
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x0
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x1
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x2
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x3
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x4
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x5
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x6
                        resultVecSpanSetter, Expr.PreIncrementAssign(i)   // 0x7
                        )
                    )
                );

            //  if(i < (resultVecSpan.Length & ~0b11))
            //  {
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x0
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x1
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x2
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x3
            //  }
            block.Add(
                Expr.IfThen(
                    Expr.LessThan(
                        i,
                        Expr.And(Expr.Property(resultVecSpan, MemberTable._Span <Vector <T> > .Length), Expr.Constant(~0b11))
                        ),
                    Expr.Block(
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x0
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x1
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x2
                        resultVecSpanSetter, Expr.PreIncrementAssign(i)   // 0x3
                        )
                    )
                );

            //  if(i < (resultVecSpan.Length & ~0b1))
            //  {
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x0
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x1
            //  }
            block.Add(
                Expr.IfThen(
                    Expr.LessThan(
                        i,
                        Expr.And(Expr.Property(resultVecSpan, MemberTable._Span <Vector <T> > .Length), Expr.Constant(~0b1))
                        ),
                    Expr.Block(
                        resultVecSpanSetter, Expr.PreIncrementAssign(i),  // 0x0
                        resultVecSpanSetter, Expr.PreIncrementAssign(i)   // 0x1
                        )
                    )
                );

            //  if(i < resultVecSpan.Length)
            //  {
            //      resultVecSpan[i] = simdCall(xVecSpan[i], ...); ++i;  // 0x0
            //  }
            block.Add(
                Expr.IfThen(
                    Expr.LessThan(
                        i,
                        Expr.Property(resultVecSpan, MemberTable._Span <Vector <T> > .Length)
                        ),
                    Expr.Block(
                        resultVecSpanSetter, Expr.PreIncrementAssign(i)   // 0x0
                        )
                    )
                );

            //  for(i = Vector<T>.Count * resultVecSpan.Length; i < resultSpan.Length; )
            //  {
            //      resultSpan[i] = exprCall(xSpan[i], ...); ++i;
            //  }
            block.Add(
                ExpressionEx.For(
                    Expr.Assign(
                        i,
                        Expr.Multiply(
                            Expr.Constant(Vector <T> .Count),
                            Expr.Property(resultVecSpan, MemberTable._Span <Vector <T> > .Length)
                            )
                        ),
                    Expr.LessThan(i, Expr.Property(resultSpan, MemberTable._Span <T> .Length)),
                    Expr.Empty(),
                    Expr.Block(
                        resultSpanSetter, Expr.PreIncrementAssign(i)
                        )
                    )
                );
            var retval = Expr.Lambda <TDelegate>(Expr.Block(variables, block), parameters).Compile();

            _cache.TryAdd(expr, retval);
            return(retval);
        }
Пример #26
0
        // The resulting lambda processes N samples, using buffers provided for Input and Output:
        //  void Process(int N, double t0, double T, double[] Input0 ..., double[] Output0 ...)
        //  { ... }
        private Delegate DefineProcess()
        {
            // Map expressions to identifiers in the syntax tree.
            List <KeyValuePair <Expression, LinqExpr> > inputs  = new List <KeyValuePair <Expression, LinqExpr> >();
            List <KeyValuePair <Expression, LinqExpr> > outputs = new List <KeyValuePair <Expression, LinqExpr> >();

            // Lambda code generator.
            CodeGen code = new CodeGen();

            // Create parameters for the basic simulation info (N, t, Iterations).
            ParamExpr SampleCount = code.Decl <int>(Scope.Parameter, "SampleCount");
            ParamExpr t           = code.Decl(Scope.Parameter, Simulation.t);

            // Create buffer parameters for each input...
            foreach (Expression i in Input)
            {
                inputs.Add(new KeyValuePair <Expression, LinqExpr>(i, code.Decl <double[]>(Scope.Parameter, i.ToString())));
            }

            // ... and output.
            foreach (Expression i in Output)
            {
                outputs.Add(new KeyValuePair <Expression, LinqExpr>(i, code.Decl <double[]>(Scope.Parameter, i.ToString())));
            }

            // Create globals to store previous values of inputs.
            foreach (Expression i in Input.Distinct())
            {
                AddGlobal(i.Evaluate(t_t0));
            }

            // Define lambda body.

            // int Zero = 0
            LinqExpr Zero = LinqExpr.Constant(0);

            // double h = T / Oversample
            LinqExpr h = LinqExpr.Constant(TimeStep / (double)Oversample);

            // Load the globals to local variables and add them to the map.
            foreach (KeyValuePair <Expression, GlobalExpr <double> > i in globals)
            {
                code.Add(LinqExpr.Assign(code.Decl(i.Key), i.Value));
            }

            foreach (KeyValuePair <Expression, LinqExpr> i in inputs)
            {
                code.Add(LinqExpr.Assign(code.Decl(i.Key), code[i.Key.Evaluate(t_t0)]));
            }

            // Create arrays for linear systems.
            int      M   = Solution.Solutions.OfType <NewtonIteration>().Max(i => i.Equations.Count(), 0);
            int      N   = Solution.Solutions.OfType <NewtonIteration>().Max(i => i.UnknownDeltas.Count(), 0) + 1;
            LinqExpr JxF = code.DeclInit <double[][]>("JxF", LinqExpr.NewArrayBounds(typeof(double[]), LinqExpr.Constant(M)));

            for (int j = 0; j < M; ++j)
            {
                code.Add(LinqExpr.Assign(LinqExpr.ArrayAccess(JxF, LinqExpr.Constant(j)), LinqExpr.NewArrayBounds(typeof(double), LinqExpr.Constant(N))));
            }

            // for (int n = 0; n < SampleCount; ++n)
            ParamExpr n = code.Decl <int>("n");

            code.For(
                () => code.Add(LinqExpr.Assign(n, Zero)),
                LinqExpr.LessThan(n, SampleCount),
                () => code.Add(LinqExpr.PreIncrementAssign(n)),
                () =>
            {
                // Prepare input samples for oversampling interpolation.
                Dictionary <Expression, LinqExpr> dVi = new Dictionary <Expression, LinqExpr>();
                foreach (Expression i in Input.Distinct())
                {
                    LinqExpr Va = code[i];
                    // Sum all inputs with this key.
                    IEnumerable <LinqExpr> Vbs = inputs.Where(j => j.Key.Equals(i)).Select(j => j.Value);
                    LinqExpr Vb = LinqExpr.ArrayAccess(Vbs.First(), n);
                    foreach (LinqExpr j in Vbs.Skip(1))
                    {
                        Vb = LinqExpr.Add(Vb, LinqExpr.ArrayAccess(j, n));
                    }

                    // dVi = (Vb - Va) / Oversample
                    code.Add(LinqExpr.Assign(
                                 Decl <double>(code, dVi, i, "d" + i.ToString().Replace("[t]", "")),
                                 LinqExpr.Multiply(LinqExpr.Subtract(Vb, Va), LinqExpr.Constant(1.0 / (double)Oversample))));
                }

                // Prepare output sample accumulators for low pass filtering.
                Dictionary <Expression, LinqExpr> Vo = new Dictionary <Expression, LinqExpr>();
                foreach (Expression i in Output.Distinct())
                {
                    code.Add(LinqExpr.Assign(
                                 Decl <double>(code, Vo, i, i.ToString().Replace("[t]", "")),
                                 LinqExpr.Constant(0.0)));
                }

                // int ov = Oversample;
                // do { -- ov; } while(ov > 0)
                ParamExpr ov = code.Decl <int>("ov");
                code.Add(LinqExpr.Assign(ov, LinqExpr.Constant(Oversample)));
                code.DoWhile(() =>
                {
                    // t += h
                    code.Add(LinqExpr.AddAssign(t, h));

                    // Interpolate the input samples.
                    foreach (Expression i in Input.Distinct())
                    {
                        code.Add(LinqExpr.AddAssign(code[i], dVi[i]));
                    }

                    // Compile all of the SolutionSets in the solution.
                    foreach (SolutionSet ss in Solution.Solutions)
                    {
                        if (ss is LinearSolutions)
                        {
                            // Linear solutions are easy.
                            LinearSolutions S = (LinearSolutions)ss;
                            foreach (Arrow i in S.Solutions)
                            {
                                code.DeclInit(i.Left, i.Right);
                            }
                        }
                        else if (ss is NewtonIteration)
                        {
                            NewtonIteration S = (NewtonIteration)ss;

                            // Start with the initial guesses from the solution.
                            foreach (Arrow i in S.Guesses)
                            {
                                code.DeclInit(i.Left, i.Right);
                            }

                            // int it = iterations
                            LinqExpr it = code.ReDeclInit <int>("it", Iterations);
                            // do { ... --it } while(it > 0)
                            code.DoWhile((Break) =>
                            {
                                // Solve the un-solved system.
                                Solve(code, JxF, S.Equations, S.UnknownDeltas);

                                // Compile the pre-solved solutions.
                                if (S.KnownDeltas != null)
                                {
                                    foreach (Arrow i in S.KnownDeltas)
                                    {
                                        code.DeclInit(i.Left, i.Right);
                                    }
                                }

                                // bool done = true
                                LinqExpr done = code.ReDeclInit("done", true);
                                foreach (Expression i in S.Unknowns)
                                {
                                    LinqExpr v  = code[i];
                                    LinqExpr dv = code[NewtonIteration.Delta(i)];

                                    // done &= (|dv| < |v|*epsilon)
                                    code.Add(LinqExpr.AndAssign(done, LinqExpr.LessThan(LinqExpr.Multiply(Abs(dv), LinqExpr.Constant(1e4)), LinqExpr.Add(Abs(v), LinqExpr.Constant(1e-6)))));
                                    // v += dv
                                    code.Add(LinqExpr.AddAssign(v, dv));
                                }
                                // if (done) break
                                code.Add(LinqExpr.IfThen(done, Break));

                                // --it;
                                code.Add(LinqExpr.PreDecrementAssign(it));
                            }, LinqExpr.GreaterThan(it, Zero));

                            //// bool failed = false
                            //LinqExpr failed = Decl(code, code, "failed", LinqExpr.Constant(false));
                            //for (int i = 0; i < eqs.Length; ++i)
                            //    // failed |= |JxFi| > epsilon
                            //    code.Add(LinqExpr.OrAssign(failed, LinqExpr.GreaterThan(Abs(eqs[i].ToExpression().Compile(map)), LinqExpr.Constant(1e-3))));

                            //code.Add(LinqExpr.IfThen(failed, ThrowSimulationDiverged(n)));
                        }
                    }

                    // Update the previous timestep variables.
                    foreach (SolutionSet S in Solution.Solutions)
                    {
                        foreach (Expression i in S.Unknowns.Where(i => globals.Keys.Contains(i.Evaluate(t_t0))))
                        {
                            code.Add(LinqExpr.Assign(code[i.Evaluate(t_t0)], code[i]));
                        }
                    }

                    // Vo += i
                    foreach (Expression i in Output.Distinct())
                    {
                        LinqExpr Voi = LinqExpr.Constant(0.0);
                        try
                        {
                            Voi = code.Compile(i);
                        }
                        catch (Exception Ex)
                        {
                            Log.WriteLine(MessageType.Warning, Ex.Message);
                        }
                        code.Add(LinqExpr.AddAssign(Vo[i], Voi));
                    }

                    // Vi_t0 = Vi
                    foreach (Expression i in Input.Distinct())
                    {
                        code.Add(LinqExpr.Assign(code[i.Evaluate(t_t0)], code[i]));
                    }

                    // --ov;
                    code.Add(LinqExpr.PreDecrementAssign(ov));
                }, LinqExpr.GreaterThan(ov, Zero));

                // Output[i][n] = Vo / Oversample
                foreach (KeyValuePair <Expression, LinqExpr> i in outputs)
                {
                    code.Add(LinqExpr.Assign(LinqExpr.ArrayAccess(i.Value, n), LinqExpr.Multiply(Vo[i.Key], LinqExpr.Constant(1.0 / (double)Oversample))));
                }

                // Every 256 samples, check for divergence.
                if (Vo.Any())
                {
                    code.Add(LinqExpr.IfThen(LinqExpr.Equal(LinqExpr.And(n, LinqExpr.Constant(0xFF)), Zero),
                                             LinqExpr.Block(Vo.Select(i => LinqExpr.IfThenElse(IsNotReal(i.Value),
                                                                                               ThrowSimulationDiverged(n),
                                                                                               LinqExpr.Assign(i.Value, RoundDenormToZero(i.Value)))))));
                }
            });

            // Copy the global state variables back to the globals.
            foreach (KeyValuePair <Expression, GlobalExpr <double> > i in globals)
            {
                code.Add(LinqExpr.Assign(i.Value, code[i.Key]));
            }

            LinqExprs.LambdaExpression lambda = code.Build();
            Delegate ret = lambda.Compile();

            return(ret);
        }
Пример #27
0
        // Generate code to perform row reduction.
        private static void RowReduce(CodeGen code, LinqExpr Ab, int M, int N)
        {
            // For each variable in the system...
            for (int j = 0; j + 1 < N; ++j)
            {
                LinqExpr _j  = LinqExpr.Constant(j);
                LinqExpr Abj = code.ReDeclInit <double[]>("Abj", LinqExpr.ArrayAccess(Ab, _j));
                // int pi = j
                LinqExpr pi = code.ReDeclInit <int>("pi", _j);
                // double max = |Ab[j][j]|
                LinqExpr max = code.ReDeclInit <double>("max", Abs(LinqExpr.ArrayAccess(Abj, _j)));

                // Find a pivot row for this variable.
                //code.For(j + 1, M, _i =>
                //{
                for (int i = j + 1; i < M; ++i)
                {
                    LinqExpr _i = LinqExpr.Constant(i);

                    // if(|Ab[i][j]| > max) { pi = i, max = |Ab[i][j]| }
                    LinqExpr maxj = code.ReDeclInit <double>("maxj", Abs(LinqExpr.ArrayAccess(LinqExpr.ArrayAccess(Ab, _i), _j)));
                    code.Add(LinqExpr.IfThen(
                                 LinqExpr.GreaterThan(maxj, max),
                                 LinqExpr.Block(
                                     LinqExpr.Assign(pi, _i),
                                     LinqExpr.Assign(max, maxj))));
                }

                // (Maybe) swap the pivot row with the current row.
                LinqExpr Abpi = code.ReDecl <double[]>("Abpi");
                code.Add(LinqExpr.IfThen(
                             LinqExpr.NotEqual(_j, pi), LinqExpr.Block(
                                 new[] { LinqExpr.Assign(Abpi, LinqExpr.ArrayAccess(Ab, pi)) }.Concat(
                                     Enumerable.Range(j, N + 1 - j).Select(x => Swap(
                                                                               LinqExpr.ArrayAccess(Abj, LinqExpr.Constant(x)),
                                                                               LinqExpr.ArrayAccess(Abpi, LinqExpr.Constant(x)),
                                                                               code.ReDecl <double>("swap")))))));

                //// It's hard to believe this swap isn't faster than the above...
                //code.Add(LinqExpr.IfThen(LinqExpr.NotEqual(_j, pi), LinqExpr.Block(
                //    Swap(LinqExpr.ArrayAccess(Ab, _j), LinqExpr.ArrayAccess(Ab, pi), Redeclare<double[]>(code, "temp")),
                //    LinqExpr.Assign(Abj, LinqExpr.ArrayAccess(Ab, _j)))));

                // Eliminate the rows after the pivot.
                LinqExpr p = code.ReDeclInit <double>("p", LinqExpr.ArrayAccess(Abj, _j));
                //code.For(j + 1, M, _i =>
                //{
                for (int i = j + 1; i < M; ++i)
                {
                    LinqExpr _i  = LinqExpr.Constant(i);
                    LinqExpr Abi = code.ReDeclInit <double[]>("Abi", LinqExpr.ArrayAccess(Ab, _i));

                    // s = Ab[i][j] / p
                    LinqExpr s = code.ReDeclInit <double>("scale", LinqExpr.Divide(LinqExpr.ArrayAccess(Abi, _j), p));
                    // Ab[i] -= Ab[j] * s
                    for (int ji = j + 1; ji < N + 1; ++ji)
                    {
                        code.Add(LinqExpr.SubtractAssign(
                                     LinqExpr.ArrayAccess(Abi, LinqExpr.Constant(ji)),
                                     LinqExpr.Multiply(LinqExpr.ArrayAccess(Abj, LinqExpr.Constant(ji)), s)));
                    }
                }
            }
        }
Пример #28
0
        /// <summary>
        /// Creates verifier which checks object public properties.
        /// </summary>
        /// <typeparam name="TVerifier">Verifier type.</typeparam>
        /// <param name="propertyFilter">Object public properties filter; can be null to generate code for all the properties.</param>
        /// <param name="checkExprFunc">Function which generates property value check expression (if returns true then check is failed).
        /// Function obtains variable which holds property value and additional arguments.</param>
        /// <param name="createExceptionExpr">New exception creation expression used when check is failed.</param>
        /// <returns>Verifier instance.</returns>
        public static TVerifier Create <TVerifier>(
            Func <Type, bool> propertyFilter,
            Func <Expression, IList <ParameterExpression>, Expr> checkExprFunc,
            LambdaExpression createExceptionExpr) where TVerifier : class
        {
            VerifyUtil.NotNull(checkExprFunc, "checkExprFunc");
            VerifyUtil.NotNull(createExceptionExpr, "createExceptionExpr");

            Type argumentsType = typeof(TVerifier).GetGenericArguments()[0];
            Type type          = argumentsType.GetGenericArguments()[0];

            // Prepare lambda action parameters - first is Argument<T>, others are additionalParamTypes
            var objectParam      = Expr.Parameter(argumentsType);
            var objectVar        = Expr.Parameter(type);
            var genericArguments = typeof(TVerifier).GetGenericArguments();
            var additionalParams = genericArguments.Skip(1).Take(genericArguments.Length - 2).Select(Expr.Parameter).ToList();

            Expr lambdaBody;

            if (type.IsAnonymous())
            {
                // Take createExceptionExpr lambda, extract first parameter from it and replace additional parameters in body
                var exceptionNameParam   = createExceptionExpr.Parameters[0];
                var exceptionObjectParam = createExceptionExpr.Parameters[1];
                var exceptionBody        = createExceptionExpr.ReplaceParams(additionalParams);

                // Obtain type public properties to check
                propertyFilter = propertyFilter ?? (_ => true);
                var properties = type.GetProperties().OrderBy(pi => pi.Name).Where(pi => propertyFilter(pi.PropertyType));

                // Obtain argument values from fields since it's faster
                var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).OrderBy(fi => fi.Name).Where(fi => propertyFilter(fi.FieldType)).ToList();

                // Generate "if (checkExpr) then throw createExceptionExpr;" for each of the properties
                var propertyChecks = properties
                                     .Select(
                    (pi, i) =>
                {
                    var valueVar = Expr.Field(objectVar, fields[i]);
                    return((Expr)Expr.IfThen(
                               checkExprFunc(valueVar, additionalParams),
                               Expr.Throw(
                                   exceptionBody
                                   .Replace(exceptionNameParam, Expr.Constant(pi.Name))
                                   .Replace(exceptionObjectParam, valueVar.ConvertIfNeeded(exceptionObjectParam.Type)))));
                })
                                     .ToList();

                // Prepare null check - if null is supplied then all checks are passed since nothing to check
                var checksBody = propertyChecks.Any()
                                        ? Expr.IfThen(
                    Expr.ReferenceNotEqual(objectVar, Expr.Constant(null, type)),
                    propertyChecks.Count > 1 ? Expr.Block(propertyChecks) : propertyChecks[0])
                                        : null;

                lambdaBody = propertyChecks.Any()
                                        ? (Expr)Expr.Block(
                    new[] { objectVar },
                    new Expr[] { Expr.Assign(objectVar, Expr.Field(objectParam, "Holder")) }
                    .Concat(new[] { checksBody })
                    .Concat(new[] { objectParam }))
                                        : objectParam;
            }
            else
            {
                // Not anonymous type - throw an exception
                lambdaBody = Expr.Block(
                    Expr.Throw(
                        Expr.New(
                            VerifyArgsExceptionCtor,
                            Expr.Constant(string.Format(ErrorMessages.NotAnonymousType, type)))),
                    objectParam);
            }

            // Pull it all together, execute checks only if supplied object is not null
            var lambda = Expr.Lambda <TVerifier>(
                lambdaBody,
                new[] { objectParam }.Concat(additionalParams));

            return(lambda.Compile());
        }
Пример #29
0
        private static LinqExpression[] CompileGroup(ExpressionGroup group, CompilationData data)
        {
            switch (group)
            {
            case Persist p:
                foreach (var s in p.State)
                {
                    if (!(s.InitialValue is Constant))
                    {
                        throw new Exception("Persist initial value is not constant");
                    }

                    data.StateValues.Add(((Constant)s.InitialValue).Value);
                }

                var assigns = new List <LinqExpression>();
                // TODO: Resolve parent scopes
                data.ResolveState = s => LinqExpression.ArrayAccess(data.StateArray, LinqExpression.Constant(s.Id));
                for (var i = 0; i < p.NewValue.Count; i++)
                {
                    var newValueExpr = Compile(p.NewValue[i], data);
                    assigns.Add(LinqExpression.Assign(
                                    LinqExpression.ArrayAccess(data.StateArray, LinqExpression.Constant(i)), newValueExpr));
                }

                data.Statements.AddRange(assigns);
                return(Enumerable.Range(0, p.Size)
                       .Select(i => LinqExpression.ArrayAccess(data.StateArray, LinqExpression.Constant(i)))
                       .ToArray());

            case Loop l:
                var stateList = new List <ParameterExpression>();
                foreach (var s in l.State)
                {
                    var initial  = Compile(s.InitialValue, data);
                    var variable = LinqExpression.Variable(initial.Type);
                    data.Statements.Add(LinqExpression.Assign(variable, initial));
                    data.Variables.Add(variable);
                    stateList.Add(variable);
                }

                // TODO: Resolve parent scopes
                data.ResolveState = s => stateList[s.Id];
                var parentStatements = data.Statements;
                // Make a new cache that copies in the old one, but won't leak State expressions
                data.Cache = new Dictionary <CachedExpression, ParameterExpression>(data.Cache);

                // Create a new statements list to put in the loop body
                var s1        = data.Statements = new List <LinqExpression>();
                var condition = Compile(l.Condition, data);
                var s2        = data.Statements = new List <LinqExpression>();
                var newState  = l.Body.Select(e => Compile(e, data)).ToArray();

                // Ensure that the entire state is only set at the end of the loop
                for (var i = 0; i < newState.Length; i++)
                {
                    var s = newState[i];
                    if (!(s is ParameterExpression))
                    {
                        var tmpVar = LinqExpression.Variable(s.Type);
                        data.Variables.Add(tmpVar);
                        s2.Add(LinqExpression.Assign(tmpVar, s));
                        newState[i] = tmpVar;
                    }
                }

                var breakLabel = LinqExpression.Label();
                var body       = LinqExpression.Block(s1
                                                      .Concat(new[]
                {
                    LinqExpression.IfThen(
                        LinqExpression.LessThan(condition, LinqExpression.Constant(1f)),
                        LinqExpression.Break(breakLabel))
                })
                                                      .Concat(s2)
                                                      .Concat(newState.Select((e, i) =>
                                                                              LinqExpression.Assign(stateList[i], e))));
                parentStatements.Add(LinqExpression.Loop(body, breakLabel));
                return(stateList.ToArray());

            default:
                throw new NotSupportedException();
            }
        }