diff --git a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs index 6ebcfae52..3dfc1dcc4 100644 --- a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs @@ -160,6 +160,8 @@ namespace Decompiler yield return tryCatchStmt; } else if (node is ILBlock) { yield return TransformBlock((ILBlock)node); + } else if (node is ILComment) { + yield return new CommentStatement(((ILComment)node).Text).WithAnnotation(((ILComment)node).ILRanges); } else { throw new Exception("Unknown node type"); } diff --git a/ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs b/ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs index 5dae13326..e361b3202 100644 --- a/ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs +++ b/ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs @@ -57,7 +57,17 @@ namespace Decompiler public override string ToString() { StringBuilder sb = new StringBuilder(); - sb.AppendFormat("{0}:{1} {2} {3}", this.Name, this.Label != null ? " *" : "", this.Code.GetName(), this.Operand); + sb.AppendFormat("{0}:{1} {2} ", this.Name, this.Label != null ? " *" : "", this.Code.GetName()); + if (this.Operand is ILLabel) { + sb.Append(((ILLabel)this.Operand).Name); + } else if (this.Operand is ILLabel[]) { + foreach(ILLabel label in (ILLabel[])this.Operand) { + sb.Append(label.Name); + sb.Append(" "); + } + } else { + sb.Append(this.Operand.ToString()); + } if (this.StackBefore != null) { sb.Append(" StackBefore = {"); bool first = true; @@ -234,6 +244,9 @@ namespace Decompiler // Genertate temporary variables to replace stack foreach(ByteCode byteCode in body) { + if (byteCode.StackBefore == null) + continue; + int argIdx = 0; int popCount = byteCode.PopCount ?? byteCode.StackBefore.Count; for (int i = byteCode.StackBefore.Count - popCount; i < byteCode.StackBefore.Count; i++) { @@ -395,8 +408,18 @@ namespace Decompiler // Convert stack-based IL code to ILAst tree foreach(ByteCode byteCode in body) { + ILRange ilRange = new ILRange() { From = byteCode.Offset, To = byteCode.EndOffset }; + + if (byteCode.StackBefore == null) { + ast.Add(new ILComment() { + Text = "Unreachable code: " + byteCode.Code.GetName(), + ILRanges = new List(new[] { ilRange }) + }); + continue; + } + ILExpression expr = new ILExpression(byteCode.Code, byteCode.Operand); - expr.ILRanges.Add(new ILRange() { From = byteCode.Offset, To = byteCode.EndOffset }); + expr.ILRanges.Add(ilRange); // Label for this instruction if (byteCode.Label != null) { diff --git a/ICSharpCode.Decompiler/ILAst/ILAstTypes.cs b/ICSharpCode.Decompiler/ILAst/ILAstTypes.cs index 143548b6d..a7c8e297b 100644 --- a/ICSharpCode.Decompiler/ILAst/ILAstTypes.cs +++ b/ICSharpCode.Decompiler/ILAst/ILAstTypes.cs @@ -83,6 +83,17 @@ namespace Decompiler } } + public class ILComment: ILNode + { + public string Text; + public List ILRanges { get; set; } + + public override void WriteTo(ITextOutput output) + { + output.WriteLine("// " + this.Text); + } + } + public class ILTryCatchBlock: ILNode { public class CatchBlock: ILBlock