2 #include "structdefs.h"
14 #define ARRAYSET(array, type, index, value) \
15 ((type *)(&(& array->___length___)[1]))[index]=value
17 #define ARRAYGET(array, type, index) \
18 ((type *)(&(& array->___length___)[1]))[index]
20 extern int classsize[];
21 jmp_buf error_handler;
26 float failurechance=0;
28 int injectinstructionfailures;
30 float instfailurechance=0;
39 #include "checkpoint.h"
41 #include "SimpleHash.h"
42 #include "GenericHashtable.h"
43 #include <sys/select.h>
46 #include "instrument.h"
49 struct genhashtable * activetasks;
50 struct parameterwrapper * objectqueues[NUMCLASSES];
51 struct genhashtable * failedtasks;
52 struct taskparamdescriptor * currtpd;
53 struct RuntimeHash * forward;
54 struct RuntimeHash * reverse;
57 int main(int argc, char **argv) {
59 GC_init(); // Initialize the garbage collector
65 initializeexithandler();
66 /* Create table for failed tasks */
67 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
68 (int (*)(void *,void *)) &comparetpd);
69 /* Create queue of active tasks */
70 activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
71 (int (*)(void *,void *)) &comparetpd);
74 /* Process task information */
77 /* Create startup object */
78 createstartupobject(argc, argv);
80 /* Start executing the tasks */
84 void createstartupobject(int argc, char ** argv) {
87 /* Allocate startup object */
89 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
90 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
92 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
93 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
95 /* Build array of strings */
96 startupobject->___parameters___=stringarray;
98 int length=strlen(argv[i]);
100 struct ___String___ *newstring=NewString(NULL, argv[i],length);
102 struct ___String___ *newstring=NewString(argv[i],length);
104 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
107 /* Set initialized flag for startup object */
108 flagorand(startupobject,1,0xFFFFFFFF);
111 int hashCodetpd(struct taskparamdescriptor *ftd) {
112 int hash=(int)ftd->task;
114 for(i=0;i<ftd->numParameters;i++) {
115 hash^=(int)ftd->parameterArray[i];
120 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
122 if (ftd1->task!=ftd2->task)
124 for(i=0;i<ftd1->numParameters;i++)
125 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
130 #define TAGARRAYINTERVAL 10
131 #define OBJECTARRAYINTERVAL 10
133 /* This function sets a tag. */
135 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
137 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
139 struct ___Object___ * tagptr=obj->___tags___;
141 obj->___tags___=(struct ___Object___ *)tagd;
143 /* Have to check if it is already set */
144 if (tagptr->type==TAGTYPE) {
145 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
149 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
150 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
151 obj=(struct ___Object___ *)ptrarray[2];
152 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
153 td=(struct ___TagDescriptor___ *) obj->___tags___;
155 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
157 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
158 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
159 obj->___tags___=(struct ___Object___ *) ao;
160 ao->___cachedCode___=2;
164 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
165 for(i=0;i<ao->___cachedCode___;i++) {
166 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
170 if (ao->___cachedCode___<ao->___length___) {
171 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
172 ao->___cachedCode___++;
175 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
176 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
177 obj=(struct ___Object___ *)ptrarray[2];
178 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
179 ao=(struct ArrayObject *)obj->___tags___;
181 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
183 aonew->___cachedCode___=ao->___length___+1;
184 for(i=0;i<ao->___length___;i++) {
185 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
187 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
193 struct ___Object___ * tagset=tagd->___tagset___;
196 tagd->___tagset___=obj;
197 } else if (tagset->type!=OBJECTARRAYTYPE) {
199 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
200 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
201 obj=(struct ___Object___ *)ptrarray[2];
202 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
204 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
206 ARRAYSET(ao, struct ___Object___ *, 0, tagd->___tagset___);
207 ARRAYSET(ao, struct ___Object___ *, 1, obj);
208 ao->___cachedCode___=2;
209 tagd->___tagset___=(struct ___Object___ *)ao;
211 struct ArrayObject *ao=(struct ArrayObject *) tagset;
212 if (ao->___cachedCode___<ao->___length___) {
213 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
217 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
218 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
219 obj=(struct ___Object___ *)ptrarray[2];
220 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
221 ao=(struct ArrayObject *)tagd->___tagset___;
223 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
225 aonew->___cachedCode___=ao->___cachedCode___+1;
226 for(i=0;i<ao->___length___;i++) {
227 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
229 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
230 tagd->___tagset___=(struct ___Object___ *) ao;
236 /* This function clears a tag. */
238 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
240 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
242 /* We'll assume that tag is alway there.
243 Need to statically check for this of course. */
244 struct ___Object___ * tagptr=obj->___tags___;
246 if (tagptr->type==TAGTYPE) {
247 if ((struct ___TagDescriptor___ *)tagptr==tagd)
248 obj->___tags___=NULL;
250 printf("ERROR 1 in tagclear\n");
252 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
254 for(i=0;i<ao->___cachedCode___;i++) {
255 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
257 ao->___cachedCode___--;
258 if (i<ao->___cachedCode___)
259 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
260 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
261 if (ao->___cachedCode___==0)
262 obj->___tags___=NULL;
266 printf("ERROR 2 in tagclear\n");
270 struct ___Object___ *tagset=tagd->___tagset___;
271 if (tagset->type!=OBJECTARRAYTYPE) {
273 tagd->___tagset___=NULL;
275 printf("ERROR 3 in tagclear\n");
277 struct ArrayObject *ao=(struct ArrayObject *) tagset;
279 for(i=0;i<ao->___cachedCode___;i++) {
280 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
282 ao->___cachedCode___--;
283 if (i<ao->___cachedCode___)
284 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
285 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
286 if (ao->___cachedCode___==0)
287 tagd->___tagset___=NULL;
291 printf("ERROR 4 in tagclear\n");
299 /* This function allocates a new tag. */
301 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
302 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[STARTUPTYPE]);
304 struct ___TagDescriptor___ * allocate_tag(int index) {
305 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[STARTUPTYPE]);
314 /* This function updates the flag for object ptr. It or's the flag
315 with the or mask and and's it with the andmask. */
317 void flagbody(struct ___Object___ *ptr, int flag);
319 void flagorand(void * ptr, int ormask, int andmask) {
320 int oldflag=((int *)ptr)[1];
321 int flag=ormask|oldflag;
323 // Not sure why this was necessary
324 // if (flag==oldflag) /* Don't do anything */
330 void intflagorand(void * ptr, int ormask, int andmask) {
331 int oldflag=((int *)ptr)[1];
332 int flag=ormask|oldflag;
334 if (flag==oldflag) /* Don't do anything */
336 else flagbody(ptr, flag);
339 void flagorandinit(void * ptr, int ormask, int andmask) {
340 int oldflag=((int *)ptr)[1];
341 int flag=ormask|oldflag;
346 void flagbody(struct ___Object___ *ptr, int flag) {
347 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
350 /*Remove object from all queues */
351 while(flagptr!=NULL) {
352 struct parameterwrapper *next;
353 struct ___Object___ * tag=ptr->___tags___;
354 RuntimeHashget(flagptr->objectset, (int) ptr, (int *) &next);
355 RuntimeHashremove(flagptr->objectset, (int)ptr, (int) next);
360 struct QueueItem *tmpptr;
361 struct parameterwrapper * parameter=objectqueues[ptr->type];
363 struct parameterwrapper * prevptr=NULL;
364 struct ___Object___ *tagptr=ptr->___tags___;
366 /* Outer loop iterates through all parameter queues an object of
367 this type could be in. */
369 while(parameter!=NULL) {
371 if (parameter->numbertags>0) {
374 else if(tagptr->type==TAGTYPE) {
375 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
376 for(i=0;i<parameter->numbertags;i++) {
377 //slotid is parameter->tagarray[2*i];
378 int tagid=parameter->tagarray[2*i+1];
379 if (tagid!=tagptr->flag)
380 goto nextloop; /*We don't have this tag */
383 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
384 for(i=0;i<parameter->numbertags;i++) {
385 //slotid is parameter->tagarray[2*i];
386 int tagid=parameter->tagarray[2*i+1];
388 for(j=0;j<ao->___cachedCode___;j++) {
389 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, i)->flag)
400 for(i=0;i<parameter->numberofterms;i++) {
401 int andmask=parameter->intarray[i*2];
402 int checkmask=parameter->intarray[i*2+1];
403 if ((flag&andmask)==checkmask) {
404 enqueuetasks(parameter, prevptr, ptr);
410 parameter=parameter->next;
412 ptr->flagptr=prevptr;
416 void enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr) {
417 void * taskpointerarray[MAXTASKPARAMS];
419 int numparams=parameter->task->numParameters;
420 int numiterators=parameter->task->numTotal-1;
422 struct taskdescriptor * task=parameter->task;
424 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
426 /* Add enqueued object to parameter vector */
427 taskpointerarray[parameter->slot]=ptr;
429 /* Reset iterators */
430 for(j=0;j<numiterators;j++) {
431 toiReset(¶meter->iterators[j]);
434 /* Find initial state */
435 for(j=0;j<numiterators;j++) {
437 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
438 toiNext(¶meter->iterators[j], taskpointerarray);
440 /* Need to backtrack */
441 toiReset(¶meter->iterators[j]);
445 /* Nothing to enqueue */
452 /* Enqueue current state */
453 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
455 tpd->numParameters=numiterators+1;
456 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
457 for(j=0;j<=numiterators;j++)
458 tpd->parameterArray[j]=taskpointerarray[j];
461 if (!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd)) {
462 genputtable(activetasks, tpd, tpd);
464 RUNFREE(tpd->parameterArray);
468 /* This loop iterates to the next parameter combination */
472 for(j=numiterators-1; j<numiterators;j++) {
474 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
475 toiNext(¶meter->iterators[j], taskpointerarray);
477 /* Need to backtrack */
478 toiReset(¶meter->iterators[j]);
482 /* Nothing more to enqueue */
489 /* Handler for signals. The signals catch null pointer errors and
490 arithmatic errors. */
492 void myhandler(int sig, siginfo_t *info, void *uap) {
494 printf("sig=%d\n",sig);
497 longjmp(error_handler,1);
502 struct RuntimeHash *fdtoobject;
504 void addreadfd(int fd) {
507 FD_SET(fd, &readfds);
510 void removereadfd(int fd) {
511 FD_CLR(fd, &readfds);
512 if (maxreadfd==(fd+1)) {
514 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
525 void executetasks() {
526 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
528 /* Set up signal handlers */
529 struct sigaction sig;
530 sig.sa_sigaction=&myhandler;
531 sig.sa_flags=SA_SIGINFO;
532 sigemptyset(&sig.sa_mask);
534 /* Catch bus errors, segmentation faults, and floating point exceptions*/
535 sigaction(SIGBUS,&sig,0);
536 sigaction(SIGSEGV,&sig,0);
537 sigaction(SIGFPE,&sig,0);
538 sigaction(SIGPIPE,&sig,0);
543 fdtoobject=allocateRuntimeHash(100);
545 /* Map first block of memory to protected, anonymous page */
546 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
549 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
551 /* Check if any filedescriptors have IO pending */
554 struct timeval timeout={0,0};
558 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
560 /* Process ready fd's */
562 for(fd=0;fd<maxreadfd;fd++) {
563 if (FD_ISSET(fd, &tmpreadfds)) {
564 /* Set ready flag on object */
566 // printf("Setting fd %d\n",fd);
567 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
568 intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
575 /* See if there are any active tasks */
576 if (hashsize(activetasks)>0) {
578 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
579 genfreekey(activetasks, currtpd);
581 /* Check if this task has failed */
582 if (gencontains(failedtasks, currtpd)) {
583 // Free up task parameter descriptor
584 RUNFREE(currtpd->parameterArray);
588 int numparams=currtpd->task->numParameters;
589 int numtotal=currtpd->task->numTotal;
591 /* Make sure that the parameters are still in the queues */
592 for(i=0;i<numparams;i++) {
593 void * parameter=currtpd->parameterArray[i];
594 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
595 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
597 /* Check that object is still in queue */
598 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter)) {
599 RUNFREE(currtpd->parameterArray);
603 /* Check that object still has necessary tags */
604 for(j=0;j<pd->numbertags;j++) {
605 int slotid=pd->tagarray[2*i]+numparams;
606 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
607 if (!containstag(parameter, tagd)) {
608 RUNFREE(currtpd->parameterArray);
614 taskpointerarray[i+OFFSET]=parameter;
617 for(;i<numtotal;i++) {
618 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
622 /* Checkpoint the state */
623 forward=allocateRuntimeHash(100);
624 reverse=allocateRuntimeHash(100);
625 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
627 if (x=setjmp(error_handler)) {
631 printf("Fatal Error=%d, Recovering!\n",x);
633 genputtable(failedtasks,currtpd,currtpd);
634 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
635 freeRuntimeHash(forward);
636 freeRuntimeHash(reverse);
641 if (injectfailures) {
642 if ((((double)random())/RAND_MAX)<failurechance) {
643 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
644 longjmp(error_handler,10);
647 /* Actually call task */
649 ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
650 taskpointerarray[1]=NULL;
654 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
655 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
656 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
658 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
659 freeRuntimeHash(forward);
660 freeRuntimeHash(reverse);
662 // Free up task parameter descriptor
663 RUNFREE(currtpd->parameterArray);
673 /* This function processes an objects tags */
674 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
677 for(i=0;i<pd->numbertags;i++) {
678 int slotid=pd->tagarray[2*i];
679 int tagid=pd->tagarray[2*i+1];
681 if (statusarray[slotid+numparams]==0) {
682 parameter->iterators[*iteratorcount].istag=1;
683 parameter->iterators[*iteratorcount].tagid=tagid;
684 parameter->iterators[*iteratorcount].slot=slotid+numparams;
685 parameter->iterators[*iteratorcount].tagobjectslot=index;
686 statusarray[slotid+numparams]=1;
693 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
696 struct RuntimeHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
698 parameter->iterators[*iteratorcount].istag=0;
699 parameter->iterators[*iteratorcount].slot=index;
700 parameter->iterators[*iteratorcount].objectset=objectset;
701 statusarray[index]=1;
703 for(i=0;i<pd->numbertags;i++) {
704 int slotid=pd->tagarray[2*i];
705 int tagid=pd->tagarray[2*i+1];
706 if (statusarray[slotid+numparams]!=0) {
707 /* This tag has already been enqueued, use it to narrow search */
708 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
712 parameter->iterators[*iteratorcount].numtags=tagcount;
717 /* This function builds the iterators for a task & parameter */
719 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
720 int statusarray[MAXTASKPARAMS];
722 int numparams=task->numParameters;
724 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
726 statusarray[index]=1; /* Initial parameter */
727 /* Process tags for initial iterator */
729 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
733 /* Check for objects with existing tags */
734 for(i=0;i<numparams;i++) {
735 if (statusarray[i]==0) {
736 struct parameterdescriptor *pd=task->descriptorarray[i];
738 for(j=0;j<pd->numbertags;j++) {
739 int slotid=pd->tagarray[2*j];
740 if(statusarray[slotid+numparams]!=0) {
741 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
742 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
748 /* Nothing with a tag enqueued */
750 for(i=0;i<numparams;i++) {
751 if (statusarray[i]==0) {
752 struct parameterdescriptor *pd=task->descriptorarray[i];
753 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
754 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
767 /* This function processes the task information to create queues for
768 each parameter type. */
770 void processtasks() {
772 for(i=0;i<numtasks;i++) {
773 struct taskdescriptor * task=taskarray[i];
776 for(j=0;j<task->numParameters;j++) {
777 struct parameterdescriptor *param=task->descriptorarray[j];
778 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
779 struct parameterwrapper ** ptr=&objectqueues[param->type];
781 param->queue=parameter;
782 parameter->objectset=allocateRuntimeHash(10);
783 parameter->numberofterms=param->numberterms;
784 parameter->intarray=param->intarray;
785 parameter->numbertags=param->numbertags;
786 parameter->tagarray=param->tagarray;
787 parameter->task=task;
788 /* Link new queue in */
794 /* Build iterators for parameters */
795 for(j=0;j<task->numParameters;j++) {
796 struct parameterdescriptor *param=task->descriptorarray[j];
797 struct parameterwrapper *parameter=param->queue;
799 builditerators(task, j, parameter);
804 void toiReset(struct tagobjectiterator * it) {
807 } else if (it->numtags>0) {
810 RuntimeHashiterator(it->objectset, &it->it);
814 int toiHasNext(struct tagobjectiterator *it, void ** objectarray) {
817 /* Get object with tags */
818 struct ___Object___ *obj=objectarray[it->tagobjectslot];
819 struct ___Object___ *tagptr=obj->___tags___;
820 if (tagptr->type==TAGTYPE) {
821 if ((it->tagobjindex==0)&& /* First object */
822 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
827 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
828 int tagindex=it->tagobjindex;
829 for(;tagindex<ao->___cachedCode___;tagindex++) {
830 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
831 if (td->flag==it->tagid) {
832 it->tagobjindex=tagindex; /* Found right type of tag */
838 } else if (it->numtags>0) {
839 /* Use tags to locate appropriate objects */
840 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
841 struct ___Object___ *objptr=tag->___tagset___;
843 if (objptr->type!=OBJECTARRAYTYPE) {
844 if (it->tagobjindex>0)
846 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
848 for(i=1;i<it->numtags;i++) {
849 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
850 if (!containstag(objptr,tag2))
855 struct ArrayObject *ao=(struct ArrayObject *) objptr;
858 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
859 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
860 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
862 for(i=1;i<it->numtags;i++) {
863 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
864 if (!containstag(objptr,tag2))
871 it->tagobjindex=tagindex;
875 return RunhasNext(&it->it);
879 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
881 struct ___Object___ * objptr=tag->___tagset___;
882 if (objptr->type==OBJECTARRAYTYPE) {
883 struct ArrayObject *ao=(struct ArrayObject *)objptr;
884 for(j=0;j<ao->___cachedCode___;j++) {
885 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
893 void toiNext(struct tagobjectiterator *it , void ** objectarray) {
894 /* hasNext has all of the intelligence */
897 /* Get object with tags */
898 struct ___Object___ *obj=objectarray[it->tagobjectslot];
899 struct ___Object___ *tagptr=obj->___tags___;
900 if (tagptr->type==TAGTYPE) {
902 objectarray[it->slot]=tagptr;
904 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
905 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
907 } else if (it->numtags>0) {
908 /* Use tags to locate appropriate objects */
909 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
910 struct ___Object___ *objptr=tag->___tagset___;
911 if (objptr->type!=OBJECTARRAYTYPE) {
913 objectarray[it->slot]=objptr;
915 struct ArrayObject *ao=(struct ArrayObject *) objptr;
916 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
920 objectarray[it->slot]=(void *)Runkey(&it->it);
928 void exithandler(int sig, siginfo_t *info, void * uap) {
932 void initializeexithandler() {
933 struct sigaction sig;
934 sig.sa_sigaction=&exithandler;
935 sig.sa_flags=SA_SIGINFO;
936 sigemptyset(&sig.sa_mask);
937 sigaction(SIGUSR2, &sig, 0);
941 /* This function inject failures */
943 void injectinstructionfailure() {
945 if (injectinstructionfailures) {
948 instructioncount=failurecount;
949 instaccum+=failurecount;
950 if ((((double)random())/RAND_MAX)<instfailurechance) {
953 printf("FAILURE!!! %d\n",numfailures);
954 longjmp(error_handler,11);
959 if (injectinstructionfailures) {
962 instaccum+=failurecount;
963 if ((((double)random())/RAND_MAX)<instfailurechance) {
966 printf("FAILURE!!! %d\n",numfailures);
974 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
975 struct ArrayObject * chararray=VAR(___s___)->___value___;
977 int offset=VAR(___s___)->___offset___;
978 for(i=0;i<VAR(___s___)->___count___;i++) {
979 short sc=((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
984 /* Object allocation function */
987 void * allocate_new(void * ptr, int type) {
988 struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
998 /* Array allocation function */
1000 struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
1001 struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
1004 printf("ERROR: negative array\n");
1007 v->___length___=length;
1017 void * allocate_new(int type) {
1018 void * v=FREEMALLOC(classsize[type]);
1023 /* Array allocation function */
1025 struct ArrayObject * allocate_newarray(int type, int length) {
1026 struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
1028 v->___length___=length;
1034 /* Converts C character arrays into Java strings */
1036 struct ___String___ * NewString(void * ptr, const char *str,int length) {
1038 struct ___String___ * NewString(const char *str,int length) {
1042 struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
1043 int ptrarray[]={1, (int) ptr, (int) chararray};
1044 struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
1045 chararray=(struct ArrayObject *) ptrarray[2];
1047 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
1048 struct ___String___ * strobj=allocate_new(STRINGTYPE);
1050 strobj->___value___=chararray;
1051 strobj->___count___=length;
1052 strobj->___offset___=0;
1054 for(i=0;i<length;i++) {
1055 ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i]; }
1059 /* Generated code calls this if we fail a bounds check */
1061 void failedboundschk() {
1063 printf("Array out of bounds\n");
1070 longjmp(error_handler,2);
1074 /* Abort task call */
1077 longjmp(error_handler,4);
1079 printf("Aborting\n");