Esempio n. 1
0
        static InstructionPushPopInfo?GetInstructionPops(InstructionVM instr)
        {
            var code = instr.Code;

            if (code == Code.Pop || code == Code.Nop)
            {
                return(null);
            }
            int pushes, pops;

            instr.CalculateStackUsage(out pushes, out pops);
            if (pops < 0)
            {
                return(null);
            }
            if (pushes == 1 && (code == Code.Call || code == Code.Callvirt || code == Code.Calli))
            {
                return(new InstructionPushPopInfo(pops, GetMethodSig(instr.InstructionOperandVM.Other).GetRetType()));
            }
            if (pushes == 1 && code == Code.Newobj)
            {
                var ctor = instr.InstructionOperandVM.Other as IMethod;
                return(new InstructionPushPopInfo(pops, ctor == null ? null : ctor.DeclaringType.ToTypeSig()));
            }
            if (pushes != 0)
            {
                return(null);
            }
            return(new InstructionPushPopInfo(pops));
        }
Esempio n. 2
0
 static void WriteLong(this ITextOutput output, InstructionVM instr)
 {
     output.WriteShort(instr);
     output.WriteSpace();
     output.Write(instr.Code.ToOpCode().Name, TextTokenType.OpCode);
     output.WriteSpace();
     Write(output, instr.InstructionOperandVM);
 }
Esempio n. 3
0
 static void WriteShort(this ITextOutput output, InstructionVM instr)
 {
     output.Write(instr.Index.ToString(), TextTokenType.Number);
     output.WriteSpace();
     output.Write('(', TextTokenType.Operator);
     output.Write(string.Format("{0:X4}", instr.Offset), TextTokenType.Number);
     output.Write(')', TextTokenType.Operator);
 }
Esempio n. 4
0
 static InstructionVM RemoveNullInstance(InstructionVM vm)
 {
     Debug.Assert(vm != null);
     if (vm == null || vm == InstructionVM.Null)
     {
         return(null);
     }
     return(vm);
 }
Esempio n. 5
0
        public object Clone()
        {
            var inst = new InstructionVM(ownerModule);

            inst.Code = this.Code;
            inst.InstructionOperandVM.InitializeFrom(this.InstructionOperandVM);
            inst.SequencePoint = this.SequencePoint;

            return(inst);
        }
Esempio n. 6
0
 public ExceptionHandlerOptions(Dictionary<object, object> ops, ExceptionHandler eh)
 {
     this.TryStart = (InstructionVM)BodyUtils.TryGetVM(ops, eh.TryStart);
     this.TryEnd = (InstructionVM)BodyUtils.TryGetVM(ops, eh.TryEnd);
     this.FilterStart = (InstructionVM)BodyUtils.TryGetVM(ops, eh.FilterStart);
     this.HandlerStart = (InstructionVM)BodyUtils.TryGetVM(ops, eh.HandlerStart);
     this.HandlerEnd = (InstructionVM)BodyUtils.TryGetVM(ops, eh.HandlerEnd);
     this.CatchType = eh.CatchType;
     this.HandlerType = eh.HandlerType;
 }
Esempio n. 7
0
 public ExceptionHandlerOptions(Dictionary <object, object> ops, ExceptionHandler eh)
 {
     this.TryStart     = (InstructionVM)BodyUtils.TryGetVM(ops, eh.TryStart);
     this.TryEnd       = (InstructionVM)BodyUtils.TryGetVM(ops, eh.TryEnd);
     this.FilterStart  = (InstructionVM)BodyUtils.TryGetVM(ops, eh.FilterStart);
     this.HandlerStart = (InstructionVM)BodyUtils.TryGetVM(ops, eh.HandlerStart);
     this.HandlerEnd   = (InstructionVM)BodyUtils.TryGetVM(ops, eh.HandlerEnd);
     this.CatchType    = eh.CatchType;
     this.HandlerType  = eh.HandlerType;
 }
Esempio n. 8
0
        public static object ToOperandVM(Dictionary <object, object> ops, object operand)
        {
            var targets = operand as IList <Instruction>;

            if (targets != null)
            {
                var newTargets = new InstructionVM[targets.Count];
                for (int i = 0; i < newTargets.Length; i++)
                {
                    newTargets[i] = (InstructionVM)TryGetVM(ops, (object)targets[i] ?? InstructionVM.Null);
                }
                return(newTargets);
            }

            return(TryGetVM(ops, operand));
        }
Esempio n. 9
0
 bool RemoveInstructionAndAddPopsCanExecute(InstructionVM[] instrs)
 {
     return instrs.Any(a => GetInstructionPops(a) != null);
 }
Esempio n. 10
0
        void RemoveInstructionAndAddPops(InstructionVM[] instrs)
        {
            foreach (var instr in instrs) {
                var info = GetInstructionPops(instr);
                if (info == null)
                    continue;

                var popCount = info.Value.PopCount;
                var origCode = instr.Code;
                if (origCode == Code.Callvirt && instr.Index >= 1 && instructionsListVM[instr.Index - 1].Code == Code.Constrained)
                    instructionsListVM[instr.Index - 1].Code = Code.Nop;

                int index = instr.Index + 1;
                if (popCount == 0)
                    instr.Code = Code.Nop;
                else {
                    instr.Code = Code.Pop;
                    while (--popCount > 0)
                        InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Pop));
                }

                if (info.Value.Pushes)
                    AddPushDefaultValue(0, ref index, info.Value.PushType);
            }
        }
Esempio n. 11
0
 bool InvertBranchCanExecute(InstructionVM[] instrs)
 {
     return instrs.Any(a => InvertBcc(a.Code) != null);
 }
Esempio n. 12
0
 void InvertBranch(InstructionVM[] instrs)
 {
     foreach (var instr in instrs) {
         var code = InvertBcc(instr.Code);
         if (code != null)
             instr.Code = code.Value;
     }
 }
Esempio n. 13
0
 bool ConvertBranchToUnconditionalBranchCanExecute(InstructionVM[] instrs)
 {
     return instrs.Any(a => GetBccPopCount(a.Code) > 0);
 }
Esempio n. 14
0
        public object Clone()
        {
            var inst = new InstructionVM(ownerModule);

            inst.Code = this.Code;
            inst.InstructionOperandVM.InitializeFrom(this.InstructionOperandVM);
            inst.SequencePoint = this.SequencePoint;

            return inst;
        }
Esempio n. 15
0
        void ReplaceInstructionWithNop(InstructionVM[] instrs)
        {
            var old1 = InstructionsListVM.DisableAutoUpdateProps;
            var old2 = DisableHasError();
            try {
                InstructionsListVM.DisableAutoUpdateProps = true;

                foreach (var instr in instrs)
                    instr.Code = Code.Nop;
            }
            finally {
                RestoreHasError(old2);
                InstructionsListVM.DisableAutoUpdateProps = old1;
            }
            InstructionsUpdateIndexes(0);
        }
Esempio n. 16
0
 bool ReplaceInstructionWithNopCanExecute(InstructionVM[] instrs)
 {
     return instrs.Any(a => a.Code != Code.Nop);
 }
Esempio n. 17
0
 static InstructionPushPopInfo? GetInstructionPops(InstructionVM instr)
 {
     var code = instr.Code;
     if (code == Code.Pop || code == Code.Nop)
         return null;
     int pushes, pops;
     instr.CalculateStackUsage(out pushes, out pops);
     if (pops < 0)
         return null;
     if (pushes == 1 && (code == Code.Call || code == Code.Callvirt || code == Code.Calli))
         return new InstructionPushPopInfo(pops, GetMethodSig(instr.InstructionOperandVM.Other).GetRetType());
     if (pushes == 1 && code == Code.Newobj) {
         var ctor = instr.InstructionOperandVM.Other as IMethod;
         return new InstructionPushPopInfo(pops, ctor == null ? null : ctor.DeclaringType.ToTypeSig());
     }
     if (pushes != 0)
         return null;
     return new InstructionPushPopInfo(pops);
 }
Esempio n. 18
0
 static InstructionVM RemoveNullInstance(InstructionVM vm)
 {
     Debug.Assert(vm != null);
     if (vm == null || vm == InstructionVM.Null)
         return null;
     return vm;
 }
Esempio n. 19
0
        void ConvertBranchToUnconditionalBranch(InstructionVM[] instrs)
        {
            foreach (var instr in instrs) {
                var popCount = GetBccPopCount(instr.Code);
                if (popCount <= 0)
                    continue;

                var targetOperand = instr.InstructionOperandVM.Value;
                instr.Code = Code.Pop;
                int index = instr.Index + 1;
                while (--popCount > 0)
                    InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Pop));
                var brInstr = CreateInstructionVM(Code.Br);
                brInstr.InstructionOperandVM.OperandListItem = targetOperand ?? InstructionVM.Null;
                InstructionsListVM.Insert(index++, brInstr);
            }
        }