model: refactor end-of-execution output
[c11tester.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 <inttypes.h>
12
13 #include "mymemory.h"
14 #include "modeltypes.h"
15 #include "schedule.h"
16
17 class ModelAction;
18 class Thread;
19
20 /**
21  * A flag used for the promise counting/combination problem within a node,
22  * denoting whether a particular Promise is
23  * <ol><li>@b applicable: can be satisfied by this Node's ModelAction and</li>
24  * <li>@b fulfilled: satisfied by this Node's ModelAction under the current
25  * configuration.</li></ol>
26  */
27
28 #define PROMISE_IGNORE 0 /**< This promise is inapplicable; ignore it */
29 #define PROMISE_UNFULFILLED 1 /**< This promise is applicable but unfulfilled */
30 #define PROMISE_FULFILLED 2 /**< This promise is applicable and fulfilled */
31 #define PROMISE_MASK 0xf
32 #define PROMISE_RMW 0x10
33
34 typedef int promise_t;
35
36 struct future_value {
37         uint64_t value;
38         modelclock_t expiration;
39 };
40
41 struct fairness_info {
42         unsigned int enabled_count;
43         unsigned int turns;
44         bool priority;
45 };
46
47
48 /**
49  * @brief A single node in a NodeStack
50  *
51  * Represents a single node in the NodeStack. Each Node is associated with up
52  * to one action and up to one parent node. A node holds information
53  * regarding the last action performed (the "associated action"), the thread
54  * choices that have been explored (explored_children) and should be explored
55  * (backtrack), and the actions that the last action may read from.
56  */
57 class Node {
58 public:
59         Node(ModelAction *act = NULL, Node *par = NULL, int nthreads = 2, Node *prevfairness = NULL);
60         ~Node();
61         /* return true = thread choice has already been explored */
62         bool has_been_explored(thread_id_t tid);
63         /* return true = backtrack set is empty */
64         bool backtrack_empty();
65
66         void explore_child(ModelAction *act, enabled_type_t * is_enabled);
67         /* return false = thread was already in backtrack */
68         bool set_backtrack(thread_id_t id);
69         thread_id_t get_next_backtrack();
70         bool is_enabled(Thread *t);
71         bool is_enabled(thread_id_t tid);
72         enabled_type_t enabled_status(thread_id_t tid);
73
74         ModelAction * get_action() { return action; }
75         bool has_priority(thread_id_t tid);
76         int get_num_threads() {return num_threads;}
77         /** @return the parent Node to this Node; that is, the action that
78          * occurred previously in the stack. */
79         Node * get_parent() const { return parent; }
80
81         bool add_future_value(uint64_t value, modelclock_t expiration);
82         uint64_t get_future_value();
83         modelclock_t get_future_value_expiration();
84         bool increment_future_value();
85         bool future_value_empty();
86
87         void add_read_from(const ModelAction *act);
88         const ModelAction * get_read_from();
89         bool increment_read_from();
90         bool read_from_empty();
91         int get_read_from_size();
92         const ModelAction * get_read_from_at(int i);
93
94         void set_promise(unsigned int i, bool is_rmw);
95         bool get_promise(unsigned int i);
96         bool increment_promise();
97         bool promise_empty();
98         enabled_type_t *get_enabled_array() {return enabled_array;}
99
100         void set_misc_max(int i);
101         int get_misc();
102         bool increment_misc();
103         bool misc_empty();
104
105         void add_relseq_break(const ModelAction *write);
106         const ModelAction * get_relseq_break();
107         bool increment_relseq_break();
108         bool relseq_break_empty();
109
110         void print();
111         void print_may_read_from();
112
113         MEMALLOC
114 private:
115         void explore(thread_id_t tid);
116
117         ModelAction *action;
118         Node *parent;
119         int num_threads;
120         std::vector< bool, ModelAlloc<bool> > explored_children;
121         std::vector< bool, ModelAlloc<bool> > backtrack;
122         std::vector< struct fairness_info, ModelAlloc< struct fairness_info> > fairness;
123         int numBacktracks;
124         enabled_type_t *enabled_array;
125
126         /** The set of ModelActions that this the action at this Node may read
127          *  from. Only meaningful if this Node represents a 'read' action. */
128         std::vector< const ModelAction *, ModelAlloc< const ModelAction * > > may_read_from;
129
130         unsigned int read_from_index;
131
132         std::vector< struct future_value, ModelAlloc<struct future_value> > future_values;
133         std::vector< promise_t, ModelAlloc<promise_t> > promises;
134         int future_index;
135
136         std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > relseq_break_writes;
137         int relseq_break_index;
138
139         int misc_index;
140         int misc_max;
141 };
142
143 typedef std::vector< Node *, ModelAlloc< Node * > > node_list_t;
144
145 /**
146  * @brief A stack of nodes
147  *
148  * Holds a Node linked-list that can be used for holding backtracking,
149  * may-read-from, and replay information. It is used primarily as a
150  * stack-like structure, in that backtracking points and replay nodes are
151  * only removed from the top (most recent).
152  */
153 class NodeStack {
154 public:
155         NodeStack();
156         ~NodeStack();
157         ModelAction * explore_action(ModelAction *act, enabled_type_t * is_enabled);
158         Node * get_head();
159         Node * get_next();
160         void reset_execution();
161         void pop_restofstack(int numAhead);
162         int get_total_nodes() { return total_nodes; }
163
164         void print();
165
166         MEMALLOC
167 private:
168         node_list_t node_list;
169         unsigned int iter;
170
171         int total_nodes;
172 };
173
174 #endif /* __NODESTACK_H__ */