Exemple #1
0
        private bool TryOptimiseToXor(MatchResult matchResult, ILInstructionExpression expression)
        {
            var lefts  = matchResult.Captures["left"];
            var rights = matchResult.Captures["right"];

            if (((ILVariableExpression)lefts[0]).Variable == ((ILVariableExpression)lefts[1]).Variable &&
                ((ILVariableExpression)rights[0]).Variable == ((ILVariableExpression)rights[1]).Variable)
            {
                // Unregister remaining variable references.
                ((ILVariableExpression)lefts[1]).Variable  = null;
                ((ILVariableExpression)rights[1]).Variable = null;

                // Replace with XOR pseudo opcode.
                var newExpression = new ILInstructionExpression(
                    expression.OriginalOffset,
                    ILOpCodes.__XOR_DWORD,
                    null,
                    VMType.Dword);
                newExpression.Arguments.Add((ILExpression)lefts[0].Parent.Remove());
                newExpression.Arguments.Add((ILExpression)rights[0].Parent.Remove());
                newExpression.FlagsVariable = expression.FlagsVariable;
                expression.FlagsVariable    = null;
                expression.ReplaceWith(newExpression);

                return(true);
            }

            return(false);
        }
Exemple #2
0
        private bool TryOptimiseToSub(MatchResult matchResult, ILInstructionExpression expression)
        {
            var(left, right) = GetOperands(matchResult);

            // Replace with SUB pseudo opcode.
            var newExpression = new ILInstructionExpression(
                expression.OriginalOffset,
                ILOpCodes.__SUB_DWORD,
                null,
                VMType.Dword);

            newExpression.Arguments.Add((ILExpression)left.Parent.Remove());
            newExpression.Arguments.Add((ILExpression)right.Parent.Remove());
            newExpression.FlagsVariable = expression.FlagsVariable;
            expression.FlagsVariable    = null;
            expression.ReplaceWith(newExpression);

            return(true);
        }
Exemple #3
0
        public override bool VisitInstructionExpression(ILInstructionExpression expression)
        {
            // Any push expression that pushes the same type as its argument is superfluous.

            bool changed = base.VisitInstructionExpression(expression);

            var match = PushPattern.Match(expression);

            if (match.Success)
            {
                var expr = (ILExpression)match.Captures["expr"][0];
                if (expression.ExpressionType == expr.ExpressionType)
                {
                    expression.ReplaceWith(expr.Remove());
                    changed = true;
                }
            }

            return(changed);
        }
Exemple #4
0
        private static bool TryOptimiseToNot(ILInstructionExpression expression, MatchResult matchResult)
        {
            var(left, right) = GetOperands(matchResult);
            if (left.Variable == right.Variable)
            {
                // Unregister one of the variable uses.
                right.Variable = null;

                // Replace with NOT pseudo opcode.
                var newExpression = new ILInstructionExpression(
                    expression.OriginalOffset,
                    ILOpCodes.__NOT_DWORD,
                    null,
                    VMType.Dword);
                newExpression.Arguments.Add((ILExpression)left.Parent.Remove());
                newExpression.FlagsVariable = expression.FlagsVariable;
                expression.FlagsVariable    = null;
                expression.ReplaceWith(newExpression);

                return(true);
            }

            return(false);
        }