7d278e2c6f8ac8b4c43261d3b0b85c6a2c6fbbe2
[IRC.git] / Robust / src / Runtime / multicoreruntime.c
1 #include "runtime.h"
2 #include "structdefs.h"
3 #include "mem.h"
4 #ifndef MULTICORE
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <signal.h>
8 #endif
9 #ifndef RAW
10 #include <stdio.h>
11 #endif
12 #ifdef MULTICORE
13 #include "runtime_arch.h"
14 #endif
15 //#include "option.h"
16
17 extern int classsize[];
18 extern int typearray[];
19 extern int typearray2[];
20 #ifndef MULTICORE
21 jmp_buf error_handler;
22 int instructioncount;
23
24 char *options;
25 int injectfailures=0;
26 float failurechance=0;
27 int errors=0;
28 int injectinstructionfailures;
29 int failurecount;
30 float instfailurechance=0;
31 int numfailures;
32 int instaccum=0;
33 #ifdef DMALLOC
34 #include "dmalloc.h"
35 #endif
36 #endif
37
38 int debugtask=0;
39
40 int instanceof(struct ___Object___ *ptr, int type) {
41   int i=ptr->type;
42   do {
43     if (i==type)
44       return 1;
45     i=typearray[i];
46   } while(i!=-1);
47   i=ptr->type;
48   if (i>NUMCLASSES) {
49     do {
50       if (i==type)
51         return 1;
52       i=typearray2[i-NUMCLASSES];
53     } while(i!=-1);
54   }
55   return 0;
56 }
57
58 #ifdef MULTICORE
59 void initializeexithandler() {
60 }
61 #else
62 void exithandler(int sig, siginfo_t *info, void * uap) {
63 #ifdef DEBUG
64   printf("exit in exithandler\n");
65 #endif
66   exit(0);
67 }
68
69 void initializeexithandler() {
70   struct sigaction sig;
71   sig.sa_sigaction=&exithandler;
72   sig.sa_flags=SA_SIGINFO;
73   sigemptyset(&sig.sa_mask);
74   sigaction(SIGUSR2, &sig, 0);
75 }
76 #endif
77
78 /* This function inject failures */
79
80 void injectinstructionfailure() {
81 #ifdef MULTICORE
82   // not supported in MULTICORE version
83   return;
84 #else
85 #ifdef TASK
86   if (injectinstructionfailures) {
87     if (numfailures==0)
88       return;
89     instructioncount=failurecount;
90     instaccum+=failurecount;
91     if ((((double)random())/RAND_MAX)<instfailurechance) {
92       if (numfailures>0)
93         numfailures--;
94       printf("FAILURE!!! %d\n",numfailures);
95       longjmp(error_handler,11);
96     }
97   }
98 #else
99 #ifdef THREADS
100   if (injectinstructionfailures) {
101     if (numfailures==0)
102       return;
103     instaccum+=failurecount;
104     if ((((double)random())/RAND_MAX)<instfailurechance) {
105       if (numfailures>0)
106         numfailures--;
107       printf("FAILURE!!! %d\n",numfailures);
108       threadexit();
109     }
110   }
111 #endif
112 #endif
113 #endif
114 }
115
116 #ifdef D___Double______nativeparsedouble____L___String___
117 double CALL01(___Double______nativeparsedouble____L___String___,struct ___String___ * ___str___) {
118   int length=VAR(___str___)->___count___;
119   int maxlength=(length>60) ? 60 : length;
120   char str[maxlength+1];
121   struct ArrayObject * chararray=VAR(___str___)->___value___;
122   int i;
123   int offset=VAR(___str___)->___offset___;
124   for(i=0; i<maxlength; i++) {
125     str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
126   }
127   str[i]=0;
128   double d=atof(str);
129   return d;
130 }
131 #endif
132
133 #ifdef D___String______convertdoubletochar____D__AR_C
134 int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
135   int length=VAR(___chararray___)->___length___;
136   char str[length];
137   int i;
138   int num=snprintf(str, length, "%f",___val___);
139   if (num>=length)
140     num=length-1;
141   for(i=0; i<length; i++) {
142     ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
143   }
144   return num;
145 }
146 #else
147 int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
148   return 0;
149 }
150 #endif
151
152 void CALL11(___System______exit____I,int ___status___, int ___status___) {
153 #ifdef MULTICORE
154   BAMBOO_EXIT(___status___);
155 #else
156 #ifdef DEBUG
157   printf("exit in CALL11\n");
158 #endif
159   exit(___status___);
160 #endif
161 }
162
163 //#ifdef D___Vector______removeElement_____AR_L___Object____I_I
164 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
165   char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
166   memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
167 }
168 //#endif
169
170 void CALL11(___System______printI____I,int ___status___, int ___status___) {
171 #ifdef MULTICORE
172   BAMBOO_DEBUGPRINT(0x1111);
173   BAMBOO_DEBUGPRINT_REG(___status___);
174 #else
175 #ifdef DEBUG
176   printf("printI in CALL11\n");
177 #endif
178   printf("%d\n", ___status___);
179 #endif
180 }
181
182 long CALL00(___System______currentTimeMillis____) {
183 #ifdef MULTICORE
184   // not supported in MULTICORE version
185   return -1;
186 #else
187   struct timeval tv; long long retval;
188   gettimeofday(&tv, NULL);
189   retval = tv.tv_sec; /* seconds */
190   retval*=1000; /* milliseconds */
191   retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
192   return retval;
193 #endif
194 }
195
196 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
197 #ifdef MULTICORE
198 #else
199   struct ArrayObject * chararray=VAR(___s___)->___value___;
200   int i;
201   int offset=VAR(___s___)->___offset___;
202   for(i=0; i<VAR(___s___)->___count___; i++) {
203     short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
204     putchar(sc);
205   }
206 #endif
207 }
208
209 /* Object allocation function */
210
211 #ifdef MULTICORE_GC
212 void * allocate_new(void * ptr, int type) {
213   struct ___Object___ * v=(struct ___Object___ *)FREEMALLOC((struct garbagelist *) ptr, classsize[type]);
214 #ifdef DEBUG
215   printf("(%x,%x): new object: %x \n", udn_tile_coord_x(),
216          udn_tile_coord_y(), v);
217 #endif
218   v->type=type;
219   v->version = 0;
220   v->lock = NULL;
221   v->lockcount = 0;
222   initlock(v);
223 #ifdef GC_PROFILE_S
224   extern unsigned int gc_num_obj;
225   gc_num_obj++;
226 #endif
227   return v;
228 }
229
230 /* Array allocation function */
231
232 struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
233   struct ArrayObject * v=(struct ArrayObject *)FREEMALLOC((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
234 #ifdef DEBUG
235   printf("(%x,%x): new array object: %x \n", udn_tile_coord_x(),
236          udn_tile_coord_y(), v);
237 #endif
238   v->type=type;
239   v->version = 0;
240   v->lock = NULL;
241   if (length<0) {
242     return NULL;
243   }
244   v->___length___=length;
245   initlock(v);
246 #ifdef GC_PROFILE_S
247   extern unsigned int gc_num_obj;
248   gc_num_obj++;
249 #endif
250   return v;
251 }
252
253 #else
254 void * allocate_new(int type) {
255   struct ___Object___ * v=FREEMALLOC(classsize[type]);
256   v->type=type;
257   v->version = 0;
258   //v->numlocks = 0;
259   v->lock = NULL;
260   initlock(v);
261   return v;
262 }
263
264 /* Array allocation function */
265
266 struct ArrayObject * allocate_newarray(int type, int length) {
267   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
268   v->type=type;
269   v->version = 0;
270   //v->numlocks = 0;
271   v->lock = NULL;
272   v->___length___=length;
273   initlock(v);
274   return v;
275 }
276 #endif
277
278
279 /* Converts C character arrays into Java strings */
280 #ifdef MULTICORE_GC
281 struct ___String___ * NewString(void * ptr, const char *str,int length) {
282 #else
283 struct ___String___ * NewString(const char *str,int length) {
284 #endif
285   int i;
286 #ifdef MULTICORE_GC
287   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
288   int ptrarray[]={1, (int) ptr, (int) chararray};
289   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
290   chararray=(struct ArrayObject *) ptrarray[2];
291 #else
292   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
293   struct ___String___ * strobj=allocate_new(STRINGTYPE);
294 #endif
295   strobj->___value___=chararray;
296   strobj->___count___=length;
297   strobj->___offset___=0;
298
299   for(i=0; i<length; i++) {
300     ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
301   }
302   return strobj;
303 }
304
305 /* Generated code calls this if we fail a bounds check */
306
307 void failedboundschk() {
308 #ifndef TASK
309   printf("Array out of bounds\n");
310 #ifdef THREADS
311   threadexit();
312 #else
313   exit(-1);
314 #endif
315 #else
316 #ifndef MULTICORE
317   printf("Array out of bounds\n");
318   longjmp(error_handler,2);
319 #endif
320 #endif
321 }
322
323 /* Abort task call */
324 void abort_task() {
325 #ifdef TASK
326 #ifndef MULTICORE
327   printf("Aborting\n");
328   longjmp(error_handler,4);
329 #endif
330 #else
331   printf("Aborting\n");
332   exit(-1);
333 #endif
334 }