bugfix - add stl-model.h wrappers, to provide more control over STL
[c11tester.git] / nodestack.cc
1 #define __STDC_FORMAT_MACROS
2 #include <inttypes.h>
3
4 #include <string.h>
5
6 #include "nodestack.h"
7 #include "action.h"
8 #include "common.h"
9 #include "model.h"
10 #include "threads-model.h"
11 #include "modeltypes.h"
12
13 /**
14  * @brief Node constructor
15  *
16  * Constructs a single Node for use in a NodeStack. Each Node is associated
17  * with exactly one ModelAction (exception: the first Node should be created
18  * as an empty stub, to represent the first thread "choice") and up to one
19  * parent.
20  *
21  * @param act The ModelAction to associate with this Node. May be NULL.
22  * @param par The parent Node in the NodeStack. May be NULL if there is no
23  * parent.
24  * @param nthreads The number of threads which exist at this point in the
25  * execution trace.
26  */
27 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness) :
28         read_from_status(READ_FROM_PAST),
29         action(act),
30         uninit_action(NULL),
31         parent(par),
32         num_threads(nthreads),
33         explored_children(num_threads),
34         backtrack(num_threads),
35         fairness(num_threads),
36         numBacktracks(0),
37         enabled_array(NULL),
38         read_from_past(),
39         read_from_past_idx(0),
40         read_from_promises(),
41         read_from_promise_idx(-1),
42         future_values(),
43         future_index(-1),
44         resolve_promise(),
45         resolve_promise_idx(-1),
46         relseq_break_writes(),
47         relseq_break_index(0),
48         misc_index(0),
49         misc_max(0),
50         yield_data(NULL)
51 {
52         ASSERT(act);
53         act->set_node(this);
54         int currtid = id_to_int(act->get_tid());
55         int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
56
57         if (model->params.fairwindow != 0) {
58                 for (int i = 0; i < num_threads; i++) {
59                         ASSERT(i < ((int)fairness.size()));
60                         struct fairness_info *fi = &fairness[i];
61                         struct fairness_info *prevfi = (parent && i < parent->get_num_threads()) ? &parent->fairness[i] : NULL;
62                         if (prevfi) {
63                                 *fi = *prevfi;
64                         }
65                         if (parent && parent->is_enabled(int_to_id(i))) {
66                                 fi->enabled_count++;
67                         }
68                         if (i == currtid) {
69                                 fi->turns++;
70                                 fi->priority = false;
71                         }
72                         /* Do window processing */
73                         if (prevfairness != NULL) {
74                                 if (prevfairness->parent->is_enabled(int_to_id(i)))
75                                         fi->enabled_count--;
76                                 if (i == prevtid) {
77                                         fi->turns--;
78                                 }
79                                 /* Need full window to start evaluating
80                                  * conditions
81                                  * If we meet the enabled count and have no
82                                  * turns, give us priority */
83                                 if ((fi->enabled_count >= model->params.enabledcount) &&
84                                                 (fi->turns == 0))
85                                         fi->priority = true;
86                         }
87                 }
88         }
89 }
90
91 int Node::get_yield_data(int tid1, int tid2) const {
92         if (tid1<num_threads && tid2 < num_threads)
93                 return yield_data[YIELD_INDEX(tid1,tid2,num_threads)];
94         else
95                 return YIELD_S | YIELD_D;
96 }
97
98 void Node::update_yield(Scheduler * scheduler) {
99         yield_data=(int *) model_calloc(1, sizeof(int)*num_threads*num_threads);
100         //handle base case
101         if (parent == NULL) {
102                 for(int i = 0; i < num_threads*num_threads; i++) {
103                         yield_data[i] = YIELD_S | YIELD_D;
104                 }
105                 return;
106         }
107         int curr_tid=id_to_int(action->get_tid());
108
109         for(int u = 0; u < num_threads; u++) {
110                 for(int v = 0; v < num_threads; v++) {
111                         int yield_state=parent->get_yield_data(u, v);
112                         bool next_enabled=scheduler->is_enabled(int_to_id(v));
113                         bool curr_enabled=parent->is_enabled(int_to_id(v));
114                         if (!next_enabled) {
115                                 //Compute intersection of ES and E
116                                 yield_state&=~YIELD_E;
117                                 //Check to see if we disabled the thread
118                                 if (u==curr_tid && curr_enabled)
119                                         yield_state|=YIELD_D;
120                         }
121                         yield_data[YIELD_INDEX(u, v, num_threads)]=yield_state;
122                 }
123                 yield_data[YIELD_INDEX(u, curr_tid, num_threads)]=(yield_data[YIELD_INDEX(u, curr_tid, num_threads)]&~YIELD_P)|YIELD_S;
124         }
125         //handle curr.yield(t) part of computation
126         if (action->is_yield()) {
127                 for(int v = 0; v < num_threads; v++) {
128                         int yield_state=yield_data[YIELD_INDEX(curr_tid, v, num_threads)];
129                         if ((yield_state & (YIELD_E | YIELD_D)) && (!(yield_state & YIELD_S)))
130                                 yield_state |= YIELD_P;
131                         yield_state &= YIELD_P;
132                         if (scheduler->is_enabled(int_to_id(v))) {
133                                 yield_state|=YIELD_E;
134                         }
135                         yield_data[YIELD_INDEX(curr_tid, v, num_threads)]=yield_state;
136                 }
137         }
138 }
139
140 /** @brief Node desctructor */
141 Node::~Node()
142 {
143         delete action;
144         if (uninit_action)
145                 delete uninit_action;
146         if (enabled_array)
147                 model_free(enabled_array);
148         if (yield_data)
149                 model_free(yield_data);
150 }
151
152 /** Prints debugging info for the ModelAction associated with this Node */
153 void Node::print() const
154 {
155         action->print();
156         model_print("          thread status: ");
157         if (enabled_array) {
158                 for (int i = 0; i < num_threads; i++) {
159                         char str[20];
160                         enabled_type_to_string(enabled_array[i], str);
161                         model_print("[%d: %s]", i, str);
162                 }
163                 model_print("\n");
164         } else
165                 model_print("(info not available)\n");
166         model_print("          backtrack: %s", backtrack_empty() ? "empty" : "non-empty ");
167         for (int i = 0; i < (int)backtrack.size(); i++)
168                 if (backtrack[i] == true)
169                         model_print("[%d]", i);
170         model_print("\n");
171
172         model_print("          read from past: %s", read_from_past_empty() ? "empty" : "non-empty ");
173         for (int i = read_from_past_idx + 1; i < (int)read_from_past.size(); i++)
174                 model_print("[%d]", read_from_past[i]->get_seq_number());
175         model_print("\n");
176
177         model_print("          read-from promises: %s", read_from_promise_empty() ? "empty" : "non-empty ");
178         for (int i = read_from_promise_idx + 1; i < (int)read_from_promises.size(); i++)
179                 model_print("[%d]", read_from_promises[i]->get_seq_number());
180         model_print("\n");
181
182         model_print("          future values: %s", future_value_empty() ? "empty" : "non-empty ");
183         for (int i = future_index + 1; i < (int)future_values.size(); i++)
184                 model_print("[%#" PRIx64 "]", future_values[i].value);
185         model_print("\n");
186
187         model_print("          promises: %s\n", promise_empty() ? "empty" : "non-empty");
188         model_print("          misc: %s\n", misc_empty() ? "empty" : "non-empty");
189         model_print("          rel seq break: %s\n", relseq_break_empty() ? "empty" : "non-empty");
190 }
191
192 /*********************************** promise **********************************/
193
194 /**
195  * Sets a promise to explore meeting with the given node.
196  * @param i is the promise index.
197  */
198 void Node::set_promise(unsigned int i)
199 {
200         if (i >= resolve_promise.size())
201                 resolve_promise.resize(i + 1, false);
202         resolve_promise[i] = true;
203 }
204
205 /**
206  * Looks up whether a given promise should be satisfied by this node.
207  * @param i The promise index.
208  * @return true if the promise should be satisfied by the given ModelAction.
209  */
210 bool Node::get_promise(unsigned int i) const
211 {
212         return (i < resolve_promise.size()) && (int)i == resolve_promise_idx;
213 }
214
215 /**
216  * Increments to the next promise to resolve.
217  * @return true if we have a valid combination.
218  */
219 bool Node::increment_promise()
220 {
221         DBG();
222         if (resolve_promise.empty())
223                 return false;
224         int prev_idx = resolve_promise_idx;
225         resolve_promise_idx++;
226         for ( ; resolve_promise_idx < (int)resolve_promise.size(); resolve_promise_idx++)
227                 if (resolve_promise[resolve_promise_idx])
228                         return true;
229         resolve_promise_idx = prev_idx;
230         return false;
231 }
232
233 /**
234  * Returns whether the promise set is empty.
235  * @return true if we have explored all promise combinations.
236  */
237 bool Node::promise_empty() const
238 {
239         for (int i = resolve_promise_idx + 1; i < (int)resolve_promise.size(); i++)
240                 if (i >= 0 && resolve_promise[i])
241                         return false;
242         return true;
243 }
244
245 /** @brief Clear any promise-resolution information for this Node */
246 void Node::clear_promise_resolutions()
247 {
248         resolve_promise.clear();
249         resolve_promise_idx = -1;
250 }
251
252 /******************************* end promise **********************************/
253
254 void Node::set_misc_max(int i)
255 {
256         misc_max = i;
257 }
258
259 int Node::get_misc() const
260 {
261         return misc_index;
262 }
263
264 bool Node::increment_misc()
265 {
266         return (misc_index < misc_max) && ((++misc_index) < misc_max);
267 }
268
269 bool Node::misc_empty() const
270 {
271         return (misc_index + 1) >= misc_max;
272 }
273
274 /**
275  * Checks if the Thread associated with this thread ID has been explored from
276  * this Node already.
277  * @param tid is the thread ID to check
278  * @return true if this thread choice has been explored already, false
279  * otherwise
280  */
281 bool Node::has_been_explored(thread_id_t tid) const
282 {
283         int id = id_to_int(tid);
284         return explored_children[id];
285 }
286
287 /**
288  * Checks if the backtracking set is empty.
289  * @return true if the backtracking set is empty
290  */
291 bool Node::backtrack_empty() const
292 {
293         return (numBacktracks == 0);
294 }
295
296 /**
297  * Mark the appropriate backtracking information for exploring a thread choice.
298  * @param act The ModelAction to explore
299  */
300 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
301 {
302         if (!enabled_array)
303                 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
304         if (is_enabled != NULL)
305                 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
306         else {
307                 for (int i = 0; i < num_threads; i++)
308                         enabled_array[i] = THREAD_DISABLED;
309         }
310
311         explore(act->get_tid());
312 }
313
314 /**
315  * Records a backtracking reference for a thread choice within this Node.
316  * Provides feedback as to whether this thread choice is already set for
317  * backtracking.
318  * @return false if the thread was already set to be backtracked, true
319  * otherwise
320  */
321 bool Node::set_backtrack(thread_id_t id)
322 {
323         int i = id_to_int(id);
324         ASSERT(i < ((int)backtrack.size()));
325         if (backtrack[i])
326                 return false;
327         backtrack[i] = true;
328         numBacktracks++;
329         return true;
330 }
331
332 thread_id_t Node::get_next_backtrack()
333 {
334         /** @todo Find next backtrack */
335         unsigned int i;
336         for (i = 0; i < backtrack.size(); i++)
337                 if (backtrack[i] == true)
338                         break;
339         /* Backtrack set was empty? */
340         ASSERT(i != backtrack.size());
341
342         backtrack[i] = false;
343         numBacktracks--;
344         return int_to_id(i);
345 }
346
347 void Node::clear_backtracking()
348 {
349         for (unsigned int i = 0; i < backtrack.size(); i++)
350                 backtrack[i] = false;
351         for (unsigned int i = 0; i < explored_children.size(); i++)
352                 explored_children[i] = false;
353         numBacktracks = 0;
354 }
355
356 bool Node::is_enabled(Thread *t) const
357 {
358         int thread_id = id_to_int(t->get_id());
359         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
360 }
361
362 enabled_type_t Node::enabled_status(thread_id_t tid) const
363 {
364         int thread_id = id_to_int(tid);
365         if (thread_id < num_threads)
366                 return enabled_array[thread_id];
367         else
368                 return THREAD_DISABLED;
369 }
370
371 bool Node::is_enabled(thread_id_t tid) const
372 {
373         int thread_id = id_to_int(tid);
374         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
375 }
376
377 bool Node::has_priority(thread_id_t tid) const
378 {
379         return fairness[id_to_int(tid)].priority;
380 }
381
382 bool Node::has_priority_over(thread_id_t tid1, thread_id_t tid2) const
383 {
384         return get_yield_data(id_to_int(tid1), id_to_int(tid2)) & YIELD_P;
385 }
386
387 /*********************************** read from ********************************/
388
389 /**
390  * Get the current state of the may-read-from set iteration
391  * @return The read-from type we should currently be checking (past or future)
392  */
393 read_from_type_t Node::get_read_from_status()
394 {
395         if (read_from_status == READ_FROM_PAST && read_from_past.empty())
396                 increment_read_from();
397         return read_from_status;
398 }
399
400 /**
401  * Iterate one step in the may-read-from iteration. This includes a step in
402  * reading from the either the past or the future.
403  * @return True if there is a new read-from to explore; false otherwise
404  */
405 bool Node::increment_read_from()
406 {
407         clear_promise_resolutions();
408         if (increment_read_from_past()) {
409                read_from_status = READ_FROM_PAST;
410                return true;
411         } else if (increment_read_from_promise()) {
412                 read_from_status = READ_FROM_PROMISE;
413                 return true;
414         } else if (increment_future_value()) {
415                 read_from_status = READ_FROM_FUTURE;
416                 return true;
417         }
418         read_from_status = READ_FROM_NONE;
419         return false;
420 }
421
422 /**
423  * @return True if there are any new read-froms to explore
424  */
425 bool Node::read_from_empty() const
426 {
427         return read_from_past_empty() &&
428                 read_from_promise_empty() &&
429                 future_value_empty();
430 }
431
432 /**
433  * Get the total size of the may-read-from set, including both past and future
434  * values
435  * @return The size of may-read-from
436  */
437 unsigned int Node::read_from_size() const
438 {
439         return read_from_past.size() +
440                 read_from_promises.size() +
441                 future_values.size();
442 }
443
444 /******************************* end read from ********************************/
445
446 /****************************** read from past ********************************/
447
448 /** @brief Prints info about read_from_past set */
449 void Node::print_read_from_past()
450 {
451         for (unsigned int i = 0; i < read_from_past.size(); i++)
452                 read_from_past[i]->print();
453 }
454
455 /**
456  * Add an action to the read_from_past set.
457  * @param act is the action to add
458  */
459 void Node::add_read_from_past(const ModelAction *act)
460 {
461         read_from_past.push_back(act);
462 }
463
464 /**
465  * Gets the next 'read_from_past' action from this Node. Only valid for a node
466  * where this->action is a 'read'.
467  * @return The first element in read_from_past
468  */
469 const ModelAction * Node::get_read_from_past() const
470 {
471         if (read_from_past_idx < read_from_past.size())
472                 return read_from_past[read_from_past_idx];
473         else
474                 return NULL;
475 }
476
477 const ModelAction * Node::get_read_from_past(int i) const
478 {
479         return read_from_past[i];
480 }
481
482 int Node::get_read_from_past_size() const
483 {
484         return read_from_past.size();
485 }
486
487 /**
488  * Checks whether the readsfrom set for this node is empty.
489  * @return true if the readsfrom set is empty.
490  */
491 bool Node::read_from_past_empty() const
492 {
493         return ((read_from_past_idx + 1) >= read_from_past.size());
494 }
495
496 /**
497  * Increments the index into the readsfrom set to explore the next item.
498  * @return Returns false if we have explored all items.
499  */
500 bool Node::increment_read_from_past()
501 {
502         DBG();
503         if (read_from_past_idx < read_from_past.size()) {
504                 read_from_past_idx++;
505                 return read_from_past_idx < read_from_past.size();
506         }
507         return false;
508 }
509
510 /************************** end read from past ********************************/
511
512 /***************************** read_from_promises *****************************/
513
514 /**
515  * Add an action to the read_from_promises set.
516  * @param reader The read which generated the Promise; we use the ModelAction
517  * instead of the Promise because the Promise does not last across executions
518  */
519 void Node::add_read_from_promise(const ModelAction *reader)
520 {
521         read_from_promises.push_back(reader);
522 }
523
524 /**
525  * Gets the next 'read-from-promise' from this Node. Only valid for a node
526  * where this->action is a 'read'.
527  * @return The current element in read_from_promises
528  */
529 Promise * Node::get_read_from_promise() const
530 {
531         ASSERT(read_from_promise_idx >= 0 && read_from_promise_idx < ((int)read_from_promises.size()));
532         return read_from_promises[read_from_promise_idx]->get_reads_from_promise();
533 }
534
535 /**
536  * Gets a particular 'read-from-promise' form this Node. Only vlaid for a node
537  * where this->action is a 'read'.
538  * @param i The index of the Promise to get
539  * @return The Promise at index i, if the Promise is still available; NULL
540  * otherwise
541  */
542 Promise * Node::get_read_from_promise(int i) const
543 {
544         return read_from_promises[i]->get_reads_from_promise();
545 }
546
547 /** @return The size of the read-from-promise set */
548 int Node::get_read_from_promise_size() const
549 {
550         return read_from_promises.size();
551 }
552
553 /**
554  * Checks whether the read_from_promises set for this node is empty.
555  * @return true if the read_from_promises set is empty.
556  */
557 bool Node::read_from_promise_empty() const
558 {
559         return ((read_from_promise_idx + 1) >= ((int)read_from_promises.size()));
560 }
561
562 /**
563  * Increments the index into the read_from_promises set to explore the next item.
564  * @return Returns false if we have explored all promises.
565  */
566 bool Node::increment_read_from_promise()
567 {
568         DBG();
569         if (read_from_promise_idx < ((int)read_from_promises.size())) {
570                 read_from_promise_idx++;
571                 return (read_from_promise_idx < ((int)read_from_promises.size()));
572         }
573         return false;
574 }
575
576 /************************* end read_from_promises *****************************/
577
578 /****************************** future values *********************************/
579
580 /**
581  * Adds a value from a weakly ordered future write to backtrack to. This
582  * operation may "fail" if the future value has already been run (within some
583  * sloppiness window of this expiration), or if the futurevalues set has
584  * reached its maximum.
585  * @see model_params.maxfuturevalues
586  *
587  * @param value is the value to backtrack to.
588  * @return True if the future value was successully added; false otherwise
589  */
590 bool Node::add_future_value(struct future_value fv)
591 {
592         uint64_t value = fv.value;
593         modelclock_t expiration = fv.expiration;
594         thread_id_t tid = fv.tid;
595         int idx = -1; /* Highest index where value is found */
596         for (unsigned int i = 0; i < future_values.size(); i++) {
597                 if (future_values[i].value == value && future_values[i].tid == tid) {
598                         if (expiration <= future_values[i].expiration)
599                                 return false;
600                         idx = i;
601                 }
602         }
603         if (idx > future_index) {
604                 /* Future value hasn't been explored; update expiration */
605                 future_values[idx].expiration = expiration;
606                 return true;
607         } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
608                 /* Future value has been explored and is within the "sloppy" window */
609                 return false;
610         }
611
612         /* Limit the size of the future-values set */
613         if (model->params.maxfuturevalues > 0 &&
614                         (int)future_values.size() >= model->params.maxfuturevalues)
615                 return false;
616
617         future_values.push_back(fv);
618         return true;
619 }
620
621 /**
622  * Gets the next 'future_value' from this Node. Only valid for a node where
623  * this->action is a 'read'.
624  * @return The first element in future_values
625  */
626 struct future_value Node::get_future_value() const
627 {
628         ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
629         return future_values[future_index];
630 }
631
632 /**
633  * Checks whether the future_values set for this node is empty.
634  * @return true if the future_values set is empty.
635  */
636 bool Node::future_value_empty() const
637 {
638         return ((future_index + 1) >= ((int)future_values.size()));
639 }
640
641 /**
642  * Increments the index into the future_values set to explore the next item.
643  * @return Returns false if we have explored all values.
644  */
645 bool Node::increment_future_value()
646 {
647         DBG();
648         if (future_index < ((int)future_values.size())) {
649                 future_index++;
650                 return (future_index < ((int)future_values.size()));
651         }
652         return false;
653 }
654
655 /************************** end future values *********************************/
656
657 /**
658  * Add a write ModelAction to the set of writes that may break the release
659  * sequence. This is used during replay exploration of pending release
660  * sequences. This Node must correspond to a release sequence fixup action.
661  *
662  * @param write The write that may break the release sequence. NULL means we
663  * allow the release sequence to synchronize.
664  */
665 void Node::add_relseq_break(const ModelAction *write)
666 {
667         relseq_break_writes.push_back(write);
668 }
669
670 /**
671  * Get the write that may break the current pending release sequence,
672  * according to the replay / divergence pattern.
673  *
674  * @return A write that may break the release sequence. If NULL, that means
675  * the release sequence should not be broken.
676  */
677 const ModelAction * Node::get_relseq_break() const
678 {
679         if (relseq_break_index < (int)relseq_break_writes.size())
680                 return relseq_break_writes[relseq_break_index];
681         else
682                 return NULL;
683 }
684
685 /**
686  * Increments the index into the relseq_break_writes set to explore the next
687  * item.
688  * @return Returns false if we have explored all values.
689  */
690 bool Node::increment_relseq_break()
691 {
692         DBG();
693         if (relseq_break_index < ((int)relseq_break_writes.size())) {
694                 relseq_break_index++;
695                 return (relseq_break_index < ((int)relseq_break_writes.size()));
696         }
697         return false;
698 }
699
700 /**
701  * @return True if all writes that may break the release sequence have been
702  * explored
703  */
704 bool Node::relseq_break_empty() const
705 {
706         return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
707 }
708
709 void Node::explore(thread_id_t tid)
710 {
711         int i = id_to_int(tid);
712         ASSERT(i < ((int)backtrack.size()));
713         if (backtrack[i]) {
714                 backtrack[i] = false;
715                 numBacktracks--;
716         }
717         explored_children[i] = true;
718 }
719
720 NodeStack::NodeStack() :
721         node_list(),
722         head_idx(-1),
723         total_nodes(0)
724 {
725         total_nodes++;
726 }
727
728 NodeStack::~NodeStack()
729 {
730         for (unsigned int i = 0; i < node_list.size(); i++)
731                 delete node_list[i];
732 }
733
734 void NodeStack::print() const
735 {
736         model_print("............................................\n");
737         model_print("NodeStack printing node_list:\n");
738         for (unsigned int it = 0; it < node_list.size(); it++) {
739                 if ((int)it == this->head_idx)
740                         model_print("vvv following action is the current iterator vvv\n");
741                 node_list[it]->print();
742         }
743         model_print("............................................\n");
744 }
745
746 /** Note: The is_enabled set contains what actions were enabled when
747  *  act was chosen. */
748 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
749 {
750         DBG();
751
752         if ((head_idx + 1) < (int)node_list.size()) {
753                 head_idx++;
754                 return node_list[head_idx]->get_action();
755         }
756
757         /* Record action */
758         Node *head = get_head();
759         Node *prevfairness = NULL;
760         if (head) {
761                 head->explore_child(act, is_enabled);
762                 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
763                         prevfairness = node_list[head_idx - model->params.fairwindow];
764         }
765
766         int next_threads = model->get_num_threads();
767         if (act->get_type() == THREAD_CREATE)
768                 next_threads++;
769         node_list.push_back(new Node(act, head, next_threads, prevfairness));
770         total_nodes++;
771         head_idx++;
772         return NULL;
773 }
774
775 /**
776  * Empties the stack of all trailing nodes after a given position and calls the
777  * destructor for each. This function is provided an offset which determines
778  * how many nodes (relative to the current replay state) to save before popping
779  * the stack.
780  * @param numAhead gives the number of Nodes (including this Node) to skip over
781  * before removing nodes.
782  */
783 void NodeStack::pop_restofstack(int numAhead)
784 {
785         /* Diverging from previous execution; clear out remainder of list */
786         unsigned int it = head_idx + numAhead;
787         for (unsigned int i = it; i < node_list.size(); i++)
788                 delete node_list[i];
789         node_list.resize(it);
790         node_list.back()->clear_backtracking();
791 }
792
793 Node * NodeStack::get_head() const
794 {
795         if (node_list.empty() || head_idx < 0)
796                 return NULL;
797         return node_list[head_idx];
798 }
799
800 Node * NodeStack::get_next() const
801 {
802         if (node_list.empty()) {
803                 DEBUG("Empty\n");
804                 return NULL;
805         }
806         unsigned int it = head_idx + 1;
807         if (it == node_list.size()) {
808                 DEBUG("At end\n");
809                 return NULL;
810         }
811         return node_list[it];
812 }
813
814 void NodeStack::reset_execution()
815 {
816         head_idx = -1;
817 }