Browse Source

Disassemble the CIL bytecode and write it into the methods as comments

pull/1/head^2
David Srbecký 18 years ago
parent
commit
3f3885d92e
  1. 51
      src/CodeDomBuilder.cs
  2. 4
      src/MainForm.Designer.cs
  3. 2
      src/Program.cs

51
src/CodeDomBuilder.cs

@ -7,6 +7,7 @@ using System.Windows.Forms; @@ -7,6 +7,7 @@ using System.Windows.Forms;
using Microsoft.CSharp;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace Decompiler
{
@ -25,7 +26,7 @@ namespace Decompiler @@ -25,7 +26,7 @@ namespace Decompiler
options.BlankLinesBetweenMembers = true;
options.BracingStyle = "C";
options.ElseOnClosing = true;
options.IndentString = "\t";
options.IndentString = " ";
options.VerbatimOrder = true;
provider.GenerateCodeFromCompileUnit(codeCompileUnit, stringWriter, options);
@ -184,8 +185,56 @@ namespace Decompiler @@ -184,8 +185,56 @@ namespace Decompiler
codeMethod.Parameters.Add(codeParam);
}
codeMethod.Statements.AddRange(CreateMetodBody(methodDef));
codeType.Members.Add(codeMethod);
}
}
object FormatInstructionOperand(object operand)
{
if (operand == null) {
return string.Empty;
} else if (operand is Instruction) {
return string.Format("IL_{0:X2}", ((Instruction)operand).Offset);
} else if (operand is MethodReference) {
return ((MethodReference)operand).Name + "()";
} else if (operand is TypeReference) {
return ((TypeReference)operand).FullName;
} else if (operand is VariableDefinition) {
return ((VariableDefinition)operand).Name;
} else if (operand is ParameterDefinition) {
return ((ParameterDefinition)operand).Name;
} else if (operand is string) {
return "\"" + operand + "\"";
} else if (operand is int) {
return operand.ToString();
} else {
return "(" + operand.GetType() + ")";
}
}
CodeStatementCollection CreateMetodBody(MethodDefinition methodDef)
{
CodeStatementCollection codeStmtCol = new CodeStatementCollection();
methodDef.Body.Simplify();
foreach(Instruction instr in methodDef.Body.Instructions) {
OpCode opCode = instr.OpCode;
string decription =
string.Format("IL_{0:X2}: {1, -11} {2, -15} # {3}->{4} {5} {6}",
instr.Offset,
opCode,
FormatInstructionOperand(instr.Operand),
opCode.StackBehaviourPop,
opCode.StackBehaviourPush,
opCode.FlowControl == FlowControl.Next ? string.Empty : "Flow=" + opCode.FlowControl,
opCode.OpCodeType == OpCodeType.Macro ? "(macro)" : string.Empty);
codeStmtCol.Add(new CodeCommentStatement(decription));
}
return codeStmtCol;
}
}
}

4
src/MainForm.Designer.cs generated

@ -40,12 +40,14 @@ namespace Decompiler @@ -40,12 +40,14 @@ namespace Decompiler
this.sourceCode.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.sourceCode.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.sourceCode.Location = new System.Drawing.Point(12, 12);
this.sourceCode.Multiline = true;
this.sourceCode.Name = "sourceCode";
this.sourceCode.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.sourceCode.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.sourceCode.Size = new System.Drawing.Size(736, 665);
this.sourceCode.TabIndex = 0;
this.sourceCode.WordWrap = false;
//
// MainForm
//

2
src/Program.cs

@ -21,7 +21,7 @@ namespace Decompiler @@ -21,7 +21,7 @@ namespace Decompiler
[STAThread]
private static void Main(string[] args)
{
string sourceCode = Decompile(@"..\..\tests\ClassMembers\bin\Debug\ClassMembers.dll");
string sourceCode = Decompile(@"..\..\tests\QuickSort\bin\Debug\QuickSort.exe");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Loading…
Cancel
Save