diff --git a/src/CodeDomBuilder.cs b/src/CodeDomBuilder.cs index b427cb120..443c904f4 100644 --- a/src/CodeDomBuilder.cs +++ b/src/CodeDomBuilder.cs @@ -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 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 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; + } } } diff --git a/src/MainForm.Designer.cs b/src/MainForm.Designer.cs index af33459d4..70fe75762 100644 --- a/src/MainForm.Designer.cs +++ b/src/MainForm.Designer.cs @@ -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 // diff --git a/src/Program.cs b/src/Program.cs index e7e71b2fb..dc69bfe76 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -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);