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