some changes towards reading from future writes
[cdsspec-compiler.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(0)
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  * Adds a value from a weakly ordered future write to backtrack to.
62  * @param value is the value to backtrack to.
63  */
64
65 void Node::add_future_value(uint64_t value) {
66         for(int i=0;i<future_values.size();i++)
67                 if (future_values[i]==value)
68                         return;
69         future_values.push_back(value);
70 }
71
72
73 /**
74  * Checks if the Thread associated with this thread ID has been explored from
75  * this Node already.
76  * @param tid is the thread ID to check
77  * @return true if this thread choice has been explored already, false
78  * otherwise
79  */
80 bool Node::has_been_explored(thread_id_t tid)
81 {
82         int id = id_to_int(tid);
83         return explored_children[id];
84 }
85
86 /**
87  * Checks if the backtracking set is empty.
88  * @return true if the backtracking set is empty
89  */
90 bool Node::backtrack_empty()
91 {
92         return (numBacktracks == 0);
93 }
94
95
96 /**
97  * Checks whether the readsfrom set for this node is empty.
98  * @return true if the readsfrom set is empty.
99  */
100 bool Node::readsfrom_empty() {
101         return ((read_from_index+1)>=may_read_from.size());
102 }
103
104 /** 
105  * Checks whether the future_values set for this node is empty.
106  * @return true if the future_values set is empty.
107  */
108
109 bool Node::futurevalues_empty() {
110         return ((future_index+1)>=future_values.size());
111 }
112
113 /**
114  * Mark the appropriate backtracking information for exploring a thread choice.
115  * @param act The ModelAction to explore
116  */
117 void Node::explore_child(ModelAction *act)
118 {
119         explore(act->get_tid());
120 }
121
122 /**
123  * Records a backtracking reference for a thread choice within this Node.
124  * Provides feedback as to whether this thread choice is already set for
125  * backtracking.
126  * @return false if the thread was already set to be backtracked, true
127  * otherwise
128  */
129 bool Node::set_backtrack(thread_id_t id)
130 {
131         int i = id_to_int(id);
132         if (backtrack[i])
133                 return false;
134         backtrack[i] = true;
135         numBacktracks++;
136         return true;
137 }
138
139 thread_id_t Node::get_next_backtrack()
140 {
141         /** @todo Find next backtrack */
142         unsigned int i;
143         for (i = 0; i < backtrack.size(); i++)
144                 if (backtrack[i] == true)
145                         break;
146         /* Backtrack set was empty? */
147         ASSERT(i != backtrack.size());
148
149         backtrack[i] = false;
150         numBacktracks--;
151         return int_to_id(i);
152 }
153
154 bool Node::is_enabled(Thread *t)
155 {
156         return id_to_int(t->get_id()) < num_threads;
157 }
158
159 /**
160  * Add an action to the may_read_from set.
161  * @param act is the action to add
162  */
163 void Node::add_read_from(const ModelAction *act)
164 {
165         may_read_from.push_back(act);
166 }
167
168 /**
169  * Gets the next 'future_value' value from this Node. Only valid for a node
170  * where this->action is a 'read'.
171  * @return The first element in future_values
172  */
173
174 uint64_t Node::get_future_value() {
175         ASSERT(future_index<future_values.size());
176         return future_values[future_index];
177 }
178
179 /**
180  * Gets the next 'may_read_from' action from this Node. Only valid for a node
181  * where this->action is a 'read'.
182  * @todo Perform reads_from backtracking/replay properly, so that this function
183  * may remove elements from may_read_from
184  * @return The first element in may_read_from
185  */
186 const ModelAction * Node::get_read_from() {
187         ASSERT(read_from_index<may_read_from.size());
188         return may_read_from[read_from_index];
189 }
190
191 /**
192  * Increments the index into the readsfrom set to explore the next item.
193  * @return Returns false if we have explored all items.
194  */
195 bool Node::increment_read_from() {
196         read_from_index++;
197         return (read_from_index<may_read_from.size());
198 }
199
200 /**
201  * Increments the index into the future_values set to explore the next item.
202  * @return Returns false if we have explored all values.
203  */
204
205 bool Node::increment_future_values() {
206         future_index++;
207         return (future_index<future_values.size());
208 }
209
210 void Node::explore(thread_id_t tid)
211 {
212         int i = id_to_int(tid);
213         if (backtrack[i]) {
214                 backtrack[i] = false;
215                 numBacktracks--;
216         }
217         explored_children[i] = true;
218 }
219
220 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
221                                                node_list_t::iterator end)
222 {
223         node_list_t::iterator it;
224
225         for (it = start; it != end; it++)
226                 delete (*it);
227         list->erase(start, end);
228 }
229
230 NodeStack::NodeStack()
231         : total_nodes(0)
232 {
233         node_list.push_back(new Node());
234         total_nodes++;
235         iter = node_list.begin();
236 }
237
238 NodeStack::~NodeStack()
239 {
240         clear_node_list(&node_list, node_list.begin(), node_list.end());
241 }
242
243 void NodeStack::print()
244 {
245         node_list_t::iterator it;
246         printf("............................................\n");
247         printf("NodeStack printing node_list:\n");
248         for (it = node_list.begin(); it != node_list.end(); it++) {
249                 if (it == this->iter)
250                         printf("vvv following action is the current iterator vvv\n");
251                 (*it)->print();
252         }
253         printf("............................................\n");
254 }
255
256 ModelAction * NodeStack::explore_action(ModelAction *act)
257 {
258         DBG();
259
260         ASSERT(!node_list.empty());
261         node_list_t::iterator it=iter;
262         it++;
263
264         if (it != node_list.end()) {
265                 iter++;
266                 return (*iter)->get_action();
267         }
268
269         /* Record action */
270         get_head()->explore_child(act);
271         node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
272         total_nodes++;
273         iter++;
274         return NULL;
275 }
276
277 /**
278  * Empties the stack of all trailing nodes after a given position and calls the
279  * destructor for each. This function is provided an offset which determines
280  * how many nodes (relative to the current replay state) to save before popping
281  * the stack.
282  * @param numAhead gives the number of Nodes (including this Node) to skip over
283  * before removing nodes.
284  */
285 void NodeStack::pop_restofstack(int numAhead)
286 {
287         /* Diverging from previous execution; clear out remainder of list */
288         node_list_t::iterator it = iter;
289         while (numAhead--)
290                 it++;
291         clear_node_list(&node_list, it, node_list.end());
292 }
293
294 Node * NodeStack::get_head()
295 {
296         if (node_list.empty())
297                 return NULL;
298         return *iter;
299 }
300
301 Node * NodeStack::get_next()
302 {
303         node_list_t::iterator it = iter;
304         if (node_list.empty()) {
305                 DEBUG("Empty\n");
306                 return NULL;
307         }
308         it++;
309         if (it == node_list.end()) {
310                 DEBUG("At end\n");
311                 return NULL;
312         }
313         return *it;
314 }
315
316 void NodeStack::reset_execution()
317 {
318         iter = node_list.begin();
319 }