did something I shouldnt have, mixed changes for two different projects at once,...
[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 <stdio.h>
8 #include "option.h"
9 #include "methodheaders.h"
10 #ifdef DSTM
11 #ifdef RECOVERY
12 #include "DSTM/interface_recovery/dstm.h"
13 #include "DSTM/interface_recovery/altprelookup.h"
14
15 #ifdef RECOVERYSTATS
16   extern int numRecovery;
17   extern unsigned int deadMachine[8];
18   extern unsigned int sizeOfRedupedData[8];
19   extern double elapsedTime[8];
20 #endif
21   
22 #else
23 #include "DSTM/interface/dstm.h"
24 #include "DSTM/interface/altprelookup.h"
25 #include "DSTM/interface/prefetch.h"
26 #endif
27 #endif
28 #ifdef STM
29 #include "tm.h"
30 #include <pthread.h>
31 #endif
32 #ifdef STMLOG
33 #define ARRAY_LENGTH 700003
34 __thread int counter;
35 __thread int event[ARRAY_LENGTH];
36 __thread unsigned long long clkticks[ARRAY_LENGTH];
37 unsigned long long beginClock=0;
38 #define FILENAME  "log"
39 #endif
40 #ifdef EVENTMONITOR
41 #include "monitor.h"
42 __thread int objcount=0;
43 #define ASSIGNUID(x) {                                  \
44     int number=((objcount++)<<EVTHREADSHIFT)|threadnum; \
45     x->objuid=number;                                   \
46   }
47 #else
48 #define ASSIGNUID(x)
49 #endif
50
51 #if defined(THREADS)||defined(STM)
52 /* Global barrier for STM */
53 pthread_barrier_t barrier;
54 pthread_barrierattr_t attr;
55 #endif
56
57 #include <string.h>
58
59 extern int classsize[];
60 extern int typearray[];
61 extern int typearray2[];
62 jmp_buf error_handler;
63 int instructioncount;
64
65 char *options;
66 int injectfailures=0;
67 float failurechance=0;
68 int errors=0;
69 int debugtask=0;
70 int injectinstructionfailures;
71 int failurecount;
72 float instfailurechance=0;
73 int numfailures;
74 int instaccum=0;
75 typedef unsigned long long ticks;
76 #ifdef DMALLOC
77 #include "dmalloc.h"
78 #endif
79
80 int instanceof(struct ___Object___ *ptr, int type) {
81   int i=ptr->type;
82   do {
83     if (i==type)
84       return 1;
85     i=typearray[i];
86   } while(i!=-1);
87   i=ptr->type;
88   if (i>NUMCLASSES) {
89     do {
90       if (i==type)
91         return 1;
92       i=typearray2[i-NUMCLASSES];
93     } while(i!=-1);
94   }
95   return 0;
96 }
97
98 void exithandler(int sig, siginfo_t *info, void * uap) {
99   exit(0);
100 }
101
102 void initializeexithandler() {
103   struct sigaction sig;
104   sig.sa_sigaction=&exithandler;
105   sig.sa_flags=SA_SIGINFO;
106   sigemptyset(&sig.sa_mask);
107   sigaction(SIGUSR2, &sig, 0);
108 }
109
110
111 /* This function inject failures */
112
113 void injectinstructionfailure() {
114 #ifdef TASK
115   if (injectinstructionfailures) {
116     if (numfailures==0)
117       return;
118     instructioncount=failurecount;
119     instaccum+=failurecount;
120     if ((((double)random())/RAND_MAX)<instfailurechance) {
121       if (numfailures>0)
122         numfailures--;
123       printf("FAILURE!!! %d\n",numfailures);
124       longjmp(error_handler,11);
125     }
126   }
127 #else
128 #ifdef THREADS
129   if (injectinstructionfailures) {
130     if (numfailures==0)
131       return;
132     instaccum+=failurecount;
133     if ((((double)random())/RAND_MAX)<instfailurechance) {
134       if (numfailures>0)
135         numfailures--;
136       printf("FAILURE!!! %d\n",numfailures);
137       threadexit();
138     }
139   }
140 #endif
141 #endif
142 }
143
144 #ifdef D___Double______nativeparsedouble____L___String___
145 double CALL01(___Double______nativeparsedouble____L___String___,struct ___String___ * ___str___) {
146   int length=VAR(___str___)->___count___;
147   int maxlength=(length>60)?60:length;
148   char str[maxlength+1];
149   struct ArrayObject * chararray=VAR(___str___)->___value___;
150   int i;
151   int offset=VAR(___str___)->___offset___;
152   for(i=0; i<maxlength; i++) {
153     str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
154   }
155   str[i]=0;
156   double d=atof(str);
157   return d;
158 }
159 #endif
160
161 #ifdef D___Double______nativeparsedouble_____AR_B_I_I 
162 double CALL23(___Double______nativeparsedouble_____AR_B_I_I, int start, int length,int start,int length,struct ArrayObject * ___str___) {
163   int maxlength=(length>60)?60:length;
164   char str[maxlength+1];
165   struct ArrayObject * bytearray=VAR(___str___);
166   int i;
167   for(i=0; i<maxlength; i++) {
168     str[i]=(((char *)&bytearray->___length___)+sizeof(int))[i+start];
169   }
170   str[i]=0;
171   double d=atof(str);
172   return d;
173 }
174 #endif
175
176 #ifdef D___String______convertdoubletochar____D__AR_C
177 int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
178   int length=VAR(___chararray___)->___length___;
179   char str[length];
180   int i;
181   int num=snprintf(str, length, "%f",___val___);
182   if (num>=length)
183     num=length-1;
184   for(i=0; i<length; i++) {
185     ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
186   }
187   return num;
188 }
189 #endif
190 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
191 void deepArrayCopy(struct ___Object___ * dst, struct ___Object___ * src) {
192   int dsttype=((int *)dst)[0];
193   int srctype=((int *)src)[0];
194 #ifdef STMARRAY
195   src=src->___objlocation___;
196 #endif
197   if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
198     return;
199   struct ArrayObject *aodst=(struct ArrayObject *)dst;
200   struct ArrayObject *aosrc=(struct ArrayObject *)src;
201   int dstlength=aodst->___length___;
202   int srclength=aosrc->___length___;
203   if (dstlength!=srclength)
204     return;
205   unsigned INTPTR *pointer=pointerarray[srctype];
206   if (pointer==0) {
207     int elementsize=classsize[srctype];
208     int size=srclength*elementsize;
209     //primitives
210     memcpy(((char *)&aodst->___length___)+sizeof(int) , ((char *)&aosrc->___length___)+sizeof(int), size);
211   } else {
212     //objects
213     int i;
214     for(i=0;i<srclength;i++) {
215       struct ___Object___ * ptr=((struct ___Object___**)(((char*) &aosrc->___length___)+sizeof(int)))[i];
216       int ptrtype=((int *)ptr)[0];
217       if (ptrtype>=NUMCLASSES) {
218         struct ___Object___ * dstptr=((struct ___Object___**)(((char*) &aodst->___length___)+sizeof(int)))[i];
219         deepArrayCopy(dstptr,ptr);
220       } else {
221         //hit an object
222         ((struct ___Object___ **)(((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
223       }
224     }
225   }
226 }
227
228 void CALL02(___System______deepArrayCopy____L___Object____L___Object___, struct ___Object___ * ___dst___, struct ___Object___ * ___src___) {
229   deepArrayCopy(VAR(___dst___), VAR(___src___));
230 }
231 #endif
232
233 #ifdef D___System______arraycopy____L___Object____I_L___Object____I_I
234 void arraycopy(struct ___Object___ *src, int srcPos, struct ___Object___ *dst, int destPos, int length) {
235   int dsttype=((int *)dst)[0];
236   int srctype=((int *)src)[0];
237
238   //not an array or type mismatch
239   if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
240     return;
241
242   struct ArrayObject *aodst=(struct ArrayObject *)dst;
243   struct ArrayObject *aosrc=(struct ArrayObject *)src;
244   int dstlength=aodst->___length___;
245   int srclength=aosrc->___length___;
246
247   if (length<=0)
248     return;
249   if (srcPos+length>srclength)
250     return;
251   if (destPos+length>dstlength)
252     return;
253
254   unsigned INTPTR *pointer=pointerarray[srctype];
255   if (pointer==0) {
256     int elementsize=classsize[srctype];
257     int size=length*elementsize;
258     //primitives
259     memcpy(((char *)&aodst->___length___)+sizeof(int)+destPos*elementsize, ((char *)&aosrc->___length___)+sizeof(int)+srcPos*elementsize, size);
260   } else {
261     //objects
262     int i;
263     for(i=0;i<length;i++) {
264       struct ___Object___ * ptr=((struct ___Object___**)(((char*) &aosrc->___length___)+sizeof(int)))[i+srcPos];
265       int ptrtype=((int *)ptr)[0];
266       //hit an object
267       ((struct ___Object___ **)(((char*) &aodst->___length___)+sizeof(int)))[i+destPos]=ptr;
268     }
269   }
270 }
271
272 void CALL35(___System______arraycopy____L___Object____I_L___Object____I_I, int ___srcPos___, int ___destPos___, int ___length___, struct ___Object___ * ___src___, int ___srcPos___, struct ___Object___ * ___dst___, int  ___destPos___, int ___length___) {
273   arraycopy(VAR(___src___), ___srcPos___, VAR(___dst___), ___destPos___, ___length___);
274 }
275 #endif
276
277 void CALL11(___System______exit____I,int ___status___, int ___status___) {
278 #ifdef TRANSSTATS
279 #ifndef RECOVERY
280   printf("numTransCommit = %d\n", numTransCommit);
281   printf("numTransAbort = %d\n", numTransAbort);
282   printf("nSoftAbort = %d\n", nSoftAbort);
283 #endif
284 #ifdef STM
285   printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
286   printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
287 #ifdef STMSTATS
288   int i;
289   for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
290     printf("typesCausingAbort[%2d] numaccess= %5d numabort= %3d\n", i, typesCausingAbort[i].numaccess, typesCausingAbort[i].numabort);
291   }
292 #endif
293 #endif
294 #endif
295 #ifdef EVENTMONITOR
296   dumpdata();
297 #endif
298   exit(___status___);
299 }
300
301 void CALL11(___System______logevent____I,int ___event___, int ___event___) {
302 #ifdef STMLOG
303   event[counter] = ___event___;
304   clkticks[counter] = rdtsc();
305   counter++;
306 #endif
307   return;
308 }
309
310 void CALL00(___System______logevent____) {
311 #ifdef STMLOG
312   beginClock= rdtsc();
313 #endif
314   return;
315 }
316
317 void CALL11(___System______flushToFile____I, int ___threadid___, int ___threadid___) {
318 #ifdef STMLOG
319   FILE *fp;
320   /* Flush to file */
321   char filename[20];
322   memset(filename, 0, 20);
323   sprintf(filename, "%s_%d", FILENAME, ___threadid___);
324   if ((fp = fopen(filename, "w+")) == NULL) {
325     perror("fopen");
326     return;
327   }
328   int i;
329   for (i = 0; i < counter-1; i++) {
330     fprintf(fp, "%d %lld %lld\n", event[i], clkticks[i]-beginClock, clkticks[i+1]-beginClock);
331   }
332   fprintf(fp, "%d %lld\n", event[i], clkticks[i]-beginClock);
333
334   fclose(fp);
335 #endif
336   return;
337 }
338
339 void CALL00(___System______initLog____) {
340 #ifdef STMLOG
341   counter=0;
342   int i;
343   for(i=0; i<ARRAY_LENGTH; i++) {
344     event[i] = 0;
345     clkticks[i] = 0;
346   }
347
348 #endif
349   return;
350 }
351
352 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
353 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
354   char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
355   memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
356 }
357 #endif
358
359 void CALL11(___System______printI____I,int ___status___, int ___status___) {
360   printf("%d\n",___status___);
361 }
362
363 long long CALL00(___System______currentTimeMillis____) {
364   struct timeval tv; long long retval;
365   gettimeofday(&tv, NULL);
366   retval = tv.tv_sec; /* seconds */
367   retval*=1000; /* milliseconds */
368   retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
369   return retval;
370 }
371
372 long long CALL00(___System______microTimes____) {
373   struct timeval tv; 
374   long long retval;
375   gettimeofday(&tv, NULL);
376   retval = tv.tv_sec; /* seconds */
377   retval*=1000000; /* microsecs */
378   retval+= (tv.tv_usec); /* adjust microseconds & add them in */
379   return retval;
380 }
381
382 long long CALL00(___System______getticks____) {
383   unsigned a, d;
384   asm("cpuid");
385   asm volatile("rdtsc" : "=a" (a), "=d" (d));
386   return (((ticks)a) | (((ticks)d) << 32));
387 }
388
389 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
390   struct ArrayObject * chararray=VAR(___s___)->___value___;
391   int i;
392   int offset=VAR(___s___)->___offset___;
393   for(i=0; i<VAR(___s___)->___count___; i++) {
394     short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
395     putchar(sc);
396   }
397 #ifdef RECOVERYSTATS
398   fflush(stdout);
399   fflush(stdout);
400 #endif
401 }
402
403 #ifdef D___RecoveryStat______printRecoveryStat____ 
404 #ifdef RECOVERYSTATS
405 void CALL00(___RecoveryStat______printRecoveryStat____) {
406   printRecoveryStat();
407 }
408 #else
409 void CALL00(___RecoveryStat______printRecoveryStat____) {
410   printf("No Stat\n");
411   fflush(stdout);
412 }
413 #endif
414 #endif
415
416 #ifdef DSTM
417 void CALL00(___System______clearPrefetchCache____) {
418   prehashClear();
419 }
420
421 #ifdef RANGEPREFETCH
422 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
423   /* Manual Prefetches to be inserted */
424   //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
425   //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
426   int numoffset=VAR(___offsets___)->___length___;
427   int i;
428   short offArry[numoffset+2];
429   offArry[0] = 0;
430   offArry[1] = 0;
431   for(i = 2; i<(numoffset+2); i++) {
432     offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
433     //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
434   }
435   unsigned int oid;
436   if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
437     oid =  (unsigned int) VAR(___o___); //outside transaction therefore just an oid
438   } else { //even
439     oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
440   }
441   rangePrefetch(oid, (short)(numoffset+2), offArry);
442 }
443 #else
444 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
445   return;
446 }
447 #endif
448
449 #ifdef D___Task______execution____ 
450 extern void* virtualtable[];
451 // associated with Task.execution(). finds proper execute method and call it
452 void CALL01(___Task______execution____,struct ___Task___ * ___this___)
453 {
454   unsigned int oid;
455   oid = (unsigned int) VAR(___this___);   // object id
456   int type = getObjType(oid);             // object type
457
458 #ifdef PRECISE_GC
459   int p[] = {1,0 , oid};
460   ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(p);
461 #else
462   // call the proper execute method
463   ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(oid);
464 #endif
465 }
466 #endif
467
468 #endif // DSTM
469
470 /* STM Barrier constructs */
471 #ifdef D___Barrier______setBarrier____I
472 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
473   // Barrier initialization
474   int ret;
475   if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
476     printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
477     exit(-1);
478   }
479 }
480 #endif
481
482 #ifdef D___Barrier______enterBarrier____
483 void CALL00(___Barrier______enterBarrier____) {
484   // Synchronization point
485   int ret;
486 #ifdef EVENTMONITOR
487   EVLOGEVENT(EV_ENTERBARRIER);
488 #endif
489 #ifdef PRECISE_GC
490   stopforgc((struct garbagelist *)___params___);
491 #endif
492   ret = pthread_barrier_wait(&barrier);
493 #ifdef PRECISE_GC
494   restartaftergc();
495 #endif
496   if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
497     printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
498     exit(-1);
499   }
500 #ifdef EVENTMONITOR
501   EVLOGEVENT(EV_EXITBARRIER);
502 #endif
503 }
504 #endif
505
506 /* Object allocation function */
507
508 #ifdef DSTM
509 __attribute__((malloc)) void * allocate_newglobal(int type) {
510   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
511   v->type=type;
512   //printf("DEBUG %s(), type= %x\n", __func__, type);
513 #ifdef THREADS
514   v->tid=0;
515   v->lockentry=0;
516   v->lockcount=0;
517 #endif
518   return v;
519 }
520
521 /* Array allocation function */
522
523 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
524   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
525   if (length<0) {
526     printf("ERROR: negative array\n");
527     return NULL;
528   }
529   v->type=type;
530   v->___length___=length;
531 #ifdef THREADS
532   v->tid=0;
533   v->lockentry=0;
534   v->lockcount=0;
535 #endif
536   return v;
537 }
538 #endif
539
540
541 #ifdef STM
542 // STM Versions of allocation functions
543
544 /* Object allocation function */
545 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
546 #ifdef STMARRAY
547   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type], 0);
548 #else
549   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
550 #endif
551   ASSIGNUID(v);
552   v->type=type;
553   v->___objlocation___=v;
554   return v;
555 }
556
557 /* Array allocation function */
558 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
559 #ifdef STMARRAY
560   int basesize=length*classsize[type];
561   //round the base size up
562   basesize=(basesize+LOWMASK)&HIGHMASK;
563   int numlocks=basesize>>INDEXSHIFT;
564   int bookkeepsize=numlocks*2*sizeof(int);
565   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+basesize+bookkeepsize, bookkeepsize);
566   unsigned int *intptr=(unsigned int *)(((char *)v)-sizeof(objheader_t));
567   for(;numlocks>0;numlocks--) {
568     intptr-=2;
569     intptr[0]=1;
570   }
571   v->highindex=-1;
572   v->lowindex=MAXARRAYSIZE;
573 #else
574   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
575 #endif
576   ASSIGNUID(v);
577   if (length<0) {
578     printf("ERROR: negative array\n");
579     return NULL;
580   }
581   v->___objlocation___=(struct ___Object___*)v;
582   v->type=type;
583   v->___length___=length;
584   return v;
585 }
586
587 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
588   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
589   struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
590   ASSIGNUID(v);
591   initdsmlocks(&tmp->lock);
592   tmp->version = 1;
593   v->___objlocation___=v;
594   v->type = type;
595   return v;
596 }
597
598 /* Array allocation function */
599
600 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
601 #ifdef STMARRAY
602   int basesize=length*classsize[type];
603   //round the base size up
604   basesize=(basesize+LOWMASK)&HIGHMASK;
605   int numlocks=basesize>>INDEXSHIFT;
606   int bookkeepsize=(numlocks)*2*sizeof(int);
607   int *tmpint=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+basesize+sizeof(objheader_t)+bookkeepsize);
608   for(;numlocks>0;numlocks--) {
609     tmpint[0]=1;
610     tmpint+=2;
611   }
612   objheader_t *tmp=(objheader_t *)tmpint;
613   struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
614   v->highindex=-1;
615   v->lowindex=MAXARRAYSIZE;
616 #else
617   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
618   struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
619 #endif
620 #ifdef DUALVIEW
621   tmp->lock=RW_LOCK_BIAS;
622 #else
623   initdsmlocks(&tmp->lock);
624 #endif
625   tmp->version=1;
626   ASSIGNUID(v);
627   v->type=type;
628   if (length<0) {
629     printf("ERROR: negative array %d\n", length);
630     return NULL;
631   }
632   v->___objlocation___=(struct ___Object___ *)v;
633   v->___length___=length;
634   return v;
635 }
636 #endif
637
638 #ifndef STM
639 #if defined(PRECISE_GC)
640 #ifdef MLP
641 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
642   return allocate_new_mlp(ptr, type, 0, 0);
643 }
644 __attribute__((malloc)) void * allocate_new_mlp(void * ptr, int type, int oid, int allocsite) {
645 #else
646 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
647 #endif
648   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
649   v->type=type;
650 #ifdef THREADS
651   v->tid=0;
652   v->lockentry=0;
653   v->lockcount=0;
654 #endif
655 #ifdef OPTIONAL
656   v->fses=0;
657 #endif
658 #ifdef MLP
659   v->oid=oid;
660   v->allocsite=allocsite;
661 #endif
662   return v;
663 }
664
665 /* Array allocation function */
666 #ifdef MLP
667 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
668   return allocate_newarray_mlp(ptr, type, length, 0, 0);
669 }
670  __attribute__((malloc)) struct ArrayObject * allocate_newarray_mlp(void * ptr, int type, int length, int oid, int allocsite) {
671 #else
672 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
673 #endif
674   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
675   v->type=type;
676   if (length<0) {
677     printf("ERROR: negative array\n");
678     return NULL;
679   }
680   v->___length___=length;
681 #ifdef THREADS
682   v->tid=0;
683   v->lockentry=0;
684   v->lockcount=0;
685 #endif
686 #ifdef OPTIONAL
687   v->fses=0;
688 #endif
689 #ifdef MLP
690   v->oid=oid;
691   v->allocsite=allocsite;
692 #endif
693   return v;
694 }
695
696 #else
697 __attribute__((malloc)) void * allocate_new(int type) {
698   struct ___Object___ * v=FREEMALLOC(classsize[type]);
699   v->type=type;
700 #ifdef OPTIONAL
701   v->fses=0;
702 #endif
703   return v;
704 }
705
706 /* Array allocation function */
707
708 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
709   __attribute__((malloc))  struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
710   v->type=type;
711   v->___length___=length;
712 #ifdef OPTIONAL
713   v->fses=0;
714 #endif
715   return v;
716 }
717 #endif
718 #endif
719
720 /* Converts C character arrays into Java strings */
721 #ifdef PRECISE_GC
722 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
723 #else
724 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
725 #endif
726   int i;
727 #ifdef PRECISE_GC
728   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
729   INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
730   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
731   chararray=(struct ArrayObject *) ptrarray[2];
732 #else
733   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
734   struct ___String___ * strobj=allocate_new(STRINGTYPE);
735 #endif
736   strobj->___value___=chararray;
737   strobj->___count___=length;
738   strobj->___offset___=0;
739
740   for(i=0; i<length; i++) {
741     ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
742   }
743   return strobj;
744 }
745
746 /* Generated code calls this if we fail a bounds check */
747
748 void failedboundschk() {
749 #ifndef TASK
750   printf("Array out of bounds\n");
751 #ifdef THREADS
752   threadexit();
753 #else
754   exit(-1);
755 #endif
756 #else
757   longjmp(error_handler,2);
758 #endif
759 }
760
761 /* Abort task call */
762 void abort_task() {
763 #ifdef TASK
764   longjmp(error_handler,4);
765 #else
766   printf("Aborting\n");
767   exit(-1);
768 #endif
769 }
770
771 #ifndef SANDBOX
772 #ifdef D___System______Assert____Z
773  void CALL11(___System______Assert____Z, int ___status___, int ___status___) {
774    if (!___status___) {
775      printf("Assertion violation\n");
776      *((int *)(NULL)); //force stack trace error
777    }
778  }
779 #endif
780 #endif