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