Browse Source

Use stack analysis to figure out basic data flow - use actual inputs instead of the dummy ones

pull/1/head^2
David Srbecký 18 years ago
parent
commit
4468b4a3e1
  1. 23
      src/AstMetodBodyBuilder.cs
  2. 5
      src/StackAnalysis.cs

23
src/AstMetodBodyBuilder.cs

@ -41,12 +41,17 @@ namespace Decompiler
} }
} catch (NotImplementedException) { } catch (NotImplementedException) {
} }
int argCount = Util.GetNumberOfInputs(methodDef, instr);
Ast.Expression[] args = new Ast.Expression[argCount];
for(int i = 0; i < argCount; i++) {
Instruction allocBy = stackAnalysis.StackBefore[instr].Peek(argCount - i).AllocadedBy;
string name = string.Format("expr{0:X2}", allocBy.Offset);
args[i] = new Ast.IdentifierExpression(name);
}
object codeExpr = MakeCodeDomExpression( object codeExpr = MakeCodeDomExpression(
methodDef, methodDef,
instr, instr,
new Ast.IdentifierExpression("arg1"), args);
new Ast.IdentifierExpression("arg2"),
new Ast.IdentifierExpression("arg3"));
if (codeExpr is Ast.Expression) { if (codeExpr is Ast.Expression) {
if (Util.GetNumberOfOutputs(methodDef, instr) == 1) { if (Util.GetNumberOfOutputs(methodDef, instr) == 1) {
type = type ?? "object"; type = type ?? "object";
@ -65,7 +70,7 @@ namespace Decompiler
} }
astBlock.Children.Add(new Ast.LabelStatement(string.Format("IL_{0:X2}", instr.Offset))); astBlock.Children.Add(new Ast.LabelStatement(string.Format("IL_{0:X2}", instr.Offset)));
astBlock.Children.Add(astStatement); astBlock.Children.Add(astStatement);
astBlock.Children.Add(MakeComment(" " + stackAnalysis.StackAfter[instr].ToString())); //astBlock.Children.Add(MakeComment(" " + stackAnalysis.StackAfter[instr].ToString()));
} }
return astBlock; return astBlock;
@ -253,14 +258,12 @@ namespace Decompiler
case Code.Call: case Code.Call:
Cecil.MethodReference cecilMethod = ((MethodReference)operand); Cecil.MethodReference cecilMethod = ((MethodReference)operand);
Ast.IdentifierExpression astType = new Ast.IdentifierExpression(cecilMethod.DeclaringType.FullName); Ast.IdentifierExpression astType = new Ast.IdentifierExpression(cecilMethod.DeclaringType.FullName);
List<Ast.Expression> astArgs = new List<Ast.Expression>(); List<Ast.Expression> methodArgs = new List<Ast.Expression>(args);
for(int i = 0; i < cecilMethod.Parameters.Count; i++) {
astArgs.Add(new Ast.IdentifierExpression("arg" + (cecilMethod.HasThis ? i + 1 : i)));
}
if (cecilMethod.HasThis) { if (cecilMethod.HasThis) {
return new Ast.InvocationExpression(new Ast.MemberReferenceExpression(arg1, cecilMethod.Name), astArgs); methodArgs.RemoveAt(0); // Remove 'this'
return new Ast.InvocationExpression(new Ast.MemberReferenceExpression(arg1, cecilMethod.Name), methodArgs);
} else { } else {
return new Ast.InvocationExpression(new Ast.MemberReferenceExpression(astType, cecilMethod.Name), astArgs); return new Ast.InvocationExpression(new Ast.MemberReferenceExpression(astType, cecilMethod.Name), methodArgs);
} }
case Code.Calli: throw new NotImplementedException(); case Code.Calli: throw new NotImplementedException();
case Code.Callvirt: throw new NotImplementedException(); case Code.Callvirt: throw new NotImplementedException();

5
src/StackAnalysis.cs

@ -66,6 +66,11 @@ namespace Decompiler
this.Add(slot); this.Add(slot);
} }
public CilStackSlot Peek(int depth)
{
return this[this.Count - depth];
}
public CilStack(): base() public CilStack(): base()
{ {

Loading…
Cancel
Save