Fix compilation problems for multicore code
[IRC.git] / Robust / src / Runtime / runtime.h
1 #ifndef RUNTIME
2 #define RUNTIME
3 #include <stdlib.h>
4 #ifndef MULTICORE
5 #include <setjmp.h>
6 extern jmp_buf error_handler;
7 extern int instructioncount;
8 extern int failurecount;
9 #endif
10 #ifdef DSTM
11 #ifdef RECOVERY
12 #include "DSTM/interface_recovery/dstm.h"
13 #else
14 #include "DSTM/interface/dstm.h"
15 #endif
16 #endif
17
18 #ifdef AFFINITY
19 void set_affinity();
20 #endif
21
22 #ifndef INTPTR
23 #ifdef BIT64
24 #define INTPTR long
25 #define INTPTRSHIFT 3
26 #else
27 #define INTPTR int
28 #define INTPTRSHIFT 2
29 #endif
30 #endif
31
32 #ifndef CACHELINESIZE
33 // The L1 and L2 cache line size for the
34 // AMD Opteron 6168 (dc-10) is 64 bytes.  Source:
35 // http://www.cs.virginia.edu/~skadron/cs451/opteron/opteron.ppt
36 #define CACHELINESIZE 64
37 #endif
38
39 extern void * curr_heapbase;
40 extern void * curr_heaptop;
41
42 #define likely(x) __builtin_expect((x),1)
43 #define unlikely(x) __builtin_expect((x),0)
44
45
46 #define TAGARRAYINTERVAL 10
47 #define OBJECTARRAYINTERVAL 10
48
49 #define ARRAYSET(array, type, index, value) \
50   ((type *)(& (& array->___length___)[1]))[index]=value
51
52 #define ARRAYGET(array, type, index) \
53   ((type *)(& (& array->___length___)[1]))[index]
54
55 #ifdef OPTIONAL
56 #define OPTARG(x) , x
57 #else
58 #define OPTARG(x)
59 #endif
60
61 #ifdef DSTM
62 __attribute__((malloc)) void * allocate_newglobal(int type);
63 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length);
64 #endif
65
66 #ifdef STM
67 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type);
68 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length);
69 #endif
70
71 #ifdef PRECISE_GC
72 #include "garbage.h"
73 #ifdef MLP
74 __attribute__((malloc)) void * allocate_new_mlp(void *, int type, int oid, int allocsite);
75 __attribute__((malloc)) void * allocate_new(void *, int type);
76 __attribute__((malloc)) struct ArrayObject * allocate_newarray_mlp(void *, int type, int length, int oid, int allocsite);
77 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length);
78 #else
79 __attribute__((malloc)) void * allocate_new(void *, int type);
80 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void *, int type, int length);
81 #endif
82 __attribute__((malloc)) struct ___String___ * NewString(void *, const char *str,int length);
83 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(void *ptr, int index);
84 #elif defined MULTICORE_GC
85 __attribute__((malloc)) void * allocate_new(void *, int type);
86 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void *, int type, int length);
87 __attribute__((malloc)) struct ___String___ * NewString(void *, const char *str,int length);
88 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(void *ptr, int index);
89 #else
90 __attribute__((malloc)) void * allocate_new(int type);
91 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length);
92 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length);
93 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(int index);
94 #endif
95
96
97
98 void initializeexithandler();
99 void failedboundschk();
100 void failednullptr();
101 void abort_task();
102 void injectinstructionfailure();
103 void createstartupobject();
104
105 #ifdef PRECISE_GC
106 #define VAR(name) ___params___->name
107 #define CALL00(name) name(struct name ## _params * ___params___)
108 #define CALL01(name, alt) name(struct name ## _params * ___params___)
109 #define CALL02(name, alt1, alt2) name(struct name ## _params * ___params___)
110 #define CALL11(name,rest, alt) name(struct name ## _params * ___params___, rest)
111 #define CALL12(name,rest, alt1, alt2) name(struct name ## _params * ___params___, rest)
112 #define CALL22(name, rest, rest2, alt1, alt2) name(struct name ## _params * ___params___, rest, rest2)
113 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(struct name ## _params * ___params___, rest, rest2)
114 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2)
115 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2, rest3)
116 #define CALL35(name, rest, rest2, rest3, alt1, alt2, alt3, alt4, alt5) name(struct name ## _params * ___params___, rest, rest2, rest3)
117 #elif defined MULTICORE_GC
118 #define VAR(name) ___params___->name
119 #define CALL00(name) name(struct name ## _params * ___params___)
120 #define CALL01(name, alt) name(struct name ## _params * ___params___)
121 #define CALL02(name, alt1, alt2) name(struct name ## _params * ___params___)
122 #define CALL11(name,rest, alt) name(struct name ## _params * ___params___, rest)
123 #define CALL12(name,rest, alt1, alt2) name(struct name ## _params * ___params___, rest)
124 #define CALL22(name, rest, rest2, alt1, alt2) name(struct name ## _params * ___params___, rest, rest2)
125 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(struct name ## _params * ___params___, rest, rest2)
126 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2)
127 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2, rest3)
128 #define CALL35(name, rest, rest2, rest3, alt1, alt2, alt3, alt4, alt5) name(struct name ## _params * ___params___, rest, rest2, rest3)
129 #else
130 #define VAR(name) name
131 #define CALL00(name) name()
132 #define CALL01(name, alt) name(alt)
133 #define CALL02(name, alt1, alt2) name(alt1, alt2)
134 #define CALL11(name,rest, alt) name(alt)
135 #define CALL12(name,rest, alt1, alt2) name(alt1, alt2)
136 #define CALL22(name, rest, rest2, alt1, alt2) name(alt1, alt2)
137 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(alt1, alt2, alt3)
138 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
139 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
140 #define CALL35(name, rest, rest2, rest3, alt1, alt2, alt3, alt4, alt5) name(alt1, alt2, alt3, alt4, alt5)
141 #endif
142
143 #ifdef MULTICORE
144 #include "SimpleHash.h"
145 inline void run(int argc, char** argv);
146 int receiveObject(int send_port_pending);
147 void * smemalloc_I(int coren, int size, int * allocsize);
148 #ifdef MULTICORE_GC
149 inline void setupsmemmode(void);
150 #endif
151 #endif
152
153 #ifdef TASK
154 #ifndef MULTICORE
155 #include "chash.h"
156 #include "ObjectHash.h"
157 #include "structdefs.h"
158 #endif
159 #include "task.h"
160 #ifdef OPTIONAL
161 #include "optionalstruct.h"
162 #endif
163
164 #ifdef OPTIONAL
165 struct failedtasklist {
166   struct taskdescriptor *task;
167   int index;
168   int numflags;
169   int *flags;
170   struct failedtasklist *next;
171 };
172 #endif
173
174 #ifdef MULTICORE
175 struct transObjInfo {
176   void * objptr;
177   int targetcore;
178   int * queues;
179   int length;
180 };
181 #endif
182
183 #ifdef FASTCHECK
184 extern struct ___Object___ * ___fcrevert___;
185 #endif
186
187 #ifdef MULTICORE
188 void flagorand(void * ptr, int ormask, int andmask, struct parameterwrapper ** queues, int length);
189 void flagorandinit(void * ptr, int ormask, int andmask);
190 void enqueueObject(void * ptr, struct parameterwrapper ** queues,int length);
191 #ifdef PROFILE
192 inline void setTaskExitIndex(int index);
193 inline void addNewObjInfo(void * nobj);
194 #endif
195 int * getAliasLock(void ** ptrs, int length, struct RuntimeHash * tbl);
196 void addAliasLock(void * ptr, int lock);
197 #else
198 void flagorand(void * ptr, int ormask, int andmask);
199 void flagorandinit(void * ptr, int ormask, int andmask);
200 void enqueueObject(void * ptr);
201 #endif
202 void executetasks();
203 void processtasks();
204
205 #ifndef MULTICORE
206 struct tagobjectiterator {
207   int istag; /* 0 if object iterator, 1 if tag iterator */
208   struct ObjectIterator it; /* Object iterator */
209   struct ObjectHash * objectset;
210 #ifdef OPTIONAL
211   int failedstate;
212 #endif
213   int slot;
214   int tagobjindex; /* Index for tag or object depending on use */
215   /*if tag we have an object binding */
216   int tagid;
217   int tagobjectslot;
218   /*if object, we may have one or more tag bindings */
219   int numtags;
220   int tagbindings[MAXTASKPARAMS-1]; /* list slots */
221 };
222
223 struct parameterwrapper {
224   struct parameterwrapper *next;
225   struct ObjectHash * objectset;
226   int numberofterms;
227   int * intarray;
228   int numbertags;
229   int * tagarray;
230   struct taskdescriptor * task;
231   int slot;
232   struct tagobjectiterator iterators[MAXTASKPARAMS-1];
233 };
234 #endif
235
236 struct taskparamdescriptor {
237   struct taskdescriptor * task;
238   int numParameters;
239   void ** parameterArray;
240 #ifdef OPTIONAL
241   int * failed;
242 #endif
243 };
244
245 int hashCodetpd(struct taskparamdescriptor *);
246 int comparetpd(struct taskparamdescriptor *, struct taskparamdescriptor *);
247
248 void toiReset(struct tagobjectiterator * it);
249 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
250 void toiNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
251 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams);
252 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams);
253 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter);
254 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags);
255
256 #endif
257
258 #if defined(__i386__)
259
260 static __inline__ unsigned long long rdtsc(void)
261 {
262   unsigned long long int x;
263   __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
264   return x;
265 }
266 #elif defined(__x86_64__)
267
268 static __inline__ unsigned long long rdtsc(void)
269 {
270   unsigned hi, lo;
271   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
272   return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
273 }
274
275 #elif defined(__powerpc__)
276
277 typedef unsigned long long int unsigned long long;
278
279 static __inline__ unsigned long long rdtsc(void)
280 {
281   unsigned long long int result=0;
282   unsigned long int upper, lower,tmp;
283   __asm__ volatile(
284       "0:                  \n"
285       "\tmftbu   %0           \n"
286       "\tmftb    %1           \n"
287       "\tmftbu   %2           \n"
288       "\tcmpw    %2,%0        \n"
289       "\tbne     0b         \n"
290       : "=r"(upper),"=r"(lower),"=r"(tmp)
291                    );
292   result = upper;
293   result = result<<32;
294   result = result|lower;
295
296   return(result);
297 }
298 #endif
299
300
301 #endif