Browse Source

Join consecutive expression together if possible

pull/1/head^2
David Srbecký 18 years ago
parent
commit
acdac3ecf5
  1. 5
      src/AstMetodBodyBuilder.cs
  2. 17
      src/StackExpression.cs
  3. 14
      src/StackExpressionCollection.cs

5
src/AstMetodBodyBuilder.cs

@ -20,6 +20,7 @@ namespace Decompiler
ByteCodeCollection body = new ByteCodeCollection(methodDef); ByteCodeCollection body = new ByteCodeCollection(methodDef);
StackExpressionCollection exprCol = new StackExpressionCollection(body); StackExpressionCollection exprCol = new StackExpressionCollection(body);
exprCol.Optimize();
foreach(VariableDefinition varDef in methodDef.Body.Variables) { foreach(VariableDefinition varDef in methodDef.Body.Variables) {
Ast.VariableDeclaration astVar = new Ast.VariableDeclaration(varDef.Name); Ast.VariableDeclaration astVar = new Ast.VariableDeclaration(varDef.Name);
@ -73,11 +74,11 @@ namespace Decompiler
List<Ast.Expression> allArgs = new List<Ast.Expression>(); List<Ast.Expression> allArgs = new List<Ast.Expression>();
// Add args from stack // Add args from stack
allArgs.AddRange(args); allArgs.AddRange(args);
// Args generated by nested expressions // Args generated by nested expressions (which must be closed)
foreach(StackExpression nestedExpr in expr.LastArguments) { foreach(StackExpression nestedExpr in expr.LastArguments) {
allArgs.Add(new Ast.ParenthesizedExpression((Ast.Expression)MakeCodeDomExpression(methodDef, nestedExpr))); allArgs.Add(new Ast.ParenthesizedExpression((Ast.Expression)MakeCodeDomExpression(methodDef, nestedExpr)));
} }
return MakeCodeDomExpression(methodDef, expr.ExpressionByteCode, args); return MakeCodeDomExpression(methodDef, expr.ExpressionByteCode, allArgs.ToArray());
} }
static object MakeCodeDomExpression(MethodDefinition methodDef, ByteCode byteCode, params Ast.Expression[] args) static object MakeCodeDomExpression(MethodDefinition methodDef, ByteCode byteCode, params Ast.Expression[] args)

17
src/StackExpression.cs

@ -16,6 +16,7 @@ namespace Decompiler
get { return expressionByteCode; } get { return expressionByteCode; }
} }
// A list of closed expression for last arguments
public List<StackExpression> LastArguments { public List<StackExpression> LastArguments {
get { return lastArguments; } get { return lastArguments; }
} }
@ -32,6 +33,22 @@ namespace Decompiler
} }
} }
/// <summary>
/// Expression is closed if it has no inputs and has exactly one output
/// </summary>
public bool IsClosed {
get {
return this.PopCount == 0 &&
this.PushCount == 1;
}
}
public bool IsBranchTarget {
get {
return this.FirstByteCode.BranchesHere.Count > 0;
}
}
public int PopCount { public int PopCount {
get { get {
int popCount; int popCount;

14
src/StackExpressionCollection.cs

@ -18,7 +18,19 @@ namespace Decompiler
public void Optimize() public void Optimize()
{ {
for(int i = 1; i < this.Count; i++) {
StackExpression prevExpr = this[i - 1];
StackExpression expr = this[i];
if (expr.PopCount > 0 && // This expr needs some more arguments
!expr.IsBranchTarget &&
prevExpr.IsClosed)
{
this.RemoveAt(i - 1); i--;
expr.LastArguments.Insert(0, prevExpr);
i--;
}
}
} }
} }
} }

Loading…
Cancel
Save