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