public override void TraverseChildren(IBlockStatement block) { Contract.Assume(block is BlockStatement); var decompiledBlock = (BlockStatement)block; var statements = decompiledBlock.Statements; for (int i = 0; i < statements.Count-1; i++) { IStatement initializer = null; var initializerAssignStat = statements[i] as IExpressionStatement; if (initializerAssignStat != null) { if (!(initializerAssignStat.Expression is IAssignment)) continue; initializer = initializerAssignStat; i++; } else { var initialDecl = statements[i] as ILocalDeclarationStatement; if (initialDecl == null || initialDecl.InitialValue == null) continue; initializer = initialDecl; i++; } var whileLoop = statements[i] as IWhileDoStatement; if (whileLoop == null) continue; var loopBody = whileLoop.Body as BlockStatement; if (loopBody == null) continue; var incrementer = FindLastStatement(loopBody) as IExpressionStatement; if (incrementer == null) continue; var incrementAssignment = incrementer.Expression as IAssignment; if (incrementAssignment == null || !(incrementAssignment.Source is IAddition || incrementAssignment.Source is ISubtraction)) continue; var forLoop = new ForStatement() { Condition = whileLoop.Condition, Body = loopBody }; if (initializer != null) { statements.RemoveAt(--i); forLoop.InitStatements.Add(initializer); } RemoveLastStatement(loopBody, incrementer); forLoop.IncrementStatements.Add(incrementer); statements[i] = forLoop; } base.TraverseChildren(block); }
/// <summary> /// Visits the specified for statement. /// </summary> /// <param name="forStatement">For statement.</param> /// <returns></returns> protected virtual IStatement DeepCopy(ForStatement forStatement) { forStatement.InitStatements = Substitute(forStatement.InitStatements); forStatement.Condition = Substitute(forStatement.Condition); forStatement.IncrementStatements = Substitute(forStatement.IncrementStatements); forStatement.Body = Substitute(forStatement.Body); return forStatement; }
/// <summary> /// Visits the specified for statement. /// </summary> /// <param name="forStatement">For statement.</param> public override void Visit(IForStatement forStatement) { ForStatement mutableForStatement = new ForStatement(forStatement); this.resultStatement = this.myCodeCopier.DeepCopy(mutableForStatement); }
private void DecompileForStatement(BasicBlock b, int i) { List<IStatement> statements = b.Statements; if (i > b.Statements.Count-2) return; var gotoStatement = statements[i++] as GotoStatement; if (gotoStatement == null) return; var blockStatement = statements[i++] as BlockStatement; if (blockStatement == null) return; var n = blockStatement.Statements.Count; if (n == 0) return; var whileLoop = blockStatement.Statements[0] as WhileDoStatement; if (whileLoop == null) return; if (whileLoop.Condition is IMethodCall) return; var loopBody = (BasicBlock)whileLoop.Body; if (RemoveLastStatement(loopBody, gotoStatement.TargetStatement)) { var forLoop = new ForStatement() { Condition = whileLoop.Condition, Body = loopBody }; if (i > 2) { var initializer = statements[i-3]; forLoop.InitStatements.Add(initializer); statements.RemoveAt(i-3); i--; } var incrementer = FindLastStatement(loopBody) as ExpressionStatement; if (incrementer != null) { var assign = incrementer.Expression as Assignment; if (assign != null && (assign.Source is Addition || assign.Source is Subtraction)) { RemoveStatement(loopBody, incrementer); forLoop.IncrementStatements.Add(incrementer); } } statements[i-2] = forLoop; blockStatement.Statements.RemoveAt(0); new ForLoopDecompiler(this.platformType, this.predecessors).Traverse(loopBody); } }
/// <summary> /// Rewrites the children of the given for statement. /// </summary> public virtual void RewriteChildren(ForStatement forStatement) { this.RewriteChildren((Statement)forStatement); forStatement.InitStatements = this.Rewrite(forStatement.InitStatements); forStatement.Condition = this.Rewrite(forStatement.Condition); forStatement.IncrementStatements = this.Rewrite(forStatement.IncrementStatements); forStatement.Body = this.Rewrite(forStatement.Body); }
/// <summary> /// Visits the specified for statement. /// </summary> /// <param name="forStatement">For statement.</param> /// <returns></returns> public virtual IStatement Visit(ForStatement forStatement) { forStatement.InitStatements = Visit(forStatement.InitStatements); forStatement.Condition = Visit(forStatement.Condition); forStatement.IncrementStatements = Visit(forStatement.IncrementStatements); forStatement.Body = Visit(forStatement.Body); return forStatement; }
/// <summary> /// Visits the specified for statement. /// </summary> /// <param name="forStatement">For statement.</param> public override void Visit(IForStatement forStatement) { ForStatement mutableForStatement = forStatement as ForStatement; if (alwaysMakeACopy || mutableForStatement == null) mutableForStatement = new ForStatement(forStatement); this.resultStatement = this.myCodeMutator.Visit(mutableForStatement); }