public static void WriteMethodBody (TextWriter writer, MethodBase method)
		{
			foreach (var instruction in method.GetInstructions ()) {
				writer.Write ('\t');
				WriteInstruction (writer, instruction);
				writer.WriteLine ();
			}
		}
Esempio n. 2
0
        public static MatchContext Match(MethodBase method, ILPattern pattern) {
            IList<Instruction> instructions = method.GetInstructions();
            if (instructions.Count == 0)
                throw new ArgumentException();

            var context = new MatchContext(instructions[0]);
            pattern.Match(context);
            return context;
        }
Esempio n. 3
0
		public static MatchContext Match (MethodBase method, ILPattern pattern) {
			if (method == null)
				throw new ArgumentNullException("method");
			if (pattern == null)
				throw new ArgumentNullException("pattern");

			var instructions = method.GetInstructions();
			if (instructions.Count == 0)
				throw new ArgumentException();

			var context = new MatchContext(instructions[0]);
			pattern.Match(context);
			return context;
		}
Esempio n. 4
0
        private void MapInstructions(MethodBase method, MethodDefinition method_definition)
        {
            var instructions = method.GetInstructions();

            foreach (var instruction in instructions)
            {
                var il = method_definition.Body.GetILProcessor();

                var op = OpCodeFor(instruction);

                switch (op.OperandType)
                {
                    case OperandType.InlineNone:
                        il.Emit(op);
                        break;
                    case OperandType.InlineMethod:
                        il.Emit(op, CreateReference((MethodBase)instruction.Operand, method_definition));
                        break;
                    case OperandType.InlineField:
                        il.Emit(op, CreateReference((FieldInfo)instruction.Operand, method_definition));
                        break;
                    case OperandType.InlineType:
                        il.Emit(op, CreateReference((Type)instruction.Operand, method_definition));
                        break;
                    case OperandType.InlineTok:
                        var member = (MemberInfo)instruction.Operand;
                        if (member is Type)
                            il.Emit(op, CreateTokenReference((Type)instruction.Operand, method_definition));
                        else if (member is FieldInfo)
                            il.Emit(op, CreateReference((FieldInfo)instruction.Operand, method_definition));
                        else if (member is MethodBase)
                            il.Emit(op, CreateTokenReference((MethodBase)instruction.Operand, method_definition));
                        else
                            throw new NotSupportedException();
                        break;
                    case OperandType.ShortInlineI:
                        if (op.Code == Code.Ldc_I4_S)
                            il.Emit(op, (sbyte)instruction.Operand);
                        else
                            il.Emit(op, (byte)instruction.Operand);
                        break;
                    case OperandType.InlineI:
                        il.Emit(op, (int)instruction.Operand);
                        break;
                    case OperandType.InlineI8:
                        il.Emit(op, (long)instruction.Operand);
                        break;
                    case OperandType.ShortInlineR:
                        il.Emit(op, (float)instruction.Operand);
                        break;
                    case OperandType.InlineR:
                        il.Emit(op, (double)instruction.Operand);
                        break;
                    case OperandType.ShortInlineVar:
                    case OperandType.InlineVar:
                        il.Emit(op, VariableFor(instruction, method_definition));
                        break;
                    case OperandType.ShortInlineArg:
                    case OperandType.InlineArg:
                        il.Emit(op, ParameterFor(instruction, method_definition));
                        break;
                    case OperandType.InlineString:
                        il.Emit(op, (string)instruction.Operand);
                        break;
                    case OperandType.ShortInlineBrTarget:
                    case OperandType.InlineBrTarget:
                        il.Emit(op, MC.Cil.Instruction.Create(OpCodes.Nop));
                        break;
                    case OperandType.InlineSwitch:
                        il.Emit(op, new[] { MC.Cil.Instruction.Create(OpCodes.Nop) });
                        break;
                    case OperandType.InlineSig:
                        throw new NotSupportedException("InlineSig");
                    default:
                        throw new NotSupportedException(op.OperandType.ToString());
                }
            }

            foreach (var instruction in instructions)
            {
                var op = OpCodeFor(instruction);

                switch (op.OperandType)
                {
                    case OperandType.ShortInlineBrTarget:
                    case OperandType.InlineBrTarget:
                        var br = OffsetToInstruction(instruction.Offset, instructions, method_definition);
                        var target = (MR.Instruction)instruction.Operand;
                        if (target != null)
                            br.Operand = OffsetToInstruction(target.Offset, instructions, method_definition);

                        break;

                    case OperandType.InlineSwitch:
                        var @switch = OffsetToInstruction(instruction.Offset, instructions, method_definition);
                        @switch.Operand = ((MR.Instruction[])instruction.Operand).Select(i => OffsetToInstruction(i.Offset, instructions, method_definition)).ToArray();
                        break;
                }
            }
        }
Esempio n. 5
0
        private void MapExceptions(MethodBase method, MethodDefinition method_definition)
        {
            var body = method.GetMethodBody();
            if (body == null)
                return;

            var instructions = method.GetInstructions();

            foreach (var clause in body.ExceptionHandlingClauses)
            {
                var handler = new ExceptionHandler((ExceptionHandlerType)clause.Flags)
                {
                    TryStart = OffsetToInstruction(clause.TryOffset, instructions, method_definition),
                    TryEnd = OffsetToInstruction(clause.TryOffset + clause.TryLength, instructions, method_definition),
                    HandlerStart = OffsetToInstruction(clause.HandlerOffset, instructions, method_definition),
                    HandlerEnd = OffsetToInstruction(clause.HandlerOffset + clause.HandlerLength, instructions, method_definition)
                };

                switch (handler.HandlerType)
                {
                    case ExceptionHandlerType.Catch:
                        handler.CatchType = CreateReference(clause.CatchType, method_definition);
                        break;
                    case ExceptionHandlerType.Filter:
                        handler.FilterStart = OffsetToInstruction(clause.FilterOffset, instructions, method_definition);
                        break;
                }

                method_definition.Body.ExceptionHandlers.Add(handler);
            }
        }