Merge branch 'firefox-test' of /home/git/random-fuzzer into firefox-test
[c11tester.git] / funcinst.cc
1 #include "funcinst.h"
2 #include "model.h"
3
4 FuncInst::FuncInst(ModelAction *act, FuncNode *func_node) :
5         single_location(true),
6         execution_number(0),
7         associated_reads(),
8         thrd_markers()
9 {
10         ASSERT(act);
11         ASSERT(func_node);
12         this->position = act->get_position();
13         this->location = act->get_location();
14         this->type = act->get_type();
15         this->order = act->get_mo();
16         this->func_node = func_node;
17 }
18
19 /* @param other Preceding FuncInst in the same execution trace
20  * Add other to predecessors if it has been added
21  *
22  * @return false: other is already in predecessors
23  *         true : other is added to precedessors
24  */
25 bool FuncInst::add_pred(FuncInst * other)
26 {
27         mllnode<FuncInst*> * it;
28         for (it = predecessors.begin();it != NULL;it=it->getNext()) {
29                 FuncInst * inst = it->getVal();
30                 if (inst == other)
31                         return false;
32         }
33
34         predecessors.push_back(other);
35         return true;
36 }
37
38 bool FuncInst::add_succ(FuncInst * other)
39 {
40         mllnode<FuncInst*>* it;
41         for (it = successors.begin();it != NULL;it=it->getNext()) {
42                 FuncInst * inst = it->getVal();
43                 if ( inst == other )
44                         return false;
45         }
46
47         successors.push_back(other);
48         return true;
49 }
50
51 void FuncInst::set_associated_read(thread_id_t tid, int index, uint32_t marker, uint64_t read_val)
52 {
53         int thread_id = id_to_int(tid);
54
55         if (associated_reads.size() < (uint) thread_id + 1) {
56                 int old_size = associated_reads.size();
57                 int new_size = thread_id + 1;
58
59                 associated_reads.resize(new_size);
60                 thrd_markers.resize(new_size);
61
62                 for (int i = old_size; i < new_size; i++ ) {
63                         associated_reads[i] = new ModelVector<uint64_t>();
64                         thrd_markers[i] = new ModelVector<uint32_t>();
65                 }
66         }
67
68         ModelVector<uint64_t> * read_values = associated_reads[thread_id];
69         ModelVector<uint32_t> * markers = thrd_markers[thread_id];
70         if (read_values->size() < (uint) index + 1) {
71                 int old_size = read_values->size();
72
73                 for (int i = old_size; i < index + 1; i++) {
74                         read_values->push_back(VALUE_NONE);
75                         markers->push_back(0);
76                 }
77         }
78
79         (*read_values)[index] = read_val;
80         (*markers)[index] = marker;
81 }
82
83 uint64_t FuncInst::get_associated_read(thread_id_t tid, int index, uint32_t marker)
84 {
85         int thread_id = id_to_int(tid);
86
87         if ( (*thrd_markers[thread_id])[index] == marker)
88                 return (*associated_reads[thread_id])[index];
89         else
90                 return VALUE_NONE;
91 }
92
93 /* Search the FuncInst that has the same type as act in the collision list */
94 FuncInst * FuncInst::search_in_collision(ModelAction *act)
95 {
96         action_type type = act->get_type();
97
98         mllnode<FuncInst*> * it;
99         for (it = collisions.begin();it != NULL;it = it->getNext()) {
100                 FuncInst * inst = it->getVal();
101                 if (inst->get_type() == type)
102                         return inst;
103         }
104         return NULL;
105 }
106
107 void FuncInst::add_to_collision(FuncInst * inst)
108 {
109         collisions.push_back(inst);
110 }
111
112 /* Note: is_read() is equivalent to ModelAction::is_read() */
113 bool FuncInst::is_read() const
114 {
115         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
116 }
117
118 /* Note: because of action type conversion in ModelExecution
119  * is_write() <==> pure writes (excluding rmw) */
120 bool FuncInst::is_write() const
121 {
122         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == NONATOMIC_WRITE;
123 }
124
125 void FuncInst::print()
126 {
127         model_print("func inst - pos: %s, loc: %p, type: %d,\n", position, location, type);
128 }