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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
namespace Microsoft.Boogie {
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using BoogiePL=Microsoft.Boogie;
using System.Diagnostics;
using System.Text.RegularExpressions; // for procedure inlining
public delegate void InlineCallback(Implementation/*!*/ impl);
public class Inliner : Duplicator {
protected bool inlinedSomething;
protected Program program;
private InlineCallback inlineCallback;
protected CodeCopier/*!*/ codeCopier;
protected Dictionary<string/*!*/, int>/*!*/ /* Procedure.Name -> int */ recursiveProcUnrollMap;
protected Dictionary<string/*!*/, int>/*!*/ /* Procedure.Name -> int */ inlinedProcLblMap;
protected int inlineDepth;
protected List<Variable>/*!*/ newLocalVars;
protected List<IdentifierExpr>/*!*/ newModifies;
protected string prefix;
[ContractInvariantMethod]
void ObjectInvariant() {
Contract.Invariant(program != null);
Contract.Invariant(newLocalVars != null);
Contract.Invariant(newModifies != null);
Contract.Invariant(codeCopier != null);
Contract.Invariant(recursiveProcUnrollMap != null);
Contract.Invariant(inlinedProcLblMap != null);
}
public override Expr VisitCodeExpr(CodeExpr node)
{
Inliner codeExprInliner = new Inliner(program, inlineCallback, CommandLineOptions.Clo.InlineDepth);
codeExprInliner.newLocalVars.AddRange(node.LocVars);
codeExprInliner.inlinedProcLblMap = this.inlinedProcLblMap;
List<Block> newCodeExprBlocks = codeExprInliner.DoInlineBlocks(node.Blocks, ref inlinedSomething);
return new CodeExpr(codeExprInliner.newLocalVars, newCodeExprBlocks);
}
protected void NextInlinedProcLabel(string procName) {
Contract.Requires(procName != null);
int currentId;
if (inlinedProcLblMap.TryGetValue(procName, out currentId)) {
inlinedProcLblMap[procName] = currentId + 1;
} else {
inlinedProcLblMap.Add(procName, 0);
}
}
protected string GetInlinedProcLabel(string procName) {
Contract.Requires(procName != null);
Contract.Ensures(Contract.Result<string>() != null);
return prefix + procName + "$" + inlinedProcLblMap[procName];
}
protected string GetProcVarName(string procName, string formalName) {
Contract.Requires(formalName != null);
Contract.Requires(procName != null);
Contract.Ensures(Contract.Result<string>() != null);
return GetInlinedProcLabel(procName) + "$" + formalName;
}
public Inliner(Program program, InlineCallback cb, int inlineDepth) {
this.program = program;
this.inlinedProcLblMap = new Dictionary<string/*!*/, int>();
this.recursiveProcUnrollMap = new Dictionary<string/*!*/, int>();
this.inlineDepth = inlineDepth;
this.codeCopier = new CodeCopier();
this.inlineCallback = cb;
this.newLocalVars = new List<Variable>();
this.newModifies = new List<IdentifierExpr>();
this.prefix = null;
}
// This method calculates a prefix (storing it in the prefix field) so that prepending it to any string
// is guaranteed not to create a conflict with the names of variables and blocks in scope inside impl.
protected void ComputePrefix(Program program, Implementation impl)
{
this.prefix = "inline$";
foreach (var v in impl.InParams)
{
DistinguishPrefix(v.Name);
}
foreach (var v in impl.OutParams)
{
DistinguishPrefix(v.Name);
}
foreach (var v in impl.LocVars)
{
DistinguishPrefix(v.Name);
}
foreach (var v in program.GlobalVariables)
{
DistinguishPrefix(v.Name);
}
foreach (Block b in impl.Blocks)
{
DistinguishPrefix(b.Label);
}
}
private void DistinguishPrefix(string s)
{
if (!s.StartsWith(prefix)) return;
for (int i = prefix.Length; i < s.Length; i++)
{
prefix = prefix + "$";
if (s[i] != '$') break;
}
if (prefix == s)
{
prefix = prefix + "$";
}
}
protected static void ProcessImplementation(Program program, Implementation impl, Inliner inliner) {
Contract.Requires(impl != null);
Contract.Requires(impl.Proc != null);
inliner.ComputePrefix(program, impl);
inliner.newLocalVars.AddRange(impl.LocVars);
inliner.newModifies.AddRange(impl.Proc.Modifies);
bool inlined = false;
List<Block> newBlocks = inliner.DoInlineBlocks(impl.Blocks, ref inlined);
Contract.Assert(cce.NonNullElements(newBlocks));
if (!inlined)
return;
impl.InParams = new List<Variable>(impl.InParams);
impl.OutParams = new List<Variable>(impl.OutParams);
impl.LocVars = inliner.newLocalVars;
impl.Proc.Modifies = inliner.newModifies;
impl.Blocks = newBlocks;
impl.ResetImplFormalMap();
// we need to resolve the new code
inliner.ResolveImpl(impl);
if (CommandLineOptions.Clo.PrintInlined) {
inliner.EmitImpl(impl);
}
}
public static void ProcessImplementationForHoudini(Program program, Implementation impl) {
Contract.Requires(impl != null);
Contract.Requires(program != null);
Contract.Requires(impl.Proc != null);
ProcessImplementation(program, impl, new Inliner(program, null, CommandLineOptions.Clo.InlineDepth));
}
public static void ProcessImplementation(Program program, Implementation impl) {
Contract.Requires(impl != null);
Contract.Requires(program != null);
Contract.Requires(impl.Proc != null);
ProcessImplementation(program, impl, new Inliner(program, null, -1));
}
protected void EmitImpl(Implementation impl) {
Contract.Requires(impl != null);
Contract.Requires(impl.Proc != null);
Console.WriteLine("after inlining procedure calls");
impl.Proc.Emit(new TokenTextWriter("<console>", Console.Out, /*pretty=*/ false), 0);
impl.Emit(new TokenTextWriter("<console>", Console.Out, /*pretty=*/ false), 0);
}
private sealed class DummyErrorSink : IErrorSink {
public void Error(IToken tok, string msg) {
//Contract.Requires(msg != null);
//Contract.Requires(tok != null);
// FIXME
// noop.
// This is required because during the resolution, some resolution errors happen
// (such as the ones caused addion of loop invariants J_(block.Label) by the AI package
}
}
protected void ResolveImpl(Implementation impl) {
Contract.Requires(impl != null);
Contract.Ensures(impl.Proc != null);
ResolutionContext rc = new ResolutionContext(new DummyErrorSink());
foreach (var decl in program.TopLevelDeclarations) {
decl.Register(rc);
}
impl.Proc = null; // to force Resolve() redo the operation
impl.Resolve(rc);
TypecheckingContext tc = new TypecheckingContext(new DummyErrorSink());
impl.Typecheck(tc);
}
// Redundant for this class; but gives a chance for other classes to
// override this and implement their own inlining policy
protected virtual int GetInlineCount(CallCmd callCmd, Implementation impl)
{
return GetInlineCount(impl);
}
// returns true if it is ok to further unroll the procedure
// otherwise, the procedure is not inlined at the call site
protected int GetInlineCount(Implementation impl) {
Contract.Requires(impl != null);
Contract.Requires(impl.Proc != null);
string/*!*/ procName = impl.Name;
Contract.Assert(procName != null);
int c;
if (recursiveProcUnrollMap.TryGetValue(procName, out c)) {
return c;
}
c = -1; // TryGetValue above always overwrites c
impl.CheckIntAttribute("inline", ref c);
// procedure attribute overrides implementation
impl.Proc.CheckIntAttribute("inline", ref c);
recursiveProcUnrollMap[procName] = c;
return c;
}
void CheckRecursion(Implementation impl, Stack<Procedure/*!*/>/*!*/ callStack) {
Contract.Requires(impl != null);
Contract.Requires(cce.NonNullElements(callStack));
foreach (Procedure/*!*/ p in callStack) {
Contract.Assert(p != null);
if (p == impl.Proc) {
string msg = "";
foreach (Procedure/*!*/ q in callStack) {
Contract.Assert(q != null);
msg = q.Name + " -> " + msg;
}
msg += p.Name;
//checkingCtx.Error(impl, "inlined procedure is recursive, call stack: {0}", msg);
}
}
}
private int InlineCallCmd(Block block, CallCmd callCmd, Implementation impl, List<Cmd> newCmds, List<Block> newBlocks, int lblCount)
{
Contract.Assume(impl != null);
Contract.Assert(cce.NonNull(impl.OriginalBlocks).Count > 0);
// do inline now
int nextlblCount = lblCount + 1;
string nextBlockLabel = block.Label + "$" + nextlblCount;
// run the callback before each inline
if (inlineCallback != null)
{
inlineCallback(impl);
}
// increment the counter for the procedure to be used in constructing the locals and formals
NextInlinedProcLabel(impl.Proc.Name);
BeginInline(impl);
List<Block/*!*/>/*!*/ inlinedBlocks = CreateInlinedBlocks(callCmd, impl, nextBlockLabel);
Contract.Assert(cce.NonNullElements(inlinedBlocks));
EndInline();
if (inlineDepth >= 0)
{
Debug.Assert(inlineDepth > 0);
inlineDepth = inlineDepth - 1;
}
else
{
recursiveProcUnrollMap[impl.Name] = recursiveProcUnrollMap[impl.Name] - 1;
}
bool inlinedSomething = true;
inlinedBlocks = DoInlineBlocks(inlinedBlocks, ref inlinedSomething);
if (inlineDepth >= 0)
{
inlineDepth = inlineDepth + 1;
}
else
{
recursiveProcUnrollMap[impl.Name] = recursiveProcUnrollMap[impl.Name] + 1;
}
Block/*!*/ startBlock = inlinedBlocks[0];
Contract.Assert(startBlock != null);
GotoCmd gotoCmd = new GotoCmd(Token.NoToken, new List<String> { startBlock.Label });
Block newBlock = new Block(block.tok, ((lblCount == 0) ? (block.Label) : (block.Label + "$" + lblCount)), newCmds, gotoCmd);
newBlocks.Add(newBlock);
newBlocks.AddRange(inlinedBlocks);
return nextlblCount;
}
public virtual List<Block/*!*/>/*!*/ DoInlineBlocks(List<Block/*!*/>/*!*/ blocks, ref bool inlinedSomething) {
Contract.Requires(cce.NonNullElements(blocks));
Contract.Ensures(cce.NonNullElements(Contract.Result<List<Block>>()));
List<Block/*!*/>/*!*/ newBlocks = new List<Block/*!*/>();
foreach (Block block in blocks) {
TransferCmd/*!*/ transferCmd = cce.NonNull(block.TransferCmd);
List<Cmd> cmds = block.Cmds;
List<Cmd> newCmds = new List<Cmd>();
int lblCount = 0;
for (int i = 0; i < cmds.Count; ++i)
{
Cmd cmd = cmds[i];
if (cmd is CallCmd)
{
CallCmd callCmd = (CallCmd)cmd;
Implementation impl = FindProcImpl(program, callCmd.Proc);
if (impl == null)
{
newCmds.Add(codeCopier.CopyCmd(callCmd));
continue;
}
int inline = inlineDepth >= 0 ? inlineDepth : GetInlineCount(callCmd, impl);
if (inline > 0)
{
inlinedSomething = true;
lblCount = InlineCallCmd(block, callCmd, impl, newCmds, newBlocks, lblCount);
newCmds = new List<Cmd>();
}
else if (inline == 0)
{
inlinedSomething = true;
if (CommandLineOptions.Clo.ProcedureInlining == CommandLineOptions.Inlining.Assert)
{
// add assert
newCmds.Add(new AssertCmd(callCmd.tok, Expr.False));
}
else if (CommandLineOptions.Clo.ProcedureInlining == CommandLineOptions.Inlining.Assume)
{
// add assume
newCmds.Add(new AssumeCmd(callCmd.tok, Expr.False));
}
else
{
// add call
newCmds.Add(codeCopier.CopyCmd(callCmd));
}
}
else
{
newCmds.Add(codeCopier.CopyCmd(callCmd));
}
}
else if (cmd is PredicateCmd)
{
PredicateCmd predCmd = (PredicateCmd)cmd;
this.inlinedSomething = false;
Expr newExpr = this.VisitExpr(predCmd.Expr);
if (this.inlinedSomething)
{
inlinedSomething = true;
PredicateCmd newPredCmd = (PredicateCmd)codeCopier.CopyCmd(predCmd);
newPredCmd.Expr = newExpr;
newCmds.Add(newPredCmd);
}
else
{
newCmds.Add(codeCopier.CopyCmd(predCmd));
}
}
else if (cmd is AssignCmd)
{
AssignCmd assignCmd = (AssignCmd)cmd;
this.inlinedSomething = false;
List<Expr> newRhss = new List<Expr>();
foreach (Expr rhsExpr in assignCmd.Rhss)
{
newRhss.Add(this.VisitExpr(rhsExpr));
}
if (this.inlinedSomething)
{
inlinedSomething = true;
AssignCmd newAssignCmd = (AssignCmd)codeCopier.CopyCmd(assignCmd);
newAssignCmd.Rhss = newRhss;
newCmds.Add(newAssignCmd);
}
else
{
newCmds.Add(codeCopier.CopyCmd(assignCmd));
}
}
else
{
newCmds.Add(codeCopier.CopyCmd(cmd));
}
}
Block newBlock = new Block(block.tok, ((lblCount == 0) ? (block.Label) : (block.Label + "$" + lblCount)), newCmds, codeCopier.CopyTransferCmd(transferCmd));
newBlocks.Add(newBlock);
}
return newBlocks;
}
protected void BeginInline(Implementation impl) {
Contract.Requires(impl != null);
Contract.Requires(impl.Proc != null);
Contract.Requires(newModifies != null);
Contract.Requires(newLocalVars != null);
Dictionary<Variable, Expr> substMap = new Dictionary<Variable, Expr>();
Procedure proc = impl.Proc;
foreach (Variable/*!*/ locVar in cce.NonNull(impl.OriginalLocVars)) {
Contract.Assert(locVar != null);
LocalVariable localVar = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, GetProcVarName(proc.Name, locVar.Name), locVar.TypedIdent.Type, locVar.TypedIdent.WhereExpr));
localVar.Attributes = locVar.Attributes; // copy attributes
newLocalVars.Add(localVar);
IdentifierExpr ie = new IdentifierExpr(Token.NoToken, localVar);
substMap.Add(locVar, ie);
}
for (int i = 0; i < impl.InParams.Count; i++) {
Variable inVar = cce.NonNull(impl.InParams[i]);
LocalVariable localVar = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, GetProcVarName(proc.Name, inVar.Name), inVar.TypedIdent.Type, inVar.TypedIdent.WhereExpr));
newLocalVars.Add(localVar);
if (impl.Proc != null) localVar.Attributes = impl.Proc.InParams[i].Attributes; // copy attributes
IdentifierExpr ie = new IdentifierExpr(Token.NoToken, localVar);
substMap.Add(inVar, ie);
// also add a substitution from the corresponding formal occurring in the PROCEDURE declaration
Variable procInVar = cce.NonNull(proc.InParams[i]);
if (procInVar != inVar) {
substMap.Add(procInVar, ie);
}
}
for (int i = 0; i < impl.OutParams.Count; i++) {
Variable outVar = cce.NonNull(impl.OutParams[i]);
LocalVariable localVar = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, GetProcVarName(proc.Name, outVar.Name), outVar.TypedIdent.Type, outVar.TypedIdent.WhereExpr));
if (impl.Proc != null) localVar.Attributes = impl.Proc.OutParams[i].Attributes; // copy attributes
newLocalVars.Add(localVar);
IdentifierExpr ie = new IdentifierExpr(Token.NoToken, localVar);
substMap.Add(outVar, ie);
// also add a substitution from the corresponding formal occurring in the PROCEDURE declaration
Variable procOutVar = cce.NonNull(proc.OutParams[i]);
if (procOutVar != outVar) {
substMap.Add(procOutVar, ie);
}
}
Dictionary<Variable, Expr> substMapOld = new Dictionary<Variable, Expr>();
foreach (IdentifierExpr/*!*/ mie in proc.Modifies) {
Contract.Assert(mie != null);
Variable/*!*/ mVar = cce.NonNull(mie.Decl);
LocalVariable localVar = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, GetProcVarName(proc.Name, mVar.Name), mVar.TypedIdent.Type));
newLocalVars.Add(localVar);
IdentifierExpr ie = new IdentifierExpr(Token.NoToken, localVar);
substMapOld.Add(mVar, ie);
// FIXME why are we doing this? the modifies list should already include them.
// add the modified variable to the modifies list of the procedure
if (!newModifies.Contains(mie)) {
newModifies.Add(mie);
}
}
codeCopier.Subst = Substituter.SubstitutionFromHashtable(substMap);
codeCopier.OldSubst = Substituter.SubstitutionFromHashtable(substMapOld);
}
protected void EndInline() {
codeCopier.Subst = null;
codeCopier.OldSubst = null;
}
private Cmd InlinedRequires(CallCmd callCmd, Requires req) {
Requires/*!*/ reqCopy = (Requires/*!*/)cce.NonNull(req.Clone());
if (req.Free)
reqCopy.Condition = Expr.True;
else
reqCopy.Condition = codeCopier.CopyExpr(req.Condition);
AssertCmd/*!*/ a = new AssertRequiresCmd(callCmd, reqCopy);
a.ErrorDataEnhanced = reqCopy.ErrorDataEnhanced;
return a;
}
private Cmd InlinedEnsures(CallCmd callCmd, Ensures ens) {
if (QKeyValue.FindBoolAttribute(ens.Attributes, "InlineAssume")) {
return new AssumeCmd(ens.tok, codeCopier.CopyExpr(ens.Condition));
} else if (ens.Free) {
return new AssumeCmd(ens.tok, Expr.True);
} else {
Ensures/*!*/ ensCopy = (Ensures/*!*/)cce.NonNull(ens.Clone());
ensCopy.Condition = codeCopier.CopyExpr(ens.Condition);
return new AssertEnsuresCmd(ensCopy);
}
}
private List<Cmd> RemoveAsserts(List<Cmd> cmds) {
List<Cmd> newCmdSeq = new List<Cmd>();
for (int i = 0; i < cmds.Count; i++) {
Cmd cmd = cmds[i];
if (cmd is AssertCmd) continue;
newCmdSeq.Add(cmd);
}
return newCmdSeq;
}
// result[0] is the entry block
protected List<Block/*!*/>/*!*/ CreateInlinedBlocks(CallCmd callCmd, Implementation impl, string nextBlockLabel) {
Contract.Requires(nextBlockLabel != null);
Contract.Requires(impl != null);
Contract.Requires(impl.Proc != null);
Contract.Requires(callCmd != null);
Contract.Requires(codeCopier.Subst != null);
Contract.Requires(codeCopier.OldSubst != null);
Contract.Ensures(cce.NonNullElements(Contract.Result<List<Block>>()));
List<Block/*!*/>/*!*/ implBlocks = cce.NonNull(impl.OriginalBlocks);
Contract.Assert(implBlocks.Count > 0);
Procedure proc = impl.Proc;
string startLabel = implBlocks[0].Label;
List<Block/*!*/>/*!*/ inlinedBlocks = new List<Block/*!*/>();
// create in block
List<Cmd> inCmds = new List<Cmd>();
// assign in parameters
for (int i = 0; i < impl.InParams.Count; ++i) {
Cmd cmd = Cmd.SimpleAssign(impl.tok,
(IdentifierExpr)cce.NonNull(codeCopier.Subst)(cce.NonNull(impl.InParams[i])),
cce.NonNull(callCmd.Ins[i]));
inCmds.Add(cmd);
}
// inject requires
for (int i = 0; i < proc.Requires.Count; i++) {
Requires/*!*/ req = cce.NonNull(proc.Requires[i]);
inCmds.Add(InlinedRequires(callCmd, req));
}
List<Variable> locVars = cce.NonNull(impl.OriginalLocVars);
// havoc locals and out parameters in case procedure is invoked in a loop
List<IdentifierExpr> havocVars = new List<IdentifierExpr>();
foreach (Variable v in locVars)
{
havocVars.Add((IdentifierExpr)codeCopier.Subst(v));
}
foreach (Variable v in impl.OutParams)
{
havocVars.Add((IdentifierExpr)codeCopier.Subst(v));
}
if (havocVars.Count > 0)
{
inCmds.Add(new HavocCmd(Token.NoToken, havocVars));
}
// add where clauses of local vars as assume
for (int i = 0; i < locVars.Count; ++i) {
Expr whereExpr = (cce.NonNull(locVars[i])).TypedIdent.WhereExpr;
if (whereExpr != null) {
whereExpr = Substituter.Apply(codeCopier.Subst, whereExpr);
// FIXME we cannot overwrite it, can we?!
(cce.NonNull(locVars[i])).TypedIdent.WhereExpr = whereExpr;
AssumeCmd/*!*/ a = new AssumeCmd(Token.NoToken, whereExpr);
Contract.Assert(a != null);
inCmds.Add(a);
}
}
// add where clauses of output params as assume
for (int i = 0; i < impl.OutParams.Count; ++i) {
Expr whereExpr = (cce.NonNull(impl.OutParams[i])).TypedIdent.WhereExpr;
if (whereExpr != null) {
whereExpr = Substituter.Apply(codeCopier.Subst, whereExpr);
// FIXME likewise
(cce.NonNull(impl.OutParams[i])).TypedIdent.WhereExpr = whereExpr;
AssumeCmd/*!*/ a = new AssumeCmd(Token.NoToken, whereExpr);
Contract.Assert(a != null);
inCmds.Add(a);
}
}
// assign modifies old values
foreach (IdentifierExpr/*!*/ mie in proc.Modifies) {
Contract.Assert(mie != null);
Variable/*!*/ mvar = cce.NonNull(mie.Decl);
AssignCmd assign = Cmd.SimpleAssign(impl.tok, (IdentifierExpr)cce.NonNull(codeCopier.OldSubst(mvar)), mie);
inCmds.Add(assign);
}
GotoCmd inGotoCmd = new GotoCmd(callCmd.tok, new List<String> { GetInlinedProcLabel(proc.Name) + "$" + startLabel });
Block inBlock = new Block(impl.tok, GetInlinedProcLabel(proc.Name) + "$Entry", inCmds, inGotoCmd);
inlinedBlocks.Add(inBlock);
// inject the blocks of the implementation
Block intBlock;
foreach (Block block in implBlocks) {
List<Cmd> copyCmds = codeCopier.CopyCmdSeq(block.Cmds);
if (0 <= inlineDepth) {
copyCmds = RemoveAsserts(copyCmds);
}
TransferCmd transferCmd = CreateInlinedTransferCmd(cce.NonNull(block.TransferCmd), GetInlinedProcLabel(proc.Name));
intBlock = new Block(block.tok, GetInlinedProcLabel(proc.Name) + "$" + block.Label, copyCmds, transferCmd);
inlinedBlocks.Add(intBlock);
}
// create out block
List<Cmd> outCmds = new List<Cmd>();
// inject ensures
for (int i = 0; i < proc.Ensures.Count; i++) {
Ensures/*!*/ ens = cce.NonNull(proc.Ensures[i]);
outCmds.Add(InlinedEnsures(callCmd, ens));
}
// assign out params
for (int i = 0; i < impl.OutParams.Count; ++i) {
Expr/*!*/ cout_exp = (IdentifierExpr)cce.NonNull(codeCopier.Subst(cce.NonNull(impl.OutParams[i])));
Cmd cmd = Cmd.SimpleAssign(impl.tok, cce.NonNull(callCmd.Outs[i]), cout_exp);
outCmds.Add(cmd);
}
// create out block
GotoCmd outGotoCmd = new GotoCmd(Token.NoToken, new List<String> { nextBlockLabel });
Block outBlock = new Block(impl.tok, GetInlinedProcLabel(proc.Name) + "$Return", outCmds, outGotoCmd);
inlinedBlocks.Add(outBlock);
return inlinedBlocks;
}
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;
}
protected static Implementation FindProcImpl(Program program, Procedure proc) {
Contract.Requires(program != null);
foreach (var impl in program.Implementations) {
if (impl.Proc == proc) {
return impl;
}
}
return null;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class CodeCopier {
public Substitution Subst;
public Substitution OldSubst;
public CodeCopier(Dictionary<Variable, Expr> substMap) {
Contract.Requires(substMap != null);
Subst = Substituter.SubstitutionFromHashtable(substMap);
}
public CodeCopier(Dictionary<Variable, Expr> substMap, Dictionary<Variable, Expr> oldSubstMap) {
Contract.Requires(oldSubstMap != null);
Contract.Requires(substMap != null);
Subst = Substituter.SubstitutionFromHashtable(substMap);
OldSubst = Substituter.SubstitutionFromHashtable(oldSubstMap);
}
public CodeCopier() {
}
public List<Cmd> CopyCmdSeq(List<Cmd> cmds) {
Contract.Requires(cmds != null);
Contract.Ensures(Contract.Result<List<Cmd>>() != null);
List<Cmd> newCmds = new List<Cmd>();
foreach (Cmd/*!*/ cmd in cmds) {
Contract.Assert(cmd != null);
newCmds.Add(CopyCmd(cmd));
}
return newCmds;
}
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;
}
public Cmd CopyCmd(Cmd cmd) {
Contract.Requires(cmd != null);
Contract.Ensures(Contract.Result<Cmd>() != null);
if (Subst == null) {
return cmd;
} else if (OldSubst == null) {
return Substituter.Apply(Subst, cmd);
} else {
return Substituter.ApplyReplacingOldExprs(Subst, OldSubst, cmd);
}
}
public Expr CopyExpr(Expr expr) {
Contract.Requires(expr != null);
Contract.Ensures(Contract.Result<Expr>() != null);
if (Subst == null) {
return expr;
} else if (OldSubst == null) {
return Substituter.Apply(Subst, expr);
} else {
return Substituter.ApplyReplacingOldExprs(Subst, OldSubst, expr);
}
}
} // end class CodeCopier
} // end namespace
|