bug fix
[c11tester.git] / funcnode.cc
1 #include "funcnode.h"
2 #include "predicate.h"
3
4 FuncNode::FuncNode() :
5         func_inst_map(),
6         inst_list(),
7         entry_insts(),
8         thrd_read_map()
9 {}
10
11 /* Check whether FuncInst with the same type, position, and location
12  * as act has been added to func_inst_map or not. If so, return it;
13  * if not, add it and return it.
14  *
15  * @return FuncInst with the same type, position, and location as act */
16 FuncInst * FuncNode::get_or_add_action(ModelAction *act)
17 {
18         ASSERT(act);
19         const char * position = act->get_position();
20
21         /* Actions THREAD_CREATE, THREAD_START, THREAD_YIELD, THREAD_JOIN,
22          * THREAD_FINISH, PTHREAD_CREATE, PTHREAD_JOIN,
23          * ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK are not tagged with their
24          * source line numbers
25          */
26         if (position == NULL)
27                 return NULL;
28
29         if ( func_inst_map.contains(position) ) {
30                 FuncInst * inst = func_inst_map.get(position);
31
32                 if (inst->get_type() != act->get_type() ) {
33                         // model_print("action with a different type occurs at line number %s\n", position);
34                         FuncInst * func_inst = inst->search_in_collision(act);
35
36                         if (func_inst != NULL) {
37                                 // return the FuncInst found in the collision list
38                                 return func_inst;
39                         }
40
41                         func_inst = new FuncInst(act, this);
42                         inst->get_collisions()->push_back(func_inst);
43                         inst_list.push_back(func_inst); // delete?
44
45                         return func_inst;
46                 }
47
48                 return inst;
49         }
50
51         FuncInst * func_inst = new FuncInst(act, this);
52
53         func_inst_map.put(position, func_inst);
54         inst_list.push_back(func_inst);
55
56         return func_inst;
57 }
58
59 void FuncNode::add_entry_inst(FuncInst * inst)
60 {
61         if (inst == NULL)
62                 return;
63
64         mllnode<FuncInst*>* it;
65         for (it = entry_insts.begin();it != NULL;it=it->getNext()) {
66                 if (inst == it->getVal())
67                         return;
68         }
69
70         entry_insts.push_back(inst);
71 }
72
73 /* @param inst_list a list of FuncInsts; this argument comes from ModelExecution
74  * Link FuncInsts in a list - add one FuncInst to another's predecessors and successors
75  */
76 void FuncNode::link_insts(func_inst_list_t * inst_list)
77 {
78         if (inst_list == NULL)
79                 return;
80
81         sllnode<FuncInst *>* it = inst_list->begin();
82         sllnode<FuncInst *>* prev;
83
84         if (inst_list->size() == 0)
85                 return;
86
87         /* add the first instruction to the list of entry insts */
88         FuncInst * entry_inst = it->getVal();
89         add_entry_inst(entry_inst);
90
91         it=it->getNext();
92         while (it != NULL) {
93                 prev = it;
94                 prev = it->getPrev();
95
96                 FuncInst * prev_inst = prev->getVal();
97                 FuncInst * curr_inst = it->getVal();
98
99                 prev_inst->add_succ(curr_inst);
100                 curr_inst->add_pred(prev_inst);
101
102                 it=it->getNext();
103         }
104 }
105
106 /* @param tid thread id
107  * Store the values read by atomic read actions into thrd_read_map */
108 void FuncNode::store_read(ModelAction * act, uint32_t tid)
109 {
110         ASSERT(act);
111
112         void * location = act->get_location();
113         uint64_t read_from_val = act->get_reads_from_value();
114
115         /* resize and initialize */
116         uint32_t old_size = thrd_read_map.size();
117         if (old_size <= tid) {
118                 thrd_read_map.resize(tid + 1);
119                 for (uint32_t i = old_size;i < tid + 1;i++)
120                         thrd_read_map[i] = new read_map_t();
121         }
122
123         read_map_t * read_map = thrd_read_map[tid];
124         read_map->put(location, read_from_val);
125
126         /* Store the memory locations where atomic reads happen */
127         // read_locations.add(location);
128 }
129
130 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
131 {
132         if (thrd_read_map.size() <= tid)
133                 return 0xdeadbeef;
134
135         read_map_t * read_map = thrd_read_map[tid];
136
137         /* last read value not found */
138         if ( !read_map->contains(location) )
139                 return 0xdeadbeef;
140
141         uint64_t read_val = read_map->get(location);
142         return read_val;
143 }
144
145 /* @param tid thread id
146  * Reset read map for a thread. This function shall only be called
147  * when a thread exits a function
148  */
149 void FuncNode::clear_read_map(uint32_t tid)
150 {
151         if (thrd_read_map.size() <= tid)
152                 return;
153
154         thrd_read_map[tid]->reset();
155 }
156
157 void FuncNode::generate_predicate(FuncInst *func_inst)
158 {
159
160 }
161
162 /* @param tid thread id
163  * Print the values read by the last read actions for each memory location
164  */
165 void FuncNode::print_last_read(uint32_t tid)
166 {
167         ASSERT(thrd_read_map.size() > tid);
168         read_map_t * read_map = thrd_read_map[tid];
169 /*
170         mllnode<void *> * it;
171         for (it = read_locations.begin();it != NULL;it=it->getNext()) {
172                 if ( !read_map->contains(it->getVal()) )
173                         break;
174
175                 uint64_t read_val = read_map->get(it->getVal());
176                 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);
177         }
178 */
179 }