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