Skip to content

Commit a5a189d

Browse files
authored
Merge pull request #482 from viperproject/old_state_refactoring
Use local variables for the old state
2 parents 6e4ce2f + 245e01d commit a5a189d

4 files changed

Lines changed: 16 additions & 20 deletions

File tree

src/main/scala/viper/carbon/modules/impls/DefaultFuncPredModule.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,6 @@ with DefinednessComponent with ExhaleComponent with InhaleComponent {
623623
val res = sil.Result(f.typ)()
624624
val init : Stmt = MaybeCommentBlock("Initializing the state",
625625
stateModule.initBoogieState ++ (f.formalArgs map (a => allAssumptionsAboutValue(a.typ,mainModule.translateLocalVarDecl(a),true))) ++ assumeFunctionsAt(heights(f.name)))
626-
val initOld : Stmt = MaybeCommentBlock("Initializing the old state", stateModule.initOldState)
627626
val checkPre : Stmt = checkFunctionPreconditionDefinedness(f)
628627
val checkExp : Stmt = if (f.isAbstract) MaybeCommentBlock("(no definition for abstract function)",Nil) else
629628
MaybeCommentBlock("Check definedness of function body",
@@ -632,7 +631,7 @@ with DefinednessComponent with ExhaleComponent with InhaleComponent {
632631
MaybeCommentBlock("Translate function body",
633632
translateResult(res) := translateExp(f.body.get))
634633
val checkPost = checkFunctionPostconditionDefinedness(f)
635-
val body : Stmt = Seq(init, initOld, checkPre, checkExp, exp, checkPost)
634+
val body : Stmt = Seq(init, checkPre, checkExp, exp, checkPost)
636635
val definednessChecks = Procedure(Identifier(f.name + "#definedness"), args, translateResultDecl(res), body)
637636
checkingDefinednessOfFunction = None
638637
definednessChecks

src/main/scala/viper/carbon/modules/impls/DefaultHeapModule.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class DefaultHeapModule(val verifier: Verifier)
8282
private val qpHeap = LocalVar(qpHeapName, heapTyp)
8383
private var heap: Var = originalHeap
8484
private def heapVar: Var = {assert (!usingOldState); heap}
85-
private def heapExp: Exp = if (usingPureState) dummyHeap else (if (usingOldState) Old(heap) else heap)
85+
private def heapExp: Exp = if (usingPureState) dummyHeap else heap
8686
private val nullName = Identifier("null")
8787
private val nullLit = Const(nullName)
8888
private val freshObjectName = Identifier("freshObj")
@@ -774,10 +774,6 @@ class DefaultHeapModule(val verifier: Verifier)
774774
def resetBoogieState: Stmt = {
775775
Havoc(heapVar)
776776
}
777-
def initOldState: Stmt = {
778-
val hVar = heapVar
779-
Assume(Old(hVar) === hVar)
780-
}
781777

782778
def staticStateContributions(withHeap: Boolean, withPermissions: Boolean): Seq[LocalVarDecl] = if(withHeap) Seq(LocalVarDecl(heapName, heapTyp)) else Seq()
783779
def currentStateContributions: Seq[LocalVarDecl] = Seq(LocalVarDecl(heap.name, heapTyp))

src/main/scala/viper/carbon/modules/impls/DefaultStateModule.scala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class DefaultStateModule(val verifier: Verifier) extends StateModule {
6363

6464
// initialize the state of all components and assume that afterwards the
6565
// whole state is good
66-
val firstStmt = components map (_.initBoogieState)
66+
val firstStmt = components map (_.initBoogieState)
6767
// note: this code should come afterwards, to allow the components to reset their state variables
6868
for (c <- components) {
6969
curState.put(c, c.currentStateVars)
@@ -85,12 +85,9 @@ class DefaultStateModule(val verifier: Verifier) extends StateModule {
8585
}
8686

8787
def initOldState: Stmt = {
88-
curOldState = new StateComponentMapping()
89-
for (c <- components) yield {
90-
val exps = curState.get(c)
91-
curOldState.put(c, exps) // Logic: whenever we *get* on the old state, we should wrap in "Old"
92-
exps map (e => Assume(e === Old(e))): Stmt
93-
}
88+
val freshSnapshot = freshTempStateKeepCurrentAux("old", true)
89+
curOldState = freshSnapshot._1
90+
initToCurrentStmt(freshSnapshot)
9491
}
9592

9693

@@ -104,7 +101,7 @@ class DefaultStateModule(val verifier: Verifier) extends StateModule {
104101
res ++= hashMap.get(c)
105102
}
106103
}
107-
if(usingOldState) (res map (v => Old(v))) else res // ALEX: I think this conditional should be on the element of the StateSnapshot
104+
res
108105
}
109106

110107

@@ -130,6 +127,8 @@ class DefaultStateModule(val verifier: Verifier) extends StateModule {
130127
override def stateRepositoryGet(name:String) : Option[StateSnapshot] = stateRepository.get(name)
131128

132129
override def freshTempState(name: String, discardCurrent: Boolean = false, initialise: Boolean = false): (Stmt, StateSnapshot) = {
130+
assert(name != "old")
131+
133132
val previousState = new StateSnapshot(new StateComponentMapping(), usingOldState, usingPureState)
134133

135134
curState = new StateComponentMapping() // essentially, the code below "clones" what curState should represent anyway. But, if we omit this line, we inadvertently alias the previous hash map.
@@ -149,6 +148,12 @@ class DefaultStateModule(val verifier: Verifier) extends StateModule {
149148
}
150149

151150
override def freshTempStateKeepCurrent(name: String) : StateSnapshot = {
151+
freshTempStateKeepCurrentAux(name, false)
152+
}
153+
154+
private def freshTempStateKeepCurrentAux(name: String, usedForOldState: Boolean) : StateSnapshot = {
155+
assert(usedForOldState || name != "old")
156+
152157
val freshState = new StateComponentMapping()
153158

154159
for (c <- components) yield {

src/main/scala/viper/carbon/modules/impls/QuantifiedPermModule.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class QuantifiedPermModule(val verifier: Verifier)
8585
private val originalMask = GlobalVar(maskName, maskType)
8686
private var mask: Var = originalMask // When reading, don't use this directly: use either maskVar or maskExp as needed
8787
private def maskVar : Var = {assert (!usingOldState); mask}
88-
private def maskExp : Exp = if (usingPureState) zeroMask else (if (usingOldState) Old(mask) else mask)
88+
private def maskExp : Exp = if (usingPureState) zeroMask else mask
8989
private val zeroMaskName = Identifier("ZeroMask")
9090
private val zeroMask = Const(zeroMaskName)
9191
private val zeroPMaskName = Identifier("ZeroPMask")
@@ -252,10 +252,6 @@ class QuantifiedPermModule(val verifier: Verifier)
252252
def resetBoogieState = {
253253
(maskVar := zeroMask)
254254
}
255-
def initOldState: Stmt = {
256-
val mVar = maskVar
257-
Assume(Old(mVar) === mVar)
258-
}
259255

260256
override def reset = {
261257
mask = originalMask

0 commit comments

Comments
 (0)