void WriteConditionalBranch(ActionFlowGraph afg, ConditionalBranchActionBlock block) { _writer.Write("if "); WriteExpression(block.Condition); _writer.Write(' '); WriteGoto(afg, block.Then); }
public static string ToString(ActionFlowGraph afg) { StringWriter writer = new StringWriter(); new ActionFlowGraphPrinter(writer).Print(afg); return(writer.ToString()); }
private static ActionBlock GetFirstBlock(ActionFlowGraph graph) { if (graph.Blocks.Count < 1) { return(null); } return(graph.Blocks[0]); }
public void ConditionalBranchActionBlock() { ActionFlowGraph afg = GetActionFlowGraph("SimpleIf"); ConditionalBranchActionBlock cbr = (ConditionalBranchActionBlock)afg.Blocks [0]; Assert.AreSame(afg.Blocks [2], cbr.Then, "Then"); Assert.AreSame(afg.Blocks [1], cbr.Else, "Else"); }
private CecilMethodAnalyser(ActionFlowGraph graph) { if (graph == null) { throw new ArgumentNullException("graph"); } _queryExpression = QueryExpressionFinder.FindIn(graph); }
private static bool IsNoSideEffectIndirectActivationInvocation(MethodInvocationExpression invocation) { MethodDefinition methodDefinition = MethodDefinitionFor(invocation); if (null == methodDefinition) { return(false); } ActionFlowGraph afg = FlowGraphFactory.CreateActionFlowGraph(FlowGraphFactory.CreateControlFlowGraph(methodDefinition)); if (afg.Blocks.Count == 2 && afg.Blocks[0].ActionType == ActionType.Invoke) { InvokeActionBlock invocationBlock = (InvokeActionBlock)afg.Blocks[0]; return(IsActivateInvocation(invocationBlock.Expression)); } return(false); }
public void Print(ActionFlowGraph afg) { int i = 1; foreach (ActionBlock block in afg.Blocks) { if (afg.IsBranchTarget(block)) { WriteLabel(i); } switch (block.ActionType) { case ActionType.Return: WriteReturn((ReturnActionBlock)block); break; case ActionType.Branch: WriteBranch(afg, (BranchActionBlock)block); break; case ActionType.ConditionalBranch: WriteConditionalBranch(afg, (ConditionalBranchActionBlock)block); break; case ActionType.Assign: WriteAssign((AssignActionBlock)block); break; case ActionType.Invoke: WriteInvoke((InvokeActionBlock)block); break; default: throw new InvalidOperationException(); } _writer.WriteLine(); ++i; } }
public static Expression FindIn(ActionFlowGraph graph) { var finder = new QueryExpressionFinder(graph); return(finder.QueryExpression); }
private QueryExpressionFinder(ActionFlowGraph graph) { _graph = graph; _variables = new Dictionary <int, Expression>(); FindQueryExpression(); }
private static Expression GetQueryExpression(ActionFlowGraph afg) { IDictionary <int, Expression> variables = new Dictionary <int, Expression>(); ActionBlock block = afg.Blocks[0]; while (block != null) { switch (block.ActionType) { case ActionType.Invoke: InvokeActionBlock invokeBlock = (InvokeActionBlock)block; MethodInvocationExpression invocation = invokeBlock.Expression; if (IsActivateInvocation(invocation) || IsNoSideEffectIndirectActivationInvocation(invocation)) { block = invokeBlock.Next; break; } UnsupportedExpression(invocation); break; case ActionType.ConditionalBranch: UnsupportedPredicate("Conditional blocks are not supported."); break; case ActionType.Branch: block = ((BranchActionBlock)block).Target; break; case ActionType.Assign: { AssignActionBlock assignBlock = (AssignActionBlock)block; AssignExpression assign = assignBlock.AssignExpression; VariableReferenceExpression variable = assign.Target as VariableReferenceExpression; if (null == variable) { UnsupportedExpression(assign); } else { if (variables.ContainsKey(variable.Variable.Index)) { UnsupportedExpression(assign.Expression); } variables.Add(variable.Variable.Index, assign.Expression); block = assignBlock.Next; } break; } case ActionType.Return: { Expression expression = ((ReturnActionBlock)block).Expression; VariableReferenceExpression variable = expression as VariableReferenceExpression; return(null == variable ? expression : variables[variable.Variable.Index]); } } } return(null); }
private static Expression GetQueryExpression(MethodDefinition method) { ActionFlowGraph afg = FlowGraphFactory.CreateActionFlowGraph(FlowGraphFactory.CreateControlFlowGraph(method)); return(GetQueryExpression(afg)); }
void WriteGoto(ActionFlowGraph afg, ActionBlock target) { _writer.Write("goto block{0}", afg.Blocks.IndexOf(target) + 1); }
void WriteBranch(ActionFlowGraph afg, BranchActionBlock block) { WriteGoto(afg, block.Target); }
protected void RunTestCase(string name) { ActionFlowGraph afg = GetActionFlowGraph(name); Assert.AreEqual(Normalize(LoadTestCaseFile(name + "-afg.txt")), Normalize(ToString(afg))); }