move work_list (thrd_func_list) from history.h to execution.h, and fix the memory...
[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 "threads-model.h"
10
11 memory_order orders[6] = {
12         memory_order_relaxed, memory_order_consume, memory_order_acquire,
13         memory_order_release, memory_order_acq_rel, memory_order_seq_cst
14 };
15
16 /** Performs a read action.*/
17 uint64_t model_read_action(void * obj, memory_order ord) {
18         return model->switch_to_master(new ModelAction(ATOMIC_READ, ord, obj));
19 }
20
21 /** Performs a write action.*/
22 void model_write_action(void * obj, memory_order ord, uint64_t val) {
23         model->switch_to_master(new ModelAction(ATOMIC_WRITE, ord, obj, val));
24 }
25
26 /** Performs an init action. */
27 void model_init_action(void * obj, uint64_t val) {
28         model->switch_to_master(new ModelAction(ATOMIC_INIT, memory_order_relaxed, obj, val));
29 }
30
31 /**
32  * Performs the read part of a RMW action. The next action must either be the
33  * write part of the RMW action or an explicit close out of the RMW action w/o
34  * a write.
35  */
36 uint64_t model_rmwr_action(void *obj, memory_order ord) {
37         return model->switch_to_master(new ModelAction(ATOMIC_RMWR, ord, obj));
38 }
39
40 /**
41  * Performs the read part of a RMW CAS action. The next action must
42  * either be the write part of the RMW action or an explicit close out
43  * of the RMW action w/o a write.
44  */
45 uint64_t model_rmwrcas_action(void *obj, memory_order ord, uint64_t oldval, int size) {
46         return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, ord, obj, oldval, size));
47 }
48
49
50 /** Performs the write part of a RMW action. */
51 void model_rmw_action(void *obj, memory_order ord, uint64_t val) {
52         model->switch_to_master(new ModelAction(ATOMIC_RMW, ord, obj, val));
53 }
54
55 /** Closes out a RMW action without doing a write. */
56 void model_rmwc_action(void *obj, memory_order ord) {
57         model->switch_to_master(new ModelAction(ATOMIC_RMWC, ord, obj));
58 }
59
60 /** Issues a fence operation. */
61 void model_fence_action(memory_order ord) {
62         model->switch_to_master(new ModelAction(ATOMIC_FENCE, ord, FENCE_LOCATION));
63 }
64
65 /* ---  helper functions --- */
66 uint64_t model_rmwrcas_action_helper(void *obj, int atomic_index, uint64_t oldval, int size, const char *position) {
67         return model->switch_to_master(
68                 new ModelAction(ATOMIC_RMWRCAS, position, orders[atomic_index], obj, oldval, size)
69                 );
70 }
71
72 uint64_t model_rmwr_action_helper(void *obj, int atomic_index, const char *position) {
73         return model->switch_to_master(
74                 new ModelAction(ATOMIC_RMWR, position, orders[atomic_index], obj)
75                 );
76 }
77
78 void model_rmw_action_helper(void *obj, uint64_t val, int atomic_index, const char * position) {
79         model->switch_to_master(
80                 new ModelAction(ATOMIC_RMW, position, orders[atomic_index], obj, val)
81                 );
82 }
83
84 void model_rmwc_action_helper(void *obj, int atomic_index, const char *position) {
85         model->switch_to_master(
86                 new ModelAction(ATOMIC_RMWC, position, orders[atomic_index], obj)
87                 );
88 }
89
90 // cds atomic inits
91 void cds_atomic_init8(void * obj, uint8_t val, const char * position) {
92         model->switch_to_master(
93                 new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)
94                 );
95 }
96 void cds_atomic_init16(void * obj, uint16_t val, const char * position) {
97         model->switch_to_master(
98                 new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)
99                 );
100 }
101 void cds_atomic_init32(void * obj, uint32_t val, const char * position) {
102         model->switch_to_master(
103                 new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)
104                 );
105 }
106 void cds_atomic_init64(void * obj, uint64_t val, const char * position) {
107         model->switch_to_master(
108                 new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, val)
109                 );
110 }
111
112
113 // cds atomic loads
114 uint8_t cds_atomic_load8(void * obj, int atomic_index, const char * position) {
115         return (uint8_t) ( model->switch_to_master(
116                                                                                          new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj))
117                                                                                  );
118 }
119 uint16_t cds_atomic_load16(void * obj, int atomic_index, const char * position) {
120         return (uint16_t) ( model->switch_to_master(
121                                                                                                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj))
122                                                                                         );
123 }
124 uint32_t cds_atomic_load32(void * obj, int atomic_index, const char * position) {
125         return (uint32_t) ( model->switch_to_master(
126                                                                                                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj))
127                                                                                         );
128 }
129 uint64_t cds_atomic_load64(void * obj, int atomic_index, const char * position) {
130         return model->switch_to_master(
131                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)
132                 );
133 }
134
135 // cds atomic stores
136 void cds_atomic_store8(void * obj, uint8_t val, int atomic_index, const char * position) {
137         model->switch_to_master(
138                 new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)
139                 );
140 }
141 void cds_atomic_store16(void * obj, uint16_t val, int atomic_index, const char * position) {
142         model->switch_to_master(
143                 new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)
144                 );
145 }
146 void cds_atomic_store32(void * obj, uint32_t val, int atomic_index, const char * position) {
147         model->switch_to_master(
148                 new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)
149                 );
150 }
151 void cds_atomic_store64(void * obj, uint64_t val, int atomic_index, const char * position) {
152         model->switch_to_master(
153                 new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, val)
154                 );
155 }
156
157 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position)            \
158         ({                                                                      \
159                 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position);   \
160                 uint ## size ## _t _copy = _old;                                          \
161                 uint ## size ## _t _val = val;                                            \
162                 _copy __op__ _val;                                                    \
163                 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position);        \
164                 return _old;                                                          \
165         })
166
167 // cds atomic exchange
168 uint8_t cds_atomic_exchange8(void* addr, uint8_t val, int atomic_index, const char * position) {
169         _ATOMIC_RMW_( =, 8, addr, val, atomic_index, position);
170 }
171 uint16_t cds_atomic_exchange16(void* addr, uint16_t val, int atomic_index, const char * position) {
172         _ATOMIC_RMW_( =, 16, addr, val, atomic_index, position);
173 }
174 uint32_t cds_atomic_exchange32(void* addr, uint32_t val, int atomic_index, const char * position) {
175         _ATOMIC_RMW_( =, 32, addr, val, atomic_index, position);
176 }
177 uint64_t cds_atomic_exchange64(void* addr, uint64_t val, int atomic_index, const char * position) {
178         _ATOMIC_RMW_( =, 64, addr, val, atomic_index, position);
179 }
180
181 // cds atomic fetch add
182 uint8_t cds_atomic_fetch_add8(void* addr, uint8_t val, int atomic_index, const char * position) {
183         _ATOMIC_RMW_( +=, 8, addr, val, atomic_index, position);
184 }
185 uint16_t cds_atomic_fetch_add16(void* addr, uint16_t val, int atomic_index, const char * position) {
186         _ATOMIC_RMW_( +=, 16, addr, val, atomic_index, position);
187 }
188 uint32_t cds_atomic_fetch_add32(void* addr, uint32_t val, int atomic_index, const char * position) {
189         _ATOMIC_RMW_( +=, 32, addr, val, atomic_index, position);
190 }
191 uint64_t cds_atomic_fetch_add64(void* addr, uint64_t val, int atomic_index, const char * position) {
192         _ATOMIC_RMW_( +=, 64, addr, val, atomic_index, position);
193 }
194
195 // cds atomic fetch sub
196 uint8_t cds_atomic_fetch_sub8(void* addr, uint8_t val, int atomic_index, const char * position) {
197         _ATOMIC_RMW_( -=, 8, addr, val, atomic_index, position);
198 }
199 uint16_t cds_atomic_fetch_sub16(void* addr, uint16_t val, int atomic_index, const char * position) {
200         _ATOMIC_RMW_( -=, 16, addr, val, atomic_index, position);
201 }
202 uint32_t cds_atomic_fetch_sub32(void* addr, uint32_t val, int atomic_index, const char * position) {
203         _ATOMIC_RMW_( -=, 32, addr, val, atomic_index, position);
204 }
205 uint64_t cds_atomic_fetch_sub64(void* addr, uint64_t val, int atomic_index, const char * position) {
206         _ATOMIC_RMW_( -=, 64, addr, val, atomic_index, position);
207 }
208
209 // cds atomic fetch and
210 uint8_t cds_atomic_fetch_and8(void* addr, uint8_t val, int atomic_index, const char * position) {
211         _ATOMIC_RMW_( &=, 8, addr, val, atomic_index, position);
212 }
213 uint16_t cds_atomic_fetch_and16(void* addr, uint16_t val, int atomic_index, const char * position) {
214         _ATOMIC_RMW_( &=, 16, addr, val, atomic_index, position);
215 }
216 uint32_t cds_atomic_fetch_and32(void* addr, uint32_t val, int atomic_index, const char * position) {
217         _ATOMIC_RMW_( &=, 32, addr, val, atomic_index, position);
218 }
219 uint64_t cds_atomic_fetch_and64(void* addr, uint64_t val, int atomic_index, const char * position) {
220         _ATOMIC_RMW_( &=, 64, addr, val, atomic_index, position);
221 }
222
223 // cds atomic fetch or
224 uint8_t cds_atomic_fetch_or8(void* addr, uint8_t val, int atomic_index, const char * position) {
225         _ATOMIC_RMW_( |=, 8, addr, val, atomic_index, position);
226 }
227 uint16_t cds_atomic_fetch_or16(void* addr, uint16_t val, int atomic_index, const char * position) {
228         _ATOMIC_RMW_( |=, 16, addr, val, atomic_index, position);
229 }
230 uint32_t cds_atomic_fetch_or32(void* addr, uint32_t val, int atomic_index, const char * position) {
231         _ATOMIC_RMW_( |=, 32, addr, val, atomic_index, position);
232 }
233 uint64_t cds_atomic_fetch_or64(void* addr, uint64_t val, int atomic_index, const char * position) {
234         _ATOMIC_RMW_( |=, 64, addr, val, atomic_index, position);
235 }
236
237 // cds atomic fetch xor
238 uint8_t cds_atomic_fetch_xor8(void* addr, uint8_t val, int atomic_index, const char * position) {
239         _ATOMIC_RMW_( ^=, 8, addr, val, atomic_index, position);
240 }
241 uint16_t cds_atomic_fetch_xor16(void* addr, uint16_t val, int atomic_index, const char * position) {
242         _ATOMIC_RMW_( ^=, 16, addr, val, atomic_index, position);
243 }
244 uint32_t cds_atomic_fetch_xor32(void* addr, uint32_t val, int atomic_index, const char * position) {
245         _ATOMIC_RMW_( ^=, 32, addr, val, atomic_index, position);
246 }
247 uint64_t cds_atomic_fetch_xor64(void* addr, uint64_t val, int atomic_index, const char * position) {
248         _ATOMIC_RMW_( ^=, 64, addr, val, atomic_index, position);
249 }
250
251 // cds atomic compare and exchange
252 // In order to accomodate the LLVM PASS, the return values are not true or false.
253
254 #define _ATOMIC_CMPSWP_WEAK_ _ATOMIC_CMPSWP_
255 #define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position)                            \
256         ({                                                                                              \
257                 uint ## size ## _t _desired = desired;                                                            \
258                 uint ## size ## _t _expected = expected;                                                          \
259                 uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
260                 if (_old == _expected) {                                                                    \
261                         model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); return _expected; }      \
262                 else {                                                                                        \
263                         model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; }              \
264         })
265
266 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
267 // extracted from LLVM IR is an integer type.
268
269 uint8_t cds_atomic_compare_exchange8_v1(void* addr, uint8_t expected, uint8_t desired,
270                                                                                                                                                                 int atomic_index_succ, int atomic_index_fail, const char *position )
271 {
272         _ATOMIC_CMPSWP_(8, addr, expected, desired,
273                                                                         atomic_index_succ, position);
274 }
275 uint16_t cds_atomic_compare_exchange16_v1(void* addr, uint16_t expected, uint16_t desired,
276                                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
277 {
278         _ATOMIC_CMPSWP_(16, addr, expected, desired,
279                                                                         atomic_index_succ, position);
280 }
281 uint32_t cds_atomic_compare_exchange32_v1(void* addr, uint32_t expected, uint32_t desired,
282                                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
283 {
284         _ATOMIC_CMPSWP_(32, addr, expected, desired,
285                                                                         atomic_index_succ, position);
286 }
287 uint64_t cds_atomic_compare_exchange64_v1(void* addr, uint64_t expected, uint64_t desired,
288                                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
289 {
290         _ATOMIC_CMPSWP_(64, addr, expected, desired,
291                                                                         atomic_index_succ, position);
292 }
293
294 // atomic_compare_exchange version 2
295 bool cds_atomic_compare_exchange8_v2(void* addr, uint8_t* expected, uint8_t desired,
296                                                                                                                                                  int atomic_index_succ, int atomic_index_fail, const char *position )
297 {
298         uint8_t ret = cds_atomic_compare_exchange8_v1(addr, *expected,
299                                                                                                                                                                                                 desired, atomic_index_succ, atomic_index_fail, position);
300         if (ret == *expected) return true;
301         else return false;
302 }
303 bool cds_atomic_compare_exchange16_v2(void* addr, uint16_t* expected, uint16_t desired,
304                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
305 {
306         uint16_t ret = cds_atomic_compare_exchange16_v1(addr, *expected,
307                                                                                                                                                                                                         desired, atomic_index_succ, atomic_index_fail, position);
308         if (ret == *expected) return true;
309         else return false;
310 }
311 bool cds_atomic_compare_exchange32_v2(void* addr, uint32_t* expected, uint32_t desired,
312                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
313 {
314         uint32_t ret = cds_atomic_compare_exchange32_v1(addr, *expected,
315                                                                                                                                                                                                         desired, atomic_index_succ, atomic_index_fail, position);
316         if (ret == *expected) return true;
317         else return false;
318 }
319 bool cds_atomic_compare_exchange64_v2(void* addr, uint64_t* expected, uint64_t desired,
320                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
321 {
322         uint64_t ret = cds_atomic_compare_exchange64_v1(addr, *expected,
323                                                                                                                                                                                                         desired, atomic_index_succ, atomic_index_fail, position);
324         if (ret == *expected) return true;
325         else return false;
326 }
327
328
329 // cds atomic thread fence
330
331 void cds_atomic_thread_fence(int atomic_index, const char * position) {
332         model->switch_to_master(
333                 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
334                 );
335 }
336
337 /*
338  #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ )                         \
339         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
340                 __typeof__(__e__) __q__ = (__e__);                            \
341                 __typeof__(__m__) __v__ = (__m__);                            \
342                 bool __r__;                                                   \
343                 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
344                 if (__t__ == * __q__ ) {                                      \
345                         model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
346                 else {  model_rmwc_action((void *)__p__, __x__); *__q__ = __t__;  __r__ = false;} \
347                 __r__; })
348
349  #define _ATOMIC_FENCE_( __x__ ) \
350         ({ model_fence_action(__x__);})
351  */
352
353 /*
354
355  #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ )                         \
356         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
357         __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
358         __typeof__(__m__) __v__ = (__m__);                                    \
359         __typeof__((__a__)->__f__) __copy__= __old__;                         \
360         __copy__ __o__ __v__;                                                 \
361         model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__);          \
362         __old__ = __old__;  Silence clang (-Wunused-value)                    \
363          })
364  */
365
366 void cds_func_entry(const char * funcName) {
367         if (!model) return;
368
369         Thread * th = thread_current();
370         uint32_t func_id;
371
372         ModelHistory *history = model->get_history();
373         if ( !history->getFuncMap()->contains(funcName) ) {
374                 func_id = history->get_func_counter();
375                 history->incr_func_counter();
376
377                 history->getFuncMap()->put(funcName, func_id);
378         } else {
379                 func_id = history->getFuncMap()->get(funcName);
380         }
381
382         history->enter_function(func_id, th->get_id());
383 }
384
385 void cds_func_exit(const char * funcName) {
386         if (!model) return;
387
388         Thread * th = thread_current();
389         uint32_t func_id;
390
391         ModelHistory *history = model->get_history();
392         func_id = history->getFuncMap()->get(funcName);
393
394         history->exit_function(func_id, th->get_id());
395 }