Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixing register transfers so that the assignment happens when it shou…
…ld in the instruction stream
  • Loading branch information
Frank Laub committed Mar 15, 2010
1 parent 82eb764 commit c2f579f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/DotWeb.Decompiler/Core/BasicBlock.cs
Expand Up @@ -101,13 +101,13 @@ public class BasicBlock : Node
private int stashSize = 0;
private Dictionary<int, CodeExpression> stash = new Dictionary<int, CodeExpression>();

public CodeExpression PushStash(TypeSystem typeSystem, CodeExpression rhs) {
internal CodeExpression PushStash(TypeSystem typeSystem, CodeExpressionEntry item) {
this.stashSize++;

int index = this.StashSize;
var variableName = string.Format("R_{0}", index);
var eval = new CodeTypeEvaluator(typeSystem, this.method);
var variableType = eval.Evaluate(rhs);
var variableType = eval.Evaluate(item.Expression);
var variable = new VariableDefinition(variableName, -index, this.method, variableType);
var lhs = new CodeVariableReference(variable);

Expand Down
60 changes: 38 additions & 22 deletions src/DotWeb.Decompiler/Core/Interpreter.cs
Expand Up @@ -28,11 +28,17 @@

namespace DotWeb.Decompiler.Core
{
struct CodeExpressionEntry
{
public int Index;
public CodeExpression Expression;
}

public class Interpreter
{
private readonly MethodDefinition method;
private readonly TypeSystem typeSystem;
private Stack<CodeExpression> stack = new Stack<CodeExpression>();
private Stack<CodeExpressionEntry> stack = new Stack<CodeExpressionEntry>();
private BasicBlock block;
private int duplicateCounter = 0;
private CodeTypeEvaluator typeEvaluator;
Expand Down Expand Up @@ -91,34 +97,34 @@ public class Interpreter
}

private void StashStackExtras(BasicBlock block) {
var extra = this.stack.Reverse();
foreach (var item in extra) {
var items = this.stack.Reverse();
foreach (var item in items) {
var lhs = block.PushStash(this.typeSystem, item);
var stmt = new CodeAssignStatement(lhs, item);
var last = this.block.Statements.Count;
if (last > 0)
last--;
this.block.Statements.Insert(last, stmt);
var stmt = new CodeAssignStatement(lhs, item.Expression);
this.block.Statements.Insert(item.Index, stmt);
}
this.stack.Clear();
}

private void Push(CodeExpression expr) {
this.stack.Push(expr);
this.stack.Push(new CodeExpressionEntry {
Index = this.block.Statements.Count,
Expression = expr
});
}

private CodeExpression Pop() {
if (!this.stack.Any()) {
return this.block.PopStash();
}
return this.stack.Pop();
return this.stack.Pop().Expression;
}

private CodeExpression Peek() {
if (!this.stack.Any()) {
return this.block.PeekStash();
}
return this.stack.Peek();
return this.stack.Peek().Expression;
}

private void HandleInstruction(Instruction il) {
Expand Down Expand Up @@ -206,6 +212,7 @@ public class Interpreter
case Code.Ldelem_U1:
case Code.Ldelem_U2:
case Code.Ldelem_U4:
case Code.Ldelema:
LoadElement(il);
break;
case Code.Ldlen:
Expand Down Expand Up @@ -419,13 +426,7 @@ public class Interpreter
Return(il);
break;
#endregion
#region Misc
case Code.Pop:
Pop(il);
break;
case Code.Dup:
Dup(il);
break;
#region Type Conversion
case Code.Conv_I:
case Code.Conv_Ovf_I:
case Code.Conv_Ovf_I_Un:
Expand Down Expand Up @@ -483,6 +484,14 @@ public class Interpreter
case Code.Conv_Ovf_U8_Un:
OnConvert(typeof(ulong));
break;
#endregion
#region Misc
case Code.Pop:
Pop(il);
break;
case Code.Dup:
Dup(il);
break;
case Code.Leave:
case Code.Leave_S:
Leave(il);
Expand All @@ -508,6 +517,7 @@ public class Interpreter
break;
#endregion
#region Unsupported/Unneeded
case Code.Ldobj:
case Code.Constrained:
case Code.Endfinally:
break;
Expand Down Expand Up @@ -654,6 +664,9 @@ public class Interpreter
PushCastExpression(Import(type));
}

private void LoadObject(Instruction cil) {
}

private void LoadArgument(Instruction il) {
PushArgumentReference((ParameterReference)il.Operand);
}
Expand Down Expand Up @@ -819,8 +832,11 @@ public class Interpreter
}

private void Branch(Instruction il) {
var stmt = new CodeGotoStatement((Instruction)il.Operand);
AddStatment(stmt);
//var succ = this.block.Successors.Single();
//if (succ.Predecessors.Count > 1) {
// var stmt = new CodeGotoStatement((Instruction)il.Operand);
// AddStatment(stmt);
//}
}

private void NewArray(Instruction il) {
Expand Down Expand Up @@ -875,8 +891,8 @@ public class Interpreter

private void Leave(Instruction il) {
this.stack.Clear();
CodeGotoStatement stmt = new CodeGotoStatement((Instruction)il.Operand);
AddStatment(stmt);
//var stmt = new CodeGotoStatement((Instruction)il.Operand);
//AddStatment(stmt);
}

private void CastClass(Instruction il) {
Expand Down

0 comments on commit c2f579f

Please sign in to comment.