2 #include "structdefs.h"
14 extern int classsize[];
15 jmp_buf error_handler;
20 float failurechance=0;
22 int injectinstructionfailures;
24 float instfailurechance=0;
33 #include "checkpoint.h"
35 #include "SimpleHash.h"
36 #include "GenericHashtable.h"
37 #include <sys/select.h>
40 #include "instrument.h"
43 struct genhashtable * activetasks;
44 struct parameterwrapper * objectqueues[NUMCLASSES];
45 struct genhashtable * failedtasks;
46 struct taskparamdescriptor * currtpd;
47 struct RuntimeHash * forward;
48 struct RuntimeHash * reverse;
51 int main(int argc, char **argv) {
53 GC_init(); // Initialize the garbage collector
59 initializeexithandler();
60 /* Create table for failed tasks */
61 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
62 (int (*)(void *,void *)) &comparetpd);
63 /* Create queue of active tasks */
64 activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
65 (int (*)(void *,void *)) &comparetpd);
68 /* Process task information */
71 /* Create startup object */
72 createstartupobject(argc, argv);
74 /* Start executing the tasks */
78 void createstartupobject(int argc, char ** argv) {
81 /* Allocate startup object */
83 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
84 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
86 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
87 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
89 /* Build array of strings */
90 startupobject->___parameters___=stringarray;
92 int length=strlen(argv[i]);
94 struct ___String___ *newstring=NewString(NULL, argv[i],length);
96 struct ___String___ *newstring=NewString(argv[i],length);
98 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
101 /* Set initialized flag for startup object */
102 flagorand(startupobject,1,0xFFFFFFFF);
105 int hashCodetpd(struct taskparamdescriptor *ftd) {
106 int hash=(int)ftd->task;
108 for(i=0;i<ftd->numParameters;i++) {
109 hash^=(int)ftd->parameterArray[i];
114 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
116 if (ftd1->task!=ftd2->task)
118 for(i=0;i<ftd1->numParameters;i++)
119 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
125 /* This function sets a tag. */
127 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
129 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
131 struct ___Object___ * tagptr=obj->___tags___;
133 obj->___tags___=(struct ___Object___ *)tagd;
135 /* Have to check if it is already set */
136 if (tagptr->type==TAGTYPE) {
137 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
141 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
142 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
143 obj=(struct ___Object___ *)ptrarray[2];
144 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
145 td=(struct ___TagDescriptor___ *) obj->___tags___;
147 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
149 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
150 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
151 obj->___tags___=(struct ___Object___ *) ao;
152 ao->___cachedCode___=2;
156 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
157 for(i=0;i<ao->___cachedCode___;i++) {
158 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
162 if (ao->___cachedCode___<ao->___length___) {
163 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
164 ao->___cachedCode___++;
167 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
168 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
169 obj=(struct ___Object___ *)ptrarray[2];
170 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
171 ao=(struct ArrayObject *)obj->___tags___;
173 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
175 aonew->___cachedCode___=ao->___length___+1;
176 for(i=0;i<ao->___length___;i++) {
177 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
179 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
185 struct ___Object___ * tagset=tagd->flagptr;
189 } else if (tagset->type!=OBJECTARRAYTYPE) {
191 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
192 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
193 obj=(struct ___Object___ *)ptrarray[2];
194 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
196 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
198 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
199 ARRAYSET(ao, struct ___Object___ *, 1, obj);
200 ao->___cachedCode___=2;
201 tagd->flagptr=(struct ___Object___ *)ao;
203 struct ArrayObject *ao=(struct ArrayObject *) tagset;
204 if (ao->___cachedCode___<ao->___length___) {
205 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
209 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
210 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
211 obj=(struct ___Object___ *)ptrarray[2];
212 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
213 ao=(struct ArrayObject *)tagd->flagptr;
215 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
217 aonew->___cachedCode___=ao->___cachedCode___+1;
218 for(i=0;i<ao->___length___;i++) {
219 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
221 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
222 tagd->flagptr=(struct ___Object___ *) ao;
228 /* This function clears a tag. */
230 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
232 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
234 /* We'll assume that tag is alway there.
235 Need to statically check for this of course. */
236 struct ___Object___ * tagptr=obj->___tags___;
238 if (tagptr->type==TAGTYPE) {
239 if ((struct ___TagDescriptor___ *)tagptr==tagd)
240 obj->___tags___=NULL;
242 printf("ERROR 1 in tagclear\n");
244 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
246 for(i=0;i<ao->___cachedCode___;i++) {
247 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
249 ao->___cachedCode___--;
250 if (i<ao->___cachedCode___)
251 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
252 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
253 if (ao->___cachedCode___==0)
254 obj->___tags___=NULL;
258 printf("ERROR 2 in tagclear\n");
262 struct ___Object___ *tagset=tagd->flagptr;
263 if (tagset->type!=OBJECTARRAYTYPE) {
267 printf("ERROR 3 in tagclear\n");
269 struct ArrayObject *ao=(struct ArrayObject *) tagset;
271 for(i=0;i<ao->___cachedCode___;i++) {
272 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
274 ao->___cachedCode___--;
275 if (i<ao->___cachedCode___)
276 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
277 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
278 if (ao->___cachedCode___==0)
283 printf("ERROR 4 in tagclear\n");
291 /* This function allocates a new tag. */
293 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
294 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
296 struct ___TagDescriptor___ * allocate_tag(int index) {
297 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
306 /* This function updates the flag for object ptr. It or's the flag
307 with the or mask and and's it with the andmask. */
309 void flagbody(struct ___Object___ *ptr, int flag);
311 void flagorand(void * ptr, int ormask, int andmask) {
312 int oldflag=((int *)ptr)[1];
313 int flag=ormask|oldflag;
315 // Not sure why this was necessary
316 // if (flag==oldflag) /* Don't do anything */
322 void intflagorand(void * ptr, int ormask, int andmask) {
323 int oldflag=((int *)ptr)[1];
324 int flag=ormask|oldflag;
326 if (flag==oldflag) /* Don't do anything */
328 else flagbody(ptr, flag);
331 void flagorandinit(void * ptr, int ormask, int andmask) {
332 int oldflag=((int *)ptr)[1];
333 int flag=ormask|oldflag;
338 void flagbody(struct ___Object___ *ptr, int flag) {
339 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
342 /*Remove object from all queues */
343 while(flagptr!=NULL) {
344 struct parameterwrapper *next;
345 struct ___Object___ * tag=ptr->___tags___;
346 RuntimeHashget(flagptr->objectset, (int) ptr, (int *) &next);
347 RuntimeHashremove(flagptr->objectset, (int)ptr, (int) next);
352 struct QueueItem *tmpptr;
353 struct parameterwrapper * parameter=objectqueues[ptr->type];
355 struct parameterwrapper * prevptr=NULL;
356 struct ___Object___ *tagptr=ptr->___tags___;
358 /* Outer loop iterates through all parameter queues an object of
359 this type could be in. */
361 while(parameter!=NULL) {
363 if (parameter->numbertags>0) {
366 else if(tagptr->type==TAGTYPE) {
367 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
368 for(i=0;i<parameter->numbertags;i++) {
369 //slotid is parameter->tagarray[2*i];
370 int tagid=parameter->tagarray[2*i+1];
371 if (tagid!=tagptr->flag)
372 goto nextloop; /*We don't have this tag */
375 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
376 for(i=0;i<parameter->numbertags;i++) {
377 //slotid is parameter->tagarray[2*i];
378 int tagid=parameter->tagarray[2*i+1];
380 for(j=0;j<ao->___cachedCode___;j++) {
381 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, i)->flag)
392 for(i=0;i<parameter->numberofterms;i++) {
393 int andmask=parameter->intarray[i*2];
394 int checkmask=parameter->intarray[i*2+1];
395 if ((flag&andmask)==checkmask) {
396 enqueuetasks(parameter, prevptr, ptr);
402 parameter=parameter->next;
404 ptr->flagptr=prevptr;
408 void enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr) {
409 void * taskpointerarray[MAXTASKPARAMS];
411 int numparams=parameter->task->numParameters;
412 int numiterators=parameter->task->numTotal-1;
414 struct taskdescriptor * task=parameter->task;
416 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
418 /* Add enqueued object to parameter vector */
419 taskpointerarray[parameter->slot]=ptr;
421 /* Reset iterators */
422 for(j=0;j<numiterators;j++) {
423 toiReset(¶meter->iterators[j]);
426 /* Find initial state */
427 for(j=0;j<numiterators;j++) {
429 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
430 toiNext(¶meter->iterators[j], taskpointerarray);
432 /* Need to backtrack */
433 toiReset(¶meter->iterators[j]);
437 /* Nothing to enqueue */
444 /* Enqueue current state */
445 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
447 tpd->numParameters=numiterators+1;
448 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
449 for(j=0;j<=numiterators;j++)
450 tpd->parameterArray[j]=taskpointerarray[j];
453 if (!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd)) {
454 genputtable(activetasks, tpd, tpd);
456 RUNFREE(tpd->parameterArray);
460 /* This loop iterates to the next parameter combination */
464 for(j=numiterators-1; j<numiterators;j++) {
466 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
467 toiNext(¶meter->iterators[j], taskpointerarray);
469 /* Need to backtrack */
470 toiReset(¶meter->iterators[j]);
474 /* Nothing more to enqueue */
481 /* Handler for signals. The signals catch null pointer errors and
482 arithmatic errors. */
484 void myhandler(int sig, siginfo_t *info, void *uap) {
486 printf("sig=%d\n",sig);
489 longjmp(error_handler,1);
494 struct RuntimeHash *fdtoobject;
496 void addreadfd(int fd) {
499 FD_SET(fd, &readfds);
502 void removereadfd(int fd) {
503 FD_CLR(fd, &readfds);
504 if (maxreadfd==(fd+1)) {
506 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
517 void executetasks() {
518 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
520 /* Set up signal handlers */
521 struct sigaction sig;
522 sig.sa_sigaction=&myhandler;
523 sig.sa_flags=SA_SIGINFO;
524 sigemptyset(&sig.sa_mask);
526 /* Catch bus errors, segmentation faults, and floating point exceptions*/
527 sigaction(SIGBUS,&sig,0);
528 sigaction(SIGSEGV,&sig,0);
529 sigaction(SIGFPE,&sig,0);
530 sigaction(SIGPIPE,&sig,0);
535 fdtoobject=allocateRuntimeHash(100);
537 /* Map first block of memory to protected, anonymous page */
538 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
541 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
543 /* Check if any filedescriptors have IO pending */
546 struct timeval timeout={0,0};
550 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
552 /* Process ready fd's */
554 for(fd=0;fd<maxreadfd;fd++) {
555 if (FD_ISSET(fd, &tmpreadfds)) {
556 /* Set ready flag on object */
558 // printf("Setting fd %d\n",fd);
559 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
560 intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
567 /* See if there are any active tasks */
568 if (hashsize(activetasks)>0) {
570 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
571 genfreekey(activetasks, currtpd);
573 /* Check if this task has failed */
574 if (gencontains(failedtasks, currtpd)) {
575 // Free up task parameter descriptor
576 RUNFREE(currtpd->parameterArray);
580 int numparams=currtpd->task->numParameters;
581 int numtotal=currtpd->task->numTotal;
583 /* Make sure that the parameters are still in the queues */
584 for(i=0;i<numparams;i++) {
585 void * parameter=currtpd->parameterArray[i];
586 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
587 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
589 /* Check that object is still in queue */
590 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter)) {
591 RUNFREE(currtpd->parameterArray);
595 /* Check that object still has necessary tags */
596 for(j=0;j<pd->numbertags;j++) {
597 int slotid=pd->tagarray[2*j]+numparams;
598 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
599 if (!containstag(parameter, tagd)) {
600 RUNFREE(currtpd->parameterArray);
606 taskpointerarray[i+OFFSET]=parameter;
609 for(;i<numtotal;i++) {
610 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
614 /* Checkpoint the state */
615 forward=allocateRuntimeHash(100);
616 reverse=allocateRuntimeHash(100);
617 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
619 if (x=setjmp(error_handler)) {
623 printf("Fatal Error=%d, Recovering!\n",x);
625 genputtable(failedtasks,currtpd,currtpd);
626 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
627 freeRuntimeHash(forward);
628 freeRuntimeHash(reverse);
633 if (injectfailures) {
634 if ((((double)random())/RAND_MAX)<failurechance) {
635 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
636 longjmp(error_handler,10);
639 /* Actually call task */
641 ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
642 taskpointerarray[1]=NULL;
646 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
647 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
648 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
650 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
651 freeRuntimeHash(forward);
652 freeRuntimeHash(reverse);
654 // Free up task parameter descriptor
655 RUNFREE(currtpd->parameterArray);
665 /* This function processes an objects tags */
666 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
669 for(i=0;i<pd->numbertags;i++) {
670 int slotid=pd->tagarray[2*i];
671 int tagid=pd->tagarray[2*i+1];
673 if (statusarray[slotid+numparams]==0) {
674 parameter->iterators[*iteratorcount].istag=1;
675 parameter->iterators[*iteratorcount].tagid=tagid;
676 parameter->iterators[*iteratorcount].slot=slotid+numparams;
677 parameter->iterators[*iteratorcount].tagobjectslot=index;
678 statusarray[slotid+numparams]=1;
685 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
688 struct RuntimeHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
690 parameter->iterators[*iteratorcount].istag=0;
691 parameter->iterators[*iteratorcount].slot=index;
692 parameter->iterators[*iteratorcount].objectset=objectset;
693 statusarray[index]=1;
695 for(i=0;i<pd->numbertags;i++) {
696 int slotid=pd->tagarray[2*i];
697 int tagid=pd->tagarray[2*i+1];
698 if (statusarray[slotid+numparams]!=0) {
699 /* This tag has already been enqueued, use it to narrow search */
700 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
704 parameter->iterators[*iteratorcount].numtags=tagcount;
709 /* This function builds the iterators for a task & parameter */
711 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
712 int statusarray[MAXTASKPARAMS];
714 int numparams=task->numParameters;
716 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
718 statusarray[index]=1; /* Initial parameter */
719 /* Process tags for initial iterator */
721 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
725 /* Check for objects with existing tags */
726 for(i=0;i<numparams;i++) {
727 if (statusarray[i]==0) {
728 struct parameterdescriptor *pd=task->descriptorarray[i];
730 for(j=0;j<pd->numbertags;j++) {
731 int slotid=pd->tagarray[2*j];
732 if(statusarray[slotid+numparams]!=0) {
733 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
734 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
740 /* Nothing with a tag enqueued */
742 for(i=0;i<numparams;i++) {
743 if (statusarray[i]==0) {
744 struct parameterdescriptor *pd=task->descriptorarray[i];
745 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
746 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
759 /* This function processes the task information to create queues for
760 each parameter type. */
762 void processtasks() {
764 for(i=0;i<numtasks;i++) {
765 struct taskdescriptor * task=taskarray[i];
768 for(j=0;j<task->numParameters;j++) {
769 struct parameterdescriptor *param=task->descriptorarray[j];
770 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
771 struct parameterwrapper ** ptr=&objectqueues[param->type];
773 param->queue=parameter;
774 parameter->objectset=allocateRuntimeHash(10);
775 parameter->numberofterms=param->numberterms;
776 parameter->intarray=param->intarray;
777 parameter->numbertags=param->numbertags;
778 parameter->tagarray=param->tagarray;
779 parameter->task=task;
780 /* Link new queue in */
786 /* Build iterators for parameters */
787 for(j=0;j<task->numParameters;j++) {
788 struct parameterdescriptor *param=task->descriptorarray[j];
789 struct parameterwrapper *parameter=param->queue;
791 builditerators(task, j, parameter);
796 void toiReset(struct tagobjectiterator * it) {
799 } else if (it->numtags>0) {
802 RuntimeHashiterator(it->objectset, &it->it);
806 int toiHasNext(struct tagobjectiterator *it, void ** objectarray) {
809 /* Get object with tags */
810 struct ___Object___ *obj=objectarray[it->tagobjectslot];
811 struct ___Object___ *tagptr=obj->___tags___;
812 if (tagptr->type==TAGTYPE) {
813 if ((it->tagobjindex==0)&& /* First object */
814 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
819 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
820 int tagindex=it->tagobjindex;
821 for(;tagindex<ao->___cachedCode___;tagindex++) {
822 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
823 if (td->flag==it->tagid) {
824 it->tagobjindex=tagindex; /* Found right type of tag */
830 } else if (it->numtags>0) {
831 /* Use tags to locate appropriate objects */
832 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
833 struct ___Object___ *objptr=tag->flagptr;
835 if (objptr->type!=OBJECTARRAYTYPE) {
836 if (it->tagobjindex>0)
838 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
840 for(i=1;i<it->numtags;i++) {
841 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
842 if (!containstag(objptr,tag2))
847 struct ArrayObject *ao=(struct ArrayObject *) objptr;
850 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
851 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
852 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
854 for(i=1;i<it->numtags;i++) {
855 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
856 if (!containstag(objptr,tag2))
863 it->tagobjindex=tagindex;
867 return RunhasNext(&it->it);
871 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
873 struct ___Object___ * objptr=tag->flagptr;
874 if (objptr->type==OBJECTARRAYTYPE) {
875 struct ArrayObject *ao=(struct ArrayObject *)objptr;
876 for(j=0;j<ao->___cachedCode___;j++) {
877 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
885 void toiNext(struct tagobjectiterator *it , void ** objectarray) {
886 /* hasNext has all of the intelligence */
889 /* Get object with tags */
890 struct ___Object___ *obj=objectarray[it->tagobjectslot];
891 struct ___Object___ *tagptr=obj->___tags___;
892 if (tagptr->type==TAGTYPE) {
894 objectarray[it->slot]=tagptr;
896 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
897 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
899 } else if (it->numtags>0) {
900 /* Use tags to locate appropriate objects */
901 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
902 struct ___Object___ *objptr=tag->flagptr;
903 if (objptr->type!=OBJECTARRAYTYPE) {
905 objectarray[it->slot]=objptr;
907 struct ArrayObject *ao=(struct ArrayObject *) objptr;
908 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
912 objectarray[it->slot]=(void *)Runkey(&it->it);
920 void exithandler(int sig, siginfo_t *info, void * uap) {
924 void initializeexithandler() {
925 struct sigaction sig;
926 sig.sa_sigaction=&exithandler;
927 sig.sa_flags=SA_SIGINFO;
928 sigemptyset(&sig.sa_mask);
929 sigaction(SIGUSR2, &sig, 0);
933 /* This function inject failures */
935 void injectinstructionfailure() {
937 if (injectinstructionfailures) {
940 instructioncount=failurecount;
941 instaccum+=failurecount;
942 if ((((double)random())/RAND_MAX)<instfailurechance) {
945 printf("FAILURE!!! %d\n",numfailures);
946 longjmp(error_handler,11);
951 if (injectinstructionfailures) {
954 instaccum+=failurecount;
955 if ((((double)random())/RAND_MAX)<instfailurechance) {
958 printf("FAILURE!!! %d\n",numfailures);
966 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
967 struct ArrayObject * chararray=VAR(___s___)->___value___;
969 int offset=VAR(___s___)->___offset___;
970 for(i=0;i<VAR(___s___)->___count___;i++) {
971 short sc=((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
976 /* Object allocation function */
979 void * allocate_new(void * ptr, int type) {
980 struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
990 /* Array allocation function */
992 struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
993 struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
996 printf("ERROR: negative array\n");
999 v->___length___=length;
1009 void * allocate_new(int type) {
1010 void * v=FREEMALLOC(classsize[type]);
1015 /* Array allocation function */
1017 struct ArrayObject * allocate_newarray(int type, int length) {
1018 struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
1020 v->___length___=length;
1026 /* Converts C character arrays into Java strings */
1028 struct ___String___ * NewString(void * ptr, const char *str,int length) {
1030 struct ___String___ * NewString(const char *str,int length) {
1034 struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
1035 int ptrarray[]={1, (int) ptr, (int) chararray};
1036 struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
1037 chararray=(struct ArrayObject *) ptrarray[2];
1039 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
1040 struct ___String___ * strobj=allocate_new(STRINGTYPE);
1042 strobj->___value___=chararray;
1043 strobj->___count___=length;
1044 strobj->___offset___=0;
1046 for(i=0;i<length;i++) {
1047 ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i]; }
1051 /* Generated code calls this if we fail a bounds check */
1053 void failedboundschk() {
1055 printf("Array out of bounds\n");
1062 longjmp(error_handler,2);
1066 /* Abort task call */
1069 longjmp(error_handler,4);
1071 printf("Aborting\n");