Пример #1
0
        private void TryOptimizeArgument(MsilInstruction instruction)
        {
            var parameter = instruction.Operand as ParameterSignature;

            if (Method == null || Method.Signature == null || parameter == null)
            {
                return;
            }
            int index = Method.Signature.Parameters.IndexOf(parameter);

            if (index < 0 || index > byte.MaxValue)
            {
                return;
            }

            switch (instruction.OpCode.Code)
            {
            case MsilCode.Ldarg:
                if (index <= 3)
                {
                    instruction.OpCode  = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Ldarg_0.Op2 + index];
                    instruction.Operand = null;
                }
                else
                {
                    instruction.OpCode = MsilOpCodes.Ldarg_S;
                }
                break;

            case MsilCode.Ldarga:
                instruction.OpCode = MsilOpCodes.Ldarga_S;
                break;
            }
        }
Пример #2
0
 public static MsilInstruction Create(MsilOpCode code, MsilInstruction instruction)
 {
     if (code.OperandType != MsilOperandType.ShortInlineBrTarget && code.OperandType != MsilOperandType.InlineBrTarget)
     {
         throw new ArgumentException("Opcode does not accept an instruction operand.", "code");
     }
     return(new MsilInstruction(0, code, instruction));
 }
Пример #3
0
        private void TryOptimizeLdc(MsilInstruction instruction)
        {
            int value = (int)instruction.Operand;

            if (value >= -1 && value <= 8)
            {
                instruction.OpCode  = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Ldc_I4_0.Op2 + value];
                instruction.Operand = null;
            }
            else if (value >= sbyte.MinValue && value <= sbyte.MaxValue)
            {
                instruction.OpCode  = MsilOpCodes.Ldc_I4_S;
                instruction.Operand = Convert.ToSByte(value);
            }
        }
Пример #4
0
        private void TryOptimizeVariable(MsilInstruction instruction)
        {
            var variable    = instruction.Operand as VariableSignature;
            var localVarSig = Signature != null ? Signature.Signature as LocalVariableSignature : null;

            if (localVarSig == null || variable == null)
            {
                return;
            }
            int index = localVarSig.Variables.IndexOf(variable);

            if (index < 0 || index > byte.MaxValue)
            {
                return;
            }

            switch (instruction.OpCode.Code)
            {
            case MsilCode.Ldloc:
                if (index <= 3)
                {
                    instruction.OpCode  = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Ldloc_0.Op2 + index];
                    instruction.Operand = null;
                }
                else
                {
                    instruction.OpCode = MsilOpCodes.Ldloc_S;
                }
                break;

            case MsilCode.Ldloca:
                instruction.OpCode = MsilOpCodes.Ldloca_S;
                break;

            case MsilCode.Stloc:
                if (index <= 3)
                {
                    instruction.OpCode  = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Stloc_0.Op2 + index];
                    instruction.Operand = null;
                }
                else
                {
                    instruction.OpCode = MsilOpCodes.Stloc_S;
                }
                break;
            }
        }
Пример #5
0
        private void OptimizeMacro(MsilInstruction instruction)
        {
            switch (instruction.OpCode.OperandType)
            {
            case MsilOperandType.InlineBrTarget:
                TryOptimizeBranch(instruction);
                break;

            case MsilOperandType.InlineVar:
                TryOptimizeVariable(instruction);
                break;

            case MsilOperandType.InlineArgument:
                TryOptimizeArgument(instruction);
                break;
            }

            if (instruction.OpCode.Code == MsilCode.Ldc_I4)
            {
                TryOptimizeLdc(instruction);
            }
        }
Пример #6
0
        private void TryOptimizeArgument(MsilInstruction instruction)
        {
            var parameter = instruction.Operand as ParameterSignature;
            if (Method == null || Method.Signature == null || parameter == null)
                return;
            int index = Method.Signature.Parameters.IndexOf(parameter);
            if (index < 0 || index > byte.MaxValue)
                return;

            switch (instruction.OpCode.Code)
            {
                case MsilCode.Ldarg:
                    if (index <= 3)
                    {
                        instruction.OpCode = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Ldarg_0.Op2 + index];
                        instruction.Operand = null;
                    }
                    else
                    {
                        instruction.OpCode = MsilOpCodes.Ldarg_S;
                    }
                    break;
                case MsilCode.Ldarga:
                    instruction.OpCode = MsilOpCodes.Ldarga_S;
                    break;
            }
        }
Пример #7
0
        private void OptimizeMacro(MsilInstruction instruction)
        {
            switch (instruction.OpCode.OperandType)
            {
                case MsilOperandType.InlineBrTarget:
                    TryOptimizeBranch(instruction);
                    break;
                case MsilOperandType.InlineVar:
                    TryOptimizeVariable(instruction);
                    break;
                case MsilOperandType.InlineArgument:
                    TryOptimizeArgument(instruction);
                    break;
            }

            if (instruction.OpCode.Code == MsilCode.Ldc_I4)
                TryOptimizeLdc(instruction);
        }
Пример #8
0
        private void ExpandMacro(MsilInstruction instruction)
        {
            switch (instruction.OpCode.Code)
            {
                case MsilCode.Br_S:
                    instruction.OpCode = MsilOpCodes.Br;
                    break;
                case MsilCode.Leave_S:
                    instruction.OpCode = MsilOpCodes.Leave;
                    break;
                case MsilCode.Brfalse_S:
                    instruction.OpCode = MsilOpCodes.Brfalse;
                    break;
                case MsilCode.Brtrue_S:
                    instruction.OpCode = MsilOpCodes.Brtrue;
                    break;
                case MsilCode.Beq_S:
                    instruction.OpCode = MsilOpCodes.Beq;
                    break;
                case MsilCode.Bge_S:
                    instruction.OpCode = MsilOpCodes.Bge;
                    break;
                case MsilCode.Bge_Un_S:
                    instruction.OpCode = MsilOpCodes.Bge_Un;
                    break;
                case MsilCode.Bgt_S:
                    instruction.OpCode = MsilOpCodes.Bgt;
                    break;
                case MsilCode.Bgt_Un_S:
                    instruction.OpCode = MsilOpCodes.Bgt_Un;
                    break;
                case MsilCode.Ble_S:
                    instruction.OpCode = MsilOpCodes.Ble;
                    break;
                case MsilCode.Ble_Un_S:
                    instruction.OpCode = MsilOpCodes.Ble_Un;
                    break;
                case MsilCode.Blt_S:
                    instruction.OpCode = MsilOpCodes.Blt;
                    break;
                case MsilCode.Blt_Un_S:
                    instruction.OpCode = MsilOpCodes.Blt_Un;
                    break;
                case MsilCode.Bne_Un_S:
                    instruction.OpCode = MsilOpCodes.Bne_Un;
                    break;

                case MsilCode.Ldloc_S:
                    instruction.OpCode = MsilOpCodes.Ldloc;
                    break;

                case MsilCode.Ldloca_S:
                    instruction.OpCode = MsilOpCodes.Ldloca;
                    break;

                case MsilCode.Ldloc_0:
                case MsilCode.Ldloc_1:
                case MsilCode.Ldloc_2:
                case MsilCode.Ldloc_3:
                    instruction.Operand = ((IOperandResolver) this).ResolveVariable(instruction.OpCode.Name[instruction.OpCode.Name.Length - 1] - 48);
                    instruction.OpCode = MsilOpCodes.Ldloc;
                    break;

                case MsilCode.Stloc_S:
                    instruction.OpCode = MsilOpCodes.Stloc;
                    break;

                case MsilCode.Stloc_0:
                case MsilCode.Stloc_1:
                case MsilCode.Stloc_2:
                case MsilCode.Stloc_3:
                    instruction.Operand = ((IOperandResolver) this).ResolveVariable(instruction.OpCode.Name[instruction.OpCode.Name.Length - 1] - 48);
                    instruction.OpCode = MsilOpCodes.Stloc;
                    break;

                case MsilCode.Ldarg_S:
                    instruction.OpCode = MsilOpCodes.Ldarg;
                    break;

                case MsilCode.Ldarga_S:
                    instruction.OpCode = MsilOpCodes.Ldarga;
                    break;

                case MsilCode.Ldarg_0:
                case MsilCode.Ldarg_1:
                case MsilCode.Ldarg_2:
                case MsilCode.Ldarg_3:
                    instruction.Operand = ((IOperandResolver) this).ResolveParameter(instruction.OpCode.Name[instruction.OpCode.Name.Length - 1] - 48);
                    instruction.OpCode = MsilOpCodes.Ldarg;
                    break;

                case MsilCode.Starg_S:
                    instruction.OpCode = MsilOpCodes.Starg;
                    break;

                case MsilCode.Ldc_I4_0:
                case MsilCode.Ldc_I4_1:
                case MsilCode.Ldc_I4_2:
                case MsilCode.Ldc_I4_3:
                case MsilCode.Ldc_I4_4:
                case MsilCode.Ldc_I4_5:
                case MsilCode.Ldc_I4_6:
                case MsilCode.Ldc_I4_7:
                case MsilCode.Ldc_I4_8:
                    instruction.Operand = instruction.OpCode.Name[instruction.OpCode.Name.Length - 1] - 48;
                    instruction.OpCode = MsilOpCodes.Ldc_I4;
                    break;
                case MsilCode.Ldc_I4_S:
                    instruction.OpCode = MsilOpCodes.Ldc_I4;
                    break;
                case MsilCode.Ldc_I4_M1:
                    instruction.OpCode = MsilOpCodes.Ldc_I4;
                    instruction.Operand = -1;
                    break;
            }
        }
Пример #9
0
        private void ResolveOperand(IList <MsilInstruction> instructions, MsilInstruction current)
        {
            var nextOffset = current.Offset + current.Size;

            switch (current.OpCode.OperandType)
            {
            case MsilOperandType.InlineArgument:
            case MsilOperandType.ShortInlineArgument:
                var parameter = _resolver.ResolveParameter(Convert.ToInt32(current.Operand));
                if (parameter != null)
                {
                    current.Operand = parameter;
                }
                break;

            case MsilOperandType.InlineVar:
            case MsilOperandType.ShortInlineVar:
                var variable = _resolver.ResolveVariable(Convert.ToInt32(current.Operand));
                if (variable != null)
                {
                    current.Operand = variable;
                }
                break;

            case MsilOperandType.ShortInlineBrTarget:
            case MsilOperandType.InlineBrTarget:
                var targetInstruction = instructions.FirstOrDefault(
                    x => x.Offset == nextOffset + Convert.ToInt32(current.Operand));
                if (targetInstruction != null)
                {
                    current.Operand = targetInstruction;
                }
                break;

            case MsilOperandType.InlineField:
            case MsilOperandType.InlineMethod:
            case MsilOperandType.InlineSig:
            case MsilOperandType.InlineTok:
            case MsilOperandType.InlineType:
                var member = _resolver.ResolveMember((MetadataToken)current.Operand);
                if (member != null)
                {
                    current.Operand = member;
                }
                break;

            case MsilOperandType.InlineString:
                var stringValue = _resolver.ResolveString(((MetadataToken)current.Operand).ToUInt32());
                if (stringValue != null)
                {
                    current.Operand = stringValue;
                }
                break;

            case MsilOperandType.InlineSwitch:
                var targetOffsets = (int[])current.Operand;
                var targets       = new MsilInstruction[targetOffsets.Length];
                for (int i = 0; i < targetOffsets.Length; i++)
                {
                    targets[i] = instructions.FirstOrDefault(
                        x => x.Offset == nextOffset + targetOffsets[i]);
                }
                current.Operand = targets;
                break;
            }
        }
Пример #10
0
        private void TryOptimizeVariable(MsilInstruction instruction)
        {
            var variable = instruction.Operand as VariableSignature;
            var localVarSig = Signature != null ? Signature.Signature as LocalVariableSignature : null;
            if (localVarSig == null || variable == null)
                return;
            int index = localVarSig.Variables.IndexOf(variable);
            if (index < 0 || index > byte.MaxValue)
                return;

            switch (instruction.OpCode.Code)
            {
                case MsilCode.Ldloc:
                    if (index <= 3)
                    {
                        instruction.OpCode = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Ldloc_0.Op2 + index];
                        instruction.Operand = null;
                    }
                    else
                    {
                        instruction.OpCode = MsilOpCodes.Ldloc_S;
                    }
                    break;
                case MsilCode.Ldloca:
                    instruction.OpCode = MsilOpCodes.Ldloca_S;
                    break;
                case MsilCode.Stloc:
                    if (index <= 3)
                    {
                        instruction.OpCode = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Stloc_0.Op2 + index];
                        instruction.Operand = null;
                    }
                    else
                    {
                        instruction.OpCode = MsilOpCodes.Stloc_S;
                    }
                    break;
            }
        }
Пример #11
0
        private void WriteOperand(MsilInstruction instruction)
        {
            switch (instruction.OpCode.OperandType)
            {
                case MsilOperandType.InlineArgument:
                    _writer.WriteUInt16((ushort)_builder.GetParameterIndex((ParameterSignature)instruction.Operand));
                    break;
                case MsilOperandType.ShortInlineArgument:
                    _writer.WriteByte((byte)_builder.GetParameterIndex((ParameterSignature)instruction.Operand));
                    break;

                case MsilOperandType.InlineVar:
                    _writer.WriteUInt16((ushort)_builder.GetVariableIndex((VariableSignature)instruction.Operand));
                    break;
                case MsilOperandType.ShortInlineVar:
                    _writer.WriteByte((byte)_builder.GetVariableIndex((VariableSignature)instruction.Operand));
                    break;

                case MsilOperandType.ShortInlineI:
                    _writer.WriteSByte((sbyte)instruction.Operand);
                    break;
                case MsilOperandType.InlineI:
                    _writer.WriteInt32((int)instruction.Operand);
                    break;
                case MsilOperandType.InlineI8:
                    _writer.WriteInt64((long)instruction.Operand);
                    break;
                case MsilOperandType.ShortInlineR:
                    _writer.WriteSingle((float)instruction.Operand);
                    break;
                case MsilOperandType.InlineR:
                    _writer.WriteDouble((double)instruction.Operand);
                    break;

                case MsilOperandType.InlineBrTarget:
                    _writer.WriteInt32(((MsilInstruction)instruction.Operand).Offset -
                        (instruction.Offset + instruction.Size));
                    break;

                case MsilOperandType.ShortInlineBrTarget:
                    _writer.WriteSByte((sbyte)(((MsilInstruction)instruction.Operand).Offset -
                        (instruction.Offset + instruction.Size)));
                    break;

                case MsilOperandType.InlineField:
                case MsilOperandType.InlineMethod:
                case MsilOperandType.InlineSig:
                case MsilOperandType.InlineTok:
                case MsilOperandType.InlineType:
                    var token = _builder.GetMemberToken((MetadataMember)instruction.Operand);
                    if (token.Rid == 0)
                        throw new InvalidOperationException(string.Format("Member {0} has an invalid metadata token.",
                            instruction.Operand));
                    _writer.WriteUInt32(token.ToUInt32());
                    break;

                case MsilOperandType.InlineString:
                    _writer.WriteUInt32(_builder.GetStringOffset((string)instruction.Operand));
                    break;

                case MsilOperandType.InlineSwitch:
                    var targets = (MsilInstruction[])instruction.Operand;
                    _writer.WriteInt32(targets.Length);
                    foreach (var target in targets)
                        _writer.WriteInt32(target.Offset - (instruction.Offset + instruction.Size));
                    break;

                case MsilOperandType.InlineNone:
                    break;
            }
        }
Пример #12
0
 public void Write(MsilInstruction instruction)
 {
     WriteOpCode(instruction.OpCode);
     WriteOperand(instruction);
 }
Пример #13
0
        private void TryOptimizeBranch(MsilInstruction instruction)
        {
            MsilInstruction operand         = instruction.Operand as MsilInstruction;
            int             relativeOperand = operand.Offset - (instruction.Offset + 2);

            if (operand == null || relativeOperand < sbyte.MinValue || relativeOperand > sbyte.MaxValue)
            {
                return;
            }
            switch (instruction.OpCode.Code)
            {
            case MsilCode.Br:
                instruction.OpCode = MsilOpCodes.Br_S;
                break;

            case MsilCode.Leave:
                instruction.OpCode = MsilOpCodes.Leave_S;
                break;

            case MsilCode.Brfalse:
                instruction.OpCode = MsilOpCodes.Brfalse_S;
                break;

            case MsilCode.Brtrue:
                instruction.OpCode = MsilOpCodes.Brtrue_S;
                break;

            case MsilCode.Beq:
                instruction.OpCode = MsilOpCodes.Beq_S;
                break;

            case MsilCode.Bge:
                instruction.OpCode = MsilOpCodes.Bge_S;
                break;

            case MsilCode.Bge_Un:
                instruction.OpCode = MsilOpCodes.Bge_Un_S;
                break;

            case MsilCode.Bgt:
                instruction.OpCode = MsilOpCodes.Bgt_S;
                break;

            case MsilCode.Bgt_Un:
                instruction.OpCode = MsilOpCodes.Bgt_Un_S;
                break;

            case MsilCode.Ble:
                instruction.OpCode = MsilOpCodes.Ble_S;
                break;

            case MsilCode.Ble_Un:
                instruction.OpCode = MsilOpCodes.Ble_Un_S;
                break;

            case MsilCode.Blt:
                instruction.OpCode = MsilOpCodes.Blt_S;
                break;

            case MsilCode.Blt_Un:
                instruction.OpCode = MsilOpCodes.Blt_Un_S;
                break;

            case MsilCode.Bne_Un:
                instruction.OpCode = MsilOpCodes.Bne_Un_S;
                break;
            }
        }
Пример #14
0
 private void TryOptimizeBranch(MsilInstruction instruction)
 {
     MsilInstruction operand = instruction.Operand as MsilInstruction;
     int relativeOperand = operand.Offset - (instruction.Offset + 2);
     if (operand == null || relativeOperand < sbyte.MinValue || relativeOperand > sbyte.MaxValue)
         return;
     switch (instruction.OpCode.Code)
     {
         case MsilCode.Br:
             instruction.OpCode = MsilOpCodes.Br_S;
             break;
         case MsilCode.Leave:
             instruction.OpCode = MsilOpCodes.Leave_S;
             break;
         case MsilCode.Brfalse:
             instruction.OpCode = MsilOpCodes.Brfalse_S;
             break;
         case MsilCode.Brtrue:
             instruction.OpCode = MsilOpCodes.Brtrue_S;
             break;
         case MsilCode.Beq:
             instruction.OpCode = MsilOpCodes.Beq_S;
             break;
         case MsilCode.Bge:
             instruction.OpCode = MsilOpCodes.Bge_S;
             break;
         case MsilCode.Bge_Un:
             instruction.OpCode = MsilOpCodes.Bge_Un_S;
             break;
         case MsilCode.Bgt:
             instruction.OpCode = MsilOpCodes.Bgt_S;
             break;
         case MsilCode.Bgt_Un:
             instruction.OpCode = MsilOpCodes.Bgt_Un_S;
             break;
         case MsilCode.Ble:
             instruction.OpCode = MsilOpCodes.Ble_S;
             break;
         case MsilCode.Ble_Un:
             instruction.OpCode = MsilOpCodes.Ble_Un_S;
             break;
         case MsilCode.Blt:
             instruction.OpCode = MsilOpCodes.Blt_S;
             break;
         case MsilCode.Blt_Un:
             instruction.OpCode = MsilOpCodes.Blt_Un_S;
             break;
         case MsilCode.Bne_Un:
             instruction.OpCode = MsilOpCodes.Bne_Un_S;
             break;
     }
 }
Пример #15
0
 public static MsilInstruction Create(MsilOpCode code, MsilInstruction instruction)
 {
     if (code.OperandType != MsilOperandType.ShortInlineBrTarget && code.OperandType != MsilOperandType.InlineBrTarget)
         throw new ArgumentException("Opcode does not accept an instruction operand.", "code");
     return new MsilInstruction(0, code, instruction);
 }
Пример #16
0
 private void TryOptimizeLdc(MsilInstruction instruction)
 {
     int value = (int) instruction.Operand;
     if (value >= -1 && value <= 8)
     {
         instruction.OpCode = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Ldc_I4_0.Op2 + value];
         instruction.Operand = null;
     }
     else if (value >= sbyte.MinValue && value <= sbyte.MaxValue)
     {
         instruction.OpCode = MsilOpCodes.Ldc_I4_S;
         instruction.Operand = Convert.ToSByte(value);
     }
 }
Пример #17
0
 public void Write(MsilInstruction instruction)
 {
     WriteOpCode(instruction.OpCode);
     WriteOperand(instruction);
 }
Пример #18
0
        private void ResolveOperand(IList<MsilInstruction> instructions, MsilInstruction current)
        {
            var nextOffset = current.Offset + current.Size;

            switch (current.OpCode.OperandType)
            {
                case MsilOperandType.InlineArgument:
                case MsilOperandType.ShortInlineArgument:
                    var parameter = _resolver.ResolveParameter(Convert.ToInt32(current.Operand));
                    if (parameter != null)
                        current.Operand = parameter;
                    break;

                case MsilOperandType.InlineVar:
                case MsilOperandType.ShortInlineVar:
                    var variable = _resolver.ResolveVariable(Convert.ToInt32(current.Operand));
                    if (variable != null)
                        current.Operand = variable;
                    break;

                case MsilOperandType.ShortInlineBrTarget:
                case MsilOperandType.InlineBrTarget:
                    var targetInstruction = instructions.FirstOrDefault(
                        x => x.Offset == nextOffset + Convert.ToInt32(current.Operand));
                    if (targetInstruction != null)
                        current.Operand = targetInstruction;
                    break;

                case MsilOperandType.InlineField:
                case MsilOperandType.InlineMethod:
                case MsilOperandType.InlineSig:
                case MsilOperandType.InlineTok:
                case MsilOperandType.InlineType:
                    var member = _resolver.ResolveMember((MetadataToken)current.Operand);
                    if (member != null)
                        current.Operand = member;
                    break;

                case MsilOperandType.InlineString:
                    var stringValue = _resolver.ResolveString(((MetadataToken)current.Operand).ToUInt32());
                    if (stringValue != null)
                        current.Operand = stringValue;
                    break;

                case MsilOperandType.InlineSwitch:
                    var targetOffsets = (int[])current.Operand;
                    var targets = new MsilInstruction[targetOffsets.Length];
                    for (int i = 0; i < targetOffsets.Length; i++)
                        targets[i] = instructions.FirstOrDefault(
                            x => x.Offset == nextOffset + targetOffsets[i]);
                    current.Operand = targets;
                    break;
            }
        }
Пример #19
0
        private void WriteOperand(MsilInstruction instruction)
        {
            switch (instruction.OpCode.OperandType)
            {
            case MsilOperandType.InlineArgument:
                _writer.WriteUInt16((ushort)_builder.GetParameterIndex((ParameterSignature)instruction.Operand));
                break;

            case MsilOperandType.ShortInlineArgument:
                _writer.WriteByte((byte)_builder.GetParameterIndex((ParameterSignature)instruction.Operand));
                break;

            case MsilOperandType.InlineVar:
                _writer.WriteUInt16((ushort)_builder.GetVariableIndex((VariableSignature)instruction.Operand));
                break;

            case MsilOperandType.ShortInlineVar:
                _writer.WriteByte((byte)_builder.GetVariableIndex((VariableSignature)instruction.Operand));
                break;

            case MsilOperandType.ShortInlineI:
                _writer.WriteSByte((sbyte)instruction.Operand);
                break;

            case MsilOperandType.InlineI:
                _writer.WriteInt32((int)instruction.Operand);
                break;

            case MsilOperandType.InlineI8:
                _writer.WriteInt64((long)instruction.Operand);
                break;

            case MsilOperandType.ShortInlineR:
                _writer.WriteSingle((float)instruction.Operand);
                break;

            case MsilOperandType.InlineR:
                _writer.WriteDouble((double)instruction.Operand);
                break;

            case MsilOperandType.InlineBrTarget:
                _writer.WriteInt32(((MsilInstruction)instruction.Operand).Offset -
                                   (instruction.Offset + instruction.Size));
                break;

            case MsilOperandType.ShortInlineBrTarget:
                _writer.WriteSByte((sbyte)(((MsilInstruction)instruction.Operand).Offset -
                                           (instruction.Offset + instruction.Size)));
                break;

            case MsilOperandType.InlineField:
            case MsilOperandType.InlineMethod:
            case MsilOperandType.InlineSig:
            case MsilOperandType.InlineTok:
            case MsilOperandType.InlineType:
                var token = _builder.GetMemberToken((MetadataMember)instruction.Operand);
                if (token.Rid == 0)
                {
                    throw new InvalidOperationException(string.Format("Member {0} has an invalid metadata token.",
                                                                      instruction.Operand));
                }
                _writer.WriteUInt32(token.ToUInt32());
                break;

            case MsilOperandType.InlineString:
                _writer.WriteUInt32(_builder.GetStringOffset((string)instruction.Operand));
                break;

            case MsilOperandType.InlineSwitch:
                var targets = (MsilInstruction[])instruction.Operand;
                _writer.WriteInt32(targets.Length);
                foreach (var target in targets)
                {
                    _writer.WriteInt32(target.Offset - (instruction.Offset + instruction.Size));
                }
                break;

            case MsilOperandType.InlineNone:
                break;
            }
        }
 public MsilInstructionListViewItem(MsilInstruction instruction, byte[] bytes)
 {
     Instruction = instruction;
     Bytes = bytes;
 }
Пример #21
0
        private void ExpandMacro(MsilInstruction instruction)
        {
            switch (instruction.OpCode.Code)
            {
            case MsilCode.Br_S:
                instruction.OpCode = MsilOpCodes.Br;
                break;

            case MsilCode.Leave_S:
                instruction.OpCode = MsilOpCodes.Leave;
                break;

            case MsilCode.Brfalse_S:
                instruction.OpCode = MsilOpCodes.Brfalse;
                break;

            case MsilCode.Brtrue_S:
                instruction.OpCode = MsilOpCodes.Brtrue;
                break;

            case MsilCode.Beq_S:
                instruction.OpCode = MsilOpCodes.Beq;
                break;

            case MsilCode.Bge_S:
                instruction.OpCode = MsilOpCodes.Bge;
                break;

            case MsilCode.Bge_Un_S:
                instruction.OpCode = MsilOpCodes.Bge_Un;
                break;

            case MsilCode.Bgt_S:
                instruction.OpCode = MsilOpCodes.Bgt;
                break;

            case MsilCode.Bgt_Un_S:
                instruction.OpCode = MsilOpCodes.Bgt_Un;
                break;

            case MsilCode.Ble_S:
                instruction.OpCode = MsilOpCodes.Ble;
                break;

            case MsilCode.Ble_Un_S:
                instruction.OpCode = MsilOpCodes.Ble_Un;
                break;

            case MsilCode.Blt_S:
                instruction.OpCode = MsilOpCodes.Blt;
                break;

            case MsilCode.Blt_Un_S:
                instruction.OpCode = MsilOpCodes.Blt_Un;
                break;

            case MsilCode.Bne_Un_S:
                instruction.OpCode = MsilOpCodes.Bne_Un;
                break;

            case MsilCode.Ldloc_S:
                instruction.OpCode = MsilOpCodes.Ldloc;
                break;

            case MsilCode.Ldloca_S:
                instruction.OpCode = MsilOpCodes.Ldloca;
                break;

            case MsilCode.Ldloc_0:
            case MsilCode.Ldloc_1:
            case MsilCode.Ldloc_2:
            case MsilCode.Ldloc_3:
                instruction.Operand = ((IOperandResolver)this).ResolveVariable(instruction.OpCode.Name[instruction.OpCode.Name.Length - 1] - 48);
                instruction.OpCode  = MsilOpCodes.Ldloc;
                break;

            case MsilCode.Stloc_S:
                instruction.OpCode = MsilOpCodes.Stloc;
                break;

            case MsilCode.Stloc_0:
            case MsilCode.Stloc_1:
            case MsilCode.Stloc_2:
            case MsilCode.Stloc_3:
                instruction.Operand = ((IOperandResolver)this).ResolveVariable(instruction.OpCode.Name[instruction.OpCode.Name.Length - 1] - 48);
                instruction.OpCode  = MsilOpCodes.Stloc;
                break;

            case MsilCode.Ldarg_S:
                instruction.OpCode = MsilOpCodes.Ldarg;
                break;

            case MsilCode.Ldarga_S:
                instruction.OpCode = MsilOpCodes.Ldarga;
                break;

            case MsilCode.Ldarg_0:
            case MsilCode.Ldarg_1:
            case MsilCode.Ldarg_2:
            case MsilCode.Ldarg_3:
                instruction.Operand = ((IOperandResolver)this).ResolveParameter(instruction.OpCode.Name[instruction.OpCode.Name.Length - 1] - 48);
                instruction.OpCode  = MsilOpCodes.Ldarg;
                break;

            case MsilCode.Starg_S:
                instruction.OpCode = MsilOpCodes.Starg;
                break;

            case MsilCode.Ldc_I4_0:
            case MsilCode.Ldc_I4_1:
            case MsilCode.Ldc_I4_2:
            case MsilCode.Ldc_I4_3:
            case MsilCode.Ldc_I4_4:
            case MsilCode.Ldc_I4_5:
            case MsilCode.Ldc_I4_6:
            case MsilCode.Ldc_I4_7:
            case MsilCode.Ldc_I4_8:
                instruction.Operand = instruction.OpCode.Name[instruction.OpCode.Name.Length - 1] - 48;
                instruction.OpCode  = MsilOpCodes.Ldc_I4;
                break;

            case MsilCode.Ldc_I4_S:
                instruction.OpCode = MsilOpCodes.Ldc_I4;
                break;

            case MsilCode.Ldc_I4_M1:
                instruction.OpCode  = MsilOpCodes.Ldc_I4;
                instruction.Operand = -1;
                break;
            }
        }