printrecoverystat in work.java
[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/prelookup.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 void CALL11(___System______exit____I,int ___status___, int ___status___) {
234 #ifdef TRANSSTATS
235   printf("numTransCommit = %d\n", numTransCommit);
236   printf("numTransAbort = %d\n", numTransAbort);
237   printf("nSoftAbort = %d\n", nSoftAbort);
238 #ifdef STM
239   printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
240   printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
241 #ifdef STMSTATS
242   int i;
243   for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
244     printf("typesCausingAbort[%2d] numaccess= %5d numabort= %3d\n", i, typesCausingAbort[i].numaccess, typesCausingAbort[i].numabort);
245   }
246 #endif
247 #endif
248 #endif
249 #ifdef EVENTMONITOR
250   dumpdata();
251 #endif
252   exit(___status___);
253 }
254
255 void CALL11(___System______logevent____I,int ___event___, int ___event___) {
256 #ifdef STMLOG
257   event[counter] = ___event___;
258   clkticks[counter] = rdtsc();
259   counter++;
260 #endif
261   return;
262 }
263
264 void CALL00(___System______logevent____) {
265 #ifdef STMLOG
266   beginClock= rdtsc();
267 #endif
268   return;
269 }
270
271 void CALL11(___System______flushToFile____I, int ___threadid___, int ___threadid___) {
272 #ifdef STMLOG
273   FILE *fp;
274   /* Flush to file */
275   char filename[20];
276   memset(filename, 0, 20);
277   sprintf(filename, "%s_%d", FILENAME, ___threadid___);
278   if ((fp = fopen(filename, "w+")) == NULL) {
279     perror("fopen");
280     return;
281   }
282   int i;
283   for (i = 0; i < counter-1; i++) {
284     fprintf(fp, "%d %lld %lld\n", event[i], clkticks[i]-beginClock, clkticks[i+1]-beginClock);
285   }
286   fprintf(fp, "%d %lld\n", event[i], clkticks[i]-beginClock);
287
288   fclose(fp);
289 #endif
290   return;
291 }
292
293 void CALL00(___System______initLog____) {
294 #ifdef STMLOG
295   counter=0;
296   int i;
297   for(i=0; i<ARRAY_LENGTH; i++) {
298     event[i] = 0;
299     clkticks[i] = 0;
300   }
301
302 #endif
303   return;
304 }
305
306 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
307 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
308   char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
309   memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
310 }
311 #endif
312
313 void CALL11(___System______printI____I,int ___status___, int ___status___) {
314   printf("%d\n",___status___);
315 }
316
317 long long CALL00(___System______currentTimeMillis____) {
318   struct timeval tv; long long retval;
319   gettimeofday(&tv, NULL);
320   retval = tv.tv_sec; /* seconds */
321   retval*=1000; /* milliseconds */
322   retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
323   return retval;
324 }
325
326 long long CALL00(___System______microTimes____) {
327   struct timeval tv; 
328   long long retval;
329   gettimeofday(&tv, NULL);
330   retval = tv.tv_sec; /* seconds */
331   retval*=1000000; /* microsecs */
332   retval+= (tv.tv_usec); /* adjust microseconds & add them in */
333   return retval;
334 }
335
336 long long CALL00(___System______getticks____) {
337   unsigned a, d;
338   asm("cpuid");
339   asm volatile("rdtsc" : "=a" (a), "=d" (d));
340   return (((ticks)a) | (((ticks)d) << 32));
341 }
342
343 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
344   struct ArrayObject * chararray=VAR(___s___)->___value___;
345   int i;
346   int offset=VAR(___s___)->___offset___;
347   for(i=0; i<VAR(___s___)->___count___; i++) {
348     short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
349     putchar(sc);
350   }
351
352 #ifdef RECOVERYSTATS
353   fflush(stdout);
354 #endif
355 }
356
357 #ifdef D___Work______printRecoveryStat____ 
358 #ifdef RECOVERYSTATS
359 void CALL00(___Work______printRecoveryStat____) {
360
361   printf("***** Recovery Stats *****\n");
362   printf("numRecovery = %d\n",numRecovery);
363   int i;
364   for(i=0; i < numRecovery;i++) {
365     printf("Dead Machine = %s\n",midtoIPString(deadMachine[i]));
366     printf("Recovery Time = %.2f\n",elapsedTime[i]);
367   }
368   printf("**************************\n\n");
369 }
370 #else
371 void CALL00(___Work______printRecoveryStat____) {
372   printf("No Stat\n");
373 }
374 #endif
375 #endif
376
377
378
379 #ifdef DSTM
380 void CALL00(___System______clearPrefetchCache____) {
381   prehashClear();
382 }
383
384 #ifdef RANGEPREFETCH
385 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
386   /* Manual Prefetches to be inserted */
387   //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
388   //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
389   int numoffset=VAR(___offsets___)->___length___;
390   int i;
391   short offArry[numoffset+2];
392   offArry[0] = 0;
393   offArry[1] = 0;
394   for(i = 2; i<(numoffset+2); i++) {
395     offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
396     //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
397   }
398   unsigned int oid;
399   if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
400     oid =  (unsigned int) VAR(___o___); //outside transaction therefore just an oid
401   } else { //even
402     oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
403   }
404   rangePrefetch(oid, (short)(numoffset+2), offArry);
405 }
406 #else
407 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
408   return;
409 }
410 #endif
411
412 #ifdef D___Task______execution____ 
413 extern void* virtualtable[];
414 // associated with Task.execution(). finds proper execute method and call it
415 void CALL01(___Task______execution____,struct ___Task___ * ___this___)
416 {
417   unsigned int oid;
418   oid = (unsigned int) VAR(___this___);   // object id
419   int type = getObjType(oid);             // object type
420
421 #ifdef PRECISE_GC
422   int p[] = {1,0 , oid};
423    
424   ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(p);
425 #else
426   // call the proper execute method
427   ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(oid);
428 #endif
429 }
430 #endif
431
432 #endif // DSTM
433
434 /* STM Barrier constructs */
435 #ifdef D___Barrier______setBarrier____I
436 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
437   // Barrier initialization
438   int ret;
439   if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
440     printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
441     exit(-1);
442   }
443 }
444 #endif
445
446 #ifdef D___Barrier______enterBarrier____
447 void CALL00(___Barrier______enterBarrier____) {
448   // Synchronization point
449   int ret;
450 #ifdef EVENTMONITOR
451   EVLOGEVENT(EV_ENTERBARRIER);
452 #endif
453 #ifdef PRECISE_GC
454   stopforgc((struct garbagelist *)___params___);
455 #endif
456   ret = pthread_barrier_wait(&barrier);
457 #ifdef PRECISE_GC
458   restartaftergc();
459 #endif
460   if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
461     printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
462     exit(-1);
463   }
464 #ifdef EVENTMONITOR
465   EVLOGEVENT(EV_EXITBARRIER);
466 #endif
467 }
468 #endif
469
470 /* Object allocation function */
471
472 #ifdef DSTM
473 __attribute__((malloc)) void * allocate_newglobal(int type) {
474   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
475   v->type=type;
476   //printf("DEBUG %s(), type= %x\n", __func__, type);
477 #ifdef THREADS
478   v->tid=0;
479   v->lockentry=0;
480   v->lockcount=0;
481 #endif
482   return v;
483 }
484
485 /* Array allocation function */
486
487 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
488   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
489   if (length<0) {
490     printf("ERROR: negative array\n");
491     return NULL;
492   }
493   v->type=type;
494   v->___length___=length;
495 #ifdef THREADS
496   v->tid=0;
497   v->lockentry=0;
498   v->lockcount=0;
499 #endif
500   return v;
501 }
502 #endif
503
504
505 #ifdef STM
506 // STM Versions of allocation functions
507
508 /* Object allocation function */
509 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
510 #ifdef STMARRAY
511   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type], 0);
512 #else
513   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
514 #endif
515   ASSIGNUID(v);
516   v->type=type;
517   v->___objlocation___=v;
518   return v;
519 }
520
521 /* Array allocation function */
522 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
523 #ifdef STMARRAY
524   int basesize=length*classsize[type];
525   //round the base size up
526   basesize=(basesize+LOWMASK)&HIGHMASK;
527   int numlocks=basesize>>INDEXSHIFT;
528   int bookkeepsize=numlocks*2*sizeof(int);
529   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+basesize+bookkeepsize, bookkeepsize);
530   unsigned int *intptr=(unsigned int *)(((char *)v)-sizeof(objheader_t));
531   for(;numlocks>0;numlocks--) {
532     intptr-=2;
533     intptr[0]=1;
534   }
535   v->highindex=-1;
536   v->lowindex=MAXARRAYSIZE;
537 #else
538   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
539 #endif
540   ASSIGNUID(v);
541   if (length<0) {
542     printf("ERROR: negative array\n");
543     return NULL;
544   }
545   v->___objlocation___=(struct ___Object___*)v;
546   v->type=type;
547   v->___length___=length;
548   return v;
549 }
550
551 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
552   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
553   struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
554   ASSIGNUID(v);
555   initdsmlocks(&tmp->lock);
556   tmp->version = 1;
557   v->___objlocation___=v;
558   v->type = type;
559   return v;
560 }
561
562 /* Array allocation function */
563
564 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
565 #ifdef STMARRAY
566   int basesize=length*classsize[type];
567   //round the base size up
568   basesize=(basesize+LOWMASK)&HIGHMASK;
569   int numlocks=basesize>>INDEXSHIFT;
570   int bookkeepsize=(numlocks)*2*sizeof(int);
571   int *tmpint=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+basesize+sizeof(objheader_t)+bookkeepsize);
572   for(;numlocks>0;numlocks--) {
573     tmpint[0]=1;
574     tmpint+=2;
575   }
576   objheader_t *tmp=(objheader_t *)tmpint;
577   struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
578   v->highindex=-1;
579   v->lowindex=MAXARRAYSIZE;
580 #else
581   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
582   struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
583 #endif
584 #ifdef DUALVIEW
585   tmp->lock=RW_LOCK_BIAS;
586 #else
587   initdsmlocks(&tmp->lock);
588 #endif
589   tmp->version=1;
590   ASSIGNUID(v);
591   v->type=type;
592   if (length<0) {
593     printf("ERROR: negative array %d\n", length);
594     return NULL;
595   }
596   v->___objlocation___=(struct ___Object___ *)v;
597   v->___length___=length;
598   return v;
599 }
600 #endif
601
602 #ifndef STM
603 #if defined(PRECISE_GC)
604 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
605   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
606   v->type=type;
607 #ifdef THREADS
608   v->tid=0;
609   v->lockentry=0;
610   v->lockcount=0;
611 #endif
612 #ifdef OPTIONAL
613   v->fses=0;
614 #endif
615   return v;
616 }
617
618 /* Array allocation function */
619
620 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
621   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
622   v->type=type;
623   if (length<0) {
624     printf("ERROR: negative array\n");
625     return NULL;
626   }
627   v->___length___=length;
628 #ifdef THREADS
629   v->tid=0;
630   v->lockentry=0;
631   v->lockcount=0;
632 #endif
633 #ifdef OPTIONAL
634   v->fses=0;
635 #endif
636   return v;
637 }
638
639 #else
640 __attribute__((malloc)) void * allocate_new(int type) {
641   struct ___Object___ * v=FREEMALLOC(classsize[type]);
642   v->type=type;
643 #ifdef OPTIONAL
644   v->fses=0;
645 #endif
646   return v;
647 }
648
649 /* Array allocation function */
650
651 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
652   __attribute__((malloc))  struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
653   v->type=type;
654   v->___length___=length;
655 #ifdef OPTIONAL
656   v->fses=0;
657 #endif
658   return v;
659 }
660 #endif
661 #endif
662
663 /* Converts C character arrays into Java strings */
664 #ifdef PRECISE_GC
665 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
666 #else
667 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
668 #endif
669   int i;
670 #ifdef PRECISE_GC
671   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
672   INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
673   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
674   chararray=(struct ArrayObject *) ptrarray[2];
675 #else
676   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
677   struct ___String___ * strobj=allocate_new(STRINGTYPE);
678 #endif
679   strobj->___value___=chararray;
680   strobj->___count___=length;
681   strobj->___offset___=0;
682
683   for(i=0; i<length; i++) {
684     ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
685   }
686   return strobj;
687 }
688
689 /* Generated code calls this if we fail a bounds check */
690
691 void failedboundschk() {
692 #ifndef TASK
693   printf("Array out of bounds\n");
694 #ifdef THREADS
695   threadexit();
696 #else
697   exit(-1);
698 #endif
699 #else
700   longjmp(error_handler,2);
701 #endif
702 }
703
704 /* Abort task call */
705 void abort_task() {
706 #ifdef TASK
707   longjmp(error_handler,4);
708 #else
709   printf("Aborting\n");
710   exit(-1);
711 #endif
712 }
713
714 #ifndef SANDBOX
715 #ifdef D___System______Assert____Z
716  void CALL11(___System______Assert____Z, int ___status___, int ___status___) {
717    if (!___status___) {
718      printf("Assertion violation\n");
719      *((int *)(NULL)); //force stack trace error
720    }
721  }
722 #endif
723 #endif