Makefile: rewrite header dependencies
[c11tester.git] / nodestack.cc
1 #include "nodestack.h"
2 #include "action.h"
3 #include "common.h"
4 #include "model.h"
5
6 /**
7  * @brief Node constructor
8  *
9  * Constructs a single Node for use in a NodeStack. Each Node is associated
10  * with exactly one ModelAction (exception: the first Node should be created
11  * as an empty stub, to represent the first thread "choice") and up to one
12  * parent.
13  *
14  * @param act The ModelAction to associate with this Node. May be NULL.
15  * @param par The parent Node in the NodeStack. May be NULL if there is no
16  * parent.
17  * @param nthreads The number of threads which exist at this point in the
18  * execution trace.
19  */
20 Node::Node(ModelAction *act, Node *par, int nthreads)
21         : action(act),
22         parent(par),
23         num_threads(nthreads),
24         explored_children(num_threads),
25         backtrack(num_threads),
26         numBacktracks(0),
27         may_read_from(),
28         read_from_index(0),
29         future_values(),
30         future_index(-1)
31 {
32         if (act)
33                 act->set_node(this);
34 }
35
36 /** @brief Node desctructor */
37 Node::~Node()
38 {
39         if (action)
40                 delete action;
41 }
42
43 /** Prints debugging info for the ModelAction associated with this Node */
44 void Node::print()
45 {
46         if (action)
47                 action->print();
48         else
49                 printf("******** empty action ********\n");
50 }
51
52 /** @brief Prints info about may_read_from set */
53 void Node::print_may_read_from()
54 {
55         readfrom_set_t::iterator it;
56         for (it = may_read_from.begin(); it != may_read_from.end(); it++)
57                 (*it)->print();
58 }
59
60 /**
61  * Sets a promise to explore meeting with the given node.
62  * @param i is the promise index.
63  */
64 void Node::set_promise(uint32_t i) {
65         if (i>=promises.size())
66                 promises.resize(i+1,0);
67         promises[i]=1;
68 }
69
70 /**
71  * Looks up whether a given promise should be satisfied by this node.
72  * @param i The promise index.
73  * @return true if the promise should be satisfied by the given model action.
74  */
75 bool Node::get_promise(uint32_t i) {
76         return (i<promises.size())&&(promises[i]==2);
77 }
78
79 /**
80  * Increments to the next combination of promises.
81  * @return true if we have a valid combination.
82  */
83 bool Node::increment_promise() {
84         for (unsigned int i=0;i<promises.size();i++) {
85                 if (promises[i]==1) {
86                         promises[i]=2;
87                         while (i>0) {
88                                 i--;
89                                 if (promises[i]==2)
90                                         promises[i]=1;
91                         }
92                         return true;
93                 }
94         }
95         return false;
96 }
97
98 /**
99  * Returns whether the promise set is empty.
100  * @return true if we have explored all promise combinations.
101  */
102 bool Node::promise_empty() {
103         for (unsigned int i=0;i<promises.size();i++)
104                 if (promises[i]==1)
105                         return false;
106         return true;
107 }
108
109 /**
110  * Adds a value from a weakly ordered future write to backtrack to.
111  * @param value is the value to backtrack to.
112  */
113 bool Node::add_future_value(uint64_t value) {
114         for(unsigned int i=0;i<future_values.size();i++)
115                 if (future_values[i]==value)
116                         return false;
117
118         future_values.push_back(value);
119         return true;
120 }
121
122 /**
123  * Checks whether the future_values set for this node is empty.
124  * @return true if the future_values set is empty.
125  */
126 bool Node::future_value_empty() {
127         return ((future_index+1)>=future_values.size());
128 }
129
130 /**
131  * Checks if the Thread associated with this thread ID has been explored from
132  * this Node already.
133  * @param tid is the thread ID to check
134  * @return true if this thread choice has been explored already, false
135  * otherwise
136  */
137 bool Node::has_been_explored(thread_id_t tid)
138 {
139         int id = id_to_int(tid);
140         return explored_children[id];
141 }
142
143 /**
144  * Checks if the backtracking set is empty.
145  * @return true if the backtracking set is empty
146  */
147 bool Node::backtrack_empty()
148 {
149         return (numBacktracks == 0);
150 }
151
152 /**
153  * Checks whether the readsfrom set for this node is empty.
154  * @return true if the readsfrom set is empty.
155  */
156 bool Node::read_from_empty() {
157         return ((read_from_index+1)>=may_read_from.size());
158 }
159
160 /**
161  * Mark the appropriate backtracking information for exploring a thread choice.
162  * @param act The ModelAction to explore
163  */
164 void Node::explore_child(ModelAction *act)
165 {
166         explore(act->get_tid());
167 }
168
169 /**
170  * Records a backtracking reference for a thread choice within this Node.
171  * Provides feedback as to whether this thread choice is already set for
172  * backtracking.
173  * @return false if the thread was already set to be backtracked, true
174  * otherwise
175  */
176 bool Node::set_backtrack(thread_id_t id)
177 {
178         int i = id_to_int(id);
179         if (backtrack[i])
180                 return false;
181         backtrack[i] = true;
182         numBacktracks++;
183         return true;
184 }
185
186 thread_id_t Node::get_next_backtrack()
187 {
188         /** @todo Find next backtrack */
189         unsigned int i;
190         for (i = 0; i < backtrack.size(); i++)
191                 if (backtrack[i] == true)
192                         break;
193         /* Backtrack set was empty? */
194         ASSERT(i != backtrack.size());
195
196         backtrack[i] = false;
197         numBacktracks--;
198         return int_to_id(i);
199 }
200
201 bool Node::is_enabled(Thread *t)
202 {
203         return id_to_int(t->get_id()) < num_threads;
204 }
205
206 /**
207  * Add an action to the may_read_from set.
208  * @param act is the action to add
209  */
210 void Node::add_read_from(const ModelAction *act)
211 {
212         may_read_from.push_back(act);
213 }
214
215 /**
216  * Gets the next 'future_value' value from this Node. Only valid for a node
217  * where this->action is a 'read'.
218  * @return The first element in future_values
219  */
220 uint64_t Node::get_future_value() {
221         ASSERT(future_index<future_values.size());
222         return future_values[future_index];
223 }
224
225 /**
226  * Gets the next 'may_read_from' action from this Node. Only valid for a node
227  * where this->action is a 'read'.
228  * @return The first element in may_read_from
229  */
230 const ModelAction * Node::get_read_from() {
231         if (read_from_index<may_read_from.size())
232                 return may_read_from[read_from_index];
233         else
234                 return NULL;
235 }
236
237 /**
238  * Increments the index into the readsfrom set to explore the next item.
239  * @return Returns false if we have explored all items.
240  */
241 bool Node::increment_read_from() {
242         read_from_index++;
243         return (read_from_index<may_read_from.size());
244 }
245
246 /**
247  * Increments the index into the future_values set to explore the next item.
248  * @return Returns false if we have explored all values.
249  */
250 bool Node::increment_future_value() {
251         future_index++;
252         return (future_index<future_values.size());
253 }
254
255 void Node::explore(thread_id_t tid)
256 {
257         int i = id_to_int(tid);
258         if (backtrack[i]) {
259                 backtrack[i] = false;
260                 numBacktracks--;
261         }
262         explored_children[i] = true;
263 }
264
265 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
266                                                node_list_t::iterator end)
267 {
268         node_list_t::iterator it;
269
270         for (it = start; it != end; it++)
271                 delete (*it);
272         list->erase(start, end);
273 }
274
275 NodeStack::NodeStack()
276         : total_nodes(0)
277 {
278         node_list.push_back(new Node());
279         total_nodes++;
280         iter = node_list.begin();
281 }
282
283 NodeStack::~NodeStack()
284 {
285         clear_node_list(&node_list, node_list.begin(), node_list.end());
286 }
287
288 void NodeStack::print()
289 {
290         node_list_t::iterator it;
291         printf("............................................\n");
292         printf("NodeStack printing node_list:\n");
293         for (it = node_list.begin(); it != node_list.end(); it++) {
294                 if (it == this->iter)
295                         printf("vvv following action is the current iterator vvv\n");
296                 (*it)->print();
297         }
298         printf("............................................\n");
299 }
300
301 ModelAction * NodeStack::explore_action(ModelAction *act)
302 {
303         DBG();
304
305         ASSERT(!node_list.empty());
306         node_list_t::iterator it=iter;
307         it++;
308
309         if (it != node_list.end()) {
310                 iter++;
311                 return (*iter)->get_action();
312         }
313
314         /* Record action */
315         get_head()->explore_child(act);
316         node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
317         total_nodes++;
318         iter++;
319         return NULL;
320 }
321
322 /**
323  * Empties the stack of all trailing nodes after a given position and calls the
324  * destructor for each. This function is provided an offset which determines
325  * how many nodes (relative to the current replay state) to save before popping
326  * the stack.
327  * @param numAhead gives the number of Nodes (including this Node) to skip over
328  * before removing nodes.
329  */
330 void NodeStack::pop_restofstack(int numAhead)
331 {
332         /* Diverging from previous execution; clear out remainder of list */
333         node_list_t::iterator it = iter;
334         while (numAhead--)
335                 it++;
336         clear_node_list(&node_list, it, node_list.end());
337 }
338
339 Node * NodeStack::get_head()
340 {
341         if (node_list.empty())
342                 return NULL;
343         return *iter;
344 }
345
346 Node * NodeStack::get_next()
347 {
348         node_list_t::iterator it = iter;
349         if (node_list.empty()) {
350                 DEBUG("Empty\n");
351                 return NULL;
352         }
353         it++;
354         if (it == node_list.end()) {
355                 DEBUG("At end\n");
356                 return NULL;
357         }
358         return *it;
359 }
360
361 void NodeStack::reset_execution()
362 {
363         iter = node_list.begin();
364 }