summaryrefslogtreecommitdiff
path: root/Source/Concurrency/RefinementCheck.cs
blob: bba7bd3574fded06bc9b3246d0397993d8078f2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Boogie;
using Microsoft.Boogie.AbstractInterpretation;
using Microsoft.Boogie.GraphUtil;
using System.Diagnostics;
using System.Diagnostics.Contracts;


namespace Microsoft.Boogie
{
    using Bpl = Microsoft.Boogie;
    class RefinementCheck
    {
        Program refinementCheckerProgram;
        LinearTypeChecker linearTypeChecker;
        MoverTypeChecker moverTypeChecker;

        public static void AddCheckers(LinearTypeChecker linearTypeChecker, MoverTypeChecker moverTypeChecker)
        {
            RefinementCheck refinementChecking = new RefinementCheck(linearTypeChecker, moverTypeChecker);
            foreach (Declaration decl in linearTypeChecker.program.TopLevelDeclarations)
            {
                if (decl is Implementation)
                {
                    Implementation impl = decl as Implementation;
                    refinementChecking.createRefinementChecker(impl);
                }
            }
        }

        private RefinementCheck(LinearTypeChecker linearTypeChecker, MoverTypeChecker moverTypeChecker)
        {
            this.linearTypeChecker = linearTypeChecker;
            this.moverTypeChecker = moverTypeChecker;
            this.refinementCheckerProgram = new Program();
            foreach (Declaration decl in linearTypeChecker.program.TopLevelDeclarations)
            {
                if (decl is TypeCtorDecl || decl is TypeSynonymDecl || decl is Constant || decl is Function || decl is Axiom)
                    this.refinementCheckerProgram.TopLevelDeclarations.Add(decl);
            }
            foreach (Variable v in linearTypeChecker.program.GlobalVariables())
            {
                this.refinementCheckerProgram.TopLevelDeclarations.Add(v);
            }
        }

        private void createRefinementChecker(Implementation impl)
        {
            addRefinementCheckingAnnotations(impl);
            refinementCheckerProgram.TopLevelDeclarations.Add(impl);
            refinementCheckerProgram.TopLevelDeclarations.Add(impl.Proc);
        }

        private Expr createNewAlpha(Implementation impl)
        {
            Expr result;
            result = Expr.True;
            foreach (AssertCmd aC in moverTypeChecker.procToActionInfo[impl.Proc].thisGate)
            {
                Expr aCExprClone = (Expr)aC.OrigExpr.Clone();
                result = Expr.Binary(BinaryOperator.Opcode.And, result, aCExprClone);
            }
            return result;
        }

        private Expr createNewAlphaOfGOld(Implementation impl, List<Variable> originalGVars,
            Dictionary<Variable, Variable> regularToOldGVar)
        {
            Expr result;
            result = Expr.True;
            foreach (AssertCmd aC in moverTypeChecker.procToActionInfo[impl.Proc].thisGate)
            {
                Expr aCExprClone = (Expr)aC.OrigExpr.Clone();
                result = Expr.Binary(BinaryOperator.Opcode.And, result, aCExprClone);
            }
            Dictionary<Variable, Expr> map = new Dictionary<Variable, Expr>();
            foreach (Variable v in originalGVars)
            {
                map[v] = Expr.Ident(regularToOldGVar[v]);
            }
            Substitution subst = Substituter.SubstitutionFromHashtable(map);
            MySubstituter mySubst = new MySubstituter(subst, subst);// ST: Check with Shaz
            result = (Expr)mySubst.Visit(result);
            return result;
        }


        private HavocCmd createNewHavocGCmd()
        {
            List<IdentifierExpr> gList = new List<IdentifierExpr>();
            foreach (GlobalVariable g in refinementCheckerProgram.GlobalVariables())
                gList.Add(new IdentifierExpr(Token.NoToken, g));
            HavocCmd havocG = new HavocCmd(Token.NoToken, gList);
            return havocG;
        }

        private AssignCmd createNewAssignGOldGetsG(List<Variable> originalGVars, Dictionary<Variable, Variable> regularToOldGVar)
        {
            List<AssignLhs> lhss_g = new List<AssignLhs>();
            List<Expr> rhss_g = new List<Expr>();
            foreach (Variable g in originalGVars)
            {
                rhss_g.Add(new IdentifierExpr(Token.NoToken, g));
                lhss_g.Add(new SimpleAssignLhs(Token.NoToken, Expr.Ident(regularToOldGVar[g])));
            }

            AssignCmd gOldGetsG = new AssignCmd(Token.NoToken, lhss_g, rhss_g);
            return gOldGetsG;
        }

        private AssignCmd createNewAssignOOldGetsO(Implementation impl, Dictionary<Variable, Variable> regularToOldOVar)
        {
            List<AssignLhs> lhss = new List<AssignLhs>();
            List<Expr> rhss = new List<Expr>();
            foreach (Variable o in impl.OutParams)
            {
                rhss.Add(new IdentifierExpr(Token.NoToken, o));
                lhss.Add(new SimpleAssignLhs(Token.NoToken, Expr.Ident(regularToOldOVar[o])));
            }

            // Implement command list that does o_old := o;
            AssignCmd oOldGetsO = new AssignCmd(Token.NoToken, lhss, rhss);
            return oOldGetsO;
        }

        private AssertCmd createNewAssertGOldEqualsG(List<Variable> originalGVars, Dictionary<Variable, Variable> regularToOldGVar)
        {
            Expr gOldEqualsG = Expr.True;
            foreach (Variable g in originalGVars)
            {
                Expr thisVarEqualsOldVar = Expr.Binary(BinaryOperator.Opcode.Eq,
                                                       new IdentifierExpr(Token.NoToken, g),
                                                       new IdentifierExpr(Token.NoToken, regularToOldGVar[g]));
                gOldEqualsG = Expr.Binary(BinaryOperator.Opcode.And, gOldEqualsG, thisVarEqualsOldVar);
            }

            AssertCmd AssertGOldEqualsGCmd = new AssertCmd(Token.NoToken, gOldEqualsG);
            return AssertGOldEqualsGCmd;
        }

        private Expr createNewGOldEqualsG(List<Variable> originalGVars, Dictionary<Variable, Variable> regularToOldGVar)
        {
            Expr gOldEqualsG = Expr.True;
            foreach (Variable g in originalGVars)
            {
                Expr thisVarEqualsOldVar = Expr.Binary(BinaryOperator.Opcode.Eq,
                                                       new IdentifierExpr(Token.NoToken, g),
                                                       new IdentifierExpr(Token.NoToken, regularToOldGVar[g]));
                gOldEqualsG = Expr.Binary(BinaryOperator.Opcode.And, gOldEqualsG, thisVarEqualsOldVar);
            }

            return gOldEqualsG;
        }

        private AssertCmd createNewAssertOOldEqualsO(Implementation impl, Dictionary<Variable, Variable> regularToOldOVar)
        {
            Expr oOldEqualsO = Expr.True;
            foreach (Variable o in impl.OutParams)
            {
                Expr thisOVarEqualsOldOVar = Expr.Binary(BinaryOperator.Opcode.Eq,
                                                       new IdentifierExpr(Token.NoToken, o),
                                                       new IdentifierExpr(Token.NoToken, regularToOldOVar[o]));
                oOldEqualsO = Expr.Binary(BinaryOperator.Opcode.And, oOldEqualsO, thisOVarEqualsOldOVar);
            }

            AssertCmd AssertOOldEqualsOCmd = new AssertCmd(Token.NoToken, oOldEqualsO);
            return AssertOOldEqualsOCmd;
        }

        private Expr createNewOOldEqualsO(Implementation impl, Dictionary<Variable, Variable> regularToOldOVar)
        {
            Expr oOldEqualsO = Expr.True;
            foreach (Variable o in impl.OutParams)
            {
                Expr thisOVarEqualsOldOVar = Expr.Binary(BinaryOperator.Opcode.Eq,
                                                       new IdentifierExpr(Token.NoToken, o),
                                                       new IdentifierExpr(Token.NoToken, regularToOldOVar[o]));
                oOldEqualsO = Expr.Binary(BinaryOperator.Opcode.And, oOldEqualsO, thisOVarEqualsOldOVar);
            }

            return oOldEqualsO;
        }


        private Expr createNewBetaOfOldOOldGoNg(Implementation impl, List<Variable> originalGVars,
            Dictionary<Variable, Variable> regularToOldGVar,
            Dictionary<Variable, Variable> regularToOldOVar)
        {
            Expr betaOfOOldOGOldG = new TransitionRelationComputation(moverTypeChecker.program,
                                                          moverTypeChecker.procToActionInfo[impl.Proc],
                                                          regularToOldGVar,
                                                          regularToOldOVar).Compute();
            Dictionary<Variable, Expr> map = new Dictionary<Variable, Expr>();
            foreach (Variable v in originalGVars)
            {
                map[v] = Expr.Ident(regularToOldGVar[v]);
            }
            foreach (Variable o in impl.OutParams)
            {
                map[o] = Expr.Ident(regularToOldOVar[o]);
            }

            Substitution always = Substituter.SubstitutionFromHashtable(new Dictionary<Variable, Expr>());
            Substitution forold = Substituter.SubstitutionFromHashtable(map);
            betaOfOOldOGOldG = Substituter.ApplyReplacingOldExprs(always, forold, betaOfOOldOGOldG);
            return betaOfOOldOGOldG;
        }

        private HavocCmd createNewHavocGnO(Implementation impl, List<Variable> originalGVars)
        {
            HavocCmd resultCmd;
            List<IdentifierExpr> varsList = new List<IdentifierExpr>();
            foreach (Variable gv in originalGVars)
            {
                varsList.Add(new IdentifierExpr(Token.NoToken, gv));
            }
            foreach (Variable v in impl.OutParams)
            {
                varsList.Add(new IdentifierExpr(Token.NoToken, v));
            }
            resultCmd = new HavocCmd(Token.NoToken, varsList);
            return resultCmd;
        }

        private void addRefinementCheckingAnnotations(Implementation impl)
        {
            // Create variables for pc, ok
            LocalVariable pc = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, "pc", Bpl.Type.Bool));
            impl.LocVars.Add(pc);
            LocalVariable pc_old = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, "pc_old", Bpl.Type.Bool));
            impl.LocVars.Add(pc_old);
            LocalVariable ok = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, "ok", Bpl.Type.Bool));
            impl.LocVars.Add(ok);


            // Create variables for g_old, o_old
            Dictionary<Variable, Variable> regularToOldGVar = new Dictionary<Variable, Variable>();
            Dictionary<Variable, Variable> regularToOldOVar = new Dictionary<Variable, Variable>();
            ActionInfo actionInfoForThisProc = moverTypeChecker.procToActionInfo[impl.Proc];

            List<Variable> originalGVars = new List<Variable>();
            List<Variable> _old_OriginalGVars = new List<Variable>();


            foreach (GlobalVariable gv in refinementCheckerProgram.GlobalVariables())
                originalGVars.Add(gv);

            foreach (Variable v in refinementCheckerProgram.GlobalVariables()) //ST: Check that this is the right set of global vars
            {
                GlobalVariable gv = v as GlobalVariable;
                LocalVariable v_old = new LocalVariable(Token.NoToken,
                    new TypedIdent(Token.NoToken, gv.TypedIdent.Name + "_old", gv.TypedIdent.Type)); //gv.TypedIdent
                impl.LocVars.Add(v_old); //ST: Ask Shaz whether to add v_old to proc, probably not.
                _old_OriginalGVars.Add(v_old);
                regularToOldGVar.Add(gv, v_old);
            }

            // List<Variable> originalOVars = new List<Variable>();
            List<Variable> _old_OriginalOVars = new List<Variable>();


            //foreach (Variable v in impl.OutParams)
            //    originalOVars.Add(v);

            foreach (Variable o in impl.OutParams) //ST: Check that this is the right set of global vars
            {
                Variable o_old = new LocalVariable(Token.NoToken,
                    new TypedIdent(Token.NoToken, o.TypedIdent.Name + "_old", o.TypedIdent.Type));

                _old_OriginalOVars.Add(o_old);
                regularToOldOVar.Add(o, o_old);
            }


            // AssertCmd AssertONGEqualOldONG = new AssertCmd(Token.NoToken, Expr.Binary(BinaryOperator.Opcode.And, oOldEqualsO, gOldEqualsG));

            // Traverse CFG of impl, replace yield's in place.
            foreach (Block b in impl.Blocks)
            {
                List<Cmd> newCmdList = new List<Cmd>();
                foreach (Cmd c in b.Cmds)
                {
                    if (c is YieldCmd)
                    {
                        // Code to be inserted instead of yield
                        // 
                        // assert !pc ==> \alpha(g_old);
                        // assert ok;
                        // if (o_old == o && g_old == g) { // IfCmd
                        //    // Do nothing
                        // } else {
                        //    assert !pc;
                        //    pc := true;
                        //    assert \beta(o_old, g_old, o, g);
                        // }
                        // havoc o, g;
                        // o_old := o;
                        // g_old := g;

                        // Implementing the following instead:

                        // assert !pc ==> \alpha(g_old);
                        // assert ok;
                        // bb := (o_old == o && g_old == g) 
                        // assert !bb ==> !pc;
                        // pc := ite(!bb,true,pc);
                        // assert !bb ==> \beta(o_old, g_old, o, g);
                        // }
                        // havoc o, g;
                        // o_old := o;
                        // g_old := g;

                        Expr notPcImpliesAlphaOfGoldExpr = Expr.Binary(BinaryOperator.Opcode.Imp,
                                                                       Expr.Unary(Token.NoToken, UnaryOperator.Opcode.Not,
                                                                                    Expr.Ident(pc)),
                                                                       createNewAlphaOfGOld(impl, originalGVars, regularToOldGVar)
                                                                       );
                        AssertCmd notPcImpliesAlphaOfGOldCmd = new AssertCmd(Token.NoToken, notPcImpliesAlphaOfGoldExpr);
                        newCmdList.Add(notPcImpliesAlphaOfGOldCmd);
                        AssertCmd assertOkCmd = new AssertCmd(Token.NoToken, new IdentifierExpr(Token.NoToken, ok));
                        newCmdList.Add(assertOkCmd);

                        Expr bb = Expr.Binary(BinaryOperator.Opcode.And,
                                    createNewOOldEqualsO(impl, regularToOldOVar),
                                    createNewGOldEqualsG(originalGVars, regularToOldGVar));
                        Expr notBB = Expr.Unary(Token.NoToken, UnaryOperator.Opcode.Not, bb);
                        Expr notPC = Expr.Unary(Token.NoToken, UnaryOperator.Opcode.Not, Expr.Ident(pc));
                        AssertCmd assertNotBBImpliesNotPCCmd = new AssertCmd(Token.NoToken, Expr.Imp(notBB, notPC));
                        newCmdList.Add(assertNotBBImpliesNotPCCmd);

                        // pc := ite(!bb,true,pc);
                        List<Expr> iteArgs = new List<Expr>();
                        iteArgs.Add(Expr.Unary(Token.NoToken, UnaryOperator.Opcode.Not, bb));
                        iteArgs.Add(Expr.True);
                        iteArgs.Add(Expr.Ident(pc));
                        Expr pcNew = new NAryExpr(Token.NoToken, new IfThenElse(Token.NoToken), iteArgs);
                        List<AssignLhs> pcUpdateLHS = new List<AssignLhs>();
                        pcUpdateLHS.Add(new SimpleAssignLhs(Token.NoToken, new IdentifierExpr(Token.NoToken, pc)));
                        List<Expr> pcUpdateRHS = new List<Expr>();
                        pcUpdateRHS.Add(pcNew);
                        AssignCmd pcGetsUpdated = new AssignCmd(Token.NoToken, pcUpdateLHS, pcUpdateRHS);

                        // assert !bb ==> \beta(o_old, g_old, o, g);
                        Expr betaExpr = createNewBetaOfOldOOldGoNg(impl, originalGVars, regularToOldGVar, regularToOldOVar);
                        AssertCmd betaAssertCmd = new AssertCmd(Token.NoToken, Expr.Imp(Expr.Unary(Token.NoToken, UnaryOperator.Opcode.Not, bb),
                                                                                        betaExpr));
                        newCmdList.Add(betaAssertCmd);

                        HavocCmd havocOnG = createNewHavocGnO(impl, originalGVars);
                        newCmdList.Add(havocOnG);

                        if (originalGVars.Count > 0)
                        {
                            AssignCmd gOldGetsG = createNewAssignGOldGetsG(originalGVars, regularToOldGVar);
                            newCmdList.Add(gOldGetsG);
                        }

                        if (impl.OutParams.Count > 0)
                        {
                            AssignCmd oOldGetsO = createNewAssignOOldGetsO(impl, regularToOldOVar);
                            newCmdList.Add(oOldGetsO);
                        }
                    }
                    else
                    {
                        newCmdList.Add((Cmd)c.Clone());
                    }
                }
                b.Cmds = newCmdList;
            }
        }

        private sealed class MySubstituter : Duplicator
        {
            private readonly Substitution outsideOld;
            private readonly Substitution insideOld;
            [ContractInvariantMethod]
            void ObjectInvariant()
            {
                Contract.Invariant(insideOld != null);
            }

            public MySubstituter(Substitution outsideOld, Substitution insideOld)
                : base()
            {
                Contract.Requires(outsideOld != null && insideOld != null);
                this.outsideOld = outsideOld;
                this.insideOld = insideOld;
            }

            private bool insideOldExpr = false;

            public override Expr VisitIdentifierExpr(IdentifierExpr node)
            {
                Contract.Ensures(Contract.Result<Expr>() != null);
                Expr e = null;

                if (insideOldExpr)
                {
                    e = insideOld(node.Decl);
                }
                else
                {
                    e = outsideOld(node.Decl);
                }
                return e == null ? base.VisitIdentifierExpr(node) : e;
            }

            public override Expr VisitOldExpr(OldExpr node)
            {
                Contract.Ensures(Contract.Result<Expr>() != null);
                bool previouslyInOld = insideOldExpr;
                insideOldExpr = true;
                Expr tmp = (Expr)this.Visit(node.Expr);
                OldExpr e = new OldExpr(node.tok, tmp);
                insideOldExpr = previouslyInOld;
                return e;
            }
        }

        class TransitionRelationComputation
        {
            private Program program;
            private ActionInfo first;
            private ActionInfo second;
            private Stack<Block> dfsStack;
            private Expr transitionRelation;
            private Dictionary<Variable, Variable> regularToOldGVar;
            private Dictionary<Variable, Variable> regularToOldOVar;

            public TransitionRelationComputation(Program program, ActionInfo second, Dictionary<Variable, Variable> regularToOldGVar, Dictionary<Variable, Variable> regularToOldOVar)
            {
                this.program = program;
                this.first = null;
                this.second = second;
                this.regularToOldGVar = regularToOldGVar;
                this.regularToOldOVar = regularToOldOVar;
                this.dfsStack = new Stack<Block>();
                this.transitionRelation = Expr.False;
            }

            public Expr Compute()
            {
                Search(second.thatAction.Blocks[0], false);
                Dictionary<Variable, Expr> map = new Dictionary<Variable, Expr>();
                List<Variable> boundVars = new List<Variable>();
                if (first != null)
                {
                    foreach (Variable v in first.thisAction.LocVars)
                    {
                        BoundVariable bv = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, v.Name, v.TypedIdent.Type));
                        map[v] = new IdentifierExpr(Token.NoToken, bv);
                        boundVars.Add(bv);
                    }
                }
                foreach (Variable v in second.thatAction.LocVars)
                {
                    BoundVariable bv = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, v.Name, v.TypedIdent.Type));
                    map[v] = new IdentifierExpr(Token.NoToken, bv);
                    boundVars.Add(bv);
                }
                Substitution subst = Substituter.SubstitutionFromHashtable(map);
                if (boundVars.Count > 0)
                    return new ExistsExpr(Token.NoToken, boundVars, Substituter.Apply(subst, transitionRelation));
                else
                    return transitionRelation;
            }

            private Expr CalculatePathCondition()
            {
                Expr returnExpr = Expr.True;
                foreach (Variable v in program.GlobalVariables())
                {
                    var eqExpr = Expr.Eq(new IdentifierExpr(Token.NoToken, v), new OldExpr(Token.NoToken, new IdentifierExpr(Token.NoToken, v)));
                    returnExpr = Expr.And(eqExpr, returnExpr);
                }
                if (first != null)
                {
                    foreach (Variable v in first.thisOutParams)
                    {
                        var eqExpr = Expr.Eq(new IdentifierExpr(Token.NoToken, v), new OldExpr(Token.NoToken, new IdentifierExpr(Token.NoToken, v)));
                        returnExpr = Expr.And(eqExpr, returnExpr);
                    }
                }
                foreach (Variable v in second.thatOutParams)
                {
                    var eqExpr = Expr.Eq(new IdentifierExpr(Token.NoToken, v), new OldExpr(Token.NoToken, new IdentifierExpr(Token.NoToken, v)));
                    returnExpr = Expr.And(eqExpr, returnExpr);
                }
                Block[] dfsStackAsArray = dfsStack.Reverse().ToArray();
                for (int i = dfsStackAsArray.Length - 1; i >= 0; i--)
                {
                    Block b = dfsStackAsArray[i];
                    for (int j = b.Cmds.Count - 1; j >= 0; j--)
                    {
                        Cmd cmd = b.Cmds[j];
                        if (cmd is AssumeCmd)
                        {
                            AssumeCmd assumeCmd = cmd as AssumeCmd;
                            returnExpr = Expr.And(new OldExpr(Token.NoToken, assumeCmd.Expr), returnExpr);
                        }
                        else if (cmd is AssignCmd)
                        {
                            AssignCmd assignCmd = (cmd as AssignCmd).AsSimpleAssignCmd;
                            Dictionary<Variable, Expr> map = new Dictionary<Variable, Expr>();
                            for (int k = 0; k < assignCmd.Lhss.Count; k++)
                            {
                                map[assignCmd.Lhss[k].DeepAssignedVariable] = assignCmd.Rhss[k];
                            }
                            Substitution subst = Substituter.SubstitutionFromHashtable(new Dictionary<Variable, Expr>());
                            Substitution oldSubst = Substituter.SubstitutionFromHashtable(map);
                            returnExpr = (Expr)new MySubstituter(subst, oldSubst).Visit(returnExpr);
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                }
                return returnExpr;
            }

            private void Search(Block b, bool inFirst)
            {
                dfsStack.Push(b);
                if (b.TransferCmd is ReturnExprCmd)
                {
                    if (first == null || inFirst)
                    {
                        transitionRelation = Expr.Or(transitionRelation, CalculatePathCondition());
                    }
                    else
                    {
                        Search(first.thisAction.Blocks[0], true);
                    }
                }
                else
                {
                    GotoCmd gotoCmd = b.TransferCmd as GotoCmd;
                    foreach (Block target in gotoCmd.labelTargets)
                    {
                        Search(target, inFirst);
                    }
                }
                dfsStack.Pop();
            }
        }
    }
}