Skip to content

Commit

Permalink
Emit better code for binary await expression with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-safar committed Mar 16, 2012
1 parent bc865e6 commit 5d624eb
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 358 deletions.
8 changes: 7 additions & 1 deletion mcs/mcs/async.cs
Expand Up @@ -40,6 +40,12 @@ public Await (Expression expr, Location loc)
}
}

public AwaitStatement Statement {
get {
return stmt;
}
}

protected override void CloneTo (CloneContext clonectx, Expression target)
{
var t = (Await) target;
Expand Down Expand Up @@ -113,7 +119,7 @@ public override object Accept (StructuralVisitor visitor)
}
}

class AwaitStatement : YieldStatement<AsyncInitializer>
public class AwaitStatement : YieldStatement<AsyncInitializer>
{
sealed class AwaitableMemberAccess : MemberAccess
{
Expand Down
11 changes: 11 additions & 0 deletions mcs/mcs/ecore.cs
Expand Up @@ -1933,6 +1933,12 @@ private ReducedExpression (Expression expr, Expression orig_expr)

#region Properties

public override bool IsSideEffectFree {
get {
return expr.IsSideEffectFree;
}
}

public Expression OriginalExpression {
get {
return orig_expr;
Expand Down Expand Up @@ -2005,6 +2011,11 @@ public override void Emit (EmitContext ec)
expr.Emit (ec);
}

public override Expression EmitToField (EmitContext ec)
{
return expr.EmitToField(ec);
}

public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
{
expr.EmitBranchable (ec, target, on_true);
Expand Down
21 changes: 21 additions & 0 deletions mcs/mcs/expression.cs
Expand Up @@ -3929,6 +3929,27 @@ public override void EmitSideEffect (EmitContext ec)
}
}

public override Expression EmitToField (EmitContext ec)
{
if ((oper & Operator.LogicalMask) == 0) {
var await_expr = left as Await;
if (await_expr != null && right.IsSideEffectFree) {
await_expr.Statement.EmitPrologue (ec);
left = await_expr.Statement.GetResultExpression (ec);
return this;
}

await_expr = right as Await;
if (await_expr != null && left.IsSideEffectFree) {
await_expr.Statement.EmitPrologue (ec);
right = await_expr.Statement.GetResultExpression (ec);
return this;
}
}

return base.EmitToField (ec);
}

protected override void CloneTo (CloneContext clonectx, Expression t)
{
Binary target = (Binary) t;
Expand Down
13 changes: 13 additions & 0 deletions mcs/tests/test-async-13.cs
Expand Up @@ -340,6 +340,19 @@ async Task<int> BinaryTest_4 ()
return 0;
}

async Task<int> BinaryTest_5 ()
{
var r1 = await Task.FromResult (1) == 9;
if (r1)
return 1;

var r2 = 1 == await Task.FromResult (1);
if (!r2)
return 2;

return 0;
}

async Task<int> CallTest_1 ()
{
return Call (
Expand Down

0 comments on commit 5d624eb

Please sign in to comment.