4 #include "multicoreruntime.h"
5 #include "methodheaders.h"
6 #include "multicoregarbage.h"
8 extern int classsize[];
9 extern int typearray[];
10 extern int typearray2[];
11 extern int* supertypes[];
14 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);
633 #ifndef BAMBOO_MEMPROF
634 BAMBOO_PRINT(0xbbbbbbbb);
639 INLINE void getprofiledata_I() {
640 //profile mode, send msgs to other cores to request pouring out progiling data
642 // use numconfirm to check if all cores have finished output task profiling
643 // information. This is safe as when the execution reaches this phase there
644 // should have no other msgs except the PROFILEFINISH msg, there should be
646 numconfirm=NUMCORESACTIVE-1;
647 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
648 for(i = 1; i < NUMCORESACTIVE; ++i) {
649 // send profile request msg to core i
650 send_msg_2(i, PROFILEOUTPUT, totalexetime);
653 // pour profiling data on startup core
657 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
658 if(numconfirm != 0) {
660 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
664 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
671 INLINE void checkCoreStatus() {
675 (waitconfirm && (numconfirm == 0))) {
676 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
677 corestatus[BAMBOO_NUM_OF_CORE] = 0;
678 numsendobjs[BAMBOO_NUM_OF_CORE] = self_numsendobjs;
679 numreceiveobjs[BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
680 // check the status of all cores
681 for(i = 0; i < NUMCORESACTIVE; ++i) {
682 if(corestatus[i] != 0) {
686 if(i == NUMCORESACTIVE) {
687 // check if the sum of send objs and receive obj are the same
688 // yes->check if the info is the latest; no->go on executing
690 for(i = 0; i < NUMCORESACTIVE; ++i) {
691 sumsendobj += numsendobjs[i];
693 for(i = 0; i < NUMCORESACTIVE; ++i) {
694 sumsendobj -= numreceiveobjs[i];
696 if(0 == sumsendobj) {
698 // the first time found all cores stall
699 // send out status confirm msg to all other cores
700 // reset the corestatus array too
701 corestatus[BAMBOO_NUM_OF_CORE] = 1;
703 numconfirm = NUMCORESACTIVE - 1;
704 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
705 for(i = 1; i < NUMCORESACTIVE; ++i) {
707 // send status confirm msg to core i
708 send_msg_1(i, STATUSCONFIRM);
712 // all the core status info are the latest
713 // terminate; for profiling mode, send request to all
714 // other cores to pour out profiling data
715 recordtotalexetime();
717 CACHEADAPT_DISABLE_TIMER();
718 GC_OUTPUT_PROFILE_DATA();
720 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
721 terminate(); // All done.
724 // still some objects on the fly on the network
725 // reset the waitconfirm and numconfirm
730 // not all cores are stall, keep on waiting
734 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
738 // main function for each core
739 inline void run(int argc, char** argv) {
741 bool sendStall = false;
743 bool tocontinue = false;
745 corenum = BAMBOO_GET_NUM_OF_CORE();
747 // initialize runtime data structures
750 // other architecture related initialization
754 CACHEADAPT_ENABLE_TIMER();
756 initializeexithandler();
758 // main process of the execution module
759 if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
761 // non-executing cores, only processing communications
767 /* Create queue of active tasks */
768 activetasks= genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
769 (int (*)(void *,void *)) &comparetpd);
771 /* Process task information */
774 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
775 /* Create startup object */
776 createstartupobject(argc, argv);
780 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
782 // run the initStaticAndGlobal method to initialize the static blocks and
784 initStaticAndGlobal();
786 // run the main method in the specified mainclass
787 mgc_main(argc, argv);
794 // check if there are new active tasks can be executed
800 while(receiveObject_I() != -1) {
803 // check if there are some pending objects,
804 // if yes, enqueue them and executetasks again
805 tocontinue = checkObjQueue();
807 tocontinue = trystartthread();
815 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
826 // wait for some time
832 // send StallMsg to startup core
834 send_msg_4(STARTUPCORE,TRANSTALL,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);