private IEnumerable <IStatement> ParseMethodExecutions(IEnumerable <string> lines) { var result = new List <IStatement>(); int blockDepth = 0; foreach (string line in lines.Select(entry => entry.Trim())) { try { if (IsLineIgnored(line) || line == "{") { continue; } if (line == "}") { if (blockDepth > 0) { result.Add(new ControlStatementEnd()); blockDepth--; } continue; } if (ControlStatement.TryParse(line) is { } controlStatement) { blockDepth++; result.Add(controlStatement); } else if (ForStatement.TryParse(line) is { } forStatement) { blockDepth++; result.Add(forStatement); }
//////////////////////////////////////////////////////////////////////////// public override Statement visit(ConditionalBranch s) { ControlStatement result = s; // var ne = si.fae.visit(this); ProgramVariable oc = s.condition; ProgramVariable nc = s.condition; EqualityDatabase.Representative rep = database.tryGetRepresentative(s.condition); if (rep != null) { Expression ne = rep.expression; if (ne is LiteralExpression) { if (((ne as LiteralExpression).value as BooleanValue).value) { result = new UnconditionalBranch(s.trueBranch.source, s.trueBranch.target); } else { result = new UnconditionalBranch(s.falseBranch.source, s.falseBranch.target); } nc = null; } else { if (ne is ProgramVariableExpression) { s.condition = (ne as ProgramVariableExpression).programVariable; nc = s.condition; } else { nc = (EqualityDatabase.getCompactNegationIfBoolean(ne) as ProgramVariableExpression). programVariable; result = new ConditionalBranch( database.statementId.basicBlock, nc, s.falseBranch.target, s.trueBranch.target ); } } } if (nc != null && !database.conditionVariableIndices.ContainsKey(nc.name)) { database.addConditionVariable(nc); } return(result); }
public BasicBlock() { label = null; cfg = null; statements = new List <StatementInfo>(); controlStatement = null; myPredecessors = new List <BasicEdge>(); preState = new StateInformation(); postState = preState; }
//////////////////////////////////////////////////////////////////////// public void mergeNextBlock() { Debug.Assert(controlStatement != null); Debug.Assert(controlStatement is UnconditionalBranch); BasicBlock next = controlStatement.successors.First().target; Debug.Assert(next.controlStatement != null); Debug.Assert(next.predecessors.Count() == 1); Debug.Assert(next != this); statements.RemoveAt(statements.Count - 1); //collect control statement int preNumStatements = statements.Count; next.removePredecessor(successors.First()); StateInformation insertionPrestate = (statements.Count > 0) ? statements.Last().postState : preState; int i = 0; foreach (var nsi in next.statements) { nsi.preState = (i == 0) ? insertionPrestate : statements.Last().postState; nsi.basicBlock = this; nsi.index = statements.Count; statements.Add(nsi); i++; } postState = statements.Last().postState; controlStatement = statements.Last().statement as ControlStatement; foreach (var e in controlStatement.successors) { e.setSource(this); } assertLocalInvariant(); next.controlStatement = null; next.statements = new List <StatementInfo>(); next.postState = next.preState; next.setControlStatement(new Block(next)); if (cfg.endNode.label == next.label) { cfg.endNode = this; } next.delete(); foreach (var e in controlStatement.successors) { e.target.assertLocalInvariant(); } assertLocalInvariant(); }
private void correctControlStatement(StatementId si, ControlStatement newS) { IEnumerable <Tuple <BasicEdge.Guard, StatementId> > os = getSuccessors(si); IEnumerable <Tuple <BasicEdge.Guard, StatementId> > ns = getSuccessors(si, newS); var dif = new HashSet <BasicBlock>(from s in os select s.Item2.basicBlock); var nss = new HashSet <BasicBlock>(from s in ns select s.Item2.basicBlock); Debug.Assert(nss.IsSubsetOf(dif)); dif.ExceptWith(nss); if (dif.Count == 0) { return; } var q = new Queue <BasicBlock>(dif); var numReaches = new Dictionary <BasicBlock, int>(); foreach (var d in dif) { numReaches.Add(d, 1); } while (q.Count > 0) { BasicBlock bb = q.Dequeue(); Debug.Assert(bb.statements.First().preState.evaluatorInformation.numIncompletePredecessors > 0); bb.statements.First().preState.evaluatorInformation.numIncompletePredecessors--; if (bb.statements.First().preState.evaluatorInformation.numIncompletePredecessors == 0) { if (bb.predecessors.Count - numReaches[bb] == 0) //will be deleted { foreach (var nbbe in bb.successors) { BasicBlock nbb = nbbe.target; q.Enqueue(nbb); if (!numReaches.ContainsKey(nbb)) { numReaches.Add(nbb, 1); } else { numReaches[nbb]++; } } } else { ready.Enqueue(bb.statements[0].statementId); } } } }
//////////////////////////////////////////////////////////////////////// public void delete() { // Console.WriteLine("\tDeleting block {0}", label); Debug.Assert(predecessors.Count() == 0); if (successors.Count > 0) { setControlStatement(new Block(this)); } Debug.Assert(successors.Count() == 0); cfg.removeNode(label); preState = null; postState = null; cfg = null; controlStatement = null; label = null; statements = null; }
//////////////////////////////////////////////////////////////////////// public void setControlStatement(ControlStatement tc) { Debug.Assert(tc != null); if (controlStatement != null) { Debug.Assert(!ReferenceEquals(controlStatement, tc)); var os = new List <BasicEdge>(successors); foreach (var e in successors) { e.target.removePredecessor(e); } controlStatement = tc; statements[statements.Count - 1].statement = tc; foreach (var e in tc.successors) { Debug.Assert(e.source == this); } foreach (var e in tc.successors) { Debug.Assert(e.target.predecessors.Contains(e)); } foreach (var se in os) { if (se.target.predecessors.Count == 0) { se.target.delete(); } } } else { Debug.Assert(controlStatement == null); foreach (var e in tc.successors) { Debug.Assert(e.source == this); } controlStatement = tc; var cs = new StatementInfo(tc, postState, new StateInformation(), this, statements.Count); statements.Add(cs); postState = cs.postState; } assertLocalInvariant(); }
private static int FindSectionEnd(Dictionary <ControlStatement,int> sections,ControlStatement current) { int res = int.MaxValue; int start = sections[current]; foreach (var item in sections) { if (item.Value <= start) { continue; } if (item.Value < res) { res = item.Value; } } return(res); }
///////////////////////////////////////////////////////// protected override IEnumerable <Tuple <BasicEdge.Guard, StatementId> > getSuccessors(StatementId si, ControlStatement cs) { return(si.predecessors); }
private IEnumerable <IStatement> ParseMethodExecutions(IEnumerable <string> lines) { var result = new List <IStatement>(); int controlsequenceDepth = 0; foreach (string line in lines.Select(entry => entry.Trim())) { if (this.IsLineIgnored(line)) { continue; } if (line == "{") { continue; } if (line == "}") { if (controlsequenceDepth > 0) { result.Add(new ControlStatementEnd()); controlsequenceDepth--; } continue; } IStatement statement; if (this.Class.Name == "FightCommonInformations") { } if (Regex.IsMatch(line, ControlStatement.Pattern)) { statement = ControlStatement.Parse(line); controlsequenceDepth++; } else if (Regex.IsMatch(line, AssignationStatement.Pattern)) { statement = AssignationStatement.Parse(line); } else if (Regex.IsMatch(line, InvokeExpression.Pattern)) { statement = InvokeExpression.Parse(line); if (!string.IsNullOrEmpty((statement as InvokeExpression).ReturnVariableAssignation) && string.IsNullOrEmpty((statement as InvokeExpression).Preffix) && this.Fields.Count(entry => entry.Name == ((InvokeExpression)statement).ReturnVariableAssignation) > 0) { (statement as InvokeExpression).Preffix = "(" + this.Fields.Where(entry => entry.Name == ((InvokeExpression)statement).ReturnVariableAssignation) .First() .Type + ")"; // cast } // cast to generic type if (!string.IsNullOrEmpty((statement as InvokeExpression).Target) && (statement as InvokeExpression).Name == "Add" && this.Fields.Count(entry => entry.Name == ((InvokeExpression)statement).Target.Split('.').Last()) > 0) { string generictype = this.Fields.Where(entry => entry.Name == ((InvokeExpression)statement).Target.Split('.').Last()).First().Type.Split('<').Last().Split('>').First(); (statement as InvokeExpression).Args[0] = "(" + generictype + ")" + (statement as InvokeExpression).Args[0]; } } else { statement = new UnknownStatement { Value = line } }; result.Add(statement); } return(result); }
protected abstract IEnumerable <Tuple <BasicEdge.Guard, StatementId> > getSuccessors(StatementId s, ControlStatement cs);
///////////////////////////////////////////////////////// protected override IEnumerable <Tuple <BasicEdge.Guard, StatementId> > getSuccessors(StatementId si, ControlStatement cs) { return(from s in cs.successors select new Tuple <BasicEdge.Guard, StatementId>(s.guard, s.target.statements[0].statementId)); }