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