Browse Source

Handle unreachable code

pull/37/head
David Srbecký 15 years ago
parent
commit
adfc7fc3e4
  1. 2
      ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs
  2. 27
      ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs
  3. 11
      ICSharpCode.Decompiler/ILAst/ILAstTypes.cs

2
ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs

@ -160,6 +160,8 @@ namespace Decompiler
yield return tryCatchStmt; yield return tryCatchStmt;
} else if (node is ILBlock) { } else if (node is ILBlock) {
yield return TransformBlock((ILBlock)node); yield return TransformBlock((ILBlock)node);
} else if (node is ILComment) {
yield return new CommentStatement(((ILComment)node).Text).WithAnnotation(((ILComment)node).ILRanges);
} else { } else {
throw new Exception("Unknown node type"); throw new Exception("Unknown node type");
} }

27
ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs

@ -57,7 +57,17 @@ namespace Decompiler
public override string ToString() public override string ToString()
{ {
StringBuilder sb = new StringBuilder(); 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) { if (this.StackBefore != null) {
sb.Append(" StackBefore = {"); sb.Append(" StackBefore = {");
bool first = true; bool first = true;
@ -234,6 +244,9 @@ namespace Decompiler
// Genertate temporary variables to replace stack // Genertate temporary variables to replace stack
foreach(ByteCode byteCode in body) { foreach(ByteCode byteCode in body) {
if (byteCode.StackBefore == null)
continue;
int argIdx = 0; int argIdx = 0;
int popCount = byteCode.PopCount ?? byteCode.StackBefore.Count; int popCount = byteCode.PopCount ?? byteCode.StackBefore.Count;
for (int i = byteCode.StackBefore.Count - popCount; i < byteCode.StackBefore.Count; i++) { 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 // Convert stack-based IL code to ILAst tree
foreach(ByteCode byteCode in body) { 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<ILRange>(new[] { ilRange })
});
continue;
}
ILExpression expr = new ILExpression(byteCode.Code, byteCode.Operand); 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 // Label for this instruction
if (byteCode.Label != null) { if (byteCode.Label != null) {

11
ICSharpCode.Decompiler/ILAst/ILAstTypes.cs

@ -83,6 +83,17 @@ namespace Decompiler
} }
} }
public class ILComment: ILNode
{
public string Text;
public List<ILRange> ILRanges { get; set; }
public override void WriteTo(ITextOutput output)
{
output.WriteLine("// " + this.Text);
}
}
public class ILTryCatchBlock: ILNode public class ILTryCatchBlock: ILNode
{ {
public class CatchBlock: ILBlock public class CatchBlock: ILBlock

Loading…
Cancel
Save