Example #1
0
        private static Instruction /*?*/ PurgeNary <Instruction>(Instruction instruction, INamedEntity variable, ExpressionCanonicalizer <Instruction> canonicalizer)
            where Instruction : Microsoft.Cci.Analysis.Instruction, new()
        {
            Contract.Requires(instruction != null);
            Contract.Requires(variable != null);
            Contract.Requires(canonicalizer != null);

            Contract.Assume(instruction.Operand1 is Instruction);
            var operand1 = Purge((Instruction)instruction.Operand1, variable, canonicalizer);

            if (operand1 == null)
            {
                return(null);
            }
            var operand2andBeyond = instruction.Operand2 as Instruction[];

            Contract.Assume(operand2andBeyond != null);
            var n = operand2andBeyond.Length;

            Instruction[] copy = null;
            for (int i = 0; i < n; i++)
            {
                Contract.Assume(operand2andBeyond[i] != null);
                var opi = Purge(operand2andBeyond[i], variable, canonicalizer);
                if (opi == null)
                {
                    return(null);
                }
                if (opi != operand2andBeyond[i])
                {
                    if (copy == null)
                    {
                        copy = new Instruction[n];
                        for (int j = 0; j < i; j++)
                        {
                            copy[j] = operand2andBeyond[j];
                        }
                    }
                    Contract.Assume(copy.Length == n);
                    copy[i] = opi;
                }
            }
            if (operand1 != instruction.Operand1 || copy != null)
            {
                return(canonicalizer.GetCanonicalExpression(instruction, operand1, null, copy));
            }
            return(instruction);
        }
Example #2
0
        /// <summary>
        /// Uses Arithmetic and Boolean laws to simplify expressions.
        /// </summary>
        /// <typeparam name="Instruction"></typeparam>
        /// <param name="instruction"></param>
        /// <param name="variable"></param>
        /// <param name="canonicalizer"></param>
        /// <returns></returns>
        internal static Instruction /*?*/ PurgeBinary <Instruction>(Instruction instruction, INamedEntity variable, ExpressionCanonicalizer <Instruction> canonicalizer)
            where Instruction : Microsoft.Cci.Analysis.Instruction, new()
        {
            Contract.Requires(instruction != null);
            Contract.Requires(variable != null);
            Contract.Requires(canonicalizer != null);

            var operation = instruction.Operation;

            Contract.Assume(instruction.Operand1 is Instruction);
            var operand1 = (Instruction)instruction.Operand1;

            Contract.Assume(instruction.Operand2 is Instruction);
            var operand2 = (Instruction)instruction.Operand2;

            operand1 = Purge(operand1, variable, canonicalizer);
            operand2 = Purge(operand2, variable, canonicalizer);

            switch (operation.OperationCode)
            {
            case OperationCode.And:
            case OperationCode.Or:
                if (operand1 == null)
                {
                    return(operand2);
                }
                if (operand2 == null)
                {
                    return(operand1);
                }
                if (operand1 != instruction.Operand1 || operand2 != instruction.Operand2)
                {
                    return(canonicalizer.GetCanonicalExpression(instruction, operand1, operand2));
                }
                break;
            }
            if (operand1 != instruction.Operand1 || operand2 != instruction.Operand2)
            {
                return(null);
            }
            return(instruction);
        }