*** empty log message ***
[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 __attribute__((malloc)) void * allocate_new(void *, int type);
66 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void *, int type, int length);
67 __attribute__((malloc)) struct ___String___ * NewString(void *, const char *str,int length);
68 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(void *ptr, int index);
69 #elif defined MULTICORE_GC
70 __attribute__((malloc)) void * allocate_new(void *, int type);
71 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void *, int type, int length);
72 __attribute__((malloc)) struct ___String___ * NewString(void *, const char *str,int length);
73 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(void *ptr, int index);
74 #else
75 __attribute__((malloc)) void * allocate_new(int type);
76 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length);
77 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length);
78 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(int index);
79 #endif
80
81
82
83 void initializeexithandler();
84 void failedboundschk();
85 void failednullptr();
86 void abort_task();
87 void injectinstructionfailure();
88 void createstartupobject();
89
90 #ifdef PRECISE_GC
91 #define VAR(name) ___params___->name
92 #define CALL00(name) name(struct name ## _params * ___params___)
93 #define CALL01(name, alt) name(struct name ## _params * ___params___)
94 #define CALL02(name, alt1, alt2) name(struct name ## _params * ___params___)
95 #define CALL11(name,rest, alt) name(struct name ## _params * ___params___, rest)
96 #define CALL12(name,rest, alt1, alt2) name(struct name ## _params * ___params___, rest)
97 #define CALL22(name, rest, rest2, alt1, alt2) name(struct name ## _params * ___params___, rest, rest2)
98 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(struct name ## _params * ___params___, rest, rest2)
99 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2)
100 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2, rest3)
101 #else
102 #define VAR(name) name
103 #define CALL00(name) name()
104 #define CALL01(name, alt) name(alt)
105 #define CALL02(name, alt1, alt2) name(alt1, alt2)
106 #define CALL11(name,rest, alt) name(alt)
107 #define CALL12(name,rest, alt1, alt2) name(alt1, alt2)
108 #define CALL22(name, rest, rest2, alt1, alt2) name(alt1, alt2)
109 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(alt1, alt2, alt3)
110 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
111 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
112 #endif
113
114 #ifdef TASK
115 #include "SimpleHash.h"
116 #ifndef MULTICORE
117 #include "chash.h"
118 #include "ObjectHash.h"
119 #include "structdefs.h"
120 #endif
121 #include "task.h"
122 #ifdef OPTIONAL
123 #include "optionalstruct.h"
124 #endif
125
126 #ifdef OPTIONAL
127 struct failedtasklist {
128   struct taskdescriptor *task;
129   int index;
130   int numflags;
131   int *flags;
132   struct failedtasklist *next;
133 };
134 #endif
135
136 #ifdef MULTICORE
137 struct transObjInfo {
138   void * objptr;
139   int targetcore;
140   int * queues;
141   int length;
142 };
143 #endif
144
145 #ifdef FASTCHECK
146 extern struct ___Object___ * ___fcrevert___;
147 #endif
148
149 #ifdef MULTICORE
150 inline void run(void * arg);
151 #ifdef MULTICORE_GC
152 inline void setupsmemmode(void);
153 #endif
154 int receiveObject(void);
155 void flagorand(void * ptr, int ormask, int andmask, struct parameterwrapper ** queues, int length);
156 void flagorandinit(void * ptr, int ormask, int andmask);
157 void enqueueObject(void * ptr, struct parameterwrapper ** queues,int length);
158 #ifdef PROFILE
159 inline void setTaskExitIndex(int index);
160 inline void addNewObjInfo(void * nobj);
161 #endif
162 int * getAliasLock(void ** ptrs, int length, struct RuntimeHash * tbl);
163 void addAliasLock(void * ptr, int lock);
164 void * smemalloc_I(int coren, int size, int * allocsize);
165 #else
166 void flagorand(void * ptr, int ormask, int andmask);
167 void flagorandinit(void * ptr, int ormask, int andmask);
168 void enqueueObject(void * ptr);
169 #endif
170 void executetasks();
171 void processtasks();
172
173 #ifndef MULTICORE
174 struct tagobjectiterator {
175   int istag; /* 0 if object iterator, 1 if tag iterator */
176   struct ObjectIterator it; /* Object iterator */
177   struct ObjectHash * objectset;
178 #ifdef OPTIONAL
179   int failedstate;
180 #endif
181   int slot;
182   int tagobjindex; /* Index for tag or object depending on use */
183   /*if tag we have an object binding */
184   int tagid;
185   int tagobjectslot;
186   /*if object, we may have one or more tag bindings */
187   int numtags;
188   int tagbindings[MAXTASKPARAMS-1]; /* list slots */
189 };
190
191 struct parameterwrapper {
192   struct parameterwrapper *next;
193   struct ObjectHash * objectset;
194   int numberofterms;
195   int * intarray;
196   int numbertags;
197   int * tagarray;
198   struct taskdescriptor * task;
199   int slot;
200   struct tagobjectiterator iterators[MAXTASKPARAMS-1];
201 };
202 #endif
203
204 struct taskparamdescriptor {
205   struct taskdescriptor * task;
206   int numParameters;
207   void ** parameterArray;
208 #ifdef OPTIONAL
209   int * failed;
210 #endif
211 };
212
213 int hashCodetpd(struct taskparamdescriptor *);
214 int comparetpd(struct taskparamdescriptor *, struct taskparamdescriptor *);
215
216 void toiReset(struct tagobjectiterator * it);
217 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
218 void toiNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
219 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams);
220 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams);
221 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter);
222 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags);
223
224 #endif
225
226 #if defined(__i386__)
227
228 static __inline__ unsigned long long rdtsc(void)
229 {
230   unsigned long long int x;
231   __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
232   return x;
233 }
234 #elif defined(__x86_64__)
235
236 static __inline__ unsigned long long rdtsc(void)
237 {
238   unsigned hi, lo;
239   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
240   return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
241 }
242
243 #elif defined(__powerpc__)
244
245 typedef unsigned long long int unsigned long long;
246
247 static __inline__ unsigned long long rdtsc(void)
248 {
249   unsigned long long int result=0;
250   unsigned long int upper, lower,tmp;
251   __asm__ volatile(
252       "0:                  \n"
253       "\tmftbu   %0           \n"
254       "\tmftb    %1           \n"
255       "\tmftbu   %2           \n"
256       "\tcmpw    %2,%0        \n"
257       "\tbne     0b         \n"
258       : "=r"(upper),"=r"(lower),"=r"(tmp)
259                    );
260   result = upper;
261   result = result<<32;
262   result = result|lower;
263
264   return(result);
265 }
266 #endif
267
268
269 #endif