Esempio n. 1
0
        public CilBodyOptions CopyTo(CilBodyOptions options)
        {
            var ops = new Dictionary <object, object>();

            foreach (var vm in LocalsListVM)
            {
                ops.Add(vm, vm.CreateLocalOptions().Create());
            }
            foreach (var vm in InstructionsListVM)
            {
                ops.Add(vm, new Instruction());
            }

            options.KeepOldMaxStack = KeepOldMaxStack;
            options.InitLocals      = InitLocals;
            options.MaxStack        = MaxStack.Value;
            options.LocalVarSigTok  = LocalVarSigTok.Value;
            options.HeaderSize      = HeaderSize.Value;
            options.Locals.Clear();
            options.Locals.AddRange(LocalsListVM.Select(a => (Local)ops[a]));
            options.Instructions.Clear();
            options.Instructions.AddRange(InstructionsListVM.Select(a => a.CreateInstructionOptions().CopyTo(ops, (Instruction)ops[a])));
            options.ExceptionHandlers.Clear();
            options.ExceptionHandlers.AddRange(ExceptionHandlersListVM.Select(a => a.CreateExceptionHandlerOptions().Create(ops)));
            return(options);
        }
Esempio n. 2
0
        void LocalsUpdateIndexes(int i)
        {
            var old = DisableHasError();

            try {
                LocalsListVM.DefaultUpdateIndexes(i);
                UpdateLocalOperands();
                UpdateBranchOperands();
            }
            finally {
                RestoreHasError(old);
            }
        }
Esempio n. 3
0
        public void InitializeFrom(CilBodyOptions options)
        {
            try {
                LocalsListVM.DisableAutoUpdateProps            = true;
                InstructionsListVM.DisableAutoUpdateProps      = true;
                ExceptionHandlersListVM.DisableAutoUpdateProps = true;
                HasError_disabled = true;

                var ops = new Dictionary <object, object>();
                foreach (var local in options.Locals)
                {
                    ops.Add(local, new LocalVM(TypeSigCreatorOptions, new LocalOptions(local)));
                }
                foreach (var instr in options.Instructions)
                {
                    ops.Add(instr, new InstructionVM());
                }
                foreach (var instr in options.Instructions)
                {
                    ((InstructionVM)ops[instr]).Initialize(new InstructionOptions(ops, instr));
                }

                KeepOldMaxStack        = options.KeepOldMaxStack;
                InitLocals             = options.InitLocals;
                MaxStack.Value         = options.MaxStack;
                LocalVarSigTok.Value   = options.LocalVarSigTok;
                HeaderSize.Value       = options.HeaderSize;
                HeaderRVA.Value        = (uint)options.HeaderRVA;
                HeaderFileOffset.Value = unchecked ((ulong)(long)options.HeaderFileOffset);
                RVA.Value        = (uint)options.RVA;
                FileOffset.Value = unchecked ((ulong)(long)options.FileOffset);
                LocalsListVM.Clear();
                LocalsListVM.AddRange(options.Locals.Select(a => (LocalVM)ops[a]));
                InstructionsListVM.Clear();
                InstructionsListVM.AddRange(options.Instructions.Select(a => (InstructionVM)ops[a]));
                ExceptionHandlersListVM.Clear();
                ExceptionHandlersListVM.AddRange(options.ExceptionHandlers.Select(a => new ExceptionHandlerVM(TypeSigCreatorOptions, new ExceptionHandlerOptions(ops, a))));
            }
            finally {
                HasError_disabled = false;
                LocalsListVM.DisableAutoUpdateProps            = false;
                InstructionsListVM.DisableAutoUpdateProps      = false;
                ExceptionHandlersListVM.DisableAutoUpdateProps = false;
            }
            LocalsUpdateIndexes(0);
            InstructionsUpdateIndexes(0);
            ExceptionHandlersUpdateIndexes(0);
            HasErrorUpdated();
        }
Esempio n. 4
0
        void LocalsUpdateIndexes(int i)
        {
            var old_disable_UpdateLocalOperands  = disable_UpdateLocalOperands;
            var old_disable_UpdateBranchOperands = disable_UpdateBranchOperands;
            var old = DisableHasError();

            try {
                disable_UpdateLocalOperands  = true;
                disable_UpdateBranchOperands = true;
                LocalsListVM.DefaultUpdateIndexes(i);
                disable_UpdateLocalOperands  = old_disable_UpdateLocalOperands;
                disable_UpdateBranchOperands = old_disable_UpdateBranchOperands;

                UpdateLocalOperands();
                UpdateBranchOperands();
            }
            finally {
                disable_UpdateLocalOperands  = old_disable_UpdateLocalOperands;
                disable_UpdateBranchOperands = old_disable_UpdateBranchOperands;
                RestoreHasError(old);
            }
        }
Esempio n. 5
0
        void AddPushDefaultValue(int count, ref int index, TypeSig pushType)
        {
            pushType = pushType.RemovePinned();
            switch (count > 10 ? ElementType.End : pushType.RemovePinnedAndModifiers().GetElementType())
            {
            case ElementType.Void:
                break;

            case ElementType.Boolean:
            case ElementType.Char:
            case ElementType.I1:
            case ElementType.U1:
            case ElementType.I2:
            case ElementType.U2:
            case ElementType.I4:
            case ElementType.U4:
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Ldc_I4_0));
                break;

            case ElementType.I8:
            case ElementType.U8:
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Ldc_I4_0));
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Conv_I8));
                break;

            case ElementType.R4:
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Ldc_R4));
                break;

            case ElementType.R8:
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Ldc_R8));
                break;

            case ElementType.I:
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Ldc_I4_0));
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Conv_I));
                break;

            case ElementType.U:
            case ElementType.Ptr:
            case ElementType.FnPtr:
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Ldc_I4_0));
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Conv_U));
                break;

            case ElementType.ValueType:
                var td = ((ValueTypeSig)pushType).TypeDefOrRef.ResolveTypeDef();
                if (td != null && td.IsEnum)
                {
                    var undType = td.GetEnumUnderlyingType().RemovePinnedAndModifiers();
                    var et      = undType.GetElementType();
                    if ((ElementType.Boolean <= et && et <= ElementType.R8) || et == ElementType.I || et == ElementType.U)
                    {
                        AddPushDefaultValue(count + 1, ref index, undType);
                        break;
                    }
                }
                goto case ElementType.TypedByRef;

            case ElementType.TypedByRef:
            case ElementType.Var:
            case ElementType.MVar:
                var local = new LocalVM(TypeSigCreatorOptions, new LocalOptions(new Local(pushType)));
                LocalsListVM.Add(local);

                var newInstr = CreateInstructionVM(Code.Ldloca);
                newInstr.InstructionOperandVM.OperandListItem = local;
                InstructionsListVM.Insert(index++, newInstr);

                newInstr = CreateInstructionVM(Code.Initobj);
                newInstr.InstructionOperandVM.Other = local.Type.ToTypeDefOrRef();
                InstructionsListVM.Insert(index++, newInstr);

                newInstr = CreateInstructionVM(Code.Ldloc);
                newInstr.InstructionOperandVM.OperandListItem = local;
                InstructionsListVM.Insert(index++, newInstr);
                break;

            case ElementType.GenericInst:
                if (((GenericInstSig)pushType).GenericType is ValueTypeSig)
                {
                    goto case ElementType.TypedByRef;
                }
                goto case ElementType.Class;

            case ElementType.End:
            case ElementType.String:
            case ElementType.ByRef:
            case ElementType.Class:
            case ElementType.Array:
            case ElementType.ValueArray:
            case ElementType.R:
            case ElementType.Object:
            case ElementType.SZArray:
            case ElementType.CModReqd:
            case ElementType.CModOpt:
            case ElementType.Internal:
            case ElementType.Module:
            case ElementType.Sentinel:
            case ElementType.Pinned:
            default:
                InstructionsListVM.Insert(index++, CreateInstructionVM(Code.Ldnull));
                break;
            }
        }