public static Bpl.StmtList BuildStmtList(Bpl.Cmd cmd, Bpl.TransferCmd tcmd) { Bpl.StmtListBuilder builder = new Bpl.StmtListBuilder(); builder.Add(cmd); builder.Add(tcmd); return(builder.Collect(Bpl.Token.NoToken)); }
public override TransferCmd VisitTransferCmd(TransferCmd node) { //Contract.Requires(node != null); Contract.Ensures(Contract.Result<TransferCmd>() != null); return base.VisitTransferCmd((TransferCmd)node.Clone()); }
// hasPredicatedRegion is true iff the block or its targets are predicated // (i.e. we enter, stay within or exit a predicated region). void PredicateTransferCmd(Expr p, Block src, List<Cmd> cmdSeq, TransferCmd cmd, out bool hasPredicatedRegion) { hasPredicatedRegion = predMap.ContainsKey(src); if (cmd is GotoCmd) { var gCmd = (GotoCmd)cmd; hasPredicatedRegion = hasPredicatedRegion || gCmd.labelTargets.Cast<Block>().Any(b => predMap.ContainsKey(b)); if (gCmd.labelTargets.Count == 1) { if (defMap.ContainsKey(gCmd.labelTargets[0])) { PredicateCmd(p, Expr.True, cmdSeq, Cmd.SimpleAssign(Token.NoToken, Expr.Ident(predMap[gCmd.labelTargets[0]]), Expr.True)); } } else { Debug.Assert(gCmd.labelTargets.Count > 1); Debug.Assert(gCmd.labelTargets.Cast<Block>().All(t => uni.IsUniform(impl.Name, t) || partInfo.ContainsKey(t))); foreach (Block target in gCmd.labelTargets) { if (!partInfo.ContainsKey(target)) continue; // In this case we not only predicate with the current predicate p, // but also with the "part predicate"; this ensures that we do not // update a predicate twice when it occurs in both parts. var part = partInfo[target]; if (defMap.ContainsKey(part.realDest)) { PredicateCmd(p == null ? part.pred : Expr.And(p, part.pred), Expr.True, cmdSeq, Cmd.SimpleAssign(Token.NoToken, Expr.Ident(predMap[part.realDest]), part.pred)); } var predsExitingLoop = new Dictionary<Block, List<Expr>>(); foreach (Block exit in LoopsExited(src, target)) { List<Expr> predList; if (!predsExitingLoop.ContainsKey(exit)) predList = predsExitingLoop[exit] = new List<Expr>(); else predList = predsExitingLoop[exit]; predList.Add(part.pred); } foreach (var pred in predsExitingLoop) { PredicateCmd(p == null ? part.pred : Expr.And(p, part.pred), Expr.True, cmdSeq, Cmd.SimpleAssign(Token.NoToken, Expr.Ident(predMap[pred.Key]), Expr.Not(pred.Value.Aggregate(Expr.Or)))); } } } } else if (cmd is ReturnCmd) { // Blocks which end in a return will never share a predicate with a block // which appears after it. Furthermore, such a block cannot be part of a // loop. So it is safe to do nothing here. } else { Console.WriteLine("Unsupported cmd: " + cmd.GetType().ToString()); } }
public virtual TransferCmd VisitTransferCmd(TransferCmd node) { Contract.Requires(node != null); Contract.Ensures(Contract.Result<TransferCmd>() != null); return node; }
void PredicateTransferCmd(List<Cmd> cmdSeq, TransferCmd cmd) { if (cmd is GotoCmd) { var gCmd = (GotoCmd)cmd; var targets = new List<Expr>( gCmd.labelTargets.Cast<Block>().Select(b => blockIds[b])); if (targets.Count == 1) { PredicateCmd(cmdSeq, Cmd.SimpleAssign(Token.NoToken, cur, targets[0])); } else { PredicateCmd(cmdSeq, new HavocCmd(Token.NoToken, new List<IdentifierExpr> { cur })); PredicateCmd(cmdSeq, new AssumeCmd(Token.NoToken, targets.Select(t => (Expr)Expr.Eq(cur, t)).Aggregate(Expr.Or))); } foreach (Block b in gCmd.labelTargets) { if (blockGraph.Predecessors(b).Count() == 1) { if (!doneBlocks.Contains(b)) { var assumes = b.Cmds.Cast<Cmd>().TakeWhile(c => c is AssumeCmd); if (assumes.Count() > 0) { foreach (AssumeCmd aCmd in assumes) { cmdSeq.Add(new AssumeCmd(Token.NoToken, Expr.Imp(Expr.Eq(cur, blockIds[b]), aCmd.Expr))); } b.Cmds = new List<Cmd>(b.Cmds.Cast<Cmd>().Skip(assumes.Count()).ToArray()); } } } } } else if (cmd is ReturnCmd) { PredicateCmd(cmdSeq, Cmd.SimpleAssign(Token.NoToken, cur, returnBlockId)); } else { Console.WriteLine("Unsupported cmd: " + cmd.GetType().ToString()); } }
public Parser(Scanner/*!*/ scanner, Errors/*!*/ errors, bool disambiguation) : this(scanner, errors) { // initialize readonly fields Pgm = new Program(); dummyExpr = new LiteralExpr(Token.NoToken, false); dummyCmd = new AssumeCmd(Token.NoToken, dummyExpr); dummyBlock = new Block(Token.NoToken, "dummyBlock", new List<Cmd>(), new ReturnCmd(Token.NoToken)); dummyType = new BasicType(Token.NoToken, SimpleType.Bool); dummyExprSeq = new List<Expr> (); dummyTransferCmd = new ReturnCmd(Token.NoToken); dummyStructuredCmd = new BreakCmd(Token.NoToken, null); }
public override TransferCmd VisitTransferCmd(TransferCmd node) { Contract.Ensures(Contract.Result<TransferCmd>() == node); return node; }
public TransferCmd CopyTransferCmd(TransferCmd cmd) { Contract.Requires(cmd != null); Contract.Ensures(Contract.Result<TransferCmd>() != null); TransferCmd transferCmd; GotoCmd gotocmd = cmd as GotoCmd; if (gotocmd != null) { Contract.Assert(gotocmd.labelNames != null); List<String> labels = new List<String>(); labels.AddRange(gotocmd.labelNames); transferCmd = new GotoCmd(cmd.tok, labels); } else { ReturnExprCmd returnExprCmd = cmd as ReturnExprCmd; if (returnExprCmd != null) { transferCmd = new ReturnExprCmd(cmd.tok, CopyExpr(returnExprCmd.Expr)); } else { transferCmd = new ReturnCmd(cmd.tok); } } return transferCmd; }
void TransferCmd(out TransferCmd/*!*/ tc) { Contract.Ensures(Contract.ValueAtReturn(out tc) != null); tc = dummyTransferCmd; Token y; List<IToken>/*!*/ xs; List<String> ss = new List<String>(); if (la.kind == 39) { Get(); y = t; Idents(out xs); foreach(IToken/*!*/ s in xs){ Contract.Assert(s != null); ss.Add(s.val); } tc = new GotoCmd(y, ss); } else if (la.kind == 40) { Get(); tc = new ReturnCmd(t); } else SynErr(108); Expect(9); }
private HashSet<int> GetStateOfTargetBlock(TransferCmd tc, Dictionary<Absy, int> bodyGraphForImplPhaseJ, int yTypeCheckCurrentPhaseNum, List<int> initialStates, List<int> finalStates) { HashSet<int> targetBlockEntryStates = new HashSet<int>(); if (tc is ReturnCmd) { ReturnCmd returnCmd = tc as ReturnCmd; int source = bodyGraphForImplPhaseJ[tc]; finalStates.Add(source); //Console.WriteLine(" There is a return baby ! " + source.ToString()); // Do Nothing } else if (tc is GotoCmd) { GotoCmd transferCmd = tc as GotoCmd; foreach (Block block in transferCmd.labelTargets) { if (block.Cmds.Count == 0) { targetBlockEntryStates.Add(bodyGraphForImplPhaseJ[block.TransferCmd]); //Target block is empty. Add state of target block's transfer command (Goto or Return) } else if (block.Cmds.Count >= 1) { if (IsCallCmdExitPoint(block.Cmds[0], yTypeCheckCurrentPhaseNum)) { // Create artificial final state and put this into final states int targetState = Math.Abs(Guid.NewGuid().GetHashCode()); finalStates.Add(targetState); targetBlockEntryStates.Add(targetState); } else { targetBlockEntryStates.Add(bodyGraphForImplPhaseJ[block.Cmds[0]]); } } } } return targetBlockEntryStates; }
protected TransferCmd CreateInlinedTransferCmd(TransferCmd transferCmd, string procLabel) { Contract.Requires(procLabel != null); Contract.Requires(transferCmd != null); TransferCmd newTransferCmd; GotoCmd gotoCmd = transferCmd as GotoCmd; if (gotoCmd != null) { List<String> gotoSeq = gotoCmd.labelNames; List<String> newGotoSeq = new List<String>(); foreach (string/*!*/ blockLabel in cce.NonNull(gotoSeq)) { Contract.Assert(blockLabel != null); newGotoSeq.Add(procLabel + "$" + blockLabel); } newTransferCmd = new GotoCmd(transferCmd.tok, newGotoSeq); } else { newTransferCmd = new GotoCmd(transferCmd.tok, new List<String> { procLabel + "$Return" }); } return newTransferCmd; }
public override TransferCmd VisitTransferCmd(TransferCmd node) { node.tok = Token.NoToken; TokenCount++; return base.VisitTransferCmd(node); }
public Block(IToken tok, string/*!*/ label, List<Cmd>/*!*/ cmds, TransferCmd transferCmd) : base(tok) { Contract.Requires(label != null); Contract.Requires(cmds != null); Contract.Requires(tok != null); this.Label = label; this.Cmds = cmds; this.TransferCmd = transferCmd; this.Predecessors = new List<Block>(); this.liveVarsBefore = null; this.TraversingStatus = VisitState.ToVisit; this.iterations = 0; }
public BigBlock(IToken tok, string labelName, [Captured] List<Cmd> simpleCmds, StructuredCmd ec, TransferCmd tc) { Contract.Requires(simpleCmds != null); Contract.Requires(tok != null); Contract.Requires(ec == null || tc == null); this.tok = tok; this.LabelName = labelName; this.Anonymous = labelName == null; this.simpleCmds = simpleCmds; this.ec = ec; this.tc = tc; }
void Dump(StructuredCmd scmd, TransferCmd tcmd) { Contract.Requires(scmd == null || tcmd == null); Contract.Ensures(label == null && simpleCmds == null); if (label == null && simpleCmds == null && scmd == null && tcmd == null) { // nothing to do } else { if (simpleCmds == null) { simpleCmds = new List<Cmd>(); } bigBlocks.Add(new BigBlock(Token.NoToken, label, simpleCmds, scmd, tcmd)); label = null; simpleCmds = null; } }
public void Add(TransferCmd tcmd) { Contract.Requires(tcmd != null); Dump(null, tcmd); }