コード例 #1
0
        static void StackSpilling2()
        {
            var t = CSharpExpression.Await(CSharpExpression.Await(Expression.Constant(Task.FromResult(Task.FromResult(42)))));
            var r = Expression.Add(t, t);

            _ = Spiller.Spill(r);
        }
コード例 #2
0
        public void Spiller_Basics()
        {
            // NB: This really tests the LINQ stack spiller but with our ceremony around it.

            var a = Expression.Constant(1);
            var b = Expression.Constant(2);

            var x = Expression.TryFinally(a, Expression.Empty());
            var y = Expression.TryFinally(b, Expression.Empty());

            var e = Expression.Add(x, y);

            var r = Spiller.Spill(e);

            Assert.AreEqual(ExpressionType.Block, r.NodeType);
            var si = (BlockExpression)r;

            Assert.AreEqual(2, si.Variables.Count);
            var sv1 = si.Variables[0];
            var sv2 = si.Variables[1];

            Assert.AreEqual(1, si.Expressions.Count); // NB: LINQ stack spiller has a spurious block
            var si1 = si.Expressions[0];

            Assert.AreEqual(ExpressionType.Block, si1.NodeType);
            var sb = (BlockExpression)si1;

            Assert.AreEqual(3, sb.Expressions.Count);
            var se1 = sb.Expressions[0];
            var se2 = sb.Expressions[1];
            var se3 = sb.Expressions[2];

            Assert.AreEqual(ExpressionType.Assign, se1.NodeType);
            var sa1 = (BinaryExpression)se1;

            Assert.AreSame(sv1, sa1.Left);
            //Assert.AreSame(x, sa1.Right); // NB: LINQ stack spiller could clone a child tree; should use an equality comparer here

            Assert.AreEqual(ExpressionType.Assign, se2.NodeType);
            var sa2 = (BinaryExpression)se2;

            Assert.AreSame(sv2, sa2.Left);
            //Assert.AreSame(y, sa2.Right); // NB: LINQ stack spiller could clone a child tree; should use an equality comparer here

            Assert.AreEqual(ExpressionType.Add, se3.NodeType);
            var sa3 = (BinaryExpression)se3;

            Assert.AreSame(sv1, sa3.Left);
            Assert.AreSame(sv2, sa3.Right);
        }
コード例 #3
0
        public void Spiller_Await()
        {
            var a = Expression.Constant(Task.FromResult(1));
            var b = Expression.Constant(Task.FromResult(2));

            var x = CSharpExpression.Await(a);
            var y = CSharpExpression.Await(b);

            var e = Expression.Add(x, y);

            var r = Spiller.Spill(e);

            Assert.AreEqual(ExpressionType.Block, r.NodeType);
            var si = (BlockExpression)r;

            Assert.AreEqual(2, si.Variables.Count);
            var sv1 = si.Variables[0];
            var sv2 = si.Variables[1];

            Assert.AreEqual(1, si.Expressions.Count); // NB: LINQ stack spiller has a spurious block
            var si1 = si.Expressions[0];

            Assert.AreEqual(ExpressionType.Block, si1.NodeType);
            var sb = (BlockExpression)si1;

            Assert.AreEqual(3, sb.Expressions.Count);
            var se1 = sb.Expressions[0];
            var se2 = sb.Expressions[1];
            var se3 = sb.Expressions[2];

            Assert.AreEqual(ExpressionType.Assign, se1.NodeType);
            var sa1 = (BinaryExpression)se1;

            Assert.AreSame(sv1, sa1.Left);
            //Assert.AreSame(x, sa1.Right); // NB: LINQ stack spiller could clone a child tree; should use an equality comparer here

            Assert.AreEqual(ExpressionType.Assign, se2.NodeType);
            var sa2 = (BinaryExpression)se2;

            Assert.AreSame(sv2, sa2.Left);
            //Assert.AreSame(y, sa2.Right); // NB: LINQ stack spiller could clone a child tree; should use an equality comparer here

            Assert.AreEqual(ExpressionType.Add, se3.NodeType);
            var sa3 = (BinaryExpression)se3;

            Assert.AreSame(sv1, sa3.Left);
            Assert.AreSame(sv2, sa3.Right);
        }
コード例 #4
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);
        }
コード例 #5
0
 static void StackSpilling4()
 {
     var e = (Expression <Func <int> >)(() => F(Math.Abs(-1), Task.FromResult(2).Result, Math.Abs(-3)));
     var r = new TaskRewriter().Visit(e.Body);
     var x = Spiller.Spill(r);
 }
コード例 #6
0
 static void StackSpilling3()
 {
     var e = (Expression <Func <int> >)(() => Task.FromResult(1).Result + Task.FromResult(2 + Task.FromResult(3).Result).Result);
     var r = new TaskRewriter().Visit(e.Body);
     var x = Spiller.Spill(r);
 }