Merge branch 'master' into brian
[cdsspec-compiler.git] / nodestack.h
1 /** @file nodestack.h
2  *  @brief Stack of operations for use in backtracking.
3 */
4
5 #ifndef __NODESTACK_H__
6 #define __NODESTACK_H__
7
8 #include <list>
9 #include <vector>
10 #include <cstddef>
11 #include "threads.h"
12 #include "mymemory.h"
13
14 class ModelAction;
15
16 typedef std::list< const ModelAction *, MyAlloc< const ModelAction * > > readfrom_set_t;
17
18 /**
19  * @brief A single node in a NodeStack
20  *
21  * Represents a single node in the NodeStack. Each Node is associated with up
22  * to one action and up to one parent node. A node holds information
23  * regarding the last action performed (the "associated action"), the thread
24  * choices that have been explored (explored_children) and should be explored
25  * (backtrack), and the actions that the last action may read from.
26  */
27 class Node {
28 public:
29         Node(ModelAction *act = NULL, Node *par = NULL, int nthreads = 1);
30         ~Node();
31         /* return true = thread choice has already been explored */
32         bool has_been_explored(thread_id_t tid);
33         /* return true = backtrack set is empty */
34         bool backtrack_empty();
35         void explore_child(ModelAction *act);
36         /* return false = thread was already in backtrack */
37         bool set_backtrack(thread_id_t id);
38         thread_id_t get_next_backtrack();
39         bool is_enabled(Thread *t);
40         ModelAction * get_action() { return action; }
41
42         /** @return the parent Node to this Node; that is, the action that
43          * occurred previously in the stack. */
44         Node * get_parent() const { return parent; }
45
46         void add_read_from(const ModelAction *act);
47         const ModelAction * get_next_read_from();
48
49         void print();
50         void print_may_read_from();
51
52         MEMALLOC
53 private:
54         void explore(thread_id_t tid);
55
56         ModelAction *action;
57         Node *parent;
58         int num_threads;
59         std::vector< bool, MyAlloc<bool> > explored_children;
60         std::vector< bool, MyAlloc<bool> > backtrack;
61         int numBacktracks;
62
63         /** The set of ModelActions that this the action at this Node may read
64          *  from. Only meaningful if this Node represents a 'read' action. */
65         readfrom_set_t may_read_from;
66 };
67
68 typedef std::list< Node *, MyAlloc< Node * > > node_list_t;
69
70 /**
71  * @brief A stack of nodes
72  *
73  * Holds a Node linked-list that can be used for holding backtracking,
74  * may-read-from, and replay information. It is used primarily as a
75  * stack-like structure, in that backtracking points and replay nodes are
76  * only removed from the top (most recent).
77  */
78 class NodeStack {
79 public:
80         NodeStack();
81         ~NodeStack();
82         ModelAction * explore_action(ModelAction *act);
83         Node * get_head();
84         Node * get_next();
85         void reset_execution();
86
87         int get_total_nodes() { return total_nodes; }
88
89         void print();
90
91         MEMALLOC
92 private:
93         node_list_t node_list;
94         node_list_t::iterator iter;
95
96         int total_nodes;
97 };
98
99 #endif /* __NODESTACK_H__ */