3 #include "multicoreruntime.h"
4 #include "methodheaders.h"
5 #include "multicoregarbage.h"
6 #include "multicore_arch.h"
9 extern int classsize[];
10 extern int typearray[];
11 extern int typearray2[];
12 extern int* supertypes[];
15 extern struct genhashtable * activetasks;
22 int instanceofif(int otype, int type) {
29 int num = supertypes[otype][0];
30 for(int i = 1; i < num + 1; i++) {
31 int t = supertypes[otype][i];
32 if(instanceofif(t, type) == 1) {
39 int instanceof(struct ___Object___ *ptr, int type) {
44 if(instanceofif(i, type) == 1) {
51 i=typearray2[i-NUMCLASSES];
57 void initializeexithandler() {
60 /* This function inject failures */
61 void injectinstructionfailure() {
62 // not supported in MULTICORE version
66 #ifdef D___Double______nativeparsedouble____L___String___
67 double CALL01(___Double______nativeparsedouble____L___String___,
68 struct ___String___ * ___str___) {
69 int length=VAR(___str___)->___count___;
70 int maxlength=(length>60) ? 60 : length;
71 char str[maxlength+1];
72 struct ArrayObject * chararray=VAR(___str___)->___value___;
74 int offset=VAR(___str___)->___offset___;
75 for(i=0; i<maxlength; i++) {
77 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
80 double d=0.0; //atof(str); TODO Unimplemented nativeparsedoulbe
85 #ifdef D___Double______nativeparsedouble_____AR_B_I_I
86 double CALL23(___Double______nativeparsedouble_____AR_B_I_I,
91 struct ArrayObject * ___str___) {
92 int maxlength=(length>60)?60:length;
93 char str[maxlength+1];
94 struct ArrayObject * bytearray=VAR(___str___);
96 for(i=0; i<maxlength; i++) {
97 str[i]=(((char *)&bytearray->___length___)+sizeof(int))[i+start];
100 double d=0.0; //atof(str); TODO Unimplemented nativeparsedouble
105 typedef union jvalue {
115 #ifdef D___Double______doubleToRawLongBits____D
116 long long CALL11(___Double______doubleToRawLongBits____D,
118 double ___value___) {
122 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
123 /* On little endian ARM processors when using FPA, word order of
124 doubles is still big endian. So take that into account here. When
125 using VFP, word order of doubles follows byte order. */
126 #define SWAP_DOUBLE(a) (((a) << 32) | (((a) >> 32) & 0x00000000ffffffff))
127 val.j = SWAP_DOUBLE(val.j);
134 #ifdef D___Double______longBitsToDouble____J
135 double CALL11(___Double______longBitsToDouble____J,
136 long long ___bits___,
137 long long ___bits___) {
141 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
143 #define SWAP_DOUBLE(a) (((a) << 32) | (((a) >> 32) & 0x00000000ffffffff))
145 val.j = SWAP_DOUBLE(val.j);
152 #ifdef D___String______convertdoubletochar____D__AR_C
153 int CALL12(___String______convertdoubletochar____D__AR_C,
156 struct ArrayObject * ___chararray___) {
157 int length=VAR(___chararray___)->___length___;
160 int num=snprintf(str, length, "%f",___val___);
163 for(i=0; i<length; i++) {
164 ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=
171 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
172 void deepArrayCopy(struct ___Object___ * dst,
173 struct ___Object___ * src) {
174 int dsttype=((int *)dst)[0];
175 int srctype=((int *)src)[0];
176 if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
178 struct ArrayObject *aodst=(struct ArrayObject *)dst;
179 struct ArrayObject *aosrc=(struct ArrayObject *)src;
180 int dstlength=aodst->___length___;
181 int srclength=aosrc->___length___;
182 if (dstlength!=srclength)
184 unsigned INTPTR *pointer=pointerarray[srctype];
186 int elementsize=classsize[srctype];
187 int size=srclength*elementsize;
189 memcpy(((char *)&aodst->___length___)+sizeof(int) ,
190 ((char *)&aosrc->___length___)+sizeof(int), size);
194 for(i=0;i<srclength;i++) {
195 struct ___Object___ * ptr=
196 ((struct ___Object___**)(((char*)&aosrc->___length___)+sizeof(int)))[i];
197 int ptrtype=((int *)ptr)[0];
198 if (ptrtype>=NUMCLASSES) {
199 struct ___Object___ * dstptr=((struct ___Object___**)
200 (((char*)&aodst->___length___)+sizeof(int)))[i];
201 deepArrayCopy(dstptr,ptr);
204 ((struct ___Object___ **)
205 (((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
211 void CALL02(___System______deepArrayCopy____L___Object____L___Object___,
212 struct ___Object___ * ___dst___,
213 struct ___Object___ * ___src___) {
214 deepArrayCopy(VAR(___dst___), VAR(___src___));
218 #ifdef D___System______arraycopy____L___Object____I_L___Object____I_I
219 void arraycopy(struct ___Object___ *src,
221 struct ___Object___ *dst,
224 int dsttype=((int *)dst)[0];
225 int srctype=((int *)src)[0];
227 //not an array or type mismatch
228 if (dsttype<NUMCLASSES||srctype<NUMCLASSES/*||srctype!=dsttype*/)
231 struct ArrayObject *aodst=(struct ArrayObject *)dst;
232 struct ArrayObject *aosrc=(struct ArrayObject *)src;
233 int dstlength=aodst->___length___;
234 int srclength=aosrc->___length___;
238 if (srcPos+length>srclength)
240 if (destPos+length>dstlength)
243 unsigned INTPTR *pointer=pointerarray[srctype];
245 int elementsize=classsize[srctype];
246 int size=length*elementsize;
248 memcpy(((char *)&aodst->___length___)+sizeof(int)+destPos*elementsize,
249 ((char *)&aosrc->___length___)+sizeof(int)+srcPos*elementsize, size);
253 for(i=0;i<length;i++) {
254 struct ___Object___ * ptr=((struct ___Object___**)
255 (((char*)&aosrc->___length___)+sizeof(int)))[i+srcPos];
256 int ptrtype=((int *)ptr)[0];
258 ((struct ___Object___ **)
259 (((char*) &aodst->___length___)+sizeof(int)))[i+destPos]=ptr;
264 void CALL35(___System______arraycopy____L___Object____I_L___Object____I_I,
268 struct ___Object___ * ___src___,
270 struct ___Object___ * ___dst___,
273 arraycopy(VAR(___src___), ___srcPos___, VAR(___dst___), ___destPos___,
278 #ifdef D___System______exit____I
279 void CALL11(___System______exit____I,
282 // gc_profile mode, output gc prfiling data
284 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
285 BAMBOO_PRINT(BAMBOO_GET_EXE_TIME());
286 BAMBOO_PRINT(0xbbbbbbbb);
287 CACHEADAPT_DISABLE_TIMER();
288 GC_OUTPUT_PROFILE_DATA();
291 BAMBOO_EXIT_APP(___status___);
295 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
296 void CALL23(___Vector______removeElement_____AR_L___Object____I_I,
299 struct ArrayObject * ___array___,
302 char* offset=((char *)(&VAR(___array___)->___length___))
303 +sizeof(unsigned int)+sizeof(void *)*___index___;
304 memmove(offset, offset+sizeof(void *),
305 (___size___-___index___-1)*sizeof(void *));
309 #ifdef D___System______printI____I
310 void CALL11(___System______printI____I,
313 BAMBOO_PRINT(0x1111);
314 BAMBOO_PRINT_REG(___status___);
318 #ifdef D___System______currentTimeMillis____
319 long long CALL00(___System______currentTimeMillis____) {
320 //TilePro64 is 700mHz
321 return ((unsigned long long)BAMBOO_GET_EXE_TIME())/700000;
325 #ifdef D___System______setgcprofileflag____
326 void CALL00(___System______setgcprofileflag____) {
329 extern volatile bool gc_profile_flag;
330 gc_profile_flag = true;
336 #ifdef D___System______resetgcprofileflag____
337 void CALL00(___System______resetgcprofileflag____) {
340 extern volatile bool gc_profile_flag;
341 gc_profile_flag = false;
347 #ifdef D___System______gc____
348 void CALL00(___System______gc____) {
350 if(BAMBOO_NUM_OF_CORE == STARTUPCORE) {
351 if(!gc_status_info.gcprocessing && !gcflag) {
354 for(int i = 0; i < NUMCORESACTIVE; i++) {
355 // reuse the gcnumsendobjs & gcnumreceiveobjs
356 gcnumsendobjs[0][i] = 0;
357 gcnumreceiveobjs[0][i] = 0;
359 for(int i = 0; i < NUMCORES4GC; i++) {
360 if(i != STARTUPCORE) {
361 send_msg_1_I(i,GCSTARTPRE);
366 // send msg to the startup core to start gc
367 send_msg_1(STARTUPCORE, GCINVOKE);
373 #ifdef D___System______printString____L___String___
374 void CALL01(___System______printString____L___String___, struct ___String___ * ___s___) {
375 #if defined(MGC)&&defined(TILERA_BME)
376 struct ArrayObject * chararray=VAR(___s___)->___value___;
378 int offset=VAR(___s___)->___offset___;
380 for(i=0; i<VAR(___s___)->___count___; i++) {
382 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
389 /* Object allocation function */
392 void * allocate_new(void * ptr,
394 struct ___Object___ * v=
395 (struct ___Object___*)FREEMALLOC((struct garbagelist*) ptr,classsize[type]);
404 extern unsigned int gc_num_obj;
410 /* Array allocation function */
412 struct ArrayObject * allocate_newarray(void * ptr,
415 struct ArrayObject * v=(struct ArrayObject *)FREEMALLOC(
416 (struct garbagelist*)ptr,
417 sizeof(struct ArrayObject)+length*classsize[type]);
426 v->___length___=length;
427 initlock((struct ___Object___ *)v);
429 extern unsigned int gc_num_obj;
436 void * allocate_new(int type) {
437 struct ___Object___ * v=FREEMALLOC(classsize[type]);
447 /* Array allocation function */
449 struct ArrayObject * allocate_newarray(int type,
451 struct ArrayObject * v=FREEMALLOC(
452 sizeof(struct ArrayObject)+length*classsize[type]);
458 v->___length___=length;
459 initlock((struct ___Object___ *) v);
464 /* Converts C character arrays into Java strings */
466 __attribute__((malloc)) struct ___String___ * NewStringShort(void * ptr,
470 __attribute__((malloc)) struct ___String___ * NewStringShort(const short *str,
475 struct ArrayObject * chararray=
476 allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
477 INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
478 struct ___String___ * strobj=
479 allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
480 chararray=(struct ArrayObject *) ptrarray[2];
482 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
483 struct ___String___ * strobj=allocate_new(STRINGTYPE);
485 strobj->___value___=chararray;
486 strobj->___count___=length;
487 strobj->___offset___=0;
489 for(i=0; i<length; i++) {
490 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=str[i];
495 /* Converts C character arrays into Java strings */
497 struct ___String___ * NewString(void * ptr,
501 struct ___String___ * NewString(const char *str,
506 struct ArrayObject * chararray=
507 allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
508 int ptrarray[]={1, (int) ptr, (int) chararray};
509 struct ___String___ * strobj=
510 allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
511 chararray=(struct ArrayObject *) ptrarray[2];
513 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
514 struct ___String___ * strobj=allocate_new(STRINGTYPE);
516 strobj->___value___=chararray;
517 strobj->___count___=length;
518 strobj->___offset___=0;
520 for(i=0; i<length; i++) {
521 ((short*)(((char*)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
526 /* Generated code calls this if we fail a bounds check */
528 void failedboundschk(int num, int index, struct ArrayObject * ao) {
530 printf("Array out of bounds at line %u with index %u of object %x with lengt\
531 h %u\n", num, index, ao, ao->___length___);
541 printf("Array out of bounds\n");
542 longjmp(error_handler,2);
549 /* Generated code calls this if we fail null ptr chk */
550 void failednullptr(void * ptr) {
553 //print out current stack
556 struct garbagelist * stackptr = (struct garbagelist *)ptr;
557 while(stackptr!=NULL) {
558 tprintf("Stack %d: \n\t", j);
559 for(i=0; i<stackptr->size; i++) {
560 if(stackptr->array[i] != NULL) {
561 tprintf("%x, ", stackptr->array[i]);
567 stackptr=stackptr->next;
572 printf("NULL ptr\n");
582 printf("NULL ptr\n");
583 longjmp(error_handler,2);
590 /* Abort task call */
594 printf("Aborting\n");
595 longjmp(error_handler,4);
598 printf("Aborting\n");
603 void initruntimedata() {
604 // initialize the arrays
605 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
606 // startup core to initialize corestatus[]
607 for(int i = 0; i < NUMCORESACTIVE; ++i) {
610 numreceiveobjs[i] = 0;
617 self_numsendobjs = 0;
618 self_numreceiveobjs = 0;
620 for(int i = 0; i < BAMBOO_MSG_BUF_LENGTH; ++i) {
625 //msglength = BAMBOO_MSG_BUF_LENGTH;
627 for(int i = 0; i < BAMBOO_OUT_BUF_LENGTH; ++i) {
633 isMsgHanging = false;
639 bamboo_cur_msp = NULL;
640 bamboo_smem_size = 0;
645 INITMULTICOREGCDATA();
649 bamboo_current_thread = NULL;
655 void disruntimedata() {
656 DISMULTICOREGCDATA();
658 BAMBOO_LOCAL_MEM_CLOSE();
659 BAMBOO_SHARE_MEM_CLOSE();
662 void recordtotalexetime() {
664 totalexetime = BAMBOO_GET_EXE_TIME()-bamboo_start_time;
666 unsigned long long timediff=BAMBOO_GET_EXE_TIME()-bamboo_start_time;
667 BAMBOO_PRINT(timediff);
668 #ifndef BAMBOO_MEMPROF
669 BAMBOO_PRINT(0xbbbbbbbb);
674 void getprofiledata_I() {
675 //profile mode, send msgs to other cores to request pouring out progiling data
677 // use numconfirm to check if all cores have finished output task profiling
678 // information. This is safe as when the execution reaches this phase there
679 // should have no other msgs except the PROFILEFINISH msg, there should be
681 numconfirm=NUMCORESACTIVE-1;
682 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
683 for(i = 1; i < NUMCORESACTIVE; ++i) {
684 // send profile request msg to core i
685 send_msg_2(i, PROFILEOUTPUT, totalexetime);
688 // pour profiling data on startup core
692 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
693 if(numconfirm != 0) {
695 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
699 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
706 void checkCoreStatus() {
710 (waitconfirm && (numconfirm == 0))) {
711 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
712 corestatus[BAMBOO_NUM_OF_CORE] = 0;
713 numsendobjs[BAMBOO_NUM_OF_CORE] = self_numsendobjs;
714 numreceiveobjs[BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
715 // check the status of all cores
716 for(i = 0; i < NUMCORESACTIVE; ++i) {
717 if(corestatus[i] != 0) {
721 if(i == NUMCORESACTIVE) {
722 // check if the sum of send objs and receive obj are the same
723 // yes->check if the info is the latest; no->go on executing
725 for(i = 0; i < NUMCORESACTIVE; ++i) {
726 sumsendobj += numsendobjs[i];
728 for(i = 0; i < NUMCORESACTIVE; ++i) {
729 sumsendobj -= numreceiveobjs[i];
731 if(0 == sumsendobj) {
733 // the first time found all cores stall
734 // send out status confirm msg to all other cores
735 // reset the corestatus array too
736 corestatus[BAMBOO_NUM_OF_CORE] = 1;
738 numconfirm = NUMCORESACTIVE - 1;
739 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
740 for(i = 1; i < NUMCORESACTIVE; ++i) {
742 // send status confirm msg to core i
743 send_msg_1(i, STATUSCONFIRM);
747 // all the core status info are the latest
748 // terminate; for profiling mode, send request to all
749 // other cores to pour out profiling data
750 recordtotalexetime();
752 CACHEADAPT_DISABLE_TIMER();
753 GC_OUTPUT_PROFILE_DATA();
755 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
756 terminate(); // All done.
759 // still some objects on the fly on the network
760 // reset the waitconfirm and numconfirm
765 // not all cores are stall, keep on waiting
769 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
773 // main function for each core
774 void run(int argc, char** argv) {
775 bool sendStall = false;
777 bool tocontinue = false;
779 corenum = BAMBOO_GET_NUM_OF_CORE();
780 // initialize runtime data structures
783 if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
784 numconfirm=NUMCORES-1;
785 for(int i=0;i<NUMCORES;i++) {
786 if (i!=STARTUPCORE) {
787 send_msg_1(i,REQNOTIFYSTART);
792 tprintf("start! \n");
793 bamboo_start_time = BAMBOO_GET_EXE_TIME();
799 CACHEADAPT_ENABLE_TIMER();
801 initializeexithandler();
803 // main process of the execution module
804 if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
806 // non-executing cores, only processing communications
812 /* Create queue of active tasks */
813 activetasks= genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
814 (int (*)(void *,void *)) &comparetpd);
816 /* Process task information */
819 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
820 /* Create startup object */
821 createstartupobject(argc, argv);
825 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
827 // run the initStaticAndGlobal method to initialize the static blocks and
829 initStaticAndGlobal();
831 // run the main method in the specified mainclass
832 mgc_main(argc, argv);
839 // check if there are new active tasks can be executed
845 while(receiveObject_I() != -1) {
848 // check if there are some pending objects,
849 // if yes, enqueue them and executetasks again
850 tocontinue = checkObjQueue();
852 tocontinue = trystartthread();
860 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
871 // wait for some time
877 // send StallMsg to startup core
879 send_msg_4(STARTUPCORE,TRANSTALL,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);