Permalink
Browse files

Make the pre-increment operators work on static fields.

  • Loading branch information...
1 parent 1df82cc commit 52b178df907b0aa32ffe5bbb32ccd9dd425fd53b @dgrunwald dgrunwald committed Mar 21, 2011
@@ -235,9 +235,7 @@ public static AssignmentOperatorType GetAssignmentOperatorForBinaryOperator(Bina
static bool IsWithoutSideEffects(Expression left)
{
- if (left is ThisReferenceExpression)
- return true;
- if (left is IdentifierExpression)
+ if (left is ThisReferenceExpression || left is IdentifierExpression || left is TypeReferenceExpression)
return true;
MemberReferenceExpression mre = left as MemberReferenceExpression;
if (mre != null)
@@ -28,16 +28,6 @@ public static bool Match<T>(this ILNode node, ILCode code, out T operand)
return false;
}
- public static bool Match<T>(this ILNode node, ILCode code, T operand)
- {
- ILExpression expr = node as ILExpression;
- if (expr != null && expr.Prefixes == null && expr.Code == code) {
- Debug.Assert(expr.Arguments.Count == 0);
- return operand.Equals(expr.Operand);
- }
- return false;
- }
-
public static bool Match(this ILNode node, ILCode code, out List<ILExpression> args)
{
ILExpression expr = node as ILExpression;
@@ -190,16 +190,14 @@ bool MakeAssignmentExpression(List<ILNode> body, ILExpression expr, int pos)
// stloc(v, exprVar)
// ->
// exprVar = stloc(v, ...))
- ILExpression nextExpr = body.ElementAtOrDefault(pos + 1) as ILExpression;
ILVariable exprVar;
ILExpression initializer;
+ if (!(expr.Match(ILCode.Stloc, out exprVar, out initializer) && exprVar.IsGenerated))
+ return false;
+ ILExpression nextExpr = body.ElementAtOrDefault(pos + 1) as ILExpression;
ILVariable v;
ILExpression stLocArg;
- if (expr.Match(ILCode.Stloc, out exprVar, out initializer) &&
- exprVar.IsGenerated &&
- nextExpr.Match(ILCode.Stloc, out v, out stLocArg) &&
- stLocArg.Match(ILCode.Ldloc, exprVar))
- {
+ if (nextExpr.Match(ILCode.Stloc, out v, out stLocArg) && stLocArg.MatchLdloc(exprVar)) {
ILExpression store2 = body.ElementAtOrDefault(pos + 2) as ILExpression;
if (StoreCanBeConvertedToAssignment(store2, exprVar)) {
// expr_44 = ...
@@ -224,6 +222,18 @@ bool MakeAssignmentExpression(List<ILNode> body, ILExpression expr, int pos)
nextExpr.Arguments[0] = initializer;
((ILExpression)body[pos]).Arguments[0] = nextExpr;
return true;
+ } else {
+ // exprVar = ...
+ // stsfld(fld, exprVar)
+ // ->
+ // exprVar = stsfld(fld, ...))
+ FieldReference field;
+ if (nextExpr.Match(ILCode.Stsfld, out field, out stLocArg) && stLocArg.MatchLdloc(exprVar)) {
+ body.RemoveAt(pos + 1); // remove stfld
+ nextExpr.Arguments[0] = initializer;
+ ((ILExpression)body[pos]).Arguments[0] = nextExpr;
+ return true;
+ }
}
return false;
}
@@ -152,7 +152,7 @@ public bool SimplifyNullCoalescing(List<ILNode> body, ILBasicBlock head, int pos
head.Body[head.Body.Count - 3].Match(ILCode.Stloc, out v, out leftExpr) &&
leftExpr.Match(ILCode.Ldloc, out leftVar) &&
head.MatchLastAndBr(ILCode.Brtrue, out endBBLabel, out leftExpr2, out rightBBLabel) &&
- leftExpr2.Match(ILCode.Ldloc, leftVar) &&
+ leftExpr2.MatchLdloc(leftVar) &&
labelToBasicBlock.TryGetValue(rightBBLabel, out rightBB) &&
rightBB.MatchSingleAndBr(ILCode.Stloc, out v2, out rightExpr, out endBBLabel2) &&
v == v2 &&
@@ -10,6 +10,8 @@ public class MutableClass
public int Field;
}
+ public static int StaticField;
+
private IncrementDecrement.MutableClass M()
{
return new IncrementDecrement.MutableClass();
@@ -35,11 +37,21 @@ public int PreIncrementInstanceField2(IncrementDecrement.MutableClass m)
return ++m.Field;
}
+ public int PreIncrementStaticField()
+ {
+ return ++IncrementDecrement.StaticField;
+ }
+
public int CompoundMultiplyInstanceField()
{
return this.M().Field *= 10;
}
+ public int CompoundXorStaticField()
+ {
+ return IncrementDecrement.StaticField ^= 100;
+ }
+
public int CompoundMultiplyArrayElement1(int[] array, int pos)
{
return array[pos] *= 10;

0 comments on commit 52b178d

Please sign in to comment.