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