missing include
[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 #else
118 #define VAR(name) name
119 #define CALL00(name) name()
120 #define CALL01(name, alt) name(alt)
121 #define CALL02(name, alt1, alt2) name(alt1, alt2)
122 #define CALL11(name,rest, alt) name(alt)
123 #define CALL12(name,rest, alt1, alt2) name(alt1, alt2)
124 #define CALL22(name, rest, rest2, alt1, alt2) name(alt1, alt2)
125 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(alt1, alt2, alt3)
126 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
127 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
128 #define CALL35(name, rest, rest2, rest3, alt1, alt2, alt3, alt4, alt5) name(alt1, alt2, alt3, alt4, alt5)
129 #endif
130
131 #ifdef MULTICORE
132 #include "SimpleHash.h"
133 inline void run(int argc, char** argv);
134 int receiveObject(int send_port_pending);
135 void * smemalloc_I(int coren, int size, int * allocsize);
136 #ifdef MULTICORE_GC
137 inline void setupsmemmode(void);
138 #endif
139 #endif
140
141 #ifdef TASK
142 #ifndef MULTICORE
143 #include "chash.h"
144 #include "ObjectHash.h"
145 #include "structdefs.h"
146 #endif
147 #include "task.h"
148 #ifdef OPTIONAL
149 #include "optionalstruct.h"
150 #endif
151
152 #ifdef OPTIONAL
153 struct failedtasklist {
154   struct taskdescriptor *task;
155   int index;
156   int numflags;
157   int *flags;
158   struct failedtasklist *next;
159 };
160 #endif
161
162 #ifdef MULTICORE
163 struct transObjInfo {
164   void * objptr;
165   int targetcore;
166   int * queues;
167   int length;
168 };
169 #endif
170
171 #ifdef FASTCHECK
172 extern struct ___Object___ * ___fcrevert___;
173 #endif
174
175 #ifdef MULTICORE
176 void flagorand(void * ptr, int ormask, int andmask, struct parameterwrapper ** queues, int length);
177 void flagorandinit(void * ptr, int ormask, int andmask);
178 void enqueueObject(void * ptr, struct parameterwrapper ** queues,int length);
179 #ifdef PROFILE
180 inline void setTaskExitIndex(int index);
181 inline void addNewObjInfo(void * nobj);
182 #endif
183 int * getAliasLock(void ** ptrs, int length, struct RuntimeHash * tbl);
184 void addAliasLock(void * ptr, int lock);
185 #else
186 void flagorand(void * ptr, int ormask, int andmask);
187 void flagorandinit(void * ptr, int ormask, int andmask);
188 void enqueueObject(void * ptr);
189 #endif
190 void executetasks();
191 void processtasks();
192
193 #ifndef MULTICORE
194 struct tagobjectiterator {
195   int istag; /* 0 if object iterator, 1 if tag iterator */
196   struct ObjectIterator it; /* Object iterator */
197   struct ObjectHash * objectset;
198 #ifdef OPTIONAL
199   int failedstate;
200 #endif
201   int slot;
202   int tagobjindex; /* Index for tag or object depending on use */
203   /*if tag we have an object binding */
204   int tagid;
205   int tagobjectslot;
206   /*if object, we may have one or more tag bindings */
207   int numtags;
208   int tagbindings[MAXTASKPARAMS-1]; /* list slots */
209 };
210
211 struct parameterwrapper {
212   struct parameterwrapper *next;
213   struct ObjectHash * objectset;
214   int numberofterms;
215   int * intarray;
216   int numbertags;
217   int * tagarray;
218   struct taskdescriptor * task;
219   int slot;
220   struct tagobjectiterator iterators[MAXTASKPARAMS-1];
221 };
222 #endif
223
224 struct taskparamdescriptor {
225   struct taskdescriptor * task;
226   int numParameters;
227   void ** parameterArray;
228 #ifdef OPTIONAL
229   int * failed;
230 #endif
231 };
232
233 int hashCodetpd(struct taskparamdescriptor *);
234 int comparetpd(struct taskparamdescriptor *, struct taskparamdescriptor *);
235
236 void toiReset(struct tagobjectiterator * it);
237 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
238 void toiNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
239 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams);
240 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams);
241 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter);
242 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags);
243
244 #endif
245
246 #if defined(__i386__)
247
248 static __inline__ unsigned long long rdtsc(void)
249 {
250   unsigned long long int x;
251   __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
252   return x;
253 }
254 #elif defined(__x86_64__)
255
256 static __inline__ unsigned long long rdtsc(void)
257 {
258   unsigned hi, lo;
259   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
260   return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
261 }
262
263 #elif defined(__powerpc__)
264
265 typedef unsigned long long int unsigned long long;
266
267 static __inline__ unsigned long long rdtsc(void)
268 {
269   unsigned long long int result=0;
270   unsigned long int upper, lower,tmp;
271   __asm__ volatile(
272       "0:                  \n"
273       "\tmftbu   %0           \n"
274       "\tmftb    %1           \n"
275       "\tmftbu   %2           \n"
276       "\tcmpw    %2,%0        \n"
277       "\tbne     0b         \n"
278       : "=r"(upper),"=r"(lower),"=r"(tmp)
279                    );
280   result = upper;
281   result = result<<32;
282   result = result|lower;
283
284   return(result);
285 }
286 #endif
287
288
289 #endif