Fix tabbing.... Please fix your editors so they do tabbing correctly!!! (Spaces...
[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 ___String___ * NewStringShort(void *, const short *str,int length);
84 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(void *ptr, int index);
85 #elif defined MULTICORE_GC
86 __attribute__((malloc)) void * allocate_new(void *, int type);
87 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void *, int type, int length);
88 __attribute__((malloc)) struct ___String___ * NewString(void *, const char *str,int length);
89 __attribute__((malloc)) struct ___String___ * NewStringShort(void *, const short *str,int length);
90 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(void *ptr, int index);
91 #else
92 __attribute__((malloc)) void * allocate_new(int type);
93 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length);
94 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length);
95 __attribute__((malloc)) struct ___String___ * NewStringShort(const short *str,int length);
96 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(int index);
97 #endif
98
99
100
101 void initializeexithandler();
102 void failedboundschk(int num);
103 void failednullptr(void * stackptr);
104 void abort_task();
105 void injectinstructionfailure();
106 #ifdef MULTICORE
107 void createstartupobject(int argc, char ** argv);
108 #else
109 void createstartupobject();
110 #endif
111
112 #ifdef PRECISE_GC
113 #define VAR(name) ___params___->name
114 #define CALL00(name) name(struct name ## _params * ___params___)
115 #define CALL01(name, alt) name(struct name ## _params * ___params___)
116 #define CALL02(name, alt1, alt2) name(struct name ## _params * ___params___)
117 #define CALL11(name,rest, alt) name(struct name ## _params * ___params___, rest)
118 #define CALL12(name,rest, alt1, alt2) name(struct name ## _params * ___params___, rest)
119 #define CALL22(name, rest, rest2, alt1, alt2) name(struct name ## _params * ___params___, rest, rest2)
120 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(struct name ## _params * ___params___, rest, rest2)
121 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2)
122 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2, rest3)
123 #define CALL35(name, rest, rest2, rest3, alt1, alt2, alt3, alt4, alt5) name(struct name ## _params * ___params___, rest, rest2, rest3)
124 #elif defined MULTICORE_GC
125 #define VAR(name) ___params___->name
126 #define CALL00(name) name(struct name ## _params * ___params___)
127 #define CALL01(name, alt) name(struct name ## _params * ___params___)
128 #define CALL02(name, alt1, alt2) name(struct name ## _params * ___params___)
129 #define CALL11(name,rest, alt) name(struct name ## _params * ___params___, rest)
130 #define CALL12(name,rest, alt1, alt2) name(struct name ## _params * ___params___, rest)
131 #define CALL22(name, rest, rest2, alt1, alt2) name(struct name ## _params * ___params___, rest, rest2)
132 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(struct name ## _params * ___params___, rest, rest2)
133 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2)
134 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2, rest3)
135 #define CALL35(name, rest, rest2, rest3, alt1, alt2, alt3, alt4, alt5) name(struct name ## _params * ___params___, rest, rest2, rest3)
136 #else
137 #define VAR(name) name
138 #define CALL00(name) name()
139 #define CALL01(name, alt) name(alt)
140 #define CALL02(name, alt1, alt2) name(alt1, alt2)
141 #define CALL11(name,rest, alt) name(alt)
142 #define CALL12(name,rest, alt1, alt2) name(alt1, alt2)
143 #define CALL22(name, rest, rest2, alt1, alt2) name(alt1, alt2)
144 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(alt1, alt2, alt3)
145 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
146 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
147 #define CALL35(name, rest, rest2, rest3, alt1, alt2, alt3, alt4, alt5) name(alt1, alt2, alt3, alt4, alt5)
148 #endif
149
150 #ifdef MULTICORE
151 #include "SimpleHash.h"
152 inline void run(int argc, char** argv);
153 int receiveObject(int send_port_pending);
154 void * smemalloc_I(int coren, int size, int * allocsize);
155 #ifdef MULTICORE_GC
156 inline void setupsmemmode(void);
157 #endif
158 #endif
159
160 #ifdef THREADS
161 #define MAXLOCKS 256
162
163 struct lockpair {
164   struct ___Object___ *object;
165   int islastlock;
166 };
167
168 struct lockvector {
169   int index;
170   struct lockpair locks[MAXLOCKS];
171 };
172
173 #ifndef MAC
174 extern __thread struct lockvector lvector;
175 extern __thread int mythreadid;
176 #endif
177 #endif
178
179 #ifdef TASK
180 #ifndef MULTICORE
181 #include "chash.h"
182 #include "ObjectHash.h"
183 #include "structdefs.h"
184 #endif
185 #include "task.h"
186 #ifdef OPTIONAL
187 #include "optionalstruct.h"
188 #endif
189
190
191 #ifdef OPTIONAL
192 struct failedtasklist {
193   struct taskdescriptor *task;
194   int index;
195   int numflags;
196   int *flags;
197   struct failedtasklist *next;
198 };
199 #endif
200
201 #ifdef MULTICORE
202 struct transObjInfo {
203   void * objptr;
204   int targetcore;
205   int * queues;
206   int length;
207 };
208 #endif
209
210 #ifdef FASTCHECK
211 extern struct ___Object___ * ___fcrevert___;
212 #endif
213
214 #ifdef MULTICORE
215 void flagorand(void * ptr, int ormask, int andmask, struct parameterwrapper ** queues, int length);
216 void flagorandinit(void * ptr, int ormask, int andmask);
217 void enqueueObject(void * ptr, struct parameterwrapper ** queues,int length);
218 #ifdef PROFILE
219 inline void setTaskExitIndex(int index);
220 inline void addNewObjInfo(void * nobj);
221 #endif
222 int * getAliasLock(void ** ptrs, int length, struct RuntimeHash * tbl);
223 void addAliasLock(void * ptr, int lock);
224 #else
225 void flagorand(void * ptr, int ormask, int andmask);
226 void flagorandinit(void * ptr, int ormask, int andmask);
227 void enqueueObject(void * ptr);
228 #endif
229 void executetasks();
230 void processtasks();
231
232 #ifndef MULTICORE
233 struct tagobjectiterator {
234   int istag; /* 0 if object iterator, 1 if tag iterator */
235   struct ObjectIterator it; /* Object iterator */
236   struct ObjectHash * objectset;
237 #ifdef OPTIONAL
238   int failedstate;
239 #endif
240   int slot;
241   int tagobjindex; /* Index for tag or object depending on use */
242   /*if tag we have an object binding */
243   int tagid;
244   int tagobjectslot;
245   /*if object, we may have one or more tag bindings */
246   int numtags;
247   int tagbindings[MAXTASKPARAMS-1]; /* list slots */
248 };
249
250 struct parameterwrapper {
251   struct parameterwrapper *next;
252   struct ObjectHash * objectset;
253   int numberofterms;
254   int * intarray;
255   int numbertags;
256   int * tagarray;
257   struct taskdescriptor * task;
258   int slot;
259   struct tagobjectiterator iterators[MAXTASKPARAMS-1];
260 };
261 #endif
262
263 struct taskparamdescriptor {
264   struct taskdescriptor * task;
265   int numParameters;
266   void ** parameterArray;
267 #ifdef OPTIONAL
268   int * failed;
269 #endif
270 };
271
272 int hashCodetpd(struct taskparamdescriptor *);
273 int comparetpd(struct taskparamdescriptor *, struct taskparamdescriptor *);
274
275 void toiReset(struct tagobjectiterator * it);
276 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
277 void toiNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
278 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams);
279 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams);
280 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter);
281 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags);
282
283 #endif
284
285 #if defined(__i386__)
286
287 static __inline__ unsigned long long rdtsc(void) {
288   unsigned long long int x;
289   __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
290   return x;
291 }
292 #elif defined(__x86_64__)
293
294 static __inline__ unsigned long long rdtsc(void) {
295   unsigned hi, lo;
296   __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
297   return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
298 }
299
300 #elif defined(__powerpc__)
301
302 typedef unsigned long long int unsigned long long;
303
304 static __inline__ unsigned long long rdtsc(void) {
305   unsigned long long int result=0;
306   unsigned long int upper, lower,tmp;
307   __asm__ volatile (
308     "0:                  \n"
309     "\tmftbu   %0           \n"
310     "\tmftb    %1           \n"
311     "\tmftbu   %2           \n"
312     "\tcmpw    %2,%0        \n"
313     "\tbne     0b         \n"
314     : "=r" (upper),"=r" (lower),"=r" (tmp)
315     );
316   result = upper;
317   result = result<<32;
318   result = result|lower;
319
320   return(result);
321 }
322 #endif
323
324
325 #endif