Example #1
0
        public void AliasEliminator_NoAlias()
        {
            var v1 = Expression.Parameter(typeof(int));
            var v2 = Expression.Parameter(typeof(int));
            var e1 = Expression.Parameter(typeof(Exception));
            var e2 = Expression.Parameter(typeof(Exception));
            var d1 = Expression.Parameter(typeof(IDisposable));
            var d2 = Expression.Parameter(typeof(IDisposable));
            var r1 = Expression.Default(typeof(IDisposable));
            var c1 = Expression.Parameter(typeof(int[]));
            var c2 = Expression.Parameter(typeof(int[]));
            var i1 = Expression.Default(typeof(int));
            var b1 = Expression.Default(typeof(bool));

            var es = new Expression[]
            {
                Expression.Block(new[] { v1 }, Expression.Block(new[] { v2 }, Expression.Add(v1, v2))),
                Expression.Lambda(Expression.Lambda(Expression.Add(v1, v2), v2), v1),
                Expression.TryCatch(Expression.Empty(), Expression.Catch(e1, Expression.TryCatch(Expression.Empty(), Expression.Catch(e2, Expression.Empty())))),
                Expression.TryCatch(Expression.Empty(), Expression.Catch(typeof(Exception), Expression.TryCatch(Expression.Empty(), Expression.Catch(typeof(Exception), Expression.Empty())))),
                CSharpExpression.Using(d1, r1, CSharpExpression.Using(d2, r1, Expression.Empty())),
                CSharpExpression.Using(r1, CSharpExpression.Using(r1, Expression.Empty())),
                CSharpExpression.ForEach(v1, c1, CSharpExpression.ForEach(v2, c2, Expression.Empty())),
                CSharpExpression.For(new[] { Expression.Assign(v1, i1) }, b1, null, CSharpExpression.For(new[] { Expression.Assign(v2, i1) }, b1, null, Expression.Empty())),
            };

            foreach (var e in es)
            {
                Assert.AreSame(e, AliasEliminator.Eliminate(e));
            }
        }
Example #2
0
        public void AliasEliminator_Alias_Lambda()
        {
            var x = Expression.Parameter(typeof(int));

            var e = Expression.Lambda(Expression.Lambda(x, x), x);
            var r = (LambdaExpression)AliasEliminator.Eliminate(e);

            var v1 = r.Parameters[0];
            var e1 = (LambdaExpression)r.Body;
            var v2 = e1.Parameters[0];
            var e2 = e1.Body;

            Assert.AreSame(v2, e2);
            Assert.AreNotSame(v1, v2);
        }
Example #3
0
        public void AliasEliminator_Alias_Block()
        {
            var x = Expression.Parameter(typeof(int));

            var e = Expression.Block(new[] { x }, Expression.Block(new[] { x }, x));
            var r = (BlockExpression)AliasEliminator.Eliminate(e);

            var v1 = r.Variables[0];
            var e1 = (BlockExpression)r.Expressions[0];
            var v2 = e1.Variables[0];
            var e2 = e1.Expressions[0];

            Assert.AreSame(v2, e2);
            Assert.AreNotSame(v1, v2);
        }
Example #4
0
        public void AliasEliminator_Siblings()
        {
            var x = Expression.Parameter(typeof(int));

            var b1 = Expression.Block(new[] { x }, Expression.Empty());
            var b2 = Expression.Block(new[] { x }, Expression.Empty());

            var b = Expression.Block(b1, b2);

            var r = (BlockExpression)AliasEliminator.Eliminate(b);

            var r1 = (BlockExpression)r.Expressions[0];
            var r2 = (BlockExpression)r.Expressions[1];

            Assert.AreNotSame(r1.Variables[0], r2.Variables[0]);
        }
Example #5
0
        public void AliasEliminator_Alias_ForEach()
        {
            var x  = Expression.Parameter(typeof(int));
            var xs = Expression.Default(typeof(int[]));

            var e = CSharpExpression.ForEach(x, xs, CSharpExpression.ForEach(x, xs, x));
            var r = (ForEachCSharpStatement)AliasEliminator.Eliminate(e);

            var v1 = r.Variable;
            var e1 = (ForEachCSharpStatement)r.Body;
            var v2 = e1.Variable;
            var e2 = e1.Body;

            Assert.AreSame(v2, e2);
            Assert.AreNotSame(v1, v2);
        }
Example #6
0
        public void AliasEliminator_Alias_Using()
        {
            var x = Expression.Parameter(typeof(IDisposable));
            var y = Expression.Default(typeof(IDisposable));

            var e = CSharpExpression.Using(x, y, CSharpExpression.Using(x, y, x));
            var r = (UsingCSharpStatement)AliasEliminator.Eliminate(e);

            var v1 = r.Variable;
            var e1 = (UsingCSharpStatement)r.Body;
            var v2 = e1.Variable;
            var e2 = e1.Body;

            Assert.AreSame(v2, e2);
            Assert.AreNotSame(v1, v2);
        }
Example #7
0
        public void AliasEliminator_Alias_Catch()
        {
            var x = Expression.Parameter(typeof(Exception));

            var e = Expression.TryCatch(Expression.Empty(), Expression.Catch(x, Expression.TryCatch(Expression.Empty(), Expression.Catch(x, Expression.Block(typeof(void), x)))));
            var r = (TryExpression)AliasEliminator.Eliminate(e);

            var h1 = r.Handlers[0];
            var v1 = h1.Variable;
            var e1 = (TryExpression)h1.Body;
            var h2 = e1.Handlers[0];
            var v2 = h2.Variable;
            var e2 = ((BlockExpression)h2.Body).Expressions[0];

            Assert.AreSame(v2, e2);
            Assert.AreNotSame(v1, v2);
        }
Example #8
0
        public void AliasEliminator_Alias_For()
        {
            var x = Expression.Parameter(typeof(int));
            var i = Expression.Assign(x, Expression.Constant(0));
            var b = Expression.Default(typeof(bool));

            var e = CSharpExpression.For(new[] { i }, b, null, CSharpExpression.For(new[] { i }, b, null, x));
            var r = (ForCSharpStatement)AliasEliminator.Eliminate(e);

            var v1 = r.Variables[0];
            var e1 = (ForCSharpStatement)r.Body;
            var v2 = e1.Variables[0];
            var e2 = e1.Body;

            Assert.AreSame(v2, e2);
            Assert.AreNotSame(v1, v2);
        }
Example #9
0
        private Expression RewriteBody(ParameterExpression stateVar, ParameterExpression builderVar, ParameterExpression stateMachineVar, out IEnumerable <ParameterExpression> variables)
        {
            const int ExprCount = 1 /* local state var */ + 1 /* TryCatch */ + 2 /* state = -2; SetResult */ + 1 /* Label */;

            var locals = default(ParameterExpression[]);
            var exprs  = default(Expression[]);

            var result = default(ParameterExpression);
            var ex     = Expression.Parameter(typeof(Exception), "exception");

            var exit = Expression.Label("__exit");

            //
            // Keep a collection and a helper function to create variables that are hoisted to the heap
            // for use by await sites. Because only one await site can be active at a time, we can reuse
            // variables introduced for these, e.g. for awaiters of the same type.
            //
            // NB: We can replace the getVariable helper function with a local function in C# 7.0 if we
            //     get that feature.
            //
            var hoistedVars = new Dictionary <Type, ParameterExpression>();

            var getVariable = new Func <Type, string, ParameterExpression>((t, s) =>
            {
                if (!hoistedVars.TryGetValue(t, out ParameterExpression p))
                {
                    p = Expression.Parameter(t, s + hoistedVars.Count);
                    hoistedVars.Add(t, p);
                }

                return(p);
            });

            //
            // Some helpers to call AwaitOnCompleted on the async method builder for use by each await site in
            // the asynchronous code path, e.g.
            //
            //   if (!awaiter.IsCompleted)
            //   {
            //     __state = n;
            //     __builder.AwaitOnCompleted<AwaiterType, RuntimeAsyncStateMachine>(ref awaiter, ref __statemachine);
            //   }
            //
            // NB: We can replace the onCompletedFactory helper function with a local function in C# 7.0 if we
            //     get that feature.
            //
            // REVIEW: Do we have any option to call UnsafeAwaitOnCompleted at runtime, i.e. can we detect
            //         the cases where we can do this and can we do it wrt security restrictions on code
            //         that gets emitted dynamically?
            //
            var awaitOnCompletedMethod = builderVar.Type.GetMethod("AwaitOnCompleted", BindingFlags.Public | BindingFlags.Instance);
            var awaitOnCompletedArgs   = new Type[] { default(Type), typeof(RuntimeAsyncStateMachine) };

            var onCompletedFactory = new Func <Expression, Expression>(awaiter =>
            {
                awaitOnCompletedArgs[0]          = awaiter.Type;
                var awaitOnCompletedMethodClosed = awaitOnCompletedMethod.MakeGenericMethod(awaitOnCompletedArgs);
                return(Expression.Call(builderVar, awaitOnCompletedMethodClosed, awaiter, stateMachineVar));
            });

            //
            // First, reduce all nodes in the body except for await nodes. This makes subsequent rewrite
            // steps easier because we reduce to the known subset of LINQ nodes.
            //
            var reduced = Reducer.Reduce(Body);

            //
            // Next, rewrite exception handlers to synthetic equivalents where needed. This supports the
            // C# 6.0 features to await in catch and finally handlers (in addition to fault handlers in
            // order to support all LINQ nodes, which can be restricted if we want).
            //
            // This step also deals with pending branches out of exception handlers in order to properly
            // 'leave' protected regions and execute the branch after the exception handling construct.
            //
            var lowered = new CatchRewriter().Visit(reduced);

            lowered = new FinallyAndFaultRewriter().Visit(lowered);

            //
            // Next, eliminate any aliasing of variables that relies on the nesting of scoped nodes in
            // the LINQ APIs (e.g. nested blocks with reused ParmeterExpression nodes). We do this so we
            // don't have to worry about hoisting variables out of the async lambda body and causing the
            // meaning of the hoisted variable to change to another use of the same variable in a scoped
            // tree node higher up. This can happen during stack spilling, e.g.
            //
            //   {
            //     int x;                   // @0
            //     {
            //       int x;                 // @0 - same instance shadowing x in outer block
            //       F(x, await t);
            //     }
            //   }
            //
            // ==>
            //
            //   int x;                     // @0 hoisted to heap by stack spilling
            //   () =>
            //   {
            //     int x;                   // !!! the binding of x has now changed to the declaration
            //     __spill0 = x;            // !!! in the inner block
            //     __spill1 = await t;
            //     F(__spill0, __spill1);
            //   }
            //
            var aliasFree = AliasEliminator.Eliminate(lowered);

            //
            // Next, perform stack spilling in order to be able to pause the asynchronous method in the
            // middle of an expression without changing the left-to-right subexpression evaluation
            // semantics dictated by the C# language specification, e.g.
            //
            //   Console.ReadLine() + await Task.FromResult(Console.ReadLine)
            //
            // The first side-effect of reading from the console should happen before the second one
            // in the async operation.
            //
            var spilled = Spiller.Spill(aliasFree);

            //
            // Next, rewrite await expressions to the awaiter pattern with IsCompleted, OnCompleted,
            // and GetResult. This is where the heavy lifting (quite literally so) takes place and the
            // state machine is built. Other than rewriting await expressions, this step also takes care
            // of emitting the switch table for reentering the state machine, reentering nested try
            // blocks, and hoisting of locals. For more information, see AwaitRewriter.
            //
            // Note we need to introduce another local to keep the state of the async state machine in
            // order to deal with reentrancy of the async state machine via the OnCompleted call on an
            // awaiter while we're still exiting the state machine. This is a subtle race which we avoid
            // by making all decisions about jumps and state transitions based on a local copy of the
            // hoisted state variable used by the state machine:
            //
            //   int __localState = __state;
            //   switch (__localState)
            //   {
            //     ...
            //   }
            //
            // NB: Right now, locals used in await sites get hoisted to the heap eagerly rather than
            //     getting hoisted upon taking the asynchronous code path. This is an opportunity for
            //     future optimization, together with the use of a struct for the async state machine.
            //
            var localStateVar = Expression.Parameter(typeof(int), "__localState");
            var awaitRewriter = new AwaitRewriter(localStateVar, stateVar, getVariable, onCompletedFactory, exit);
            var rewrittenBody = awaitRewriter.Visit(spilled);

            //
            // Next, store the result of the rewritten body if the async method is non-void-returning.
            // Note this assignment will typically have a RHS which contains a non-void block expression
            // that originated from running the AwaitRewriter.
            //
            var newBody = rewrittenBody;

            if (Body.Type != typeof(void) && builderVar.Type.IsGenericType /* if not ATMB<T>, no result assignment needed */)
            {
                result  = Expression.Parameter(Body.Type, "__result");
                newBody = Expression.Assign(result, rewrittenBody);
                locals  = new[] { localStateVar, result };
            }
            else
            {
                locals = new[] { localStateVar };
            }

            exprs = new Expression[ExprCount];

            //
            // Next, we need to rewrite branching involving typed labels and percolate assignments in
            // order to avoid reduced await expressions causing branching into non-void expressions
            // which is not allowed in the lambda compiler. An example os this is shown in the comments
            // for AssignmentPercolator.
            //
            newBody = new TypedLabelRewriter().Visit(newBody);
            newBody = AssignmentPercolator.Percolate(newBody);

            var i = 0;

            //
            // Next, put the jump table to resume the async state machine on top of the rewritten body
            // returned from the AwaitRewriter. Note that the AwaitRewriter takes care of emitting the
            // nested resume jump tables for try statements, so we just have to stick the top-level
            // table around the body here. We don't do this in AwaitRewriter just to reduce the amount
            // of expression tree cloning incurred by TypedLabelRewriter and AssignmentPercolator given
            // that we know the switch tables don't contain any expressions that need such rewriting.
            //
            var resumeList = awaitRewriter.ResumeList;

            if (resumeList.Count > 0)
            {
                newBody =
                    Expression.Block(
                        typeof(void),
                        Expression.Switch(stateVar, resumeList.ToArray()),
                        newBody
                        );
            }
            else
            {
                newBody = Helpers.CreateVoid(newBody);
            }

            //
            // int __localState = __state;
            //
            exprs[i++] =
                Expression.Assign(localStateVar, stateVar);

            //
            // try
            // {
            //    // body
            // }
            // catch (Exception ex)
            // {
            //    __state = -2;
            //    __builder.SetException(ex);
            //    goto __exit;
            // }
            //
            exprs[i++] =
                Expression.TryCatch(
                    newBody,
                    Expression.Catch(ex,
                                     Expression.Block(
                                         Expression.Assign(stateVar, Helpers.CreateConstantInt32(-2)),
                                         Expression.Call(builderVar, builderVar.Type.GetMethod("SetException"), ex),
                                         Expression.Return(exit)
                                         )
                                     )
                    );

            //
            // __state = -2;
            //
            exprs[i++] = Expression.Assign(stateVar, Helpers.CreateConstantInt32(-2));

            //
            // __builder.SetResult(__result);
            //
            if (result != null)
            {
                exprs[i++] = Expression.Call(builderVar, builderVar.Type.GetMethod("SetResult"), result);
            }
            else
            {
                exprs[i++] = Expression.Call(builderVar, builderVar.Type.GetMethod("SetResult"));
            }

            //
            // __exit:
            //   return;
            //
            exprs[i++] = Expression.Label(exit);

            //
            // Finally, create the Action with the rewritten async lambda body that gets passed to the
            // runtime async state machine and hoist any newly introduced variables for awaiters and
            // such to the outer scope in order to get them stored on the heap rather than the stack.
            //
            var body = Expression.Block(locals, exprs);
            var res  = Expression.Lambda <Action>(body);

            variables = hoistedVars.Values.Concat(awaitRewriter.HoistedVariables);
            return(res);
        }