/// <summary>
            /// Inline the call to the given ctor
            /// </summary>
            private static void InlineNewObjCall(Instruction instruction, MethodBody body, MethodDefinition ctor)
            {
                // Prepare
                var prefixSeq = new ILSequence();

                ctor.Body.SimplifyMacros();

                // Create "this" variable
                var thisVariable = new VariableDefinition(ctor.DeclaringType);

                body.Variables.Add(thisVariable);
                body.InitLocals = true;

                // Store argument in variables
                var paramVariables = new List <VariableDefinition>();

                foreach (var parameter in ctor.Parameters.Reverse())
                {
                    // Create variable
                    var paramVariable = new VariableDefinition(parameter.ParameterType);
                    body.Variables.Add(paramVariable);
                    paramVariables.Insert(0, paramVariable);

                    // Pop
                    prefixSeq.Emit(OpCodes.Stloc, paramVariable);
                }

                // Clone variables first
                var source    = ctor.Body;
                var variables = new List <VariableDefinition>();

                foreach (var sv in source.Variables)
                {
                    var clone = new VariableDefinition(sv.VariableType);
                    variables.Add(clone);
                    body.Variables.Add(clone);
                }

                // Now clone instructions
                var seq = new ILSequence();

                foreach (var instr in source.Instructions)
                {
                    var ni = new Instruction(instr.OpCode, instr.Operand);
                    seq.Append(ni);
                    ni.Offset = instr.Offset;

                    // Convert variable opcodes
                    switch (instr.OpCode.OperandType)
                    {
                    case OperandType.InlineVar:
                    case OperandType.ShortInlineVar:
                    {
                        var index = source.Variables.IndexOf((VariableDefinition)instr.Operand);
                        ni.Operand = variables[index];
                    }
                    break;
                    }

                    // Convert parameter opcodes
                    switch (instr.OpCode.Code)
                    {
                    case Mono.Cecil.Cil.Code.Ldarg:
                    {
                        var index = ctor.Parameters.IndexOf((ParameterDefinition)instr.Operand);
                        ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                        ni.OpCode  = OpCodes.Ldloc;
                    }
                    break;

                    case Mono.Cecil.Cil.Code.Ldarga:
                    {
                        var index = ctor.Parameters.IndexOf((ParameterDefinition)instr.Operand);
                        ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                        ni.OpCode  = OpCodes.Ldloca;
                    }
                    break;

                    case Mono.Cecil.Cil.Code.Starg:
                    {
                        var index = ctor.Parameters.IndexOf((ParameterDefinition)instr.Operand);
                        ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                        ni.OpCode  = OpCodes.Stloc;
                    }
                    break;
                    }
                }

                // Update branch targets
                for (var i = 0; i < seq.Length; i++)
                {
                    var instr = seq[i];
                    var oldi  = source.Instructions[i];

                    if (instr.OpCode.OperandType == OperandType.InlineSwitch)
                    {
                        var olds    = (Instruction[])oldi.Operand;
                        var targets = new Instruction[olds.Length];

                        for (int j = 0; j < targets.Length; j++)
                        {
                            targets[j] = GetClone(seq, source.Instructions, olds[j]);
                        }

                        instr.Operand = targets;
                    }
                    else if (instr.OpCode.OperandType == OperandType.ShortInlineBrTarget ||
                             instr.OpCode.OperandType == OperandType.InlineBrTarget)
                    {
                        instr.Operand = GetClone(seq, source.Instructions, (Instruction)oldi.Operand);
                    }
                }

                // Clone exception handlers
                if (source.HasExceptionHandlers)
                {
                    CloneInstructions(seq, source.Instructions, body.ExceptionHandlers, source.ExceptionHandlers);
                }

                // Find call to "this" ctor
                var callToCtors = seq.Where(x => IsCallToThisCtor(x, ctor)).ToList();

                if (callToCtors.Count == 0)
                {
                    throw new CompilerException(string.Format("No call to another this ctor found in {0}", ctor));
                }
                if (callToCtors.Count > 1)
                {
                    throw new CompilerException(string.Format("Multiple calls to another this ctor found in {0}", ctor));
                }
                var callToCtor = callToCtors[0];

                // Change "ld this" to nop
                var args = callToCtor.GetCallArguments(seq, true);

                args[0].ChangeToNop(); // Replace ldarg.0

                // Replace call to this ctor with newobj
                var callSeq = new ILSequence();

                callSeq.Emit(OpCodes.Newobj, (MethodReference)callToCtor.Operand);
                callSeq.Emit(OpCodes.Stloc, thisVariable); // Save new object
                callToCtor.ChangeToNop();
                callSeq.InsertToBefore(callToCtor, seq);

                // Replace ret instructions
                var end             = seq.Emit(OpCodes.Ldloc, thisVariable);
                var retInstructions = seq.Where(x => x.OpCode.Code == Mono.Cecil.Cil.Code.Ret).ToList();

                foreach (var ins in retInstructions)
                {
                    ins.OpCode  = OpCodes.Br;
                    ins.Operand = end;
                }

                // Insert cloned instructions
                prefixSeq.InsertTo(0, seq);
                seq.InsertToAfter(instruction, body);

                // Change replaced instruction to nop
                instruction.ChangeToNop();

                // Update offsets
                body.ComputeOffsets();
            }
            /// <summary>
            /// Inline the call to the given ctor
            /// </summary>
            private static void InlineNewObjCall(Instruction instruction, MethodBody body, MethodDefinition ctor)
            {
                // Prepare 
                var prefixSeq = new ILSequence();
                ctor.Body.SimplifyMacros();

                // Create "this" variable
                var thisVariable = new VariableDefinition(ctor.DeclaringType);
                body.Variables.Add(thisVariable);
                body.InitLocals = true;

                // Store argument in variables
                var paramVariables = new List<VariableDefinition>();
                foreach (var parameter in ctor.Parameters.Reverse())
                {
                    // Create variable
                    var paramVariable = new VariableDefinition(parameter.ParameterType);
                    body.Variables.Add(paramVariable);
                    paramVariables.Insert(0, paramVariable);

                    // Pop 
                    prefixSeq.Emit(OpCodes.Stloc, paramVariable);
                }

                // Clone variables first
                var source = ctor.Body;
                var variables = new List<VariableDefinition>();
                foreach (var sv in source.Variables)
                {
                    var clone = new VariableDefinition(sv.VariableType);
                    variables.Add(clone);
                    body.Variables.Add(clone);
                }

                // Now clone instructions
                var seq = new ILSequence();
                foreach (var instr in source.Instructions)
                {
                    var ni = new Instruction(instr.OpCode, instr.Operand);
                    seq.Append(ni);
                    ni.Offset = instr.Offset;

                    // Convert variable opcodes
                    switch (instr.OpCode.OperandType)
                    {
                        case OperandType.InlineVar:
                        case OperandType.ShortInlineVar:
                            {
                                var index = source.Variables.IndexOf((VariableDefinition) instr.Operand);
                                ni.Operand = variables[index];
                            }
                            break;
                    }

                    // Convert parameter opcodes
                    switch (instr.OpCode.Code)
                    {
                        case Mono.Cecil.Cil.Code.Ldarg:
                            {
                                var index = ctor.Parameters.IndexOf((ParameterDefinition) instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Ldloc;
                            }
                            break;
                        case Mono.Cecil.Cil.Code.Ldarga:
                            {
                                var index = ctor.Parameters.IndexOf((ParameterDefinition) instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Ldloca;
                            }
                            break;
                        case Mono.Cecil.Cil.Code.Starg:
                            {
                                var index = ctor.Parameters.IndexOf((ParameterDefinition) instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Stloc;
                            }
                            break;
                    }
                }

                // Update branch targets
                for (var i = 0; i < seq.Length; i++)
                {
                    var instr = seq[i];
                    var oldi = source.Instructions[i];

                    if (instr.OpCode.OperandType == OperandType.InlineSwitch)
                    {
                        var olds = (Instruction[]) oldi.Operand;
                        var targets = new Instruction[olds.Length];

                        for (int j = 0; j < targets.Length; j++)
                        {
                            targets[j] = GetClone(seq, source.Instructions, olds[j]);
                        }

                        instr.Operand = targets;
                    }
                    else if (instr.OpCode.OperandType == OperandType.ShortInlineBrTarget ||
                             instr.OpCode.OperandType == OperandType.InlineBrTarget)
                    {
                        instr.Operand = GetClone(seq, source.Instructions, (Instruction) oldi.Operand);
                    }
                }

                // Clone exception handlers
                if (source.HasExceptionHandlers)
                {
                    CloneInstructions(seq, source.Instructions, body.ExceptionHandlers, source.ExceptionHandlers);
                }

                // Find call to "this" ctor
                var callToCtors = seq.Where(x => IsCallToThisCtor(x, ctor)).ToList();
                if (callToCtors.Count == 0)
                    throw new CompilerException(string.Format("No call to another this ctor found in {0}", ctor));
                if (callToCtors.Count > 1)
                    throw new CompilerException(string.Format("Multiple calls to another this ctor found in {0}", ctor));
                var callToCtor = callToCtors[0];

                // Change "ld this" to nop
                var args = callToCtor.GetCallArguments(seq, true);
                args[0].ChangeToNop(); // Replace ldarg.0

                // Replace call to this ctor with newobj
                var callSeq = new ILSequence();
                callSeq.Emit(OpCodes.Newobj, (MethodReference) callToCtor.Operand);
                callSeq.Emit(OpCodes.Stloc, thisVariable); // Save new object
                callToCtor.ChangeToNop();
                callSeq.InsertToBefore(callToCtor, seq);

                // Replace ret instructions
                var end = seq.Emit(OpCodes.Ldloc, thisVariable);
                var retInstructions = seq.Where(x => x.OpCode.Code == Mono.Cecil.Cil.Code.Ret).ToList();
                foreach (var ins in retInstructions)
                {
                    ins.OpCode = OpCodes.Br;
                    ins.Operand = end;
                }

                // Insert cloned instructions
                prefixSeq.InsertTo(0, seq);
                seq.InsertToAfter(instruction, body);

                // Change replaced instruction to nop
                instruction.ChangeToNop();

                // Update offsets
                body.ComputeOffsets();
            }