CBMC
abstract_event.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: abstract events
4 
5 Author: Vincent Nimal
6 
7 Date: 2012
8 
9 \*******************************************************************/
10 
13 
14 #ifndef CPROVER_GOTO_INSTRUMENT_WMM_ABSTRACT_EVENT_H
15 #define CPROVER_GOTO_INSTRUMENT_WMM_ABSTRACT_EVENT_H
16 
17 #include <util/source_location.h>
18 #include <util/graph.h>
19 
20 #include "wmm.h"
21 
22 class abstract_eventt:public graph_nodet<empty_edget>
23 {
24 protected:
26  memory_modelt model, bool lwsync_met) const;
27 
28 public:
29  /* for now, both fence functions and asm fences accepted */
30  enum class operationt { Write, Read, Fence, Lwfence, ASMfence };
31 
33  unsigned thread;
35  unsigned id;
38  bool local;
39 
40  // for ASMfence
41  bool WRfence;
42  bool WWfence;
43  bool RRfence;
44  bool RWfence;
45  bool WWcumul;
46  bool RWcumul;
47  bool RRcumul;
48 
50  operation(operationt::Write),
51  thread(0),
52  id(0),
53  local(false),
54  WRfence(false),
55  WWfence(false),
56  RRfence(false),
57  RWfence(false),
58  WWcumul(false),
59  RWcumul(false),
60  RRcumul(false)
61  {
62  }
63 
65  operationt _op,
66  unsigned _th,
67  irep_idt _var,
68  unsigned _id,
69  source_locationt _loc,
70  irep_idt _function_id,
71  bool _local)
72  : operation(_op),
73  thread(_th),
74  variable(_var),
75  id(_id),
76  source_location(_loc),
77  function_id(_function_id),
78  local(_local)
79  {
80  }
81 
83  operationt _op,
84  unsigned _th,
85  irep_idt _var,
86  unsigned _id,
87  source_locationt _loc,
88  irep_idt _function_id,
89  bool _local,
90  bool WRf,
91  bool WWf,
92  bool RRf,
93  bool RWf,
94  bool WWc,
95  bool RWc,
96  bool RRc)
97  : operation(_op),
98  thread(_th),
99  variable(_var),
100  id(_id),
101  source_location(_loc),
102  function_id(_function_id),
103  local(_local),
104  WRfence(RWf),
105  WWfence(WWf),
106  RRfence(RRf),
107  RWfence(WRf),
108  WWcumul(WWc),
109  RWcumul(RWc),
110  RRcumul(RRc)
111  {
112  }
113 
114  /* post declaration (through graph) -- doesn't copy */
115  void operator()(const abstract_eventt &other)
116  {
117  operation=other.operation;
118  thread=other.thread;
119  variable=other.variable;
120  id=other.id;
122  function_id = other.function_id;
123  local=other.local;
124  }
125 
126  bool operator==(const abstract_eventt &other) const
127  {
128  return (id == other.id);
129  }
130 
131  bool operator<(const abstract_eventt &other) const
132  {
133  return (id < other.id);
134  }
135 
136  bool is_fence() const
137  {
138  return operation==operationt::Fence ||
141  }
142 
143  /* checks the safety of the pair locally (i.e., w/o taking fences
144  or dependencies into account -- use is_unsafe on the whole
145  critical cycle for this) */
146  bool unsafe_pair(const abstract_eventt &next, memory_modelt model) const
147  {
148  return unsafe_pair_lwfence_param(next, model, false);
149  }
150 
152  const abstract_eventt &next,
153  memory_modelt model) const
154  {
155  return unsafe_pair_lwfence_param(next, model, true);
156  }
157 
158  bool unsafe_pair_asm(
159  const abstract_eventt &next,
160  memory_modelt model,
161  unsigned char met) const;
162 
163  std::string get_operation() const
164  {
165  switch(operation)
166  {
167  case operationt::Write: return "W";
168  case operationt::Read: return "R";
169  case operationt::Fence: return "F";
170  case operationt::Lwfence: return "f";
171  case operationt::ASMfence: return "asm:";
172  }
173  UNREACHABLE;
174  return "?";
175  }
176 
178  const abstract_eventt &second) const
179  {
180  return
181  (WRfence &&
182  first.operation==operationt::Write &&
183  second.operation==operationt::Read) ||
184  ((WWfence || WWcumul) &&
185  first.operation==operationt::Write &&
186  second.operation==operationt::Write) ||
187  ((RWfence || RWcumul) &&
188  first.operation==operationt::Read &&
189  second.operation==operationt::Write) ||
190  ((RRfence || RRcumul) &&
191  first.operation==operationt::Read &&
192  second.operation==operationt::Read);
193  }
194 
195  bool is_direct() const { return WWfence || WRfence || RRfence || RWfence; }
196  bool is_cumul() const { return WWcumul || RWcumul || RRcumul; }
197 
198  unsigned char fence_value() const
199  {
200  return uc(WRfence) + 2u * uc(WWfence) + 4u * uc(RRfence) +
201  8u * uc(RWfence) + 16u * uc(WWcumul) + 32u * uc(RWcumul) +
202  64u * uc(RRcumul);
203  }
204 
205 private:
206  static unsigned char uc(bool truth_value)
207  {
208  return truth_value ? 1u : 0u;
209  }
210 };
211 
212 inline std::ostream &operator<<(
213  std::ostream &s,
214  const abstract_eventt &e)
215 {
216  return s << e.get_operation() << e.variable;
217 }
218 
219 #endif // CPROVER_GOTO_INSTRUMENT_WMM_ABSTRACT_EVENT_H
std::ostream & operator<<(std::ostream &s, const abstract_eventt &e)
bool is_cumul() const
bool unsafe_pair(const abstract_eventt &next, memory_modelt model) const
bool is_corresponding_fence(const abstract_eventt &first, const abstract_eventt &second) const
std::string get_operation() const
void operator()(const abstract_eventt &other)
static unsigned char uc(bool truth_value)
bool is_fence() const
bool unsafe_pair_lwfence(const abstract_eventt &next, memory_modelt model) const
source_locationt source_location
irep_idt function_id
bool unsafe_pair_asm(const abstract_eventt &next, memory_modelt model, unsigned char met) const
bool unsafe_pair_lwfence_param(const abstract_eventt &next, memory_modelt model, bool lwsync_met) const
bool is_direct() const
abstract_eventt(operationt _op, unsigned _th, irep_idt _var, unsigned _id, source_locationt _loc, irep_idt _function_id, bool _local, bool WRf, bool WWf, bool RRf, bool RWf, bool WWc, bool RWc, bool RRc)
bool operator<(const abstract_eventt &other) const
operationt operation
abstract_eventt(operationt _op, unsigned _th, irep_idt _var, unsigned _id, source_locationt _loc, irep_idt _function_id, bool _local)
unsigned char fence_value() const
bool operator==(const abstract_eventt &other) const
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
This class represents a node in a directed graph.
Definition: graph.h:35
A Template Class for Graphs.
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
memory models
memory_modelt
Definition: wmm.h:18