Skip to content

Commit

Permalink
Handle unreachable code
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrbecky committed Feb 20, 2011
1 parent eda2073 commit adfc7fc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ IEnumerable<Statement> TransformNode(ILNode node)
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");
}
Expand Down
27 changes: 25 additions & 2 deletions ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ public List<StackSlot> CloneStack(int? popCount)
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;
Expand Down Expand Up @@ -234,6 +244,9 @@ List<ByteCode> StackAnalysis(MethodDefinition methodDef)

// 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++) {
Expand Down Expand Up @@ -395,8 +408,18 @@ List<ILNode> ConvertToAst(List<ByteCode> body)

// 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<ILRange>(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) {
Expand Down
11 changes: 11 additions & 0 deletions ICSharpCode.Decompiler/ILAst/ILAstTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ public override void WriteTo(ITextOutput output)
}
}

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 CatchBlock: ILBlock
Expand Down

0 comments on commit adfc7fc

Please sign in to comment.