2836a4e39e5f993345acca59955907caeff34976
[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 void CALL11(___System______exit____I,int ___status___, int ___status___) {
97 #ifdef MULTICORE
98   BAMBOO_EXIT(___status___);
99 #else
100 #ifdef DEBUG
101   printf("exit in CALL11\n");
102 #endif
103   exit(___status___);
104 #endif
105 }
106
107 void CALL11(___System______printI____I,int ___status___, int ___status___) {
108 #ifdef MULTICORE
109   BAMBOO_DEBUGPRINT(0x1111);
110   BAMBOO_DEBUGPRINT_REG(___status___);
111 #else
112 #ifdef DEBUG
113   printf("printI in CALL11\n");
114 #endif
115   printf("%d\n", ___status___);
116 #endif
117 }
118
119 long CALL00(___System______currentTimeMillis____) {
120 #ifdef MULTICORE
121   // not supported in MULTICORE version
122   return -1;
123 #else
124   struct timeval tv; long long retval;
125   gettimeofday(&tv, NULL);
126   retval = tv.tv_sec; /* seconds */
127   retval*=1000; /* milliseconds */
128   retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
129   return retval;
130 #endif
131 }
132
133 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
134 #ifdef MULTICORE
135 #else
136   struct ArrayObject * chararray=VAR(___s___)->___value___;
137   int i;
138   int offset=VAR(___s___)->___offset___;
139   for(i=0; i<VAR(___s___)->___count___; i++) {
140     short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
141     putchar(sc);
142   }
143 #endif
144 }
145
146 /* Object allocation function */
147
148 #ifdef PRECISE_GC
149 void * allocate_new(void * ptr, int type) {
150   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
151   v->type=type;
152   v->isolate = 1;
153   v->version = 0;
154   //v->numlocks = 0;
155   v->lock = NULL;
156 #ifdef THREADS
157   v->tid=0;
158   v->lockentry=0;
159   v->lockcount=0;
160 #endif
161   return v;
162 }
163
164 /* Array allocation function */
165
166 struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
167   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
168   v->type=type;
169   v->isolate = 1;
170   v->version = 0;
171   //v->numlocks = 0;
172   v->lock = NULL;
173   if (length<0) {
174 #ifndef MULTICORE
175     printf("ERROR: negative array\n");
176 #endif
177     return NULL;
178   }
179   v->___length___=length;
180 #ifdef THREADS
181   v->tid=0;
182   v->lockentry=0;
183   v->lockcount=0;
184 #endif
185   return v;
186 }
187
188 #else
189 void * allocate_new(int type) {
190   struct ___Object___ * v=FREEMALLOC(classsize[type]);
191   v->type=type;
192   v->isolate = 1;
193   v->version = 0;
194   //v->numlocks = 0;
195   v->lock = NULL;
196   return v;
197 }
198
199 /* Array allocation function */
200
201 struct ArrayObject * allocate_newarray(int type, int length) {
202   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
203   v->type=type;
204   v->isolate = 1;
205   v->version = 0;
206   //v->numlocks = 0;
207   v->lock = NULL;
208   v->___length___=length;
209   return v;
210 }
211 #endif
212
213
214 /* Converts C character arrays into Java strings */
215 #ifdef PRECISE_GC
216 struct ___String___ * NewString(void * ptr, const char *str,int length) {
217 #else
218 struct ___String___ * NewString(const char *str,int length) {
219 #endif
220   int i;
221 #ifdef PRECISE_GC
222   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
223   int ptrarray[]={1, (int) ptr, (int) chararray};
224   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
225   chararray=(struct ArrayObject *) ptrarray[2];
226 #else
227   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
228   struct ___String___ * strobj=allocate_new(STRINGTYPE);
229 #endif
230   strobj->___value___=chararray;
231   strobj->___count___=length;
232   strobj->___offset___=0;
233
234   for(i=0; i<length; i++) {
235     ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
236   }
237   return strobj;
238 }
239
240 /* Generated code calls this if we fail a bounds check */
241
242 void failedboundschk() {
243 #ifndef TASK
244   printf("Array out of bounds\n");
245 #ifdef THREADS
246   threadexit();
247 #else
248   exit(-1);
249 #endif
250 #else
251 #ifndef MULTICORE
252   printf("Array out of bounds\n");
253   longjmp(error_handler,2);
254 #endif
255 #endif
256 }
257
258 /* Abort task call */
259 void abort_task() {
260 #ifdef TASK
261 #ifndef MULTICORE
262   printf("Aborting\n");
263   longjmp(error_handler,4);
264 #endif
265 #else
266   printf("Aborting\n");
267   exit(-1);
268 #endif
269 }