private bool TryRewriteRefHolderTempCreation(BinaryExpression node, Dictionary <ParameterExpression, ReplacementInfo> nearestScope, out Expression result)
            {
                // NB: This detects the pattern introduced by CreateRefLocalAccess in Helpers.cs
                //
                //       temp = new RefHolder<T>(ref expr)
                //
                //     and builds a replacement for expr which may involve adding new temporaries.
                //
                //     The Replacement object keeps track of new temporaries and a replacement
                //     expression which is used when finding the call to temp.Invoke(...) that's
                //     used to access the ref local.

                if (node.Left is ParameterExpression p && nearestScope.TryGetValue(p, out var replacement))
                {
                    Debug.Assert(node.Right is NewExpression);
                    Debug.Assert(replacement.Replacement is null);

                    var right = (NewExpression)node.Right;

                    Debug.Assert(right.Arguments.Count == 1);

                    var temps = replacement.Temps;
                    var stmts = new List <Expression>();

                    bool shouldEvalExpressionForSideEffects = false;

                    replacement.Replacement = Rewrite(right.Arguments[0], isByRef: true, temps, stmts, ref shouldEvalExpressionForSideEffects);

                    if (shouldEvalExpressionForSideEffects)
                    {
                        stmts.Add(replacement.Replacement);
                    }

                    if (stmts.Count == 0)
                    {
                        result = Expression.Empty();
                        return(true);
                    }
                    else if (stmts.Count == 1)
                    {
                        // NB: Assignments of temporaries are interior expressions, so we don't need to worry about
                        //     affecting the Type of a BlockExpression.
                        result = stmts[0];
                        return(true);
                    }
                    else
                    {
                        result = Helpers.CreateVoid(stmts);
                        return(true);
                    }
                }

                result = null;
                return(false);
            }
Exemplo n.º 2
0
        /// <summary>
        /// Reduces the expression node to a simpler expression.
        /// </summary>
        /// <returns>The reduced expression.</returns>
        public sealed override Expression Reduce()
        {
            var res = ReduceCore();

            if (res.Type != typeof(void))
            {
                res = Helpers.CreateVoid(res);
            }

            return(res);
        }
Exemplo n.º 3
0
            public Expression Make(Expression switchValue, Expression defaultBody)
            {
                if (Cases.Count > 0)
                {
                    return(Expression.Switch(switchValue, defaultBody, null, Cases));
                }
                else
                {
                    var canDropSwitchValue = switchValue.IsPure(readOnly: true);
                    var hasDefaultBody     = defaultBody != null;

                    var exprs = default(Expression[]);

                    if (canDropSwitchValue)
                    {
                        if (hasDefaultBody)
                        {
                            exprs = new[] { defaultBody };
                        }
                        else
                        {
                            return(Expression.Empty());
                        }
                    }
                    else
                    {
                        if (hasDefaultBody)
                        {
                            exprs = new[] { switchValue, defaultBody };
                        }
                        else
                        {
                            exprs = new[] { switchValue };
                        }
                    }

                    return(Helpers.CreateVoid(exprs));
                }
            }
Exemplo n.º 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);
        }
Exemplo n.º 5
0
 private static Expression MakeBlock(IList <Expression> expressions)
 {
     return(Helpers.CreateVoid(expressions));
 }