fix optional arguments...lots of changes
[IRC.git] / Robust / src / Runtime / runtime.c
1 #include "runtime.h"
2 #include "structdefs.h"
3 #include <signal.h>
4 #include "mem.h"
5 #include<fcntl.h>
6 #include<errno.h>
7 #include<signal.h>
8 #include<stdio.h>
9 #include "option.h"
10 #ifdef DSTM
11 #include "dstm.h"
12 #endif
13
14 extern int classsize[];
15 jmp_buf error_handler;
16 int instructioncount;
17
18 char *options;
19 int injectfailures=0;
20 float failurechance=0;
21 int debugtask=0;
22 int injectinstructionfailures;
23 int failurecount;
24 float instfailurechance=0;
25 int numfailures;
26 int instaccum=0;
27 #ifdef DMALLOC
28 #include "dmalloc.h"
29 #endif
30
31 void exithandler(int sig, siginfo_t *info, void * uap) {
32   exit(0);
33 }
34
35 void initializeexithandler() {
36   struct sigaction sig;
37   sig.sa_sigaction=&exithandler;
38   sig.sa_flags=SA_SIGINFO;
39   sigemptyset(&sig.sa_mask);
40   sigaction(SIGUSR2, &sig, 0);
41 }
42
43
44 /* This function inject failures */
45
46 void injectinstructionfailure() {
47 #ifdef TASK
48   if (injectinstructionfailures) {
49     if (numfailures==0)
50       return;
51     instructioncount=failurecount;    
52     instaccum+=failurecount;
53     if ((((double)random())/RAND_MAX)<instfailurechance) {
54       if (numfailures>0)
55         numfailures--;
56       printf("FAILURE!!! %d\n",numfailures);
57       longjmp(error_handler,11);
58     }
59   }
60 #else
61 #ifdef THREADS
62   if (injectinstructionfailures) {
63     if (numfailures==0)
64       return;
65     instaccum+=failurecount;
66     if ((((double)random())/RAND_MAX)<instfailurechance) {
67       if (numfailures>0)
68         numfailures--;
69       printf("FAILURE!!! %d\n",numfailures);
70       threadexit();
71     }
72   }
73 #endif
74 #endif
75 }
76
77 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
78     struct ArrayObject * chararray=VAR(___s___)->___value___;
79     int i;
80     int offset=VAR(___s___)->___offset___;
81     for(i=0;i<VAR(___s___)->___count___;i++) {
82         short sc=((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
83         putchar(sc);
84     }
85 }
86
87 /* Object allocation function */
88
89 #ifdef DSTM
90 void * allocate_newglobal(transrecord_t *trans, int type) {
91   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(trans, classsize[type]);
92   v->type=type;
93 #ifdef THREADS
94   v->tid=0;
95   v->lockentry=0;
96   v->lockcount=0;
97 #endif
98   return v;
99 }
100
101 /* Array allocation function */
102
103 struct ArrayObject * allocate_newarrayglobal(transrecord_t *trans, int type, int length) {
104   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(trans, sizeof(struct ArrayObject)+length*classsize[type]);
105   if (length<0) {
106     printf("ERROR: negative array\n");
107     return NULL;
108   }
109   v->type=type;
110   v->___length___=length;
111 #ifdef THREADS
112   v->tid=0;
113   v->lockentry=0;
114   v->lockcount=0;
115 #endif
116   return v;
117 }
118 #endif
119
120
121 #ifdef PRECISE_GC
122 void * allocate_new(void * ptr, int type) {
123   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
124   v->type=type;
125 #ifdef THREADS
126   v->tid=0;
127   v->lockentry=0;
128   v->lockcount=0;
129 #endif
130 #ifdef OPTIONAL
131   v->fses=0;
132 #endif
133   return v;
134 }
135
136 /* Array allocation function */
137
138 struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
139   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
140   v->type=type;
141   if (length<0) {
142     printf("ERROR: negative array\n");
143     return NULL;
144   }
145   v->___length___=length;
146 #ifdef THREADS
147   v->tid=0;
148   v->lockentry=0;
149   v->lockcount=0;
150 #endif
151 #ifdef OPTIONAL
152   v->fses=0;
153 #endif
154   return v;
155 }
156
157 #else
158 void * allocate_new(int type) {
159   struct ___Object___ * v=FREEMALLOC(classsize[type]);
160   v->type=type;
161 #ifdef OPTIONAL
162   v->fses=0;
163 #endif
164   return v;
165 }
166
167 /* Array allocation function */
168
169 struct ArrayObject * allocate_newarray(int type, int length) {
170   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
171   v->type=type;
172   v->___length___=length;
173 #ifdef OPTIONAL
174   v->fses=0;
175 #endif
176   return v;
177 }
178 #endif
179
180
181 /* Converts C character arrays into Java strings */
182 #ifdef PRECISE_GC
183 struct ___String___ * NewString(void * ptr, const char *str,int length) {
184 #else
185 struct ___String___ * NewString(const char *str,int length) {
186 #endif
187   int i;
188 #ifdef PRECISE_GC
189   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
190   int ptrarray[]={1, (int) ptr, (int) chararray};
191   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
192   chararray=(struct ArrayObject *) ptrarray[2];
193 #else
194   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
195   struct ___String___ * strobj=allocate_new(STRINGTYPE);
196 #endif
197   strobj->___value___=chararray;
198   strobj->___count___=length;
199   strobj->___offset___=0;
200
201   for(i=0;i<length;i++) {
202     ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
203   return strobj;
204 }
205
206 /* Generated code calls this if we fail a bounds check */
207
208 void failedboundschk() {
209 #ifndef TASK
210   printf("Array out of bounds\n");
211 #ifdef THREADS
212   threadexit();
213 #else
214   exit(-1);
215 #endif
216 #else
217   longjmp(error_handler,2);
218 #endif
219 }
220
221 /* Abort task call */
222 void abort_task() {
223 #ifdef TASK
224   longjmp(error_handler,4);
225 #else
226   printf("Aborting\n");
227   exit(-1);
228 #endif
229 }