mirror of https://github.com/icsharpcode/ILSpy.git
11 changed files with 492 additions and 676 deletions
@ -0,0 +1,423 @@
@@ -0,0 +1,423 @@
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2010 Jb Evain
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
using System; |
||||
using Mono.Cecil; |
||||
using Mono.Cecil.Cil; |
||||
|
||||
namespace Decompiler |
||||
{ |
||||
public enum ILCode |
||||
{ |
||||
// For convenience, the start is exactly identical to Mono.Cecil.Cil.Code
|
||||
// The macro instructions should never be used and are therefore prepended by __
|
||||
Nop, |
||||
Break, |
||||
__Ldarg_0, |
||||
__Ldarg_1, |
||||
__Ldarg_2, |
||||
__Ldarg_3, |
||||
__Ldloc_0, |
||||
__Ldloc_1, |
||||
__Ldloc_2, |
||||
__Ldloc_3, |
||||
__Stloc_0, |
||||
__Stloc_1, |
||||
__Stloc_2, |
||||
__Stloc_3, |
||||
__Ldarg_S, |
||||
__Ldarga_S, |
||||
__Starg_S, |
||||
__Ldloc_S, |
||||
__Ldloca_S, |
||||
__Stloc_S, |
||||
Ldnull, |
||||
__Ldc_I4_M1, |
||||
__Ldc_I4_0, |
||||
__Ldc_I4_1, |
||||
__Ldc_I4_2, |
||||
__Ldc_I4_3, |
||||
__Ldc_I4_4, |
||||
__Ldc_I4_5, |
||||
__Ldc_I4_6, |
||||
__Ldc_I4_7, |
||||
__Ldc_I4_8, |
||||
__Ldc_I4_S, |
||||
Ldc_I4, |
||||
Ldc_I8, |
||||
Ldc_R4, |
||||
Ldc_R8, |
||||
Dup, |
||||
Pop, |
||||
Jmp, |
||||
Call, |
||||
Calli, |
||||
Ret, |
||||
__Br_S, |
||||
__Brfalse_S, |
||||
__Brtrue_S, |
||||
__Beq_S, |
||||
__Bge_S, |
||||
__Bgt_S, |
||||
__Ble_S, |
||||
__Blt_S, |
||||
__Bne_Un_S, |
||||
__Bge_Un_S, |
||||
__Bgt_Un_S, |
||||
__Ble_Un_S, |
||||
__Blt_Un_S, |
||||
Br, |
||||
Brfalse, |
||||
Brtrue, |
||||
Beq, |
||||
Bge, |
||||
Bgt, |
||||
Ble, |
||||
Blt, |
||||
Bne_Un, |
||||
Bge_Un, |
||||
Bgt_Un, |
||||
Ble_Un, |
||||
Blt_Un, |
||||
Switch, |
||||
Ldind_I1, |
||||
Ldind_U1, |
||||
Ldind_I2, |
||||
Ldind_U2, |
||||
Ldind_I4, |
||||
Ldind_U4, |
||||
Ldind_I8, |
||||
Ldind_I, |
||||
Ldind_R4, |
||||
Ldind_R8, |
||||
Ldind_Ref, |
||||
Stind_Ref, |
||||
Stind_I1, |
||||
Stind_I2, |
||||
Stind_I4, |
||||
Stind_I8, |
||||
Stind_R4, |
||||
Stind_R8, |
||||
Add, |
||||
Sub, |
||||
Mul, |
||||
Div, |
||||
Div_Un, |
||||
Rem, |
||||
Rem_Un, |
||||
And, |
||||
Or, |
||||
Xor, |
||||
Shl, |
||||
Shr, |
||||
Shr_Un, |
||||
Neg, |
||||
Not, |
||||
Conv_I1, |
||||
Conv_I2, |
||||
Conv_I4, |
||||
Conv_I8, |
||||
Conv_R4, |
||||
Conv_R8, |
||||
Conv_U4, |
||||
Conv_U8, |
||||
Callvirt, |
||||
Cpobj, |
||||
Ldobj, |
||||
Ldstr, |
||||
Newobj, |
||||
Castclass, |
||||
Isinst, |
||||
Conv_R_Un, |
||||
Unbox, |
||||
Throw, |
||||
Ldfld, |
||||
Ldflda, |
||||
Stfld, |
||||
Ldsfld, |
||||
Ldsflda, |
||||
Stsfld, |
||||
Stobj, |
||||
Conv_Ovf_I1_Un, |
||||
Conv_Ovf_I2_Un, |
||||
Conv_Ovf_I4_Un, |
||||
Conv_Ovf_I8_Un, |
||||
Conv_Ovf_U1_Un, |
||||
Conv_Ovf_U2_Un, |
||||
Conv_Ovf_U4_Un, |
||||
Conv_Ovf_U8_Un, |
||||
Conv_Ovf_I_Un, |
||||
Conv_Ovf_U_Un, |
||||
Box, |
||||
Newarr, |
||||
Ldlen, |
||||
Ldelema, |
||||
Ldelem_I1, |
||||
Ldelem_U1, |
||||
Ldelem_I2, |
||||
Ldelem_U2, |
||||
Ldelem_I4, |
||||
Ldelem_U4, |
||||
Ldelem_I8, |
||||
Ldelem_I, |
||||
Ldelem_R4, |
||||
Ldelem_R8, |
||||
Ldelem_Ref, |
||||
Stelem_I, |
||||
Stelem_I1, |
||||
Stelem_I2, |
||||
Stelem_I4, |
||||
Stelem_I8, |
||||
Stelem_R4, |
||||
Stelem_R8, |
||||
Stelem_Ref, |
||||
Ldelem_Any, |
||||
Stelem_Any, |
||||
Unbox_Any, |
||||
Conv_Ovf_I1, |
||||
Conv_Ovf_U1, |
||||
Conv_Ovf_I2, |
||||
Conv_Ovf_U2, |
||||
Conv_Ovf_I4, |
||||
Conv_Ovf_U4, |
||||
Conv_Ovf_I8, |
||||
Conv_Ovf_U8, |
||||
Refanyval, |
||||
Ckfinite, |
||||
Mkrefany, |
||||
Ldtoken, |
||||
Conv_U2, |
||||
Conv_U1, |
||||
Conv_I, |
||||
Conv_Ovf_I, |
||||
Conv_Ovf_U, |
||||
Add_Ovf, |
||||
Add_Ovf_Un, |
||||
Mul_Ovf, |
||||
Mul_Ovf_Un, |
||||
Sub_Ovf, |
||||
Sub_Ovf_Un, |
||||
Endfinally, |
||||
Leave, |
||||
__Leave_S, |
||||
Stind_I, |
||||
Conv_U, |
||||
Arglist, |
||||
Ceq, |
||||
Cgt, |
||||
Cgt_Un, |
||||
Clt, |
||||
Clt_Un, |
||||
Ldftn, |
||||
Ldvirtftn, |
||||
Ldarg, |
||||
Ldarga, |
||||
Starg, |
||||
Ldloc, |
||||
Ldloca, |
||||
Stloc, |
||||
Localloc, |
||||
Endfilter, |
||||
Unaligned, |
||||
Volatile, |
||||
Tail, |
||||
Initobj, |
||||
Constrained, |
||||
Cpblk, |
||||
Initblk, |
||||
No, |
||||
Rethrow, |
||||
Sizeof, |
||||
Refanytype, |
||||
Readonly, |
||||
|
||||
// Virtual codes - defined for convenience
|
||||
} |
||||
|
||||
public static class ILCodeUtil |
||||
{ |
||||
public static string GetName(this ILCode code) |
||||
{ |
||||
return code.ToString().ToLowerInvariant().Replace('_','.'); |
||||
} |
||||
|
||||
public static bool CanFallThough(this ILCode code) |
||||
{ |
||||
switch(code) { |
||||
case ILCode.Br: |
||||
case ILCode.__Br_S: |
||||
case ILCode.Leave: |
||||
case ILCode.__Leave_S: |
||||
case ILCode.Ret: |
||||
case ILCode.Endfilter: |
||||
case ILCode.Endfinally: |
||||
case ILCode.Throw: |
||||
case ILCode.Rethrow: |
||||
return false; |
||||
default: |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
public static int? GetPopCount(this Instruction inst) |
||||
{ |
||||
switch(inst.OpCode.StackBehaviourPop) { |
||||
case StackBehaviour.Pop0: return 0; |
||||
case StackBehaviour.Pop1: return 1; |
||||
case StackBehaviour.Popi: return 1; |
||||
case StackBehaviour.Popref: return 1; |
||||
case StackBehaviour.Pop1_pop1: return 2; |
||||
case StackBehaviour.Popi_pop1: return 2; |
||||
case StackBehaviour.Popi_popi: return 2; |
||||
case StackBehaviour.Popi_popi8: return 2; |
||||
case StackBehaviour.Popi_popr4: return 2; |
||||
case StackBehaviour.Popi_popr8: return 2; |
||||
case StackBehaviour.Popref_pop1: return 2; |
||||
case StackBehaviour.Popref_popi: return 2; |
||||
case StackBehaviour.Popi_popi_popi: return 3; |
||||
case StackBehaviour.Popref_popi_popi: return 3; |
||||
case StackBehaviour.Popref_popi_popi8: return 3; |
||||
case StackBehaviour.Popref_popi_popr4: return 3; |
||||
case StackBehaviour.Popref_popi_popr8: return 3; |
||||
case StackBehaviour.Popref_popi_popref: return 3; |
||||
case StackBehaviour.PopAll: return null; |
||||
case StackBehaviour.Varpop: |
||||
switch(inst.OpCode.Code) { |
||||
case Code.Call: |
||||
case Code.Callvirt: |
||||
MethodReference cecilMethod = ((MethodReference)inst.Operand); |
||||
if (cecilMethod.HasThis) { |
||||
return cecilMethod.Parameters.Count + 1 /* this */; |
||||
} else { |
||||
return cecilMethod.Parameters.Count; |
||||
} |
||||
case Code.Calli: throw new NotImplementedException(); |
||||
case Code.Ret: return null; |
||||
case Code.Newobj: |
||||
MethodReference ctorMethod = ((MethodReference)inst.Operand); |
||||
return ctorMethod.Parameters.Count; |
||||
default: throw new Exception("Unknown Varpop opcode"); |
||||
} |
||||
default: throw new Exception("Unknown pop behaviour: " + inst.OpCode.StackBehaviourPop); |
||||
} |
||||
} |
||||
|
||||
public static int GetPushCount(this Instruction inst) |
||||
{ |
||||
switch(inst.OpCode.StackBehaviourPush) { |
||||
case StackBehaviour.Push0: return 0; |
||||
case StackBehaviour.Push1: return 1; |
||||
case StackBehaviour.Push1_push1: return 2; |
||||
case StackBehaviour.Pushi: return 1; |
||||
case StackBehaviour.Pushi8: return 1; |
||||
case StackBehaviour.Pushr4: return 1; |
||||
case StackBehaviour.Pushr8: return 1; |
||||
case StackBehaviour.Pushref: return 1; |
||||
case StackBehaviour.Varpush: // Happens only for calls
|
||||
switch(inst.OpCode.Code) { |
||||
case Code.Call: |
||||
case Code.Callvirt: |
||||
MethodReference cecilMethod = ((MethodReference)inst.Operand); |
||||
if (cecilMethod.ReturnType.FullName == "System.Void") { |
||||
return 0; |
||||
} else { |
||||
return 1; |
||||
} |
||||
case Code.Calli: throw new NotImplementedException(); |
||||
default: throw new Exception("Unknown Varpush opcode"); |
||||
} |
||||
default: throw new Exception("Unknown push behaviour: " + inst.OpCode.StackBehaviourPush); |
||||
} |
||||
} |
||||
|
||||
public static void ExpandMacro(ref ILCode code, ref object operand, MethodBody methodBody) |
||||
{ |
||||
switch (code) { |
||||
case ILCode.__Ldarg_0: code = ILCode.Ldarg; operand = methodBody.GetParameter(0); break; |
||||
case ILCode.__Ldarg_1: code = ILCode.Ldarg; operand = methodBody.GetParameter(1); break; |
||||
case ILCode.__Ldarg_2: code = ILCode.Ldarg; operand = methodBody.GetParameter(2); break; |
||||
case ILCode.__Ldarg_3: code = ILCode.Ldarg; operand = methodBody.GetParameter(3); break; |
||||
case ILCode.__Ldloc_0: code = ILCode.Ldloc; operand = methodBody.Variables[0]; break; |
||||
case ILCode.__Ldloc_1: code = ILCode.Ldloc; operand = methodBody.Variables[1]; break; |
||||
case ILCode.__Ldloc_2: code = ILCode.Ldloc; operand = methodBody.Variables[2]; break; |
||||
case ILCode.__Ldloc_3: code = ILCode.Ldloc; operand = methodBody.Variables[3]; break; |
||||
case ILCode.__Stloc_0: code = ILCode.Stloc; operand = methodBody.Variables[0]; break; |
||||
case ILCode.__Stloc_1: code = ILCode.Stloc; operand = methodBody.Variables[1]; break; |
||||
case ILCode.__Stloc_2: code = ILCode.Stloc; operand = methodBody.Variables[2]; break; |
||||
case ILCode.__Stloc_3: code = ILCode.Stloc; operand = methodBody.Variables[3]; break; |
||||
case ILCode.__Ldarg_S: code = ILCode.Ldarg; break; |
||||
case ILCode.__Ldarga_S: code = ILCode.Ldarga; break; |
||||
case ILCode.__Starg_S: code = ILCode.Starg; break; |
||||
case ILCode.__Ldloc_S: code = ILCode.Ldloc; break; |
||||
case ILCode.__Ldloca_S: code = ILCode.Ldloca; break; |
||||
case ILCode.__Stloc_S: code = ILCode.Stloc; break; |
||||
case ILCode.__Ldc_I4_M1: code = ILCode.Ldc_I4; operand = -1; break; |
||||
case ILCode.__Ldc_I4_0: code = ILCode.Ldc_I4; operand = 0; break; |
||||
case ILCode.__Ldc_I4_1: code = ILCode.Ldc_I4; operand = 1; break; |
||||
case ILCode.__Ldc_I4_2: code = ILCode.Ldc_I4; operand = 2; break; |
||||
case ILCode.__Ldc_I4_3: code = ILCode.Ldc_I4; operand = 3; break; |
||||
case ILCode.__Ldc_I4_4: code = ILCode.Ldc_I4; operand = 4; break; |
||||
case ILCode.__Ldc_I4_5: code = ILCode.Ldc_I4; operand = 5; break; |
||||
case ILCode.__Ldc_I4_6: code = ILCode.Ldc_I4; operand = 6; break; |
||||
case ILCode.__Ldc_I4_7: code = ILCode.Ldc_I4; operand = 7; break; |
||||
case ILCode.__Ldc_I4_8: code = ILCode.Ldc_I4; operand = 8; break; |
||||
case ILCode.__Ldc_I4_S: code = ILCode.Ldc_I4; operand = (int) (sbyte) operand; break; |
||||
case ILCode.__Br_S: code = ILCode.Br; break; |
||||
case ILCode.__Brfalse_S: code = ILCode.Brfalse; break; |
||||
case ILCode.__Brtrue_S: code = ILCode.Brtrue; break; |
||||
case ILCode.__Beq_S: code = ILCode.Beq; break; |
||||
case ILCode.__Bge_S: code = ILCode.Bge; break; |
||||
case ILCode.__Bgt_S: code = ILCode.Bgt; break; |
||||
case ILCode.__Ble_S: code = ILCode.Ble; break; |
||||
case ILCode.__Blt_S: code = ILCode.Blt; break; |
||||
case ILCode.__Bne_Un_S: code = ILCode.Bne_Un; break; |
||||
case ILCode.__Bge_Un_S: code = ILCode.Bge_Un; break; |
||||
case ILCode.__Bgt_Un_S: code = ILCode.Bgt_Un; break; |
||||
case ILCode.__Ble_Un_S: code = ILCode.Ble_Un; break; |
||||
case ILCode.__Blt_Un_S: code = ILCode.Blt_Un; break; |
||||
case ILCode.__Leave_S: code = ILCode.Leave; break; |
||||
} |
||||
} |
||||
|
||||
public static ParameterDefinition GetParameter (this MethodBody self, int index) |
||||
{ |
||||
var method = self.Method; |
||||
|
||||
if (method.HasThis) { |
||||
if (index == 0) |
||||
return self.ThisParameter; |
||||
|
||||
index--; |
||||
} |
||||
|
||||
var parameters = method.Parameters; |
||||
|
||||
if (index < 0 || index >= parameters.Count) |
||||
return null; |
||||
|
||||
return parameters [index]; |
||||
} |
||||
} |
||||
} |
@ -1,15 +0,0 @@
@@ -1,15 +0,0 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
|
||||
namespace Decompiler |
||||
{ |
||||
public class Constants |
||||
{ |
||||
public const string Object = "System.Object"; |
||||
public const string Int32 = "System.Int32"; |
||||
public const string Boolean = "System.Boolean"; |
||||
public const string Void = "System.Void"; |
||||
} |
||||
} |
@ -1,478 +0,0 @@
@@ -1,478 +0,0 @@
|
||||
//
|
||||
// MethodBodyRocks.cs
|
||||
//
|
||||
// Author:
|
||||
// Jb Evain (jbevain@gmail.com)
|
||||
//
|
||||
// Copyright (c) 2008 - 2010 Jb Evain
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
using System; |
||||
|
||||
using Mono.Cecil.Cil; |
||||
|
||||
namespace Mono.Cecil.Rocks { |
||||
|
||||
#if INSIDE_ROCKS
|
||||
public |
||||
#endif
|
||||
static class MethodBodyRocks { |
||||
|
||||
public static ParameterDefinition GetParameter (this MethodBody self, int index) |
||||
{ |
||||
var method = self.Method; |
||||
|
||||
if (method.HasThis) { |
||||
if (index == 0) |
||||
return self.ThisParameter; |
||||
|
||||
index--; |
||||
} |
||||
|
||||
var parameters = method.Parameters; |
||||
|
||||
if (index < 0 || index >= parameters.Count) |
||||
return null; |
||||
|
||||
return parameters [index]; |
||||
} |
||||
|
||||
public static void SimplifyMacros (this MethodBody self) |
||||
{ |
||||
if (self == null) |
||||
throw new ArgumentNullException ("self"); |
||||
|
||||
foreach (var instruction in self.Instructions) { |
||||
if (instruction.OpCode.OpCodeType != OpCodeType.Macro) |
||||
continue; |
||||
|
||||
switch (instruction.OpCode.Code) { |
||||
case Code.Ldarg_0: |
||||
ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (0)); |
||||
break; |
||||
case Code.Ldarg_1: |
||||
ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (1)); |
||||
break; |
||||
case Code.Ldarg_2: |
||||
ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (2)); |
||||
break; |
||||
case Code.Ldarg_3: |
||||
ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (3)); |
||||
break; |
||||
case Code.Ldloc_0: |
||||
ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [0]); |
||||
break; |
||||
case Code.Ldloc_1: |
||||
ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [1]); |
||||
break; |
||||
case Code.Ldloc_2: |
||||
ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [2]); |
||||
break; |
||||
case Code.Ldloc_3: |
||||
ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [3]); |
||||
break; |
||||
case Code.Stloc_0: |
||||
ExpandMacro (instruction, OpCodes.Stloc, self.Variables [0]); |
||||
break; |
||||
case Code.Stloc_1: |
||||
ExpandMacro (instruction, OpCodes.Stloc, self.Variables [1]); |
||||
break; |
||||
case Code.Stloc_2: |
||||
ExpandMacro (instruction, OpCodes.Stloc, self.Variables [2]); |
||||
break; |
||||
case Code.Stloc_3: |
||||
ExpandMacro (instruction, OpCodes.Stloc, self.Variables [3]); |
||||
break; |
||||
case Code.Ldarg_S: |
||||
instruction.OpCode = OpCodes.Ldarg; |
||||
break; |
||||
case Code.Ldarga_S: |
||||
instruction.OpCode = OpCodes.Ldarga; |
||||
break; |
||||
case Code.Starg_S: |
||||
instruction.OpCode = OpCodes.Starg; |
||||
break; |
||||
case Code.Ldloc_S: |
||||
instruction.OpCode = OpCodes.Ldloc; |
||||
break; |
||||
case Code.Ldloca_S: |
||||
instruction.OpCode = OpCodes.Ldloca; |
||||
break; |
||||
case Code.Stloc_S: |
||||
instruction.OpCode = OpCodes.Stloc; |
||||
break; |
||||
case Code.Ldc_I4_M1: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, -1); |
||||
break; |
||||
case Code.Ldc_I4_0: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, 0); |
||||
break; |
||||
case Code.Ldc_I4_1: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, 1); |
||||
break; |
||||
case Code.Ldc_I4_2: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, 2); |
||||
break; |
||||
case Code.Ldc_I4_3: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, 3); |
||||
break; |
||||
case Code.Ldc_I4_4: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, 4); |
||||
break; |
||||
case Code.Ldc_I4_5: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, 5); |
||||
break; |
||||
case Code.Ldc_I4_6: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, 6); |
||||
break; |
||||
case Code.Ldc_I4_7: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, 7); |
||||
break; |
||||
case Code.Ldc_I4_8: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, 8); |
||||
break; |
||||
case Code.Ldc_I4_S: |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4, (int) (sbyte) instruction.Operand); |
||||
break; |
||||
case Code.Br_S: |
||||
instruction.OpCode = OpCodes.Br; |
||||
break; |
||||
case Code.Brfalse_S: |
||||
instruction.OpCode = OpCodes.Brfalse; |
||||
break; |
||||
case Code.Brtrue_S: |
||||
instruction.OpCode = OpCodes.Brtrue; |
||||
break; |
||||
case Code.Beq_S: |
||||
instruction.OpCode = OpCodes.Beq; |
||||
break; |
||||
case Code.Bge_S: |
||||
instruction.OpCode = OpCodes.Bge; |
||||
break; |
||||
case Code.Bgt_S: |
||||
instruction.OpCode = OpCodes.Bgt; |
||||
break; |
||||
case Code.Ble_S: |
||||
instruction.OpCode = OpCodes.Ble; |
||||
break; |
||||
case Code.Blt_S: |
||||
instruction.OpCode = OpCodes.Blt; |
||||
break; |
||||
case Code.Bne_Un_S: |
||||
instruction.OpCode = OpCodes.Bne_Un; |
||||
break; |
||||
case Code.Bge_Un_S: |
||||
instruction.OpCode = OpCodes.Bge_Un; |
||||
break; |
||||
case Code.Bgt_Un_S: |
||||
instruction.OpCode = OpCodes.Bgt_Un; |
||||
break; |
||||
case Code.Ble_Un_S: |
||||
instruction.OpCode = OpCodes.Ble_Un; |
||||
break; |
||||
case Code.Blt_Un_S: |
||||
instruction.OpCode = OpCodes.Blt_Un; |
||||
break; |
||||
case Code.Leave_S: |
||||
instruction.OpCode = OpCodes.Leave; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static void ExpandMacro(ref OpCode opCode, ref object operand, MethodBody methodBody) |
||||
{ |
||||
if (opCode.OpCodeType != OpCodeType.Macro) |
||||
return; |
||||
|
||||
switch (opCode.Code) { |
||||
case Code.Ldarg_0: opCode = OpCodes.Ldarg; operand = methodBody.GetParameter(0); break; |
||||
case Code.Ldarg_1: opCode = OpCodes.Ldarg; operand = methodBody.GetParameter(1); break; |
||||
case Code.Ldarg_2: opCode = OpCodes.Ldarg; operand = methodBody.GetParameter(2); break; |
||||
case Code.Ldarg_3: opCode = OpCodes.Ldarg; operand = methodBody.GetParameter(3); break; |
||||
case Code.Ldloc_0: opCode = OpCodes.Ldloc; operand = methodBody.Variables[0]; break; |
||||
case Code.Ldloc_1: opCode = OpCodes.Ldloc; operand = methodBody.Variables[1]; break; |
||||
case Code.Ldloc_2: opCode = OpCodes.Ldloc; operand = methodBody.Variables[2]; break; |
||||
case Code.Ldloc_3: opCode = OpCodes.Ldloc; operand = methodBody.Variables[3]; break; |
||||
case Code.Stloc_0: opCode = OpCodes.Stloc; operand = methodBody.Variables[0]; break; |
||||
case Code.Stloc_1: opCode = OpCodes.Stloc; operand = methodBody.Variables[1]; break; |
||||
case Code.Stloc_2: opCode = OpCodes.Stloc; operand = methodBody.Variables[2]; break; |
||||
case Code.Stloc_3: opCode = OpCodes.Stloc; operand = methodBody.Variables[3]; break; |
||||
case Code.Ldarg_S: opCode = OpCodes.Ldarg; break; |
||||
case Code.Ldarga_S: opCode = OpCodes.Ldarga; break; |
||||
case Code.Starg_S: opCode = OpCodes.Starg; break; |
||||
case Code.Ldloc_S: opCode = OpCodes.Ldloc; break; |
||||
case Code.Ldloca_S: opCode = OpCodes.Ldloca; break; |
||||
case Code.Stloc_S: opCode = OpCodes.Stloc; break; |
||||
case Code.Ldc_I4_M1: opCode = OpCodes.Ldc_I4; operand = -1; break; |
||||
case Code.Ldc_I4_0: opCode = OpCodes.Ldc_I4; operand = 0; break; |
||||
case Code.Ldc_I4_1: opCode = OpCodes.Ldc_I4; operand = 1; break; |
||||
case Code.Ldc_I4_2: opCode = OpCodes.Ldc_I4; operand = 2; break; |
||||
case Code.Ldc_I4_3: opCode = OpCodes.Ldc_I4; operand = 3; break; |
||||
case Code.Ldc_I4_4: opCode = OpCodes.Ldc_I4; operand = 4; break; |
||||
case Code.Ldc_I4_5: opCode = OpCodes.Ldc_I4; operand = 5; break; |
||||
case Code.Ldc_I4_6: opCode = OpCodes.Ldc_I4; operand = 6; break; |
||||
case Code.Ldc_I4_7: opCode = OpCodes.Ldc_I4; operand = 7; break; |
||||
case Code.Ldc_I4_8: opCode = OpCodes.Ldc_I4; operand = 8; break; |
||||
case Code.Ldc_I4_S: opCode = OpCodes.Ldc_I4; operand = (int) (sbyte) operand; break; |
||||
case Code.Br_S: opCode = OpCodes.Br; break; |
||||
case Code.Brfalse_S: opCode = OpCodes.Brfalse; break; |
||||
case Code.Brtrue_S: opCode = OpCodes.Brtrue; break; |
||||
case Code.Beq_S: opCode = OpCodes.Beq; break; |
||||
case Code.Bge_S: opCode = OpCodes.Bge; break; |
||||
case Code.Bgt_S: opCode = OpCodes.Bgt; break; |
||||
case Code.Ble_S: opCode = OpCodes.Ble; break; |
||||
case Code.Blt_S: opCode = OpCodes.Blt; break; |
||||
case Code.Bne_Un_S: opCode = OpCodes.Bne_Un; break; |
||||
case Code.Bge_Un_S: opCode = OpCodes.Bge_Un; break; |
||||
case Code.Bgt_Un_S: opCode = OpCodes.Bgt_Un; break; |
||||
case Code.Ble_Un_S: opCode = OpCodes.Ble_Un; break; |
||||
case Code.Blt_Un_S: opCode = OpCodes.Blt_Un; break; |
||||
case Code.Leave_S: opCode = OpCodes.Leave; break; |
||||
} |
||||
} |
||||
|
||||
static void ExpandMacro (Instruction instruction, OpCode opcode, object operand) |
||||
{ |
||||
instruction.OpCode = opcode; |
||||
instruction.Operand = operand; |
||||
} |
||||
|
||||
static void MakeMacro (Instruction instruction, OpCode opcode) |
||||
{ |
||||
instruction.OpCode = opcode; |
||||
instruction.Operand = null; |
||||
} |
||||
|
||||
public static void OptimizeMacros (this MethodBody self) |
||||
{ |
||||
if (self == null) |
||||
throw new ArgumentNullException ("self"); |
||||
|
||||
var method = self.Method; |
||||
|
||||
foreach (var instruction in self.Instructions) { |
||||
int index; |
||||
switch (instruction.OpCode.Code) { |
||||
case Code.Ldarg: |
||||
index = ((ParameterDefinition) instruction.Operand).Index; |
||||
if (index == -1 && instruction.Operand == self.ThisParameter) |
||||
index = 0; |
||||
else if (method.HasThis) |
||||
index++; |
||||
|
||||
switch (index) { |
||||
case 0: |
||||
MakeMacro (instruction, OpCodes.Ldarg_0); |
||||
break; |
||||
case 1: |
||||
MakeMacro (instruction, OpCodes.Ldarg_1); |
||||
break; |
||||
case 2: |
||||
MakeMacro (instruction, OpCodes.Ldarg_2); |
||||
break; |
||||
case 3: |
||||
MakeMacro (instruction, OpCodes.Ldarg_3); |
||||
break; |
||||
default: |
||||
if (index < 256) |
||||
ExpandMacro (instruction, OpCodes.Ldarg_S, instruction.Operand); |
||||
break; |
||||
} |
||||
break; |
||||
case Code.Ldloc: |
||||
index = ((VariableDefinition) instruction.Operand).Index; |
||||
switch (index) { |
||||
case 0: |
||||
MakeMacro (instruction, OpCodes.Ldloc_0); |
||||
break; |
||||
case 1: |
||||
MakeMacro (instruction, OpCodes.Ldloc_1); |
||||
break; |
||||
case 2: |
||||
MakeMacro (instruction, OpCodes.Ldloc_2); |
||||
break; |
||||
case 3: |
||||
MakeMacro (instruction, OpCodes.Ldloc_3); |
||||
break; |
||||
default: |
||||
if (index < 256) |
||||
ExpandMacro (instruction, OpCodes.Ldloc_S, instruction.Operand); |
||||
break; |
||||
} |
||||
break; |
||||
case Code.Stloc: |
||||
index = ((VariableDefinition) instruction.Operand).Index; |
||||
switch (index) { |
||||
case 0: |
||||
MakeMacro (instruction, OpCodes.Stloc_0); |
||||
break; |
||||
case 1: |
||||
MakeMacro (instruction, OpCodes.Stloc_1); |
||||
break; |
||||
case 2: |
||||
MakeMacro (instruction, OpCodes.Stloc_2); |
||||
break; |
||||
case 3: |
||||
MakeMacro (instruction, OpCodes.Stloc_3); |
||||
break; |
||||
default: |
||||
if (index < 256) |
||||
ExpandMacro (instruction, OpCodes.Stloc_S, instruction.Operand); |
||||
break; |
||||
} |
||||
break; |
||||
case Code.Ldarga: |
||||
index = ((ParameterDefinition) instruction.Operand).Index; |
||||
if (index == -1 && instruction.Operand == self.ThisParameter) |
||||
index = 0; |
||||
else if (method.HasThis) |
||||
index++; |
||||
if (index < 256) |
||||
ExpandMacro (instruction, OpCodes.Ldarga_S, instruction.Operand); |
||||
break; |
||||
case Code.Ldloca: |
||||
if (((VariableDefinition) instruction.Operand).Index < 256) |
||||
ExpandMacro (instruction, OpCodes.Ldloca_S, instruction.Operand); |
||||
break; |
||||
case Code.Ldc_I4: |
||||
int i = (int) instruction.Operand; |
||||
switch (i) { |
||||
case -1: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_M1); |
||||
break; |
||||
case 0: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_0); |
||||
break; |
||||
case 1: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_1); |
||||
break; |
||||
case 2: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_2); |
||||
break; |
||||
case 3: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_3); |
||||
break; |
||||
case 4: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_4); |
||||
break; |
||||
case 5: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_5); |
||||
break; |
||||
case 6: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_6); |
||||
break; |
||||
case 7: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_7); |
||||
break; |
||||
case 8: |
||||
MakeMacro (instruction, OpCodes.Ldc_I4_8); |
||||
break; |
||||
default: |
||||
if (i >= -128 && i < 128) |
||||
ExpandMacro (instruction, OpCodes.Ldc_I4_S, (sbyte) i); |
||||
break; |
||||
} |
||||
break; |
||||
} |
||||
} |
||||
|
||||
OptimizeBranches (self); |
||||
} |
||||
|
||||
static void OptimizeBranches (MethodBody body) |
||||
{ |
||||
ComputeOffsets (body); |
||||
|
||||
foreach (var instruction in body.Instructions) { |
||||
if (instruction.OpCode.OperandType != OperandType.InlineBrTarget) |
||||
continue; |
||||
|
||||
if (OptimizeBranch (instruction)) |
||||
ComputeOffsets (body); |
||||
} |
||||
} |
||||
|
||||
static bool OptimizeBranch (Instruction instruction) |
||||
{ |
||||
var offset = ((Instruction) instruction.Operand).Offset - (instruction.Offset + instruction.OpCode.Size + 4); |
||||
if (!(offset >= -128 && offset <= 127)) |
||||
return false; |
||||
|
||||
switch (instruction.OpCode.Code) { |
||||
case Code.Br: |
||||
instruction.OpCode = OpCodes.Br_S; |
||||
break; |
||||
case Code.Brfalse: |
||||
instruction.OpCode = OpCodes.Brfalse_S; |
||||
break; |
||||
case Code.Brtrue: |
||||
instruction.OpCode = OpCodes.Brtrue_S; |
||||
break; |
||||
case Code.Beq: |
||||
instruction.OpCode = OpCodes.Beq_S; |
||||
break; |
||||
case Code.Bge: |
||||
instruction.OpCode = OpCodes.Bge_S; |
||||
break; |
||||
case Code.Bgt: |
||||
instruction.OpCode = OpCodes.Bgt_S; |
||||
break; |
||||
case Code.Ble: |
||||
instruction.OpCode = OpCodes.Ble_S; |
||||
break; |
||||
case Code.Blt: |
||||
instruction.OpCode = OpCodes.Blt_S; |
||||
break; |
||||
case Code.Bne_Un: |
||||
instruction.OpCode = OpCodes.Bne_Un_S; |
||||
break; |
||||
case Code.Bge_Un: |
||||
instruction.OpCode = OpCodes.Bge_Un_S; |
||||
break; |
||||
case Code.Bgt_Un: |
||||
instruction.OpCode = OpCodes.Bgt_Un_S; |
||||
break; |
||||
case Code.Ble_Un: |
||||
instruction.OpCode = OpCodes.Ble_Un_S; |
||||
break; |
||||
case Code.Blt_Un: |
||||
instruction.OpCode = OpCodes.Blt_Un_S; |
||||
break; |
||||
case Code.Leave: |
||||
instruction.OpCode = OpCodes.Leave_S; |
||||
break; |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
static void ComputeOffsets (MethodBody body) |
||||
{ |
||||
var offset = 0; |
||||
foreach (var instruction in body.Instructions) { |
||||
instruction.Offset = offset; |
||||
offset += instruction.GetSize (); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,118 +0,0 @@
@@ -1,118 +0,0 @@
|
||||
/* |
||||
* Created by SharpDevelop. |
||||
* User: User |
||||
* Date: 05/02/2011 |
||||
* Time: 10:10 |
||||
* |
||||
* To change this template use Tools | Options | Coding | Edit Standard Headers. |
||||
*/ |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using Mono.Cecil; |
||||
using Mono.Cecil.Cil; |
||||
|
||||
namespace Decompiler.Rocks |
||||
{ |
||||
static class MyRocks |
||||
{ |
||||
public static List<T> CutRange<T>(this List<T> list, int start, int count) |
||||
{ |
||||
List<T> ret = new List<T>(count); |
||||
for (int i = 0; i < count; i++) { |
||||
ret.Add(list[start + i]); |
||||
} |
||||
list.RemoveRange(start, count); |
||||
return ret; |
||||
} |
||||
|
||||
public static bool CanFallThough(this OpCode opCode) |
||||
{ |
||||
switch(opCode.FlowControl) { |
||||
case FlowControl.Branch: return false; |
||||
case FlowControl.Cond_Branch: return true; |
||||
case FlowControl.Next: return true; |
||||
case FlowControl.Call: return true; |
||||
case FlowControl.Return: return false; |
||||
case FlowControl.Throw: return false; |
||||
case FlowControl.Meta: return true; |
||||
default: throw new NotImplementedException(); |
||||
} |
||||
} |
||||
|
||||
public static bool IsBranch(this OpCode opCode) |
||||
{ |
||||
return opCode.FlowControl == FlowControl.Branch || opCode.FlowControl == FlowControl.Cond_Branch; |
||||
} |
||||
|
||||
public static int? GetPopCount(this Instruction inst) |
||||
{ |
||||
switch(inst.OpCode.StackBehaviourPop) { |
||||
case StackBehaviour.Pop0: return 0; |
||||
case StackBehaviour.Pop1: return 1; |
||||
case StackBehaviour.Popi: return 1; |
||||
case StackBehaviour.Popref: return 1; |
||||
case StackBehaviour.Pop1_pop1: return 2; |
||||
case StackBehaviour.Popi_pop1: return 2; |
||||
case StackBehaviour.Popi_popi: return 2; |
||||
case StackBehaviour.Popi_popi8: return 2; |
||||
case StackBehaviour.Popi_popr4: return 2; |
||||
case StackBehaviour.Popi_popr8: return 2; |
||||
case StackBehaviour.Popref_pop1: return 2; |
||||
case StackBehaviour.Popref_popi: return 2; |
||||
case StackBehaviour.Popi_popi_popi: return 3; |
||||
case StackBehaviour.Popref_popi_popi: return 3; |
||||
case StackBehaviour.Popref_popi_popi8: return 3; |
||||
case StackBehaviour.Popref_popi_popr4: return 3; |
||||
case StackBehaviour.Popref_popi_popr8: return 3; |
||||
case StackBehaviour.Popref_popi_popref: return 3; |
||||
case StackBehaviour.PopAll: return null; |
||||
case StackBehaviour.Varpop: |
||||
switch(inst.OpCode.Code) { |
||||
case Code.Call: |
||||
case Code.Callvirt: |
||||
MethodReference cecilMethod = ((MethodReference)inst.Operand); |
||||
if (cecilMethod.HasThis) { |
||||
return cecilMethod.Parameters.Count + 1 /* this */; |
||||
} else { |
||||
return cecilMethod.Parameters.Count; |
||||
} |
||||
case Code.Calli: throw new NotImplementedException(); |
||||
case Code.Ret: return null; |
||||
case Code.Newobj: |
||||
MethodReference ctorMethod = ((MethodReference)inst.Operand); |
||||
return ctorMethod.Parameters.Count; |
||||
default: throw new Exception("Unknown Varpop opcode"); |
||||
} |
||||
default: throw new Exception("Unknown pop behaviour: " + inst.OpCode.StackBehaviourPop); |
||||
} |
||||
} |
||||
|
||||
public static int GetPushCount(this Instruction inst) |
||||
{ |
||||
switch(inst.OpCode.StackBehaviourPush) { |
||||
case StackBehaviour.Push0: return 0; |
||||
case StackBehaviour.Push1: return 1; |
||||
case StackBehaviour.Push1_push1: return 2; |
||||
case StackBehaviour.Pushi: return 1; |
||||
case StackBehaviour.Pushi8: return 1; |
||||
case StackBehaviour.Pushr4: return 1; |
||||
case StackBehaviour.Pushr8: return 1; |
||||
case StackBehaviour.Pushref: return 1; |
||||
case StackBehaviour.Varpush: // Happens only for calls
|
||||
switch(inst.OpCode.Code) { |
||||
case Code.Call: |
||||
case Code.Callvirt: |
||||
MethodReference cecilMethod = ((MethodReference)inst.Operand); |
||||
if (cecilMethod.ReturnType.FullName == Constants.Void) { |
||||
return 0; |
||||
} else { |
||||
return 1; |
||||
} |
||||
case Code.Calli: throw new NotImplementedException(); |
||||
default: throw new Exception("Unknown Varpush opcode"); |
||||
} |
||||
default: throw new Exception("Unknown push behaviour: " + inst.OpCode.StackBehaviourPush); |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue