From 466e199cca0678688fd8bb27a5a0003930529469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Sun, 4 Nov 2007 16:08:06 +0000 Subject: [PATCH] Assign the result of each instruction to a temporary local variable --- src/AstMetodBodyBuilder.cs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/AstMetodBodyBuilder.cs b/src/AstMetodBodyBuilder.cs index 7c99ea241..47ece438e 100644 --- a/src/AstMetodBodyBuilder.cs +++ b/src/AstMetodBodyBuilder.cs @@ -38,6 +38,13 @@ namespace Decompiler new Ast.IdentifierExpression("arg2"), new Ast.IdentifierExpression("arg3")); if (codeExpr is Ast.Expression) { + if (GetNumberOfOutputs(methodDef, instr) == 1) { + codeExpr = new Ast.AssignmentExpression( + new Ast.IdentifierExpression(string.Format("expr{0:X2}", instr.Offset)), + AssignmentOperatorType.Assign, + (Ast.Expression)codeExpr + ); + } astStatement = new ExpressionStatement((Ast.Expression)codeExpr); } else if (codeExpr is Ast.Statement) { astStatement = (Ast.Statement)codeExpr; @@ -75,6 +82,34 @@ namespace Decompiler } } + static int GetNumberOfOutputs(MethodDefinition methodDef, Instruction inst) + { + switch(inst.OpCode.StackBehaviourPush) { + case StackBehaviour.Push0: return 0; + case StackBehaviour.Push1: return 1; + case StackBehaviour.Push1_push1: return 2; + case StackBehaviour.Pushi: return 1; + case StackBehaviour.Pushi8: return 1; + case StackBehaviour.Pushr4: return 1; + case StackBehaviour.Pushr8: return 1; + case StackBehaviour.Pushref: return 1; + case StackBehaviour.Varpush: // Happens only for calls + switch(inst.OpCode.Code) { + case Code.Call: + Cecil.MethodReference cecilMethod = ((MethodReference)inst.Operand); + if (cecilMethod.ReturnType.ReturnType.FullName == Constants.Void) { + return 0; + } else { + return 1; + } + case Code.Calli: throw new NotImplementedException(); + case Code.Callvirt: throw new NotImplementedException(); + default: throw new Exception("Unknown Varpush opcode"); + } + default: throw new Exception("Unknown push behaviour: " + inst.OpCode.StackBehaviourPush); + } + } + static object MakeCodeDomExpression(MethodDefinition methodDef, Instruction inst, params Ast.Expression[] args) { OpCode opCode = inst.OpCode;