Fix snapshot code
[model-checker.git] / scfence / patch.cc
1 #include "patch.h"
2 #include "inference.h"
3
4 Patch::Patch(const ModelAction *act, memory_order mo) {
5         PatchUnit *unit = new PatchUnit(act, mo);
6         units = new SnapVector<PatchUnit*>;
7         units->push_back(unit);
8 }
9
10 Patch::Patch(const ModelAction *act1, memory_order mo1, const ModelAction *act2, memory_order mo2) {
11         units = new SnapVector<PatchUnit*>;
12         PatchUnit *unit = new PatchUnit(act1, mo1);
13         units->push_back(unit);
14         unit = new PatchUnit(act2, mo2);
15         units->push_back(unit);
16 }
17
18 Patch::Patch() {
19         units = new SnapVector<PatchUnit*>;
20 }
21
22 bool Patch::canStrengthen(Inference *curInfer) {
23         if (!isApplicable())
24                 return false;
25         bool res = false;
26         for (unsigned i = 0; i < units->size(); i++) {
27                 PatchUnit *u = (*units)[i];
28                 memory_order wildcard = u->getAct()->get_original_mo();
29                 memory_order curMO = (*curInfer)[get_wildcard_id(wildcard)];
30                 if (u->getMO() != curMO)
31                         res = true;
32         }
33         return res;
34 }
35
36 bool Patch::isApplicable() {
37         for (unsigned i = 0; i < units->size(); i++) {
38                 PatchUnit *u = (*units)[i];
39                 memory_order wildcard = u->getAct()->get_original_mo();
40                 if (is_wildcard(wildcard))
41                         continue;
42                 int compVal = Inference::compareMemoryOrder(wildcard, u->getMO());
43                 if (compVal == 2 || compVal == -1)
44                         return false;
45         }
46         return true;
47 }
48
49 void Patch::addPatchUnit(const ModelAction *act, memory_order mo) {
50         PatchUnit *unit = new PatchUnit(act, mo);
51         units->push_back(unit);
52 }
53
54 int Patch::getSize() {
55         return units->size();
56 }
57
58 PatchUnit* Patch::get(int i) {
59         return (*units)[i];
60 }
61
62 void Patch::print() {
63         for (unsigned i = 0; i < units->size(); i++) {
64                 PatchUnit *u = (*units)[i];
65                 model_print("wildcard %d -> %s\n",
66                         get_wildcard_id_zero(u->getAct()->get_original_mo()),
67                         get_mo_str(u->getMO()));
68         }
69 }