4 #include "multicoreruntime.h"
5 #include "methodheaders.h"
7 extern int classsize[];
8 extern int typearray[];
9 extern int typearray2[];
10 extern int* supertypes[];
13 extern struct genhashtable * activetasks;
21 int instanceofif(int otype, int type) {
28 int num = supertypes[otype][0];
29 for(int i = 1; i < num + 1; i++) {
30 int t = supertypes[otype][i];
31 if(instanceofif(t, type) == 1) {
38 int instanceof(struct ___Object___ *ptr, int type) {
43 if(instanceofif(i, type) == 1) {
50 i=typearray2[i-NUMCLASSES];
56 void initializeexithandler() {
59 /* This function inject failures */
60 void injectinstructionfailure() {
61 // not supported in MULTICORE version
65 #ifdef D___Double______nativeparsedouble____L___String___
66 double CALL01(___Double______nativeparsedouble____L___String___,
67 struct ___String___ * ___str___) {
68 int length=VAR(___str___)->___count___;
69 int maxlength=(length>60) ? 60 : length;
70 char str[maxlength+1];
71 struct ArrayObject * chararray=VAR(___str___)->___value___;
73 int offset=VAR(___str___)->___offset___;
74 for(i=0; i<maxlength; i++) {
76 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
79 double d=0.0; //atof(str); TODO Unimplemented nativeparsedoulbe
84 #ifdef D___Double______nativeparsedouble_____AR_B_I_I
85 double CALL23(___Double______nativeparsedouble_____AR_B_I_I,
90 struct ArrayObject * ___str___) {
91 int maxlength=(length>60)?60:length;
92 char str[maxlength+1];
93 struct ArrayObject * bytearray=VAR(___str___);
95 for(i=0; i<maxlength; i++) {
96 str[i]=(((char *)&bytearray->___length___)+sizeof(int))[i+start];
99 double d=0.0; //atof(str); TODO Unimplemented nativeparsedouble
104 typedef union jvalue {
114 #ifdef D___Double______doubleToRawLongBits____D
115 long long CALL11(___Double______doubleToRawLongBits____D,
117 double ___value___) {
121 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
122 /* On little endian ARM processors when using FPA, word order of
123 doubles is still big endian. So take that into account here. When
124 using VFP, word order of doubles follows byte order. */
125 #define SWAP_DOUBLE(a) (((a) << 32) | (((a) >> 32) & 0x00000000ffffffff))
126 val.j = SWAP_DOUBLE(val.j);
133 #ifdef D___Double______longBitsToDouble____J
134 double CALL11(___Double______longBitsToDouble____J,
135 long long ___bits___,
136 long long ___bits___) {
140 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
142 #define SWAP_DOUBLE(a) (((a) << 32) | (((a) >> 32) & 0x00000000ffffffff))
144 val.j = SWAP_DOUBLE(val.j);
151 #ifdef D___String______convertdoubletochar____D__AR_C
152 int CALL12(___String______convertdoubletochar____D__AR_C,
155 struct ArrayObject * ___chararray___) {
156 int length=VAR(___chararray___)->___length___;
159 int num=snprintf(str, length, "%f",___val___);
162 for(i=0; i<length; i++) {
163 ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=
169 int CALL12(___String______convertdoubletochar____D__AR_C,
172 struct ArrayObject ___chararray___) {
177 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
178 void deepArrayCopy(struct ___Object___ * dst,
179 struct ___Object___ * src) {
180 int dsttype=((int *)dst)[0];
181 int srctype=((int *)src)[0];
182 if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
184 struct ArrayObject *aodst=(struct ArrayObject *)dst;
185 struct ArrayObject *aosrc=(struct ArrayObject *)src;
186 int dstlength=aodst->___length___;
187 int srclength=aosrc->___length___;
188 if (dstlength!=srclength)
190 unsigned INTPTR *pointer=pointerarray[srctype];
192 int elementsize=classsize[srctype];
193 int size=srclength*elementsize;
195 memcpy(((char *)&aodst->___length___)+sizeof(int) ,
196 ((char *)&aosrc->___length___)+sizeof(int), size);
200 for(i=0;i<srclength;i++) {
201 struct ___Object___ * ptr=
202 ((struct ___Object___**)(((char*)&aosrc->___length___)+sizeof(int)))[i];
203 int ptrtype=((int *)ptr)[0];
204 if (ptrtype>=NUMCLASSES) {
205 struct ___Object___ * dstptr=((struct ___Object___**)
206 (((char*)&aodst->___length___)+sizeof(int)))[i];
207 deepArrayCopy(dstptr,ptr);
210 ((struct ___Object___ **)
211 (((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
217 void CALL02(___System______deepArrayCopy____L___Object____L___Object___,
218 struct ___Object___ * ___dst___,
219 struct ___Object___ * ___src___) {
220 deepArrayCopy(VAR(___dst___), VAR(___src___));
224 #ifdef D___System______arraycopy____L___Object____I_L___Object____I_I
225 void arraycopy(struct ___Object___ *src,
227 struct ___Object___ *dst,
230 int dsttype=((int *)dst)[0];
231 int srctype=((int *)src)[0];
233 //not an array or type mismatch
234 if (dsttype<NUMCLASSES||srctype<NUMCLASSES/*||srctype!=dsttype*/)
237 struct ArrayObject *aodst=(struct ArrayObject *)dst;
238 struct ArrayObject *aosrc=(struct ArrayObject *)src;
239 int dstlength=aodst->___length___;
240 int srclength=aosrc->___length___;
244 if (srcPos+length>srclength)
246 if (destPos+length>dstlength)
249 unsigned INTPTR *pointer=pointerarray[srctype];
251 int elementsize=classsize[srctype];
252 int size=length*elementsize;
254 memcpy(((char *)&aodst->___length___)+sizeof(int)+destPos*elementsize,
255 ((char *)&aosrc->___length___)+sizeof(int)+srcPos*elementsize, size);
259 for(i=0;i<length;i++) {
260 struct ___Object___ * ptr=((struct ___Object___**)
261 (((char*)&aosrc->___length___)+sizeof(int)))[i+srcPos];
262 int ptrtype=((int *)ptr)[0];
264 ((struct ___Object___ **)
265 (((char*) &aodst->___length___)+sizeof(int)))[i+destPos]=ptr;
270 void CALL35(___System______arraycopy____L___Object____I_L___Object____I_I,
274 struct ___Object___ * ___src___,
276 struct ___Object___ * ___dst___,
279 arraycopy(VAR(___src___), ___srcPos___, VAR(___dst___), ___destPos___,
284 void CALL11(___System______exit____I,
287 // gc_profile mode, output gc prfiling data
289 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
290 CACHEADAPT_DISABLE_TIMER();
291 GC_OUTPUT_PROFILE_DATA();
294 BAMBOO_EXIT_APP(___status___);
297 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
298 void CALL23(___Vector______removeElement_____AR_L___Object____I_I,
301 struct ArrayObject * ___array___,
304 char* offset=((char *)(&VAR(___array___)->___length___))
305 +sizeof(unsigned int)+sizeof(void *)*___index___;
306 memmove(offset, offset+sizeof(void *),
307 (___size___-___index___-1)*sizeof(void *));
311 void CALL11(___System______printI____I,
314 BAMBOO_PRINT(0x1111);
315 BAMBOO_PRINT_REG(___status___);
318 long long CALL00(___System______currentTimeMillis____) {
319 //TilePro64 is 700mHz
320 return ((unsigned long long)BAMBOO_GET_EXE_TIME())/700000;
323 void CALL00(___System______setgcprofileflag____) {
326 extern volatile bool gc_profile_flag;
327 gc_profile_flag = true;
332 void CALL00(___System______resetgcprofileflag____) {
335 extern volatile bool gc_profile_flag;
336 gc_profile_flag = false;
341 void CALL01(___System______printString____L___String___,
342 struct ___String___ * ___s___) {
345 struct ArrayObject * chararray=VAR(___s___)->___value___;
347 int offset=VAR(___s___)->___offset___;
349 for(i=0; i<VAR(___s___)->___count___; i++) {
351 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
358 /* Object allocation function */
361 void * allocate_new(void * ptr,
363 struct ___Object___ * v=
364 (struct ___Object___*)FREEMALLOC((struct garbagelist*) ptr,classsize[type]);
373 extern unsigned int gc_num_obj;
379 /* Array allocation function */
381 struct ArrayObject * allocate_newarray(void * ptr,
384 struct ArrayObject * v=(struct ArrayObject *)FREEMALLOC(
385 (struct garbagelist*)ptr,
386 sizeof(struct ArrayObject)+length*classsize[type]);
395 v->___length___=length;
398 extern unsigned int gc_num_obj;
405 void * allocate_new(int type) {
406 struct ___Object___ * v=FREEMALLOC(classsize[type]);
416 /* Array allocation function */
418 struct ArrayObject * allocate_newarray(int type,
420 struct ArrayObject * v=FREEMALLOC(
421 sizeof(struct ArrayObject)+length*classsize[type]);
427 v->___length___=length;
433 /* Converts C character arrays into Java strings */
435 __attribute__((malloc)) struct ___String___ * NewStringShort(void * ptr,
439 __attribute__((malloc)) struct ___String___ * NewStringShort(const short *str,
444 struct ArrayObject * chararray=
445 allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
446 INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
447 struct ___String___ * strobj=
448 allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
449 chararray=(struct ArrayObject *) ptrarray[2];
451 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
452 struct ___String___ * strobj=allocate_new(STRINGTYPE);
454 strobj->___value___=chararray;
455 strobj->___count___=length;
456 strobj->___offset___=0;
458 for(i=0; i<length; i++) {
459 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=str[i];
464 /* Converts C character arrays into Java strings */
466 struct ___String___ * NewString(void * ptr,
470 struct ___String___ * NewString(const char *str,
475 struct ArrayObject * chararray=
476 allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
477 int ptrarray[]={1, (int) ptr, (int) 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]=(short)str[i];
495 /* Generated code calls this if we fail a bounds check */
497 void failedboundschk(int num) {
499 printf("Array out of bounds\n");
509 printf("Array out of bounds\n");
510 longjmp(error_handler,2);
517 /* Generated code calls this if we fail null ptr chk */
518 void failednullptr(void * ptr) {
521 //print out current stack
524 struct garbagelist * stackptr = (struct garbagelist *)ptr;
525 while(stackptr!=NULL) {
526 tprintf("Stack %d: \n\t", j);
527 for(i=0; i<stackptr->size; i++) {
528 if(stackptr->array[i] != NULL) {
529 tprintf("%x, ", stackptr->array[i]);
535 stackptr=stackptr->next;
540 printf("NULL ptr\n");
550 printf("NULL ptr\n");
551 longjmp(error_handler,2);
558 /* Abort task call */
562 printf("Aborting\n");
563 longjmp(error_handler,4);
566 printf("Aborting\n");
571 INLINE void initruntimedata() {
573 // initialize the arrays
574 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
575 // startup core to initialize corestatus[]
576 for(i = 0; i < NUMCORESACTIVE; ++i) {
579 numreceiveobjs[i] = 0;
586 self_numsendobjs = 0;
587 self_numreceiveobjs = 0;
589 for(i = 0; i < BAMBOO_MSG_BUF_LENGTH; ++i) {
594 //msglength = BAMBOO_MSG_BUF_LENGTH;
596 for(i = 0; i < BAMBOO_OUT_BUF_LENGTH; ++i) {
602 isMsgHanging = false;
605 bamboo_cur_msp = NULL;
606 bamboo_smem_size = 0;
611 INITMULTICOREGCDATA();
615 bamboo_current_thread = 0;
621 INLINE void disruntimedata() {
622 DISMULTICOREGCDATA();
624 BAMBOO_LOCAL_MEM_CLOSE();
625 BAMBOO_SHARE_MEM_CLOSE();
628 INLINE void recordtotalexetime() {
630 totalexetime = BAMBOO_GET_EXE_TIME()-bamboo_start_time;
632 BAMBOO_PRINT(BAMBOO_GET_EXE_TIME()-bamboo_start_time);
634 BAMBOO_PRINT_REG(gc_num_flush_dtlb);
636 #ifndef BAMBOO_MEMPROF
637 BAMBOO_PRINT(0xbbbbbbbb);
642 INLINE void getprofiledata_I() {
643 //profile mode, send msgs to other cores to request pouring out progiling data
645 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
646 for(i = 1; i < NUMCORESACTIVE; ++i) {
647 // send profile request msg to core i
648 send_msg_2(i, PROFILEOUTPUT, totalexetime);
651 // pour profiling data on startup core
655 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
656 profilestatus[BAMBOO_NUM_OF_CORE] = 0;
657 // check the status of all cores
658 for(i = 0; i < NUMCORESACTIVE; ++i) {
659 if(profilestatus[i] != 0) {
663 if(i != NUMCORESACTIVE) {
665 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
669 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
676 INLINE void checkCoreStatus() {
680 (waitconfirm && (numconfirm == 0))) {
681 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
682 corestatus[BAMBOO_NUM_OF_CORE] = 0;
683 numsendobjs[BAMBOO_NUM_OF_CORE] = self_numsendobjs;
684 numreceiveobjs[BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
685 // check the status of all cores
686 for(i = 0; i < NUMCORESACTIVE; ++i) {
687 if(corestatus[i] != 0) {
691 if(i == NUMCORESACTIVE) {
692 // check if the sum of send objs and receive obj are the same
693 // yes->check if the info is the latest; no->go on executing
695 for(i = 0; i < NUMCORESACTIVE; ++i) {
696 sumsendobj += numsendobjs[i];
698 for(i = 0; i < NUMCORESACTIVE; ++i) {
699 sumsendobj -= numreceiveobjs[i];
701 if(0 == sumsendobj) {
703 // the first time found all cores stall
704 // send out status confirm msg to all other cores
705 // reset the corestatus array too
706 corestatus[BAMBOO_NUM_OF_CORE] = 1;
708 numconfirm = NUMCORESACTIVE - 1;
709 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
710 for(i = 1; i < NUMCORESACTIVE; ++i) {
712 // send status confirm msg to core i
713 send_msg_1(i, STATUSCONFIRM);
717 // all the core status info are the latest
718 // terminate; for profiling mode, send request to all
719 // other cores to pour out profiling data
720 recordtotalexetime();
722 CACHEADAPT_DISABLE_TIMER();
723 GC_OUTPUT_PROFILE_DATA();
725 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
726 terminate(); // All done.
729 // still some objects on the fly on the network
730 // reset the waitconfirm and numconfirm
735 // not all cores are stall, keep on waiting
739 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
743 // main function for each core
744 inline void run(int argc, char** argv) {
746 bool sendStall = false;
748 bool tocontinue = false;
750 corenum = BAMBOO_GET_NUM_OF_CORE();
752 // initialize runtime data structures
755 // other architecture related initialization
759 CACHEADAPT_ENABLE_TIMER();
761 initializeexithandler();
763 // main process of the execution module
764 if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
766 // non-executing cores, only processing communications
772 /* Create queue of active tasks */
773 activetasks= genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
774 (int (*)(void *,void *)) &comparetpd);
776 /* Process task information */
779 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
780 /* Create startup object */
781 createstartupobject(argc, argv);
785 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
787 // run the initStaticAndGlobal method to initialize the static blocks and
789 initStaticAndGlobal();
791 // run the main method in the specified mainclass
792 mgc_main(argc, argv);
799 // check if there are new active tasks can be executed
805 while(receiveObject_I() != -1) {
808 // check if there are some pending objects,
809 // if yes, enqueue them and executetasks again
810 tocontinue = checkObjQueue();
812 tocontinue = trystartthread();
820 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
831 // wait for some time
837 // send StallMsg to startup core
839 send_msg_4(STARTUPCORE,TRANSTALL,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);