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