mlookup bug fix
[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___RecoveryStat______printRecoveryStat____ 
358 #ifdef RECOVERYSTATS
359 void CALL00(___RecoveryStat______printRecoveryStat____) {
360   printRecoveryStat();
361 }
362 #else
363 void CALL00(___RecoveryStat______printRecoveryStat____) {
364   printf("No Stat\n");
365 }
366 #endif
367 #endif
368
369 #ifdef DSTM
370 void CALL00(___System______clearPrefetchCache____) {
371   prehashClear();
372 }
373
374 #ifdef RANGEPREFETCH
375 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
376   /* Manual Prefetches to be inserted */
377   //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
378   //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
379   int numoffset=VAR(___offsets___)->___length___;
380   int i;
381   short offArry[numoffset+2];
382   offArry[0] = 0;
383   offArry[1] = 0;
384   for(i = 2; i<(numoffset+2); i++) {
385     offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
386     //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
387   }
388   unsigned int oid;
389   if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
390     oid =  (unsigned int) VAR(___o___); //outside transaction therefore just an oid
391   } else { //even
392     oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
393   }
394   rangePrefetch(oid, (short)(numoffset+2), offArry);
395 }
396 #else
397 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
398   return;
399 }
400 #endif
401
402 #ifdef D___Task______execution____ 
403 extern void* virtualtable[];
404 // associated with Task.execution(). finds proper execute method and call it
405 void CALL01(___Task______execution____,struct ___Task___ * ___this___)
406 {
407   unsigned int oid;
408   oid = (unsigned int) VAR(___this___);   // object id
409   int type = getObjType(oid);             // object type
410
411 #ifdef PRECISE_GC
412   int p[] = {1,0 , oid};
413    
414   ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(p);
415 #else
416   // call the proper execute method
417   ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(oid);
418 #endif
419 }
420 #endif
421
422 #endif // DSTM
423
424 /* STM Barrier constructs */
425 #ifdef D___Barrier______setBarrier____I
426 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
427   // Barrier initialization
428   int ret;
429   if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
430     printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
431     exit(-1);
432   }
433 }
434 #endif
435
436 #ifdef D___Barrier______enterBarrier____
437 void CALL00(___Barrier______enterBarrier____) {
438   // Synchronization point
439   int ret;
440 #ifdef EVENTMONITOR
441   EVLOGEVENT(EV_ENTERBARRIER);
442 #endif
443 #ifdef PRECISE_GC
444   stopforgc((struct garbagelist *)___params___);
445 #endif
446   ret = pthread_barrier_wait(&barrier);
447 #ifdef PRECISE_GC
448   restartaftergc();
449 #endif
450   if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
451     printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
452     exit(-1);
453   }
454 #ifdef EVENTMONITOR
455   EVLOGEVENT(EV_EXITBARRIER);
456 #endif
457 }
458 #endif
459
460 /* Object allocation function */
461
462 #ifdef DSTM
463 __attribute__((malloc)) void * allocate_newglobal(int type) {
464   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
465   v->type=type;
466   //printf("DEBUG %s(), type= %x\n", __func__, type);
467 #ifdef THREADS
468   v->tid=0;
469   v->lockentry=0;
470   v->lockcount=0;
471 #endif
472   return v;
473 }
474
475 /* Array allocation function */
476
477 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
478   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
479   if (length<0) {
480     printf("ERROR: negative array\n");
481     return NULL;
482   }
483   v->type=type;
484   v->___length___=length;
485 #ifdef THREADS
486   v->tid=0;
487   v->lockentry=0;
488   v->lockcount=0;
489 #endif
490   return v;
491 }
492 #endif
493
494
495 #ifdef STM
496 // STM Versions of allocation functions
497
498 /* Object allocation function */
499 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
500 #ifdef STMARRAY
501   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type], 0);
502 #else
503   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
504 #endif
505   ASSIGNUID(v);
506   v->type=type;
507   v->___objlocation___=v;
508   return v;
509 }
510
511 /* Array allocation function */
512 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
513 #ifdef STMARRAY
514   int basesize=length*classsize[type];
515   //round the base size up
516   basesize=(basesize+LOWMASK)&HIGHMASK;
517   int numlocks=basesize>>INDEXSHIFT;
518   int bookkeepsize=numlocks*2*sizeof(int);
519   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+basesize+bookkeepsize, bookkeepsize);
520   unsigned int *intptr=(unsigned int *)(((char *)v)-sizeof(objheader_t));
521   for(;numlocks>0;numlocks--) {
522     intptr-=2;
523     intptr[0]=1;
524   }
525   v->highindex=-1;
526   v->lowindex=MAXARRAYSIZE;
527 #else
528   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
529 #endif
530   ASSIGNUID(v);
531   if (length<0) {
532     printf("ERROR: negative array\n");
533     return NULL;
534   }
535   v->___objlocation___=(struct ___Object___*)v;
536   v->type=type;
537   v->___length___=length;
538   return v;
539 }
540
541 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
542   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
543   struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
544   ASSIGNUID(v);
545   initdsmlocks(&tmp->lock);
546   tmp->version = 1;
547   v->___objlocation___=v;
548   v->type = type;
549   return v;
550 }
551
552 /* Array allocation function */
553
554 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
555 #ifdef STMARRAY
556   int basesize=length*classsize[type];
557   //round the base size up
558   basesize=(basesize+LOWMASK)&HIGHMASK;
559   int numlocks=basesize>>INDEXSHIFT;
560   int bookkeepsize=(numlocks)*2*sizeof(int);
561   int *tmpint=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+basesize+sizeof(objheader_t)+bookkeepsize);
562   for(;numlocks>0;numlocks--) {
563     tmpint[0]=1;
564     tmpint+=2;
565   }
566   objheader_t *tmp=(objheader_t *)tmpint;
567   struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
568   v->highindex=-1;
569   v->lowindex=MAXARRAYSIZE;
570 #else
571   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
572   struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
573 #endif
574 #ifdef DUALVIEW
575   tmp->lock=RW_LOCK_BIAS;
576 #else
577   initdsmlocks(&tmp->lock);
578 #endif
579   tmp->version=1;
580   ASSIGNUID(v);
581   v->type=type;
582   if (length<0) {
583     printf("ERROR: negative array %d\n", length);
584     return NULL;
585   }
586   v->___objlocation___=(struct ___Object___ *)v;
587   v->___length___=length;
588   return v;
589 }
590 #endif
591
592 #ifndef STM
593 #if defined(PRECISE_GC)
594 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
595   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
596   v->type=type;
597 #ifdef THREADS
598   v->tid=0;
599   v->lockentry=0;
600   v->lockcount=0;
601 #endif
602 #ifdef OPTIONAL
603   v->fses=0;
604 #endif
605   return v;
606 }
607
608 /* Array allocation function */
609
610 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
611   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
612   v->type=type;
613   if (length<0) {
614     printf("ERROR: negative array\n");
615     return NULL;
616   }
617   v->___length___=length;
618 #ifdef THREADS
619   v->tid=0;
620   v->lockentry=0;
621   v->lockcount=0;
622 #endif
623 #ifdef OPTIONAL
624   v->fses=0;
625 #endif
626   return v;
627 }
628
629 #else
630 __attribute__((malloc)) void * allocate_new(int type) {
631   struct ___Object___ * v=FREEMALLOC(classsize[type]);
632   v->type=type;
633 #ifdef OPTIONAL
634   v->fses=0;
635 #endif
636   return v;
637 }
638
639 /* Array allocation function */
640
641 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
642   __attribute__((malloc))  struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
643   v->type=type;
644   v->___length___=length;
645 #ifdef OPTIONAL
646   v->fses=0;
647 #endif
648   return v;
649 }
650 #endif
651 #endif
652
653 /* Converts C character arrays into Java strings */
654 #ifdef PRECISE_GC
655 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
656 #else
657 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
658 #endif
659   int i;
660 #ifdef PRECISE_GC
661   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
662   INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
663   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
664   chararray=(struct ArrayObject *) ptrarray[2];
665 #else
666   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
667   struct ___String___ * strobj=allocate_new(STRINGTYPE);
668 #endif
669   strobj->___value___=chararray;
670   strobj->___count___=length;
671   strobj->___offset___=0;
672
673   for(i=0; i<length; i++) {
674     ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
675   }
676   return strobj;
677 }
678
679 /* Generated code calls this if we fail a bounds check */
680
681 void failedboundschk() {
682 #ifndef TASK
683   printf("Array out of bounds\n");
684 #ifdef THREADS
685   threadexit();
686 #else
687   exit(-1);
688 #endif
689 #else
690   longjmp(error_handler,2);
691 #endif
692 }
693
694 /* Abort task call */
695 void abort_task() {
696 #ifdef TASK
697   longjmp(error_handler,4);
698 #else
699   printf("Aborting\n");
700   exit(-1);
701 #endif
702 }
703
704 #ifndef SANDBOX
705 #ifdef D___System______Assert____Z
706  void CALL11(___System______Assert____Z, int ___status___, int ___status___) {
707    if (!___status___) {
708      printf("Assertion violation\n");
709      *((int *)(NULL)); //force stack trace error
710    }
711  }
712 #endif
713 #endif