Synchronized block is now working. A simple test is passed.
[IRC.git] / Robust / src / Runtime / bamboo / multicoreruntime.c
1 #ifdef MULTICORE
2
3 #include "runtime.h"
4 #include "multicoreruntime.h"
5 #include "runtime_arch.h"
6 #include "GenericHashtable.h"
7 #include "structdefs.h"
8 #include "mem.h"
9 #ifndef RAW
10 #include <stdio.h>
11 #endif
12
13 #ifndef INLINE
14 #define INLINE    inline __attribute__((always_inline))
15 #endif // #ifndef INLINE
16
17 extern int classsize[];
18 extern int typearray[];
19 extern int typearray2[];
20
21 #ifdef TASK
22 extern struct genhashtable * activetasks;
23 #endif
24 #ifdef MULTICORE_GC
25 #ifdef SMEMM
26 extern unsigned int gcmem_mixed_threshold;
27 extern unsigned int gcmem_mixed_usedmem;
28 #endif // SMEMM
29 #endif // MULTICORE_GC
30
31 int debugtask=0;
32 #ifdef MGC
33 int corenum = 0;
34 #endif
35
36 int instanceof(struct ___Object___ *ptr, int type) {
37   int i=ptr->type;
38   do {
39     if (i==type)
40       return 1;
41     i=typearray[i];
42   } while(i!=-1);
43   i=ptr->type;
44   if (i>NUMCLASSES) {
45     do {
46       if (i==type)
47         return 1;
48       i=typearray2[i-NUMCLASSES];
49     } while(i!=-1);
50   }
51   return 0;
52 }
53
54 void initializeexithandler() {
55 }
56
57 /* This function inject failures */
58
59 void injectinstructionfailure() {
60   // not supported in MULTICORE version
61   return;
62 }
63
64 #ifdef D___Double______nativeparsedouble____L___String___
65 double CALL01(___Double______nativeparsedouble____L___String___,struct ___String___ * ___str___) {
66   int length=VAR(___str___)->___count___;
67   int maxlength=(length>60) ? 60 : length;
68   char str[maxlength+1];
69   struct ArrayObject * chararray=VAR(___str___)->___value___;
70   int i;
71   int offset=VAR(___str___)->___offset___;
72   for(i=0; i<maxlength; i++) {
73     str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
74   }
75   str[i]=0;
76   double d=atof(str);
77   return d;
78 }
79 #endif
80
81 #ifdef D___String______convertdoubletochar____D__AR_C
82 int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
83   int length=VAR(___chararray___)->___length___;
84   char str[length];
85   int i;
86   int num=snprintf(str, length, "%f",___val___);
87   if (num>=length)
88     num=length-1;
89   for(i=0; i<length; i++) {
90     ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
91   }
92   return num;
93 }
94 #else
95 int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
96   return 0;
97 }
98 #endif
99
100 void CALL11(___System______exit____I,int ___status___, int ___status___) {
101   BAMBOO_EXIT(___status___);
102 }
103
104 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
105   char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
106   memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
107 }
108
109 void CALL11(___System______printI____I,int ___status___, int ___status___) {
110   BAMBOO_DEBUGPRINT(0x1111);
111   BAMBOO_DEBUGPRINT_REG(___status___);
112 }
113
114 long CALL00(___System______currentTimeMillis____) {
115   // not supported in MULTICORE version
116   return -1;
117 }
118
119 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
120 #ifdef MGC
121 #ifdef TILERA_BME
122   struct ArrayObject * chararray=VAR(___s___)->___value___;
123   int i;
124   int offset=VAR(___s___)->___offset___;
125   tprintf("");
126   for(i=0; i<VAR(___s___)->___count___; i++) {
127         short sc=
128           ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
129     printf("%c", sc);
130   }
131 #endif // TILERA_BME
132 #endif // MGC
133 }
134
135 /* Object allocation function */
136
137 #ifdef MULTICORE_GC
138 void * allocate_new(void * ptr, int type) {
139   struct ___Object___ * v=
140         (struct ___Object___*)FREEMALLOC((struct garbagelist*) ptr,classsize[type]);
141   v->type=type;
142 #ifdef TASK
143   v->version = 0;
144   v->lock = NULL;
145   v->lockcount = 0;
146 #endif
147   initlock(v);
148 #ifdef GC_PROFILE
149   extern unsigned int gc_num_obj;
150   gc_num_obj++;
151 #endif
152   return v;
153 }
154
155 /* Array allocation function */
156
157 struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
158   struct ArrayObject * v=(struct ArrayObject *)
159         FREEMALLOC((struct garbagelist*)ptr,
160                 sizeof(struct ArrayObject)+length*classsize[type]);
161   v->type=type;
162 #ifdef TASK
163   v->version = 0;
164   v->lock = NULL;
165 #endif
166   if (length<0) {
167     return NULL;
168   }
169   v->___length___=length;
170   initlock(v);
171 #ifdef GC_PROFILE
172   extern unsigned int gc_num_obj;
173   gc_num_obj++;
174 #endif
175   return v;
176 }
177
178 #else
179 void * allocate_new(int type) {
180   struct ___Object___ * v=FREEMALLOC(classsize[type]);
181   v->type=type;
182 #ifdef TASK
183   v->version = 0;
184   v->lock = NULL;
185 #endif
186   initlock(v);
187   return v;
188 }
189
190 /* Array allocation function */
191
192 struct ArrayObject * allocate_newarray(int type, int length) {
193   struct ArrayObject * v=
194         FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
195   v->type=type;
196 #ifdef TASK
197   v->version = 0;
198   v->lock = NULL;
199 #endif
200   v->___length___=length;
201   initlock(v);
202   return v;
203 }
204 #endif
205
206
207 /* Converts C character arrays into Java strings */
208 #ifdef MULTICORE_GC
209 struct ___String___ * NewString(void * ptr, const char *str,int length) {
210 #else
211 struct ___String___ * NewString(const char *str,int length) {
212 #endif
213   int i;
214 #ifdef MULTICORE_GC
215   struct ArrayObject * chararray=
216         allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
217   int ptrarray[]={1, (int) ptr, (int) chararray};
218   struct ___String___ * strobj=
219         allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
220   chararray=(struct ArrayObject *) ptrarray[2];
221 #else
222   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
223   struct ___String___ * strobj=allocate_new(STRINGTYPE);
224 #endif
225   strobj->___value___=chararray;
226   strobj->___count___=length;
227   strobj->___offset___=0;
228
229   for(i=0; i<length; i++) {
230     ((short*)(((char*)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
231   }
232   return strobj;
233 }
234
235 /* Generated code calls this if we fail a bounds check */
236
237 void failedboundschk() {
238 #ifndef TASK
239   printf("Array out of bounds\n");
240 #ifdef THREADS
241   threadexit();
242 #else
243   exit(-1);
244 #endif
245 #else
246 #ifndef MULTICORE
247   printf("Array out of bounds\n");
248   longjmp(error_handler,2);
249 #else
250   BAMBOO_EXIT(0xa002);
251 #endif
252 #endif
253 }
254
255 /* Abort task call */
256 void abort_task() {
257 #ifdef TASK
258 #ifndef MULTICORE
259   printf("Aborting\n");
260   longjmp(error_handler,4);
261 #endif
262 #else
263   printf("Aborting\n");
264   exit(-1);
265 #endif
266 }
267
268 INLINE void initruntimedata() {
269   int i;
270   // initialize the arrays
271   if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
272     // startup core to initialize corestatus[]
273     for(i = 0; i < NUMCORESACTIVE; ++i) {
274       corestatus[i] = 1;
275       numsendobjs[i] = 0;
276       numreceiveobjs[i] = 0;
277 #ifdef MULTICORE_GC
278       gccorestatus[i] = 1;
279       gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
280       gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
281 #endif
282     } // for(i = 0; i < NUMCORESACTIVE; ++i)
283 #ifdef MULTICORE_GC
284     for(i = 0; i < NUMCORES4GC; ++i) {
285       gcloads[i] = 0;
286       gcrequiredmems[i] = 0;
287       gcstopblock[i] = 0;
288       gcfilledblocks[i] = 0;
289     } // for(i = 0; i < NUMCORES4GC; ++i)
290 #ifdef GC_PROFILE
291     gc_infoIndex = 0;
292     gc_infoOverflow = false;
293         gc_num_livespace = 0;
294         gc_num_freespace = 0;
295 #endif
296 #endif
297     numconfirm = 0;
298     waitconfirm = false;
299   }
300
301   busystatus = true;
302   self_numsendobjs = 0;
303   self_numreceiveobjs = 0;
304
305   for(i = 0; i < BAMBOO_MSG_BUF_LENGTH; ++i) {
306     msgdata[i] = -1;
307   }
308   msgdataindex = 0;
309   msgdatalast = 0;
310   msglength = BAMBOO_MSG_BUF_LENGTH;
311   msgdatafull = false;
312   for(i = 0; i < BAMBOO_OUT_BUF_LENGTH; ++i) {
313     outmsgdata[i] = -1;
314   }
315   outmsgindex = 0;
316   outmsglast = 0;
317   outmsgleft = 0;
318   isMsgHanging = false;
319
320   smemflag = true;
321   bamboo_cur_msp = NULL;
322   bamboo_smem_size = 0;
323
324 #ifdef MULTICORE_GC
325   bamboo_smem_zero_top = NULL;
326   gcflag = false;
327   gcprocessing = false;
328   gcphase = FINISHPHASE;
329   gcprecheck = true;
330   gccurr_heaptop = 0;
331   gcself_numsendobjs = 0;
332   gcself_numreceiveobjs = 0;
333   gcmarkedptrbound = 0;
334 #ifdef LOCALHASHTBL_TEST
335   gcpointertbl = allocateRuntimeHash_I(20);
336 #else
337   gcpointertbl = mgchashCreate_I(2000, 0.75);
338 #endif
339   gcforwardobjtbl = allocateMGCHash_I(20, 3);
340   gcobj2map = 0;
341   gcmappedobj = 0;
342   gcnumlobjs = 0;
343   gcheaptop = 0;
344   gctopcore = 0;
345   gctopblock = 0;
346   gcmovestartaddr = 0;
347   gctomove = false;
348   gcmovepending = 0;
349   gcblock2fill = 0;
350   gcsbstarttbl = BAMBOO_BASE_VA;
351   bamboo_smemtbl = (void *)gcsbstarttbl
352                + (BAMBOO_SHARED_MEM_SIZE/BAMBOO_SMEM_SIZE)*sizeof(INTPTR);
353   if(BAMBOO_NUM_OF_CORE < NUMCORES4GC) {
354         int t_size = ((BAMBOO_RMSP_SIZE)-sizeof(mgcsharedhashtbl_t)*2
355                 -128*sizeof(size_t))/sizeof(mgcsharedhashlistnode_t)-2;
356         int kk = 0;
357         unsigned int tmp_k = 1 << (sizeof(int)*8 -1);
358         while(((t_size & tmp_k) == 0) && (kk < sizeof(int)*8)) {
359           t_size = t_size << 1;
360           kk++;
361         }
362         t_size = tmp_k >> kk;
363         gcsharedptbl = mgcsharedhashCreate_I(t_size,0.30);
364   } else {
365         gcsharedptbl = NULL;
366   }
367   BAMBOO_MEMSET_WH(gcrpointertbls, 0, 
368           sizeof(mgcsharedhashtbl_t *)*NUMCORES4GC);
369 #ifdef SMEMM
370   gcmem_mixed_threshold = (unsigned int)((BAMBOO_SHARED_MEM_SIZE
371                 -bamboo_reserved_smem*BAMBOO_SMEM_SIZE)*0.8);
372   gcmem_mixed_usedmem = 0;
373 #endif
374 #ifdef GC_PROFILE
375   gc_num_obj = 0;
376   gc_num_liveobj = 0;
377   gc_num_forwardobj = 0;
378   gc_num_profiles = NUMCORESACTIVE - 1;
379 #endif
380 #ifdef GC_FLUSH_DTLB
381   gc_num_flush_dtlb = 0;
382 #endif
383   gc_localheap_s = false;
384 #ifdef GC_CACHE_ADAPT
385   gccachestage = false;
386 #endif // GC_CACHE_ADAPT
387 #endif // MULTICORE_GC
388 #ifndef INTERRUPT
389   reside = false;
390 #endif
391
392 #ifdef MGC
393   initializethreads();
394   bamboo_current_thread = NULL;
395 #endif // MGC
396
397 #ifdef TASK
398   inittaskdata();
399 #endif
400 }
401
402 INLINE void disruntimedata() {
403 #ifdef MULTICORE_GC
404 #ifdef LOCALHASHTBL_TEST
405   freeRuntimeHash(gcpointertbl);
406 #else
407   mgchashDelete(gcpointertbl);
408 #endif
409   freeMGCHash(gcforwardobjtbl);
410 #endif // MULTICORE_GC
411 #ifdef TASK
412   distaskdata()
413 #endif // TASK
414   BAMBOO_LOCAL_MEM_CLOSE();
415   BAMBOO_SHARE_MEM_CLOSE();
416 }
417
418 INLINE void checkCoreStatus() {
419   bool allStall = false;
420   int i = 0;
421   int sumsendobj = 0;
422   if((!waitconfirm) ||
423      (waitconfirm && (numconfirm == 0))) {
424     BAMBOO_DEBUGPRINT(0xee04);
425     BAMBOO_DEBUGPRINT_REG(waitconfirm);
426     BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
427     BAMBOO_DEBUGPRINT(0xf001);
428     corestatus[BAMBOO_NUM_OF_CORE] = 0;
429     numsendobjs[BAMBOO_NUM_OF_CORE] = self_numsendobjs;
430     numreceiveobjs[BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
431     // check the status of all cores
432     allStall = true;
433     BAMBOO_DEBUGPRINT_REG(NUMCORESACTIVE);
434     for(i = 0; i < NUMCORESACTIVE; ++i) {
435       BAMBOO_DEBUGPRINT(0xe000 + corestatus[i]);
436       if(corestatus[i] != 0) {
437                 allStall = false;
438                 break;
439       }
440     }  // for(i = 0; i < NUMCORESACTIVE; ++i)
441     if(allStall) {
442       // check if the sum of send objs and receive obj are the same
443       // yes->check if the info is the latest; no->go on executing
444       sumsendobj = 0;
445       for(i = 0; i < NUMCORESACTIVE; ++i) {
446                 sumsendobj += numsendobjs[i];
447                 BAMBOO_DEBUGPRINT(0xf000 + numsendobjs[i]);
448       }  // for(i = 0; i < NUMCORESACTIVE; ++i)
449       for(i = 0; i < NUMCORESACTIVE; ++i) {
450                 sumsendobj -= numreceiveobjs[i];
451                 BAMBOO_DEBUGPRINT(0xf000 + numreceiveobjs[i]);
452       }  // for(i = 0; i < NUMCORESACTIVE; ++i)
453       if(0 == sumsendobj) {
454                 if(!waitconfirm) {
455                   // the first time found all cores stall
456                   // send out status confirm msg to all other cores
457                   // reset the corestatus array too
458                   BAMBOO_DEBUGPRINT(0xee05);
459                   corestatus[BAMBOO_NUM_OF_CORE] = 1;
460                   waitconfirm = true;
461                   numconfirm = NUMCORESACTIVE - 1;
462                   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
463                   for(i = 1; i < NUMCORESACTIVE; ++i) {
464                         corestatus[i] = 1;
465                         // send status confirm msg to core i
466                         send_msg_1(i, STATUSCONFIRM, false);
467                   }   // for(i = 1; i < NUMCORESACTIVE; ++i)
468                   return;
469                 } else {
470                   // all the core status info are the latest
471                   // terminate; for profiling mode, send request to all
472                   // other cores to pour out profiling data
473                   BAMBOO_DEBUGPRINT(0xee06);
474
475 #ifdef USEIO
476                   totalexetime = BAMBOO_GET_EXE_TIME() - bamboo_start_time;
477 #else
478
479                   BAMBOO_PRINT(BAMBOO_GET_EXE_TIME() - bamboo_start_time);
480                   //BAMBOO_DEBUGPRINT_REG(total_num_t6); // TODO for test
481 #ifdef GC_FLUSH_DTLB
482                   BAMBOO_PRINT_REG(gc_num_flush_dtlb);
483 #endif
484 #ifndef BAMBOO_MEMPROF
485                   BAMBOO_PRINT(0xbbbbbbbb);
486 #endif
487 #endif
488                   // profile mode, send msgs to other cores to request pouring
489                   // out progiling data
490 #ifdef PROFILE
491                   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
492                   BAMBOO_DEBUGPRINT(0xf000);
493                   for(i = 1; i < NUMCORESACTIVE; ++i) {
494                         // send profile request msg to core i
495                         send_msg_2(i, PROFILEOUTPUT, totalexetime, false);
496                   } // for(i = 1; i < NUMCORESACTIVE; ++i)
497 #ifndef RT_TEST
498                   // pour profiling data on startup core
499                   outputProfileData();
500 #endif
501                   while(true) {
502                         BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
503                         BAMBOO_DEBUGPRINT(0xf001);
504                         profilestatus[BAMBOO_NUM_OF_CORE] = 0;
505                         // check the status of all cores
506                         allStall = true;
507                         BAMBOO_DEBUGPRINT_REG(NUMCORESACTIVE);
508                         for(i = 0; i < NUMCORESACTIVE; ++i) {
509                           BAMBOO_DEBUGPRINT(0xe000 + profilestatus[i]);
510                           if(profilestatus[i] != 0) {
511                                 allStall = false;
512                                 break;
513                           }
514                         }  // for(i = 0; i < NUMCORESACTIVE; ++i)
515                         if(!allStall) {
516                           int halt = 100;
517                           BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
518                           BAMBOO_DEBUGPRINT(0xf000);
519                           while(halt--) {
520                           }
521                         } else {
522                           BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
523                           break;
524                         }  // if(!allStall)
525                   }  // while(true)
526 #endif
527
528                   // gc_profile mode, output gc prfiling data
529 #ifdef MULTICORE_GC
530 #ifdef GC_CACHE_ADAPT
531                   bamboo_mask_timer_intr(); // disable the TILE_TIMER interrupt
532 #endif // GC_CACHE_ADAPT
533 #ifdef GC_PROFILE
534                   gc_outputProfileData();
535 #endif // #ifdef GC_PROFILE
536 #endif // #ifdef MULTICORE_GC
537                   disruntimedata();
538                   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
539                   terminate();  // All done.
540                 }  // if(!waitconfirm)
541       } else {
542                 // still some objects on the fly on the network
543                 // reset the waitconfirm and numconfirm
544                 BAMBOO_DEBUGPRINT(0xee07);
545                 waitconfirm = false;
546                 numconfirm = 0;
547           }  //  if(0 == sumsendobj)
548     } else {
549       // not all cores are stall, keep on waiting
550       BAMBOO_DEBUGPRINT(0xee08);
551       waitconfirm = false;
552       numconfirm = 0;
553     }  //  if(allStall)
554     BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
555     BAMBOO_DEBUGPRINT(0xf000);
556   }  // if((!waitconfirm) ||
557 }
558
559 // main function for each core
560 inline void run(int argc, char** argv) {
561   int i = 0;
562   bool sendStall = false;
563   bool isfirst = true;
564   bool tocontinue = false;
565
566   corenum = BAMBOO_GET_NUM_OF_CORE();
567   BAMBOO_DEBUGPRINT(0xeeee);
568   BAMBOO_DEBUGPRINT_REG(corenum);
569   BAMBOO_DEBUGPRINT(STARTUPCORE);
570
571   // initialize runtime data structures
572   initruntimedata();
573
574   // other architecture related initialization
575   initialization();
576   initCommunication();
577
578 #ifdef GC_CACHE_ADAPT
579 // enable the timer interrupt
580 #ifdef GC_CACHE_SAMPLING
581   bamboo_tile_timer_set_next_event(GC_TILE_TIMER_EVENT_SETTING); // TODO
582   bamboo_unmask_timer_intr();
583   bamboo_dtlb_sampling_process();
584 #endif // GC_CACHE_SAMPLING
585 #endif // GC_CACHE_ADAPT
586
587   initializeexithandler();
588
589   // main process of the execution module
590   if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
591 #ifdef TASK
592     // non-executing cores, only processing communications
593     activetasks = NULL;
594 #endif
595     fakeExecution();
596   } else {
597 #ifdef TASK
598     /* Create queue of active tasks */
599     activetasks=
600       genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
601                            (int (*)(void *,void *)) &comparetpd);
602
603     /* Process task information */
604     processtasks();
605
606     if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
607       /* Create startup object */
608       createstartupobject(argc, argv);
609     }
610
611     BAMBOO_DEBUGPRINT(0xee00);
612 #endif
613
614 #ifdef MGC
615         if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
616           // run the main method in the specified mainclass
617           mgc_main(argc, argv);
618         }
619 #endif
620
621     while(true) {
622
623 #ifdef MULTICORE_GC
624       // check if need to do GC
625       if(gcflag) {
626                 gc(NULL);
627           }
628 #endif // MULTICORE_GC
629
630 #ifdef TASK
631       // check if there are new active tasks can be executed
632       executetasks();
633       if(busystatus) {
634                 sendStall = false;
635       }
636
637 #ifndef INTERRUPT
638       while(receiveObject() != -1) {
639       }
640 #endif
641
642       BAMBOO_DEBUGPRINT(0xee01);
643
644       // check if there are some pending objects,
645       // if yes, enqueue them and executetasks again
646       tocontinue = checkObjQueue();
647 #elif defined MGC
648           tocontinue = trystartthread();
649           if(tocontinue) {
650                 sendStall = false;
651           }
652 #endif
653
654       if(!tocontinue) {
655                 // check if stop
656                 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
657                   if(isfirst) {
658                         BAMBOO_DEBUGPRINT(0xee03);
659                         isfirst = false;
660                   }
661                   checkCoreStatus();
662                 } else {
663                   if(!sendStall) {
664                         BAMBOO_DEBUGPRINT(0xee09);
665 #ifdef PROFILE
666                         if(!stall) {
667 #endif
668                         if(isfirst) {
669                           // wait for some time
670                           int halt = 10000;
671                           BAMBOO_DEBUGPRINT(0xee0a);
672                           while(halt--) {
673                           }
674                           isfirst = false;
675                         } else {
676                           // send StallMsg to startup core
677                           BAMBOO_DEBUGPRINT(0xee0b);
678                           // send stall msg
679                           send_msg_4(STARTUPCORE, TRANSTALL, BAMBOO_NUM_OF_CORE,
680                                                  self_numsendobjs, self_numreceiveobjs, false);
681                           sendStall = true;
682                           isfirst = true;
683                           busystatus = false;
684                         }
685 #ifdef PROFILE
686                   }
687 #endif
688                   } else {
689                         isfirst = true;
690                         busystatus = false;
691                         BAMBOO_DEBUGPRINT(0xee0c);
692                   }   // if(!sendStall)
693                 }   // if(STARTUPCORE == BAMBOO_NUM_OF_CORE)
694       }  // if(!tocontinue)
695     }  // while(true)
696   } // if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1)
697
698 } // run()
699
700 INLINE int checkMsgLength_I(int size) {
701 #ifndef CLOSE_PRINT
702   BAMBOO_DEBUGPRINT(0xcccc);
703 #endif
704   int type = msgdata[msgdataindex];
705   switch(type) {
706   case STATUSCONFIRM:
707   case TERMINATE:
708 #ifdef MULTICORE_GC
709   case GCSTARTPRE:
710   case GCSTARTINIT:
711   case GCSTART:
712   case GCSTARTMAPINFO:
713   case GCSTARTFLUSH:
714   case GCFINISH:
715   case GCMARKCONFIRM:
716   case GCLOBJREQUEST:
717 #ifdef GC_CACHE_ADAPT
718   case GCSTARTPREF:
719 #endif // GC_CACHE_ADAPT
720 #endif // MULTICORE_GC
721   {
722         msglength = 1;
723         break;
724   }
725
726 #ifdef TASK
727   case PROFILEOUTPUT:
728   case PROFILEFINISH:
729 #endif
730 #ifdef MULTICORE_GC
731   case GCSTARTCOMPACT:
732   case GCMARKEDOBJ:
733   case GCFINISHINIT:
734   case GCFINISHMAPINFO:
735   case GCFINISHFLUSH:
736 #ifdef GC_CACHE_ADAPT
737   case GCFINISHPREF:
738 #endif // GC_CACHE_ADAPT
739 #endif // MULTICORE_GC
740   {
741         msglength = 2;
742         break;
743   }
744
745   case MEMREQUEST:
746   case MEMRESPONSE:
747 #ifdef MULTICORE_GC
748   case GCMAPREQUEST:
749   case GCMAPINFO:
750   case GCMAPTBL:
751   case GCLOBJMAPPING:
752 #endif
753   {
754         msglength = 3;
755         break;
756   }
757
758   case TRANSTALL:
759 #ifdef TASK
760   case LOCKGROUNT:
761   case LOCKDENY:
762   case LOCKRELEASE:
763   case REDIRECTGROUNT:
764   case REDIRECTDENY:
765   case REDIRECTRELEASE:
766 #endif
767 #ifdef MULTICORE_GC
768   case GCFINISHPRE:
769   case GCFINISHMARK:
770   case GCMOVESTART:
771 #ifdef GC_PROFILE
772   case GCPROFILES:
773 #endif
774 #endif
775   {
776         msglength = 4;
777         break;
778   }
779
780 #ifdef TASK
781   case LOCKREQUEST:
782 #endif
783   case STATUSREPORT:
784 #ifdef MULTICORE_GC
785   case GCFINISHCOMPACT:
786   case GCMARKREPORT:
787 #endif
788   {
789         msglength = 5;
790         break;
791   }
792
793 #ifdef TASK
794   case REDIRECTLOCK:
795   {
796     msglength = 6;
797     break;
798   }
799 #endif
800
801 #ifdef TASK
802   case TRANSOBJ:   // nonfixed size
803 #endif
804 #ifdef MULTICORE_GC
805   case GCLOBJINFO:
806 #endif
807   {  // nonfixed size
808         if(size > 1) {
809           msglength = msgdata[(msgdataindex+1)&(BAMBOO_MSG_BUF_MASK)];
810         } else {
811           return -1;
812         }
813         break;
814   }
815
816   default:
817   {
818     BAMBOO_DEBUGPRINT_REG(type);
819         BAMBOO_DEBUGPRINT_REG(size);
820     BAMBOO_DEBUGPRINT_REG(msgdataindex);
821         BAMBOO_DEBUGPRINT_REG(msgdatalast);
822         BAMBOO_DEBUGPRINT_REG(msgdatafull);
823     int i = 6;
824     while(i-- > 0) {
825       BAMBOO_DEBUGPRINT(msgdata[msgdataindex+i]);
826     }
827     BAMBOO_EXIT(0xe004);
828     break;
829   }
830   }
831 #ifndef CLOSE_PRINT
832   BAMBOO_DEBUGPRINT_REG(msgdata[msgdataindex]);
833   BAMBOO_DEBUGPRINT(0xffff);
834 #endif
835   return msglength;
836 }
837
838 INLINE void processmsg_transtall_I() {
839   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
840     // non startup core can not receive stall msg
841 #ifndef CLOSE_PRINT
842     BAMBOO_DEBUGPRINT_REG(msgdata[msgdataindex] /*[1]*/);
843 #endif
844     BAMBOO_EXIT(0xe006);
845   }
846   int num_core = msgdata[msgdataindex]; //[1]
847   MSG_INDEXINC_I();
848   int data2 = msgdata[msgdataindex]; //[2];
849   MSG_INDEXINC_I();
850   int data3 = msgdata[msgdataindex]; //[3];
851   MSG_INDEXINC_I();
852   if(num_core < NUMCORESACTIVE) {
853 #ifndef CLOSE_PRINT
854     BAMBOO_DEBUGPRINT(0xe881);
855 #endif
856     corestatus[num_core] = 0;
857     numsendobjs[num_core] = data2; //[2];
858     numreceiveobjs[num_core] = data3; //[3];
859   }
860 }
861
862 INLINE void processmsg_statusconfirm_I() {
863   if((BAMBOO_NUM_OF_CORE == STARTUPCORE)
864      || (BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1)) {
865     // wrong core to receive such msg
866     BAMBOO_EXIT(0xe011);
867   } else {
868     // send response msg
869 #ifndef CLOSE_PRINT
870     BAMBOO_DEBUGPRINT(0xe887);
871 #endif
872     // cache the msg first
873     if(BAMBOO_CHECK_SEND_MODE()) {
874           cache_msg_5(STARTUPCORE, STATUSREPORT,
875                                   busystatus ? 1 : 0, BAMBOO_NUM_OF_CORE,
876                                   self_numsendobjs, self_numreceiveobjs);
877     } else {
878           send_msg_5(STARTUPCORE, STATUSREPORT, busystatus?1:0,
879                                  BAMBOO_NUM_OF_CORE, self_numsendobjs,
880                                  self_numreceiveobjs, true);
881     }
882   }
883 }
884
885 INLINE void processmsg_statusreport_I() {
886   int data1 = msgdata[msgdataindex];
887   MSG_INDEXINC_I();
888   int data2 = msgdata[msgdataindex];
889   MSG_INDEXINC_I();
890   int data3 = msgdata[msgdataindex];
891   MSG_INDEXINC_I();
892   int data4 = msgdata[msgdataindex];
893   MSG_INDEXINC_I();
894   // receive a status confirm info
895   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
896     // wrong core to receive such msg
897 #ifndef CLOSE_PRINT
898     BAMBOO_DEBUGPRINT_REG(data2);
899 #endif
900     BAMBOO_EXIT(0xe012);
901   } else {
902 #ifndef CLOSE_PRINT
903     BAMBOO_DEBUGPRINT(0xe888);
904 #endif
905     if(waitconfirm) {
906       numconfirm--;
907     }
908     corestatus[data2] = data1;
909     numsendobjs[data2] = data3;
910     numreceiveobjs[data2] = data4;
911   }
912 }
913
914 INLINE void processmsg_terminate_I() {
915 #ifndef CLOSE_PRINT
916   BAMBOO_DEBUGPRINT(0xe889);
917 #endif
918   disruntimedata();
919 #ifdef MULTICORE_GC
920 #ifdef GC_CACHE_ADAPT
921   bamboo_mask_timer_intr(); // disable the TILE_TIMER interrupt
922 #endif // GC_CACHE_ADAPT
923 #endif // MULTICORE_GC
924   BAMBOO_EXIT_APP(0);
925 }
926
927 INLINE void processmsg_memrequest_I() {
928   int data1 = msgdata[msgdataindex];
929   MSG_INDEXINC_I();
930   int data2 = msgdata[msgdataindex];
931   MSG_INDEXINC_I();
932   // receive a shared memory request msg
933   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
934     // wrong core to receive such msg
935 #ifndef CLOSE_PRINT
936     BAMBOO_DEBUGPRINT_REG(data2);
937 #endif
938     BAMBOO_EXIT(0xe013);
939   } else {
940 #ifndef CLOSE_PRINT
941     BAMBOO_DEBUGPRINT(0xe88a);
942 #endif
943     int allocsize = 0;
944     void * mem = NULL;
945 #ifdef MULTICORE_GC
946     if(gcprocessing) {
947       // is currently doing gc, dump this msg
948       if(INITPHASE == gcphase) {
949                 // if still in the initphase of gc, send a startinit msg again,
950                 // cache the msg first
951                 if(BAMBOO_CHECK_SEND_MODE()) {
952                   cache_msg_1(data2, GCSTARTINIT);
953                 } else {
954                   send_msg_1(data2, GCSTARTINIT, true);
955                 }
956       }
957     } else {
958 #endif
959     mem = smemalloc_I(data2, data1, &allocsize);
960     if(mem != NULL) {
961       // send the start_va to request core, cache the msg first
962       if(BAMBOO_CHECK_SEND_MODE()) {
963                 cache_msg_3(data2, MEMRESPONSE, mem, allocsize);
964       } else {
965                 send_msg_3(data2, MEMRESPONSE, mem, allocsize, true);
966           }
967     } //else 
968           // if mem == NULL, the gcflag of the startup core has been set
969           // and all the other cores have been informed to start gc
970 #ifdef MULTICORE_GC
971   }
972 #endif
973   }
974 }
975
976 INLINE void processmsg_memresponse_I() {
977   int data1 = msgdata[msgdataindex];
978   MSG_INDEXINC_I();
979   int data2 = msgdata[msgdataindex];
980   MSG_INDEXINC_I();
981   // receive a shared memory response msg
982 #ifndef CLOSE_PRINT
983   BAMBOO_DEBUGPRINT(0xe88b);
984 #endif
985 #ifdef MULTICORE_GC
986   // if is currently doing gc, dump this msg
987   if(!gcprocessing) {
988 #endif
989   if(data2 == 0) {
990     bamboo_smem_size = 0;
991     bamboo_cur_msp = 0;
992 #ifdef MULTICORE_GC
993         bamboo_smem_zero_top = 0;
994 #endif
995   } else {
996 #ifdef MULTICORE_GC
997     // fill header to store the size of this mem block
998     BAMBOO_MEMSET_WH(data1, '\0', BAMBOO_CACHE_LINE_SIZE); 
999     (*((int*)data1)) = data2;
1000     bamboo_smem_size = data2 - BAMBOO_CACHE_LINE_SIZE;
1001     bamboo_cur_msp = data1 + BAMBOO_CACHE_LINE_SIZE;
1002         bamboo_smem_zero_top = bamboo_cur_msp;
1003 #else
1004     bamboo_smem_size = data2;
1005     bamboo_cur_msp =(void*)(data1);
1006 #endif
1007   }
1008   smemflag = true;
1009 #ifdef MULTICORE_GC
1010 }
1011 #endif
1012 }
1013
1014 #ifdef MULTICORE_GC
1015 INLINE void processmsg_gcstartpre_I() {
1016   if(gcprocessing) {
1017         // already stall for gc
1018         // send a update pregc information msg to the master core
1019         if(BAMBOO_CHECK_SEND_MODE()) {
1020           cache_msg_4(STARTUPCORE, GCFINISHPRE, BAMBOO_NUM_OF_CORE, 
1021                   self_numsendobjs, self_numreceiveobjs);
1022         } else {
1023           send_msg_4(STARTUPCORE, GCFINISHPRE, BAMBOO_NUM_OF_CORE, 
1024                   self_numsendobjs, self_numreceiveobjs, true);
1025         }
1026   } else {
1027         // the first time to be informed to start gc
1028         gcflag = true;
1029         if(!smemflag) {
1030           // is waiting for response of mem request
1031           // let it return NULL and start gc
1032           bamboo_smem_size = 0;
1033           bamboo_cur_msp = NULL;
1034           smemflag = true;
1035           bamboo_smem_zero_top = NULL;
1036         }
1037   }
1038 }
1039
1040 INLINE void processmsg_gcstartinit_I() {
1041   gcphase = INITPHASE;
1042 }
1043
1044 INLINE void processmsg_gcstart_I() {
1045 #ifndef CLOSE_PRINT
1046   BAMBOO_DEBUGPRINT(0xe88c);
1047 #endif
1048   // set the GC flag
1049   gcphase = MARKPHASE;
1050 }
1051
1052 INLINE void processmsg_gcstartcompact_I() {
1053   gcblock2fill = msgdata[msgdataindex];
1054   MSG_INDEXINC_I();  //msgdata[1];
1055   gcphase = COMPACTPHASE;
1056 }
1057
1058 INLINE void processmsg_gcstartmapinfo_I() {
1059   gcphase = MAPPHASE;
1060 }
1061
1062 INLINE void processmsg_gcstartflush_I() {
1063   gcphase = FLUSHPHASE;
1064 }
1065
1066 INLINE void processmsg_gcfinishpre_I() {
1067   int data1 = msgdata[msgdataindex];
1068   MSG_INDEXINC_I();
1069   int data2 = msgdata[msgdataindex];
1070   MSG_INDEXINC_I();
1071   int data3 = msgdata[msgdataindex];
1072   MSG_INDEXINC_I();
1073   // received a init phase finish msg
1074   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1075     // non startup core can not receive this msg
1076 #ifndef CLOSE_PRINT
1077     BAMBOO_DEBUGPRINT_REG(data1);
1078 #endif
1079     BAMBOO_EXIT(0xe014);
1080   }
1081   // All cores should do init GC
1082   if(!gcprecheck) {
1083         gcprecheck = true;
1084   }
1085   gccorestatus[data1] = 0;
1086   gcnumsendobjs[0][data1] = data2;
1087   gcnumreceiveobjs[0][data1] = data3;
1088 }
1089
1090 INLINE void processmsg_gcfinishinit_I() {
1091   int data1 = msgdata[msgdataindex];
1092   MSG_INDEXINC_I();
1093   // received a init phase finish msg
1094   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1095     // non startup core can not receive this msg
1096 #ifndef CLOSE_PRINT
1097     BAMBOO_DEBUGPRINT_REG(data1);
1098 #endif
1099     BAMBOO_EXIT(0xe015);
1100   }
1101 #ifndef CLOSE_PRINT
1102   BAMBOO_DEBUGPRINT(0xe88c);
1103   BAMBOO_DEBUGPRINT_REG(data1);
1104 #endif
1105   // All cores should do init GC
1106   if(data1 < NUMCORESACTIVE) {
1107     gccorestatus[data1] = 0;
1108   }
1109 }
1110
1111 INLINE void processmsg_gcfinishmark_I() {
1112   int data1 = msgdata[msgdataindex];
1113   MSG_INDEXINC_I();
1114   int data2 = msgdata[msgdataindex];
1115   MSG_INDEXINC_I();
1116   int data3 = msgdata[msgdataindex];
1117   MSG_INDEXINC_I();
1118   // received a mark phase finish msg
1119   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1120     // non startup core can not receive this msg
1121 #ifndef CLOSE_PRINT
1122     BAMBOO_DEBUGPRINT_REG(data1);
1123 #endif
1124     BAMBOO_EXIT(0xe016);
1125   }
1126   // all cores should do mark
1127   if(data1 < NUMCORESACTIVE) {
1128     gccorestatus[data1] = 0;
1129         int entry_index = 0;
1130         if(waitconfirm)  {
1131           // phase 2
1132           entry_index = (gcnumsrobjs_index == 0) ? 1 : 0;
1133         } else {
1134           // phase 1
1135           entry_index = gcnumsrobjs_index;
1136         }
1137     gcnumsendobjs[entry_index][data1] = data2;
1138     gcnumreceiveobjs[entry_index][data1] = data3;
1139   }
1140 }
1141
1142 INLINE void processmsg_gcfinishcompact_I() {
1143   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1144     // non startup core can not receive this msg
1145     // return -1
1146 #ifndef CLOSE_PRINT
1147     BAMBOO_DEBUGPRINT_REG(msgdata[msgdataindex] /*[1]*/);
1148 #endif
1149     BAMBOO_EXIT(0xe017);
1150   }
1151   int cnum = msgdata[msgdataindex];
1152   MSG_INDEXINC_I();       //msgdata[1];
1153   int filledblocks = msgdata[msgdataindex];
1154   MSG_INDEXINC_I();       //msgdata[2];
1155   int heaptop = msgdata[msgdataindex];
1156   MSG_INDEXINC_I();       //msgdata[3];
1157   int data4 = msgdata[msgdataindex];
1158   MSG_INDEXINC_I();       //msgdata[4];
1159   // only gc cores need to do compact
1160   if(cnum < NUMCORES4GC) {
1161     if(COMPACTPHASE == gcphase) {
1162       gcfilledblocks[cnum] = filledblocks;
1163       gcloads[cnum] = heaptop;
1164     }
1165     if(data4 > 0) {
1166       // ask for more mem
1167       int startaddr = 0;
1168       int tomove = 0;
1169       int dstcore = 0;
1170       if(gcfindSpareMem_I(&startaddr, &tomove, &dstcore, data4, cnum)) {
1171                 // cache the msg first
1172                 if(BAMBOO_CHECK_SEND_MODE()) {
1173                   cache_msg_4(cnum, GCMOVESTART, dstcore, startaddr, tomove);
1174                 } else {
1175                   send_msg_4(cnum, GCMOVESTART, dstcore, startaddr, tomove, true);
1176                 }
1177       }
1178     } else {
1179       gccorestatus[cnum] = 0;
1180     }  // if(data4>0)
1181   }  // if(cnum < NUMCORES4GC)
1182 }
1183
1184 INLINE void processmsg_gcfinishmapinfo_I() {
1185   int data1 = msgdata[msgdataindex];
1186   MSG_INDEXINC_I();
1187   // received a map phase finish msg
1188   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1189     // non startup core can not receive this msg
1190 #ifndef CLOSE_PRINT
1191     BAMBOO_DEBUGPRINT_REG(data1);
1192 #endif
1193     BAMBOO_EXIT(0xe018);
1194   }
1195   // all cores should do flush
1196   if(data1 < NUMCORES4GC) {
1197     gccorestatus[data1] = 0;
1198   }
1199 }
1200
1201
1202 INLINE void processmsg_gcfinishflush_I() {
1203   int data1 = msgdata[msgdataindex];
1204   MSG_INDEXINC_I();
1205   // received a flush phase finish msg
1206   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1207     // non startup core can not receive this msg
1208 #ifndef CLOSE_PRINT
1209     BAMBOO_DEBUGPRINT_REG(data1);
1210 #endif
1211     BAMBOO_EXIT(0xe019);
1212   }
1213   // all cores should do flush
1214   if(data1 < NUMCORESACTIVE) {
1215     gccorestatus[data1] = 0;
1216   }
1217 }
1218
1219 INLINE void processmsg_gcmarkconfirm_I() {
1220   if((BAMBOO_NUM_OF_CORE == STARTUPCORE)
1221      || (BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1)) {
1222     // wrong core to receive such msg
1223     BAMBOO_EXIT(0xe01a);
1224   } else {
1225     // send response msg, cahce the msg first
1226     if(BAMBOO_CHECK_SEND_MODE()) {
1227           cache_msg_5(STARTUPCORE, GCMARKREPORT, BAMBOO_NUM_OF_CORE,
1228                                   gcbusystatus, gcself_numsendobjs,
1229                                   gcself_numreceiveobjs);
1230     } else {
1231           send_msg_5(STARTUPCORE, GCMARKREPORT, BAMBOO_NUM_OF_CORE,
1232                                  gcbusystatus, gcself_numsendobjs,
1233                                  gcself_numreceiveobjs, true);
1234     }
1235   }
1236 }
1237
1238 INLINE void processmsg_gcmarkreport_I() {
1239   int data1 = msgdata[msgdataindex];
1240   MSG_INDEXINC_I();
1241   int data2 = msgdata[msgdataindex];
1242   MSG_INDEXINC_I();
1243   int data3 = msgdata[msgdataindex];
1244   MSG_INDEXINC_I();
1245   int data4 = msgdata[msgdataindex];
1246   MSG_INDEXINC_I();
1247   // received a marked phase finish confirm response msg
1248   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1249     // wrong core to receive such msg
1250 #ifndef CLOSE_PRINT
1251     BAMBOO_DEBUGPRINT_REG(data2);
1252 #endif
1253     BAMBOO_EXIT(0xe01b);
1254   } else {
1255         int entry_index = 0;
1256     if(waitconfirm) {
1257           // phse 2
1258       numconfirm--;
1259           entry_index = (gcnumsrobjs_index == 0) ? 1 : 0;
1260     } else {
1261           // can never reach here
1262           // phase 1
1263           entry_index = gcnumsrobjs_index;
1264         }
1265     gccorestatus[data1] = data2;
1266     gcnumsendobjs[entry_index][data1] = data3;
1267     gcnumreceiveobjs[entry_index][data1] = data4;
1268   }
1269 }
1270
1271 INLINE void processmsg_gcmarkedobj_I() {
1272   int data1 = msgdata[msgdataindex];
1273   MSG_INDEXINC_I();
1274   // received a markedObj msg
1275   if(((int *)data1)[6] == INIT) {
1276     // this is the first time that this object is discovered,
1277     // set the flag as DISCOVERED
1278     ((int *)data1)[6] = DISCOVERED;
1279     gc_enqueue_I(data1);
1280   } 
1281   // set the remote flag
1282   ((int *)data1)[6] |= REMOTEM;
1283   gcself_numreceiveobjs++;
1284   gcbusystatus = true;
1285 }
1286
1287 INLINE void processmsg_gcmovestart_I() {
1288   gctomove = true;
1289   gcdstcore = msgdata[msgdataindex];
1290   MSG_INDEXINC_I();       //msgdata[1];
1291   gcmovestartaddr = msgdata[msgdataindex];
1292   MSG_INDEXINC_I();       //msgdata[2];
1293   gcblock2fill = msgdata[msgdataindex];
1294   MSG_INDEXINC_I();       //msgdata[3];
1295 }
1296
1297 INLINE void processmsg_gcmaprequest_I() {
1298   void * dstptr = NULL;
1299   int data1 = msgdata[msgdataindex];
1300   MSG_INDEXINC_I();
1301   int data2 = msgdata[msgdataindex];
1302   MSG_INDEXINC_I();
1303 #ifdef LOCALHASHTBL_TEST
1304   RuntimeHashget(gcpointertbl, data1, &dstptr);
1305 #else
1306   dstptr = mgchashSearch(gcpointertbl, data1);
1307 #endif
1308   if(NULL == dstptr) {
1309     // no such pointer in this core, something is wrong
1310 #ifndef CLOSE_PRINT
1311     BAMBOO_DEBUGPRINT_REG(data1);
1312     BAMBOO_DEBUGPRINT_REG(data2);
1313 #endif
1314     BAMBOO_EXIT(0xe01c);
1315   } else {
1316     // send back the mapping info, cache the msg first
1317     if(BAMBOO_CHECK_SEND_MODE()) {
1318           cache_msg_3(data2, GCMAPINFO, data1, (int)dstptr);
1319     } else {
1320           send_msg_3(data2, GCMAPINFO, data1, (int)dstptr, true);
1321     }
1322   }
1323 }
1324
1325 INLINE void processmsg_gcmapinfo_I() {
1326   int data1 = msgdata[msgdataindex];
1327   MSG_INDEXINC_I();
1328   gcmappedobj = msgdata[msgdataindex];  // [2]
1329   MSG_INDEXINC_I();
1330 #ifdef LOCALHASHTBL_TEST
1331   RuntimeHashadd_I(gcpointertbl, data1, gcmappedobj);
1332 #else
1333   mgchashInsert_I(gcpointertbl, data1, gcmappedobj);
1334 #endif
1335   if(data1 == gcobj2map) {
1336         gcismapped = true;
1337   }
1338 }
1339
1340 INLINE void processmsg_gcmaptbl_I() {
1341   int data1 = msgdata[msgdataindex];
1342   MSG_INDEXINC_I();
1343   int data2 = msgdata[msgdataindex];
1344   MSG_INDEXINC_I();
1345   gcrpointertbls[data2] = (mgcsharedhashtbl_t *)data1; 
1346 }
1347
1348 INLINE void processmsg_gclobjinfo_I() {
1349   numconfirm--;
1350
1351   int data1 = msgdata[msgdataindex];
1352   MSG_INDEXINC_I();
1353   int data2 = msgdata[msgdataindex];
1354   MSG_INDEXINC_I();
1355   if(BAMBOO_NUM_OF_CORE > NUMCORES4GC - 1) {
1356 #ifndef CLOSE_PRINT
1357     BAMBOO_DEBUGPRINT_REG(data2);
1358 #endif
1359     BAMBOO_EXIT(0xe01d);
1360   }
1361   // store the mark result info
1362   int cnum = data2;
1363   gcloads[cnum] = msgdata[msgdataindex];
1364   MSG_INDEXINC_I();       // msgdata[3];
1365   int data4 = msgdata[msgdataindex];
1366   MSG_INDEXINC_I();
1367   if(gcheaptop < data4) {
1368     gcheaptop = data4;
1369   }
1370   // large obj info here
1371   for(int k = 5; k < data1; k+=2) {
1372     int lobj = msgdata[msgdataindex];
1373     MSG_INDEXINC_I();   //msgdata[k++];
1374     int length = msgdata[msgdataindex];
1375     MSG_INDEXINC_I();   //msgdata[k++];
1376     gc_lobjenqueue_I(lobj, length, cnum);
1377     gcnumlobjs++;
1378   }  // for(int k = 5; k < msgdata[1];)
1379 }
1380
1381 INLINE void processmsg_gclobjmapping_I() {
1382   int data1 = msgdata[msgdataindex];
1383   MSG_INDEXINC_I();
1384   int data2 = msgdata[msgdataindex];
1385   MSG_INDEXINC_I();
1386 #ifdef LOCALHASHTBL_TEST
1387   RuntimeHashadd_I(gcpointertbl, data1, data2);
1388 #else
1389   mgchashInsert_I(gcpointertbl, data1, data2);
1390 #endif
1391   mgcsharedhashInsert_I(gcsharedptbl, data1, data2);
1392 }
1393
1394 #ifdef GC_PROFILE
1395 INLINE void processmsg_gcprofiles_I() {
1396   int data1 = msgdata[msgdataindex];
1397   MSG_INDEXINC_I();
1398   int data2 = msgdata[msgdataindex];
1399   MSG_INDEXINC_I();
1400   int data3 = msgdata[msgdataindex];
1401   MSG_INDEXINC_I();
1402   gc_num_obj += data1;
1403   gc_num_liveobj += data2;
1404   gc_num_forwardobj += data3;
1405   gc_num_profiles--;
1406 }
1407 #endif // GC_PROFILE
1408
1409 #ifdef GC_CACHE_ADAPT
1410 INLINE void processmsg_gcstartpref_I() {
1411   gcphase = PREFINISHPHASE;
1412 }
1413
1414 INLINE void processmsg_gcfinishpref_I() {
1415   int data1 = msgdata[msgdataindex];
1416   MSG_INDEXINC_I();
1417   // received a flush phase finish msg
1418   if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1419     // non startup core can not receive this msg
1420 #ifndef CLOSE_PRINT
1421     BAMBOO_DEBUGPRINT_REG(data1);
1422 #endif
1423     BAMBOO_EXIT(0xe01e);
1424   }
1425   // all cores should do flush
1426   if(data1 < NUMCORESACTIVE) {
1427     gccorestatus[data1] = 0;
1428   }
1429 }
1430 #endif // GC_CACHE_ADAPT
1431 #endif // #ifdef MULTICORE_GC
1432
1433 // receive object transferred from other cores
1434 // or the terminate message from other cores
1435 // Should be invoked in critical sections!!
1436 // NOTICE: following format is for threadsimulate version only
1437 //         RAW version please see previous description
1438 // format: type + object
1439 // type: -1--stall msg
1440 //      !-1--object
1441 // return value: 0--received an object
1442 //               1--received nothing
1443 //               2--received a Stall Msg
1444 //               3--received a lock Msg
1445 //               RAW version: -1 -- received nothing
1446 //                            otherwise -- received msg type
1447 int receiveObject(int send_port_pending) {
1448 #ifdef TASK
1449 #ifdef PROFILE_INTERRUPT
1450   if(!interruptInfoOverflow) {
1451     InterruptInfo* intInfo = RUNMALLOC_I(sizeof(struct interrupt_info));
1452     interruptInfoArray[interruptInfoIndex] = intInfo;
1453     intInfo->startTime = BAMBOO_GET_EXE_TIME();
1454     intInfo->endTime = -1;
1455   }
1456 #endif // PROFILE_INTERRUPT
1457 #endif // TASK
1458 msg:
1459   // get the incoming msgs
1460   if(receiveMsg(send_port_pending) == -1) {
1461     return -1;
1462   }
1463 processmsg:
1464   // processing received msgs
1465   int size = 0;
1466   MSG_REMAINSIZE_I(&size);
1467   if((size == 0) || (checkMsgLength_I(size) == -1)) {
1468     // not a whole msg
1469     // have new coming msg
1470     if((BAMBOO_MSG_AVAIL() != 0) && !msgdatafull) {
1471       goto msg;
1472     } else {
1473       return -1;
1474     }
1475   }
1476
1477   if(msglength <= size) {
1478     // have some whole msg
1479     MSGTYPE type;
1480     type = msgdata[msgdataindex]; //[0]
1481     MSG_INDEXINC_I();
1482     msgdatafull = false;
1483     switch(type) {
1484 #ifdef TASK
1485     case TRANSOBJ: {
1486       // receive a object transfer msg
1487       processmsg_transobj_I();
1488       break;
1489     }   // case TRANSOBJ
1490 #endif // TASK
1491
1492     case TRANSTALL: {
1493       // receive a stall msg
1494       processmsg_transtall_I();
1495       break;
1496     }   // case TRANSTALL
1497
1498 #ifdef TASK
1499 // GC version have no lock msgs
1500 #ifndef MULTICORE_GC
1501     case LOCKREQUEST: {
1502       // receive lock request msg, handle it right now
1503       processmsg_lockrequest_I();
1504       break;
1505     }   // case LOCKREQUEST
1506
1507     case LOCKGROUNT: {
1508       // receive lock grount msg
1509       processmsg_lockgrount_I();
1510       break;
1511     }   // case LOCKGROUNT
1512
1513     case LOCKDENY: {
1514       // receive lock deny msg
1515       processmsg_lockdeny_I();
1516       break;
1517     }   // case LOCKDENY
1518
1519     case LOCKRELEASE: {
1520       processmsg_lockrelease_I();
1521       break;
1522     }   // case LOCKRELEASE
1523 #endif // #ifndef MULTICORE_GC
1524
1525 #ifdef PROFILE
1526     case PROFILEOUTPUT: {
1527       // receive an output profile data request msg
1528       processmsg_profileoutput_I();
1529       break;
1530     }   // case PROFILEOUTPUT
1531
1532     case PROFILEFINISH: {
1533       // receive a profile output finish msg
1534       processmsg_profilefinish_I();
1535       break;
1536     }   // case PROFILEFINISH
1537 #endif // #ifdef PROFILE
1538
1539 // GC version has no lock msgs
1540 #ifndef MULTICORE_GC
1541     case REDIRECTLOCK: {
1542       // receive a redirect lock request msg, handle it right now
1543       processmsg_redirectlock_I();
1544       break;
1545     }   // case REDIRECTLOCK
1546
1547     case REDIRECTGROUNT: {
1548       // receive a lock grant msg with redirect info
1549       processmsg_redirectgrount_I();
1550       break;
1551     }   // case REDIRECTGROUNT
1552
1553     case REDIRECTDENY: {
1554       // receive a lock deny msg with redirect info
1555       processmsg_redirectdeny_I();
1556       break;
1557     }   // case REDIRECTDENY
1558
1559     case REDIRECTRELEASE: {
1560       // receive a lock release msg with redirect info
1561       processmsg_redirectrelease_I();
1562       break;
1563     }   // case REDIRECTRELEASE
1564 #endif // #ifndef MULTICORE_GC
1565 #endif // TASK
1566
1567     case STATUSCONFIRM: {
1568       // receive a status confirm info
1569       processmsg_statusconfirm_I();
1570       break;
1571     }   // case STATUSCONFIRM
1572
1573     case STATUSREPORT: {
1574       processmsg_statusreport_I();
1575       break;
1576     }   // case STATUSREPORT
1577
1578     case TERMINATE: {
1579       // receive a terminate msg
1580       processmsg_terminate_I();
1581       break;
1582     }   // case TERMINATE
1583
1584     case MEMREQUEST: {
1585       processmsg_memrequest_I();
1586       break;
1587     }   // case MEMREQUEST
1588
1589     case MEMRESPONSE: {
1590       processmsg_memresponse_I();
1591       break;
1592     }   // case MEMRESPONSE
1593
1594 #ifdef MULTICORE_GC
1595     // GC msgs
1596     case GCSTARTPRE: {
1597       processmsg_gcstartpre_I();
1598       break;
1599     }   // case GCSTARTPRE
1600         
1601         case GCSTARTINIT: {
1602       processmsg_gcstartinit_I();
1603       break;
1604     }   // case GCSTARTINIT
1605
1606     case GCSTART: {
1607       // receive a start GC msg
1608       processmsg_gcstart_I();
1609       break;
1610     }   // case GCSTART
1611
1612     case GCSTARTCOMPACT: {
1613       // a compact phase start msg
1614       processmsg_gcstartcompact_I();
1615       break;
1616     }   // case GCSTARTCOMPACT
1617
1618         case GCSTARTMAPINFO: {
1619       // received a flush phase start msg
1620       processmsg_gcstartmapinfo_I();
1621       break;
1622     }   // case GCSTARTFLUSH
1623
1624     case GCSTARTFLUSH: {
1625       // received a flush phase start msg
1626       processmsg_gcstartflush_I();
1627       break;
1628     }   // case GCSTARTFLUSH
1629
1630     case GCFINISHPRE: {
1631       processmsg_gcfinishpre_I();
1632       break;
1633     }   // case GCFINISHPRE
1634         
1635         case GCFINISHINIT: {
1636       processmsg_gcfinishinit_I();
1637       break;
1638     }   // case GCFINISHINIT
1639
1640     case GCFINISHMARK: {
1641       processmsg_gcfinishmark_I();
1642       break;
1643     }   // case GCFINISHMARK
1644
1645     case GCFINISHCOMPACT: {
1646       // received a compact phase finish msg
1647       processmsg_gcfinishcompact_I();
1648       break;
1649     }   // case GCFINISHCOMPACT
1650
1651         case GCFINISHMAPINFO: {
1652       processmsg_gcfinishmapinfo_I();
1653       break;
1654     }   // case GCFINISHMAPINFO
1655
1656     case GCFINISHFLUSH: {
1657       processmsg_gcfinishflush_I();
1658       break;
1659     }   // case GCFINISHFLUSH
1660
1661     case GCFINISH: {
1662       // received a GC finish msg
1663       gcphase = FINISHPHASE;
1664       break;
1665     }   // case GCFINISH
1666
1667     case GCMARKCONFIRM: {
1668       // received a marked phase finish confirm request msg
1669       // all cores should do mark
1670       processmsg_gcmarkconfirm_I();
1671       break;
1672     }   // case GCMARKCONFIRM
1673
1674     case GCMARKREPORT: {
1675       processmsg_gcmarkreport_I();
1676       break;
1677     }   // case GCMARKREPORT
1678
1679     case GCMARKEDOBJ: {
1680       processmsg_gcmarkedobj_I();
1681       break;
1682     }   // case GCMARKEDOBJ
1683
1684     case GCMOVESTART: {
1685       // received a start moving objs msg
1686       processmsg_gcmovestart_I();
1687       break;
1688     }   // case GCMOVESTART
1689
1690     case GCMAPREQUEST: {
1691       // received a mapping info request msg
1692       processmsg_gcmaprequest_I();
1693       break;
1694     }   // case GCMAPREQUEST
1695
1696     case GCMAPINFO: {
1697       // received a mapping info response msg
1698       processmsg_gcmapinfo_I();
1699       break;
1700     }   // case GCMAPINFO
1701
1702     case GCMAPTBL: {
1703       // received a mapping tbl response msg
1704       processmsg_gcmaptbl_I();
1705       break;
1706     }   // case GCMAPTBL
1707         
1708         case GCLOBJREQUEST: {
1709       // received a large objs info request msg
1710       transferMarkResults_I();
1711       break;
1712     }   // case GCLOBJREQUEST
1713
1714     case GCLOBJINFO: {
1715       // received a large objs info response msg
1716       processmsg_gclobjinfo_I();
1717       break;
1718     }   // case GCLOBJINFO
1719
1720     case GCLOBJMAPPING: {
1721       // received a large obj mapping info msg
1722       processmsg_gclobjmapping_I();
1723       break;
1724     }  // case GCLOBJMAPPING
1725
1726 #ifdef GC_PROFILE
1727         case GCPROFILES: {
1728       // received a gcprofiles msg
1729       processmsg_gcprofiles_I();
1730       break;
1731     }
1732 #endif // GC_PROFILE
1733
1734 #ifdef GC_CACHE_ADAPT
1735         case GCSTARTPREF: {
1736       // received a gcstartpref msg
1737       processmsg_gcstartpref_I();
1738       break;
1739     }
1740
1741         case GCFINISHPREF: {
1742       // received a gcfinishpref msg
1743       processmsg_gcfinishpref_I();
1744       break;
1745     }
1746 #endif // GC_CACHE_ADAPT
1747 #endif // #ifdef MULTICORE_GC
1748
1749     default:
1750       break;
1751     }  // switch(type)
1752     msglength = BAMBOO_MSG_BUF_LENGTH;
1753
1754     if((msgdataindex != msgdatalast) || (msgdatafull)) {
1755       // still have available msg
1756       goto processmsg;
1757     }
1758 #ifndef CLOSE_PRINT
1759     BAMBOO_DEBUGPRINT(0xe88d);
1760 #endif
1761
1762     // have new coming msg
1763     if(BAMBOO_MSG_AVAIL() != 0) {
1764       goto msg;
1765     } // TODO
1766
1767 #ifdef TASK
1768 #ifdef PROFILE_INTERRUPT
1769   if(!interruptInfoOverflow) {
1770     interruptInfoArray[interruptInfoIndex]->endTime=BAMBOO_GET_EXE_TIME();
1771     interruptInfoIndex++;
1772     if(interruptInfoIndex == INTERRUPTINFOLENGTH) {
1773       interruptInfoOverflow = true;
1774     }
1775   }
1776 #endif
1777 #endif // TASK
1778     return (int)type;
1779   } else {
1780     // not a whole msg
1781 #ifndef CLOSE_PRINT
1782     BAMBOO_DEBUGPRINT(0xe88e);
1783 #endif
1784     return -2;
1785   }
1786 }
1787
1788 #endif // MULTICORE