eliminate redundancy
[c11tester.git] / cmodelint.cc
1 #include <stdio.h>
2 #include <string>
3
4 #include "model.h"
5 #include "execution.h"
6 #include "action.h"
7 #include "history.h"
8 #include "cmodelint.h"
9 #include "snapshot-interface.h"
10 #include "threads-model.h"
11
12 memory_order orders[6] = {
13         memory_order_relaxed, memory_order_consume, memory_order_acquire,
14         memory_order_release, memory_order_acq_rel, memory_order_seq_cst
15 };
16
17 static void ensureModel() {
18         if (!model) {
19                 snapshot_system_init(10000, 1024, 1024, 40000);
20                 model = new ModelChecker();
21                 model->startChecker();
22         }
23 }
24
25 /** Performs a read action.*/
26 uint64_t model_read_action(void * obj, memory_order ord) {
27         return model->switch_to_master(new ModelAction(ATOMIC_READ, ord, obj));
28 }
29
30 /** Performs a write action.*/
31 void model_write_action(void * obj, memory_order ord, uint64_t val) {
32         model->switch_to_master(new ModelAction(ATOMIC_WRITE, ord, obj, val));
33 }
34
35 /** Performs an init action. */
36 void model_init_action(void * obj, uint64_t val) {
37         model->switch_to_master(new ModelAction(ATOMIC_INIT, memory_order_relaxed, obj, val));
38 }
39
40 /**
41  * Performs the read part of a RMW action. The next action must either be the
42  * write part of the RMW action or an explicit close out of the RMW action w/o
43  * a write.
44  */
45 uint64_t model_rmwr_action(void *obj, memory_order ord) {
46         return model->switch_to_master(new ModelAction(ATOMIC_RMWR, ord, obj));
47 }
48
49 /**
50  * Performs the read part of a RMW CAS action. The next action must
51  * either be the write part of the RMW action or an explicit close out
52  * of the RMW action w/o a write.
53  */
54 uint64_t model_rmwrcas_action(void *obj, memory_order ord, uint64_t oldval, int size) {
55         return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, ord, obj, oldval, size));
56 }
57
58
59 /** Performs the write part of a RMW action. */
60 void model_rmw_action(void *obj, memory_order ord, uint64_t val) {
61         model->switch_to_master(new ModelAction(ATOMIC_RMW, ord, obj, val));
62 }
63
64 /** Closes out a RMW action without doing a write. */
65 void model_rmwc_action(void *obj, memory_order ord) {
66         model->switch_to_master(new ModelAction(ATOMIC_RMWC, ord, obj));
67 }
68
69 /** Issues a fence operation. */
70 void model_fence_action(memory_order ord) {
71         model->switch_to_master(new ModelAction(ATOMIC_FENCE, ord, FENCE_LOCATION));
72 }
73
74 /* ---  helper functions --- */
75 uint64_t model_rmwrcas_action_helper(void *obj, int atomic_index, uint64_t oldval, int size, const char *position) {
76         ensureModel();
77         return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, position, orders[atomic_index], obj, oldval, size));
78 }
79
80 uint64_t model_rmwr_action_helper(void *obj, int atomic_index, const char *position) {
81         ensureModel();
82         return model->switch_to_master(new ModelAction(ATOMIC_RMWR, position, orders[atomic_index], obj));
83 }
84
85 void model_rmw_action_helper(void *obj, uint64_t val, int atomic_index, const char * position) {
86         ensureModel();
87         model->switch_to_master(new ModelAction(ATOMIC_RMW, position, orders[atomic_index], obj, val));
88 }
89
90 void model_rmwc_action_helper(void *obj, int atomic_index, const char *position) {
91         ensureModel();
92         model->switch_to_master(new ModelAction(ATOMIC_RMWC, position, orders[atomic_index], obj));
93 }
94
95 // cds atomic inits
96 #define CDSATOMICINT(size)                                              \
97         void cds_atomic_init ## size (void * obj, uint ## size ## _t val, const char * position) { \
98                 ensureModel();                                                      \
99                 model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)); \
100                 *((uint ## size ## _t *)obj) = val;                                 \
101         }
102
103 CDSATOMICINT(8)
104 CDSATOMICINT(16)
105 CDSATOMICINT(32)
106 CDSATOMICINT(64)
107
108 // cds atomic loads
109 #define CDSATOMICLOAD(size)                                             \
110         uint ## size ## _t cds_atomic_load ## size(void * obj, int atomic_index, const char * position) { \
111                 ensureModel();                                                      \
112                 return (uint ## size ## _t) model->switch_to_master( \
113                         new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)); \
114         }
115
116 CDSATOMICLOAD(8)
117 CDSATOMICLOAD(16)
118 CDSATOMICLOAD(32)
119 CDSATOMICLOAD(64)
120
121 // cds atomic stores
122
123 #define CDSATOMICSTORE(size)                                            \
124         void cds_atomic_store ## size(void * obj, uint ## size ## _t val, int atomic_index, const char * position) { \
125                 ensureModel();                                                        \
126                 model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)); \
127                 *((uint ## size ## _t *)obj) = val;                                   \
128         }
129
130 CDSATOMICSTORE(8)
131 CDSATOMICSTORE(16)
132 CDSATOMICSTORE(32)
133 CDSATOMICSTORE(64)
134
135
136 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position)            \
137         ({                                                                      \
138                 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position);   \
139                 uint ## size ## _t _copy = _old;                                          \
140                 uint ## size ## _t _val = val;                                            \
141                 _copy __op__ _val;                                                    \
142                 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position);        \
143                 return _old;                                                          \
144         })
145
146 // cds atomic exchange
147 #define CDSATOMICEXCHANGE(size)                                         \
148         uint ## size ## _t cds_atomic_exchange ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
149                 _ATOMIC_RMW_( =, size, addr, val, atomic_index, position);          \
150         }
151
152 CDSATOMICEXCHANGE(8)
153 CDSATOMICEXCHANGE(16)
154 CDSATOMICEXCHANGE(32)
155 CDSATOMICEXCHANGE(64)
156
157 // cds atomic fetch add
158 #define CDSATOMICADD(size)                                              \
159         uint ## size ## _t cds_atomic_fetch_add ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
160                 _ATOMIC_RMW_( +=, size, addr, val, atomic_index, position);         \
161         }
162
163 CDSATOMICADD(8)
164 CDSATOMICADD(16)
165 CDSATOMICADD(32)
166 CDSATOMICADD(64)
167
168 // cds atomic fetch sub
169 #define CDSATOMICSUB(size)                                              \
170         uint ## size ## _t cds_atomic_fetch_sub ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
171                 _ATOMIC_RMW_( -=, size, addr, val, atomic_index, position);         \
172         }
173
174 CDSATOMICSUB(8)
175 CDSATOMICSUB(16)
176 CDSATOMICSUB(32)
177 CDSATOMICSUB(64)
178
179 // cds atomic fetch and
180 #define CDSATOMICAND(size)                                              \
181         uint ## size ## _t cds_atomic_fetch_and ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
182                 _ATOMIC_RMW_( &=, size, addr, val, atomic_index, position);         \
183         }
184
185 CDSATOMICAND(8)
186 CDSATOMICAND(16)
187 CDSATOMICAND(32)
188 CDSATOMICAND(64)
189
190 // cds atomic fetch or
191 #define CDSATOMICOR(size)                                               \
192         uint ## size ## _t cds_atomic_fetch_or ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
193                 _ATOMIC_RMW_( |=, size, addr, val, atomic_index, position);         \
194         }
195
196 CDSATOMICOR(8)
197 CDSATOMICOR(16)
198 CDSATOMICOR(32)
199 CDSATOMICOR(64)
200
201 // cds atomic fetch xor
202 #define CDSATOMICXOR(size)                                              \
203         uint ## size ## _t cds_atomic_fetch_xor ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
204                 _ATOMIC_RMW_( ^=, size, addr, val, atomic_index, position);         \
205         }
206
207 CDSATOMICXOR(8)
208 CDSATOMICXOR(16)
209 CDSATOMICXOR(32)
210 CDSATOMICXOR(64)
211
212 // cds atomic compare and exchange
213 // In order to accomodate the LLVM PASS, the return values are not true or false.
214
215 #define _ATOMIC_CMPSWP_WEAK_ _ATOMIC_CMPSWP_
216 #define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position)                            \
217         ({                                                                                              \
218                 uint ## size ## _t _desired = desired;                                                            \
219                 uint ## size ## _t _expected = expected;                                                          \
220                 uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
221                 if (_old == _expected) {                                                                    \
222                         model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); return _expected; }      \
223                 else {                                                                                        \
224                         model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; }              \
225         })
226
227 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
228 // extracted from LLVM IR is an integer type.
229 #define CDSATOMICCASV1(size)                                            \
230         uint ## size ## _t cds_atomic_compare_exchange ## size ## _v1(void* addr, uint ## size ## _t expected, uint ## size ## _t desired, int atomic_index_succ, int atomic_index_fail, const char *position) { \
231                 _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index_succ, position); \
232         }
233
234 CDSATOMICCASV1(8)
235 CDSATOMICCASV1(16)
236 CDSATOMICCASV1(32)
237 CDSATOMICCASV1(64)
238
239 // atomic_compare_exchange version 2
240 #define CDSATOMICCASV2(size)                                            \
241         bool cds_atomic_compare_exchange ## size ## _v2(void* addr, uint ## size ## _t* expected, uint ## size ## _t desired, int atomic_index_succ, int atomic_index_fail, const char *position) { \
242                 uint ## size ## _t ret = cds_atomic_compare_exchange ## size ## _v1(addr, *expected, desired, atomic_index_succ, atomic_index_fail, position); \
243                 if (ret == *expected) {return true;} else {return false;}               \
244         }
245
246 CDSATOMICCASV2(8)
247 CDSATOMICCASV2(16)
248 CDSATOMICCASV2(32)
249 CDSATOMICCASV2(64)
250
251 // cds atomic thread fence
252
253 void cds_atomic_thread_fence(int atomic_index, const char * position) {
254         model->switch_to_master(
255                 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
256                 );
257 }
258
259 /*
260  #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ )                         \
261         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
262                 __typeof__(__e__) __q__ = (__e__);                            \
263                 __typeof__(__m__) __v__ = (__m__);                            \
264                 bool __r__;                                                   \
265                 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
266                 if (__t__ == * __q__ ) {                                      \
267                         model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
268                 else {  model_rmwc_action((void *)__p__, __x__); *__q__ = __t__;  __r__ = false;} \
269                 __r__; })
270
271  #define _ATOMIC_FENCE_( __x__ ) \
272         ({ model_fence_action(__x__);})
273  */
274
275 /*
276
277  #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ )                         \
278         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
279         __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
280         __typeof__(__m__) __v__ = (__m__);                                    \
281         __typeof__((__a__)->__f__) __copy__= __old__;                         \
282         __copy__ __o__ __v__;                                                 \
283         model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__);          \
284         __old__ = __old__;  Silence clang (-Wunused-value)                    \
285          })
286  */
287
288 void cds_func_entry(const char * funcName) {
289         if (!model) return;
290
291         Thread * th = thread_current();
292         uint32_t func_id;
293
294         ModelHistory *history = model->get_history();
295         if ( !history->getFuncMap()->contains(funcName) ) {
296                 // add func id to func map
297                 func_id = history->get_func_counter();
298                 history->incr_func_counter();
299                 history->getFuncMap()->put(funcName, func_id);
300
301                 // add func id to reverse func map
302                 ModelVector<const char *> * func_map_rev = history->getFuncMapRev();
303                 if ( func_map_rev->size() <= func_id )
304                         func_map_rev->resize( func_id + 1 );
305                 func_map_rev->at(func_id) = funcName;
306         } else {
307                 func_id = history->getFuncMap()->get(funcName);
308         }
309
310         history->enter_function(func_id, th->get_id());
311 }
312
313 void cds_func_exit(const char * funcName) {
314         if (!model) return;
315
316         Thread * th = thread_current();
317         uint32_t func_id;
318
319         ModelHistory *history = model->get_history();
320         func_id = history->getFuncMap()->get(funcName);
321
322         /* func_id not found; this could happen in the case where a function calls cds_func_entry
323          * when the model has been defined yet, but then an atomic inside the function initializes
324          * the model. And then cds_func_exit is called upon the function exiting.
325          */
326         if (func_id == 0)
327                 return;
328
329         history->exit_function(func_id, th->get_id());
330 }