3 import java.io.FileOutputStream;
4 import java.io.PrintWriter;
5 import java.util.HashSet;
6 import java.util.Hashtable;
7 import java.util.Iterator;
8 import java.util.LinkedList;
9 import java.util.Queue;
11 import java.util.Vector;
13 import Analysis.Locality.LocalityBinding;
14 import Analysis.Scheduling.Schedule;
15 import Analysis.TaskStateAnalysis.FEdge;
16 import Analysis.TaskStateAnalysis.FlagState;
17 import Analysis.TaskStateAnalysis.SafetyAnalysis;
18 import Analysis.OwnershipAnalysis.AllocationSite;
19 import Analysis.OwnershipAnalysis.OwnershipAnalysis;
20 import Analysis.OwnershipAnalysis.HeapRegionNode;
21 import Analysis.Prefetch.*;
22 import IR.ClassDescriptor;
24 import IR.FlagDescriptor;
25 import IR.MethodDescriptor;
27 import IR.TagVarDescriptor;
28 import IR.TaskDescriptor;
29 import IR.TypeDescriptor;
31 import IR.VarDescriptor;
32 import IR.Tree.DNFFlag;
33 import IR.Tree.DNFFlagAtom;
34 import IR.Tree.FlagExpressionNode;
35 import IR.Tree.TagExpressionList;
37 public class BuildCodeMultiCore extends BuildCode {
38 private Vector<Schedule> scheduling;
40 Schedule currentSchedule;
41 Hashtable[] fsate2qnames;
42 String objqarrayprefix= "objqueuearray4class";
43 String objqueueprefix = "objqueue4parameter_";
44 String paramqarrayprefix = "paramqueuearray4task";
45 String coreqarrayprefix = "paramqueuearrays_core";
46 String taskprefix = "task_";
47 String taskarrayprefix = "taskarray_core";
48 String otqueueprefix = "___otqueue";
49 int startupcorenum; // record the core containing startup task, suppose only one core can hava startup object
51 private OwnershipAnalysis m_oa;
52 private Vector<Vector<Integer>> m_aliasSets;
53 Hashtable<Integer, Vector<FlatNew>> m_aliasFNTbl4Para;
54 Hashtable<FlatNew, Vector<FlatNew>> m_aliasFNTbl;
55 Hashtable<FlatNew, Vector<Integer>> m_aliaslocksTbl4FN;
57 public BuildCodeMultiCore(State st,
61 Vector<Schedule> scheduling,
63 PrefetchAnalysis pa) {
64 super(st, temptovar, typeutil, sa, pa);
65 this.scheduling = scheduling;
66 this.coreNum = coreNum;
67 this.currentSchedule = null;
68 this.fsate2qnames = null;
69 this.startupcorenum = 0;
71 // sometimes there are extra cores then needed in scheduling
73 // currently, it is guaranteed that in scheduling, the corenum
74 // is started from 0 and continuous.
75 // MAY need modification here in the future when take hardware
76 // information into account.
77 if(this.scheduling.size() < this.coreNum) {
78 this.coreNum = this.scheduling.size();
82 this.m_aliasSets = null;
83 this.m_aliasFNTbl4Para = null;
84 this.m_aliasFNTbl = null;
85 this.m_aliaslocksTbl4FN = null;
88 public void setOwnershipAnalysis(OwnershipAnalysis m_oa) {
92 public void buildCode() {
93 /* Create output streams to write to */
94 PrintWriter outclassdefs=null;
95 PrintWriter outstructs=null;
96 PrintWriter outmethodheader=null;
97 PrintWriter outmethod=null;
98 PrintWriter outvirtual=null;
99 PrintWriter outtask=null;
100 PrintWriter outtaskdefs=null;
101 //PrintWriter outoptionalarrays=null;
102 //PrintWriter optionalheaders=null;
105 outstructs=new PrintWriter(new FileOutputStream(PREFIX+"structdefs.h"), true);
106 outmethodheader=new PrintWriter(new FileOutputStream(PREFIX+"methodheaders.h"), true);
107 outclassdefs=new PrintWriter(new FileOutputStream(PREFIX+"classdefs.h"), true);
108 outvirtual=new PrintWriter(new FileOutputStream(PREFIX+"virtualtable.h"), true);
109 outmethod=new PrintWriter(new FileOutputStream(PREFIX+"methods.c"), true);
111 outtask=new PrintWriter(new FileOutputStream(PREFIX+"task.h"), true);
112 outtaskdefs=new PrintWriter(new FileOutputStream(PREFIX+"taskdefs.c"), true);
115 outoptionalarrays=new PrintWriter(new FileOutputStream(PREFIX+"optionalarrays.c"), true);
116 optionalheaders=new PrintWriter(new FileOutputStream(PREFIX+"optionalstruct.h"), true);
119 /*if (state.structfile!=null) {
120 outrepairstructs=new PrintWriter(new FileOutputStream(PREFIX+state.structfile+".struct"), true);
122 } catch (Exception e) {
127 /* Build the virtual dispatch tables */
128 super.buildVirtualTables(outvirtual);
130 /* Output includes */
131 outmethodheader.println("#ifndef METHODHEADERS_H");
132 outmethodheader.println("#define METHODHEADERS_H");
133 outmethodheader.println("#include \"structdefs.h\"");
135 outmethodheader.println("#include \"dstm.h\"");*/
137 /* Output Structures */
138 super.outputStructs(outstructs);
140 // Output the C class declarations
141 // These could mutually reference each other
142 super.outputClassDeclarations(outclassdefs);
144 // Output function prototypes and structures for parameters
145 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
147 while(it.hasNext()) {
149 ClassDescriptor cn=(ClassDescriptor)it.next();
150 super.generateCallStructs(cn, outclassdefs, outstructs, outmethodheader);
152 outclassdefs.close();
155 /* Map flags to integers */
156 /* The runtime keeps track of flags using these integers */
157 it=state.getClassSymbolTable().getDescriptorsIterator();
158 while(it.hasNext()) {
159 ClassDescriptor cn=(ClassDescriptor)it.next();
163 generateTaskStructs(outstructs, outmethodheader);
165 /* Outputs generic task structures if this is a task
167 outputTaskTypes(outtask);
170 /* Build the actual methods */
171 super.outputMethods(outmethod);
174 Iterator[] taskits = new Iterator[this.coreNum];
175 for(int i = 0; i < taskits.length; ++i) {
178 int[] numtasks = new int[this.coreNum];
179 int[][] numqueues = new int[this.coreNum][numclasses];
180 /* Output code for tasks */
181 for(int i = 0; i < this.scheduling.size(); ++i) {
182 this.currentSchedule = this.scheduling.elementAt(i);
183 outputTaskCode(outtaskdefs, outmethod, outtask, taskits, numtasks, numqueues);
186 // Output task descriptors
187 boolean comma = false;
188 outtaskdefs.println("struct parameterwrapper ** objectqueues[][NUMCLASSES] = {");
189 boolean needcomma = false;
190 for(int i = 0; i < numqueues.length ; ++i) {
192 outtaskdefs.println(",");
196 outtaskdefs.println("/* object queue array for core " + i + "*/");
197 outtaskdefs.print("{");
199 for(int j = 0; j < numclasses; ++j) {
201 outtaskdefs.println(",");
205 outtaskdefs.print(this.objqarrayprefix + j + "_core" + i);
207 outtaskdefs.print("}");
209 outtaskdefs.println("};");
211 outtaskdefs.println("int numqueues[][NUMCLASSES] = {");
212 for(int i = 0; i < numqueues.length; ++i) {
214 outtaskdefs.println(",");
218 int[] tmparray = numqueues[i];
220 outtaskdefs.print("{");
221 for(int j = 0; j < tmparray.length; ++j) {
223 outtaskdefs.print(",");
227 outtaskdefs.print(tmparray[j]);
229 outtaskdefs.print("}");
231 outtaskdefs.println("};");
233 /* parameter queue arrays for all the tasks*/
234 outtaskdefs.println("struct parameterwrapper *** paramqueues[] = {");
236 for(int i = 0; i < this.coreNum ; ++i) {
238 outtaskdefs.println(",");
242 outtaskdefs.println("/* parameter queue array for core " + i + "*/");
243 outtaskdefs.print(this.coreqarrayprefix + i);
245 outtaskdefs.println("};");
247 for(int i = 0; i < taskits.length; ++i) {
248 outtaskdefs.println("struct taskdescriptor * " + this.taskarrayprefix + i + "[]={");
249 Iterator taskit = taskits[i];
252 while(taskit.hasNext()) {
253 TaskDescriptor td=(TaskDescriptor)taskit.next();
257 outtaskdefs.println(",");
258 outtaskdefs.print("&" + this.taskprefix +td.getCoreSafeSymbol(i));
261 outtaskdefs.println();
262 outtaskdefs.println("};");
264 outtaskdefs.println("struct taskdescriptor ** taskarray[]= {");
266 for(int i = 0; i < taskits.length; ++i) {
268 outtaskdefs.println(",");
271 outtaskdefs.print(this.taskarrayprefix + i);
273 outtaskdefs.println("};");
275 outtaskdefs.print("int numtasks[]= {");
277 for(int i = 0; i < taskits.length; ++i) {
279 outtaskdefs.print(",");
282 outtaskdefs.print(numtasks[i]);
284 outtaskdefs.println("};");
285 outtaskdefs.println("int corenum=0;");
288 outtask.println("#endif");
290 /* Record maximum number of task parameters */
291 outstructs.println("#define MAXTASKPARAMS "+maxtaskparams);
292 /* Record maximum number of all types, i.e. length of classsize[] */
293 outstructs.println("#define NUMTYPES "+(state.numClasses() + state.numArrays()));
294 /* Record number of cores */
295 outstructs.println("#define NUMCORES "+this.coreNum);
296 /* Record number of core containing startup task */
297 outstructs.println("#define STARTUPCORE "+this.startupcorenum);
298 } //else if (state.main!=null) {
299 /* Generate main method */
300 // outputMainMethod(outmethod);
303 /* Generate information for task with optional parameters */
304 /*if (state.TASK&&state.OPTIONAL){
305 generateOptionalArrays(outoptionalarrays, optionalheaders, state.getAnalysisResult(), state.getOptionalTaskDescriptors());
306 outoptionalarrays.close();
309 /* Output structure definitions for repair tool */
310 /*if (state.structfile!=null) {
311 buildRepairStructs(outrepairstructs);
312 outrepairstructs.close();
316 outmethodheader.println("#endif");
317 outmethodheader.close();
319 outstructs.println("#endif");
323 /** This function outputs (1) structures that parameters are
324 * passed in (when PRECISE GC is enabled) and (2) function
325 * prototypes for the tasks */
327 private void generateTaskStructs(PrintWriter output,
328 PrintWriter headersout) {
329 /* Cycle through tasks */
330 for(int i = 0; i < this.scheduling.size(); ++i) {
331 Schedule tmpschedule = this.scheduling.elementAt(i);
332 int num = tmpschedule.getCoreNum();
333 Iterator<TaskDescriptor> taskit = tmpschedule.getTasks().iterator();
335 while(taskit.hasNext()) {
336 /* Classify parameters */
337 TaskDescriptor task=taskit.next();
338 FlatMethod fm=state.getMethodFlat(task);
339 super.generateTempStructs(fm, null);
341 ParamsObject objectparams=(ParamsObject) paramstable.get(task);
342 TempObject objecttemps=(TempObject) tempstable.get(task);
344 /* Output parameter structure */
345 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
346 output.println("struct "+task.getCoreSafeSymbol(num)+"_params {");
347 output.println(" int size;");
348 output.println(" void * next;");
349 for(int j=0; j<objectparams.numPointers(); j++) {
350 TempDescriptor temp=objectparams.getPointer(j);
351 output.println(" struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
354 output.println("};\n");
355 if ((objectparams.numPointers()+fm.numTags())>maxtaskparams) {
356 maxtaskparams=objectparams.numPointers()+fm.numTags();
360 /* Output temp structure */
361 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
362 output.println("struct "+task.getCoreSafeSymbol(num)+"_locals {");
363 output.println(" int size;");
364 output.println(" void * next;");
365 for(int j=0; j<objecttemps.numPointers(); j++) {
366 TempDescriptor temp=objecttemps.getPointer(j);
367 if (temp.getType().isNull())
368 output.println(" void * "+temp.getSafeSymbol()+";");
369 else if(temp.getType().isTag())
370 output.println(" struct "+
371 (new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass))).getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
373 output.println(" struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
375 output.println("};\n");
378 /* Output task declaration */
379 headersout.print("void " + task.getCoreSafeSymbol(num)+"(");
381 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
382 headersout.print("struct "+task.getCoreSafeSymbol(num)+"_params * "+paramsprefix);
384 headersout.print("void * parameterarray[]");
385 headersout.println(");\n");
391 /* This method outputs code for each task. */
393 private void outputTaskCode(PrintWriter outtaskdefs,
394 PrintWriter outmethod,
399 /* Compile task based program */
400 outtaskdefs.println("#include \"task.h\"");
401 outtaskdefs.println("#include \"methodheaders.h\"");
403 /* Output object transfer queues into method.c*/
404 generateObjectTransQueues(outmethod);
406 //Vector[] qnames = new Vector[2];
407 int numclasses = numqueues[0].length;
408 Vector qnames[]= new Vector[numclasses];
409 for(int i = 0; i < qnames.length; ++i) {
412 Iterator<TaskDescriptor> taskit=this.currentSchedule.getTasks().iterator();
413 while(taskit.hasNext()) {
414 TaskDescriptor td=taskit.next();
415 FlatMethod fm=state.getMethodFlat(td);
416 generateTaskMethod(fm, null, outmethod);
417 generateTaskDescriptor(outtaskdefs, outtask, fm, td, qnames);
420 // generate queuearray for this core
421 int num = this.currentSchedule.getCoreNum();
422 boolean comma = false;
423 for(int i = 0; i < qnames.length; ++i) {
424 outtaskdefs.println("/* object queue array for class " + i + " on core " + num + "*/");
425 outtaskdefs.println("struct parameterwrapper * " + this.objqarrayprefix + i + "_core" + num + "[] = {");
427 Vector tmpvector = qnames[i];
428 if(tmpvector != null) {
429 for(int j = 0; j < tmpvector.size(); ++j) {
431 outtaskdefs.println(",");
435 outtaskdefs.print("&" + tmpvector.elementAt(j));
437 numqueues[num][i] = tmpvector.size();
439 numqueues[num][i] = 0;
441 outtaskdefs.println("};");
444 /* All the queues for tasks residing on this core*/
446 outtaskdefs.println("/* object queue array for tasks on core " + num + "*/");
447 outtaskdefs.println("struct parameterwrapper ** " + this.coreqarrayprefix + num + "[] = {");
448 taskit=this.currentSchedule.getTasks().iterator();
449 while(taskit.hasNext()) {
451 outtaskdefs.println(",");
455 TaskDescriptor td=taskit.next();
456 outtaskdefs.print(this.paramqarrayprefix + td.getCoreSafeSymbol(num));
458 outtaskdefs.println("};");
460 // record the iterator of tasks on this core
461 taskit=this.currentSchedule.getTasks().iterator();
462 taskits[num] = taskit;
463 numtasks[num] = this.currentSchedule.getTasks().size();
466 /** Prints out definitions for generic task structures */
467 private void outputTaskTypes(PrintWriter outtask) {
468 outtask.println("#ifndef _TASK_H");
469 outtask.println("#define _TASK_H");
470 outtask.println("#include \"ObjectHash.h\"");
471 outtask.println("#include \"structdefs.h\"");
472 outtask.println("#include \"Queue.h\"");
473 outtask.println("#include <string.h>");
474 outtask.println("#include \"runtime_arch.h\"");
475 //outtask.println("#ifdef RAW");
476 //outtask.println("#include <raw.h>");
477 //outtask.println("#endif");
479 outtask.println("struct tagobjectiterator {");
480 outtask.println(" int istag; /* 0 if object iterator, 1 if tag iterator */");
481 outtask.println(" struct ObjectIterator it; /* Object iterator */");
482 outtask.println(" struct ObjectHash * objectset;");
483 outtask.println("#ifdef OPTIONAL");
484 outtask.println(" int failedstate;");
485 outtask.println("#endif");
486 outtask.println(" int slot;");
487 outtask.println(" int tagobjindex; /* Index for tag or object depending on use */");
488 outtask.println(" /*if tag we have an object binding */");
489 outtask.println(" int tagid;");
490 outtask.println(" int tagobjectslot;");
491 outtask.println(" /*if object, we may have one or more tag bindings */");
492 outtask.println(" int numtags;");
493 outtask.println(" int tagbindings[MAXTASKPARAMS-1]; /* list slots */");
494 outtask.println("};");
496 outtask.println("struct parameterwrapper {");
497 outtask.println(" //int type;");
498 outtask.println(" struct ObjectHash * objectset;");
499 outtask.println(" int numberofterms;");
500 outtask.println(" int * intarray;");
501 outtask.println(" int numbertags;");
502 outtask.println(" int * tagarray;");
503 outtask.println(" struct taskdescriptor * task;");
504 outtask.println(" int slot;");
505 outtask.println(" struct tagobjectiterator iterators[MAXTASKPARAMS-1];");
506 outtask.println("};");
508 outtask.println("extern struct parameterwrapper ** objectqueues[][NUMCLASSES];");
509 outtask.println("extern int numqueues[][NUMCLASSES];");
511 outtask.println("struct parameterdescriptor {");
512 outtask.println(" int type;");
513 outtask.println(" int numberterms;");
514 outtask.println(" int *intarray;");
515 outtask.println(" struct parameterwrapper * queue;");
516 outtask.println(" int numbertags;");
517 outtask.println(" int *tagarray;");
518 outtask.println("};");
520 outtask.println("struct taskdescriptor {");
521 outtask.println(" void * taskptr;");
522 outtask.println(" int numParameters;");
523 outtask.println(" int numTotal;");
524 outtask.println(" struct parameterdescriptor **descriptorarray;");
525 outtask.println(" char * name;");
526 outtask.println("};");
528 outtask.println("extern struct taskdescriptor ** taskarray[];");
529 outtask.println("extern int numtasks[];");
530 outtask.println("extern int corenum;"); // define corenum to identify different core
531 outtask.println("extern struct parameterwrapper *** paramqueues[];");
535 private void generateObjectTransQueues(PrintWriter output) {
536 if(this.fsate2qnames == null) {
537 this.fsate2qnames = new Hashtable[this.coreNum];
538 for(int i = 0; i < this.fsate2qnames.length; ++i) {
539 this.fsate2qnames[i] = null;
542 int num = this.currentSchedule.getCoreNum();
543 assert(this.fsate2qnames[num] == null);
544 Hashtable<FlagState, String> flag2qname = new Hashtable<FlagState, String>();
545 this.fsate2qnames[num] = flag2qname;
546 Hashtable<FlagState, Queue<Integer>> targetCoreTbl = this.currentSchedule.getTargetCoreTable();
547 if(targetCoreTbl != null) {
548 Object[] keys = targetCoreTbl.keySet().toArray();
550 output.println("/* Object transfer queues for core" + num + ".*/");
551 for(int i = 0; i < keys.length; ++i) {
552 FlagState tmpfstate = (FlagState)keys[i];
553 Object[] targetcores = targetCoreTbl.get(tmpfstate).toArray();
554 String queuename = this.otqueueprefix + tmpfstate.getClassDescriptor().getCoreSafeSymbol(num) + tmpfstate.getuid() + "___";
555 String queueins = queuename + "ins";
556 flag2qname.put(tmpfstate, queuename);
557 output.println("struct " + queuename + " {");
558 output.println(" int * cores;");
559 output.println(" int index;");
560 output.println(" int length;");
561 output.println("};");
562 output.print("int " + queuename + "cores[] = {");
563 for(int j = 0; j < targetcores.length; ++j) {
567 output.print(((Integer)targetcores[j]).intValue());
569 output.println("};");
570 output.println("struct " + queuename + " " + queueins + "= {");
571 output.println(/*".cores = " + */ queuename + "cores,");
572 output.println(/*".index = " + */ "0,");
573 output.println(/*".length = " +*/ targetcores.length + "};");
579 private void generateTaskMethod(FlatMethod fm,
581 PrintWriter output) {
582 /*if (State.PRINTFLAT)
583 System.out.println(fm.printMethod());*/
584 TaskDescriptor task=fm.getTask();
585 assert(task != null);
586 int num = this.currentSchedule.getCoreNum();
588 //ParamsObject objectparams=(ParamsObject)paramstable.get(lb!=null?lb:task);
589 generateTaskHeader(fm, lb, task,output);
590 // output code to check if need to do gc
591 if(state.MULTICOREGC) {
592 output.println("#ifdef MULTICORE_GC");
593 output.println("gc();");
594 output.println("#endif");
596 TempObject objecttemp=(TempObject) tempstable.get(lb!=null ? lb : task);
597 /*if (state.DSM&&lb.getHasAtomic()) {
598 output.println("transrecord_t * trans;");
601 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
602 output.print(" struct "+task.getCoreSafeSymbol(num)+"_locals "+localsprefix+"={");
604 output.print(objecttemp.numPointers()+",");
605 output.print(paramsprefix);
606 for(int j=0; j<objecttemp.numPointers(); j++)
607 output.print(", NULL");
608 output.println("};");
611 for(int i=0; i<objecttemp.numPrimitives(); i++) {
612 TempDescriptor td=objecttemp.getPrimitive(i);
613 TypeDescriptor type=td.getType();
615 output.println(" void * "+td.getSafeSymbol()+";");
616 else if (type.isClass()||type.isArray())
617 output.println(" struct "+type.getSafeSymbol()+" * "+td.getSafeSymbol()+";");
619 output.println(" "+type.getSafeSymbol()+" "+td.getSafeSymbol()+";");
622 for(int i = 0; i < fm.numParameters(); ++i) {
623 TempDescriptor temp = fm.getParameter(i);
624 output.println(" int "+generateTempFlagName(fm, temp, lb)+" = "+super.generateTemp(fm, temp, lb)+
628 /* Assign labels to FlatNode's if necessary.*/
630 Hashtable<FlatNode, Integer> nodetolabel=super.assignLabels(fm);
632 /* Check to see if we need to do a GC if this is a
633 * multi-threaded program...*/
635 /*if ((state.THREAD||state.DSM)&&GENERATEPRECISEGC) {
636 if (state.DSM&&lb.isAtomic())
637 output.println("checkcollect2(&"+localsprefix+",trans);");
639 output.println("checkcollect(&"+localsprefix+");");
642 /* Create queues to store objects need to be transferred to other cores and their destination*/
643 output.println(" struct Queue * totransobjqueue = createQueue();");
644 output.println(" struct transObjInfo * tmpObjInfo = NULL;");
646 this.m_aliasSets = null;
647 this.m_aliasFNTbl4Para = null;
648 this.m_aliasFNTbl = null;
649 this.m_aliaslocksTbl4FN = null;
650 outputAliasLockCode(fm, lb, output);
652 /* generate print information for RAW version */
653 output.println("#ifdef MULTICORE");
656 output.println("int tmpsum = 0;");
657 output.println("char * taskname = \"" + task.getSymbol() + "\";");
658 output.println("int tmplen = " + task.getSymbol().length() + ";");
659 output.println("int tmpindex = 1;");
660 output.println("for(;tmpindex < tmplen; tmpindex++) {");
661 output.println(" tmpsum = tmpsum * 10 + *(taskname + tmpindex) - '0';");
664 output.println("#ifdef RAWPATH");
666 output.println("BAMBOO_DEBUGPRINT(0xAAAA);");
667 output.println("BAMBOO_DEBUGPRINT_REG(tmpsum);");
669 output.println("BAMBOO_START_CRITICAL_SECTION();");
670 output.println("tprintf(\"Process %x(%d): task %s\\n\", corenum, corenum, \"" + task.getSymbol() + "\");");
671 output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
673 //output.println("BAMBOO_DEBUGPRINT(BAMBOO_GET_EXE_TIME());");
674 output.println("#endif");
675 output.println("#ifdef DEBUG");
677 output.println("BAMBOO_DEBUGPRINT(0xAAAA);");
678 output.println("BAMBOO_DEBUGPRINT_REG(tmpsum);");
680 output.println("BAMBOO_START_CRITICAL_SECTION();");
681 output.println("tprintf(\"Process %x(%d): task %s\\n\", corenum, corenum, \"" + task.getSymbol() + "\");");
682 output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
684 output.println("#endif");
688 output.println("#endif");
690 for(int i = 0; i < fm.numParameters(); ++i) {
691 TempDescriptor temp = fm.getParameter(i);
692 output.println(" ++" + super.generateTemp(fm, temp, lb)+"->version;");
695 /* Do the actual code generation */
696 FlatNode current_node=null;
697 HashSet tovisit=new HashSet();
698 HashSet visited=new HashSet();
699 tovisit.add(fm.getNext(0));
700 while(current_node!=null||!tovisit.isEmpty()) {
701 if (current_node==null) {
702 current_node=(FlatNode)tovisit.iterator().next();
703 tovisit.remove(current_node);
705 visited.add(current_node);
706 if (nodetolabel.containsKey(current_node))
707 output.println("L"+nodetolabel.get(current_node)+":");
708 /*if (state.INSTRUCTIONFAILURE) {
709 if (state.THREAD||state.DSM) {
710 output.println("if ((++instructioncount)>failurecount) {instructioncount=0;injectinstructionfailure();}");
713 output.println("if ((--instructioncount)==0) injectinstructionfailure();");
715 if (current_node.numNext()==0) {
717 super.generateFlatNode(fm, lb, current_node, output);
718 if (current_node.kind()!=FKind.FlatReturnNode) {
719 //output.println(" flushAll();");
720 output.println("#ifdef CACHEFLUSH");
721 output.println("BAMBOO_START_CRITICAL_SECTION();");
722 output.println("#ifdef DEBUG");
723 output.println("BAMBOO_DEBUGPRINT(0xec00);");
724 output.println("#endif");
725 output.println("BAMBOO_CACHE_FLUSH_ALL();");
726 output.println("#ifdef DEBUG");
727 output.println("BAMBOO_DEBUGPRINT(0xecff);");
728 output.println("#endif");
729 output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
730 output.println("#endif");
731 outputTransCode(output);
732 output.println(" return;");
735 } else if(current_node.numNext()==1) {
737 super.generateFlatNode(fm, lb, current_node, output);
738 FlatNode nextnode=current_node.getNext(0);
739 if (visited.contains(nextnode)) {
740 output.println("goto L"+nodetolabel.get(nextnode)+";");
743 current_node=nextnode;
744 } else if (current_node.numNext()==2) {
747 super.generateFlatCondBranch(fm, lb, (FlatCondBranch)current_node, "L"+nodetolabel.get(current_node.getNext(1)), output);
748 if (!visited.contains(current_node.getNext(1)))
749 tovisit.add(current_node.getNext(1));
750 if (visited.contains(current_node.getNext(0))) {
751 output.println("goto L"+nodetolabel.get(current_node.getNext(0))+";");
754 current_node=current_node.getNext(0);
755 } else throw new Error();
758 output.println("}\n\n");
761 /** This method outputs TaskDescriptor information */
762 private void generateTaskDescriptor(PrintWriter output,
767 int num = this.currentSchedule.getCoreNum();
769 output.println("/* TaskDescriptor information for task " + task.getSymbol() + " on core " + num + "*/");
771 for (int i=0; i<task.numParameters(); i++) {
772 VarDescriptor param_var=task.getParameter(i);
773 TypeDescriptor param_type=task.getParamType(i);
774 FlagExpressionNode param_flag=task.getFlag(param_var);
775 TagExpressionList param_tag=task.getTag(param_var);
778 if (param_flag==null) {
779 output.println("int parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+"[]={");
780 output.println("0x0, 0x0 };");
783 DNFFlag dflag=param_flag.getDNF();
784 dnfterms=dflag.size();
786 Hashtable flags=(Hashtable)flagorder.get(param_type.getClassDesc());
787 output.println("int parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+"[]={");
788 for(int j=0; j<dflag.size(); j++) {
791 Vector term=dflag.get(j);
794 for(int k=0; k<term.size(); k++) {
795 DNFFlagAtom dfa=(DNFFlagAtom)term.get(k);
796 FlagDescriptor fd=dfa.getFlag();
797 boolean negated=dfa.getNegated();
798 int flagid=1<<((Integer)flags.get(fd)).intValue();
803 output.print("0x"+Integer.toHexString(andmask)+", 0x"+Integer.toHexString(checkmask));
805 output.println("};");
808 output.println("int parametertag_"+i+"_"+task.getCoreSafeSymbol(num)+"[]={");
809 //BUG...added next line to fix, test with any task program
811 for(int j=0; j<param_tag.numTags(); j++) {
814 /* for each tag we need */
815 /* which slot it is */
816 /* what type it is */
817 TagVarDescriptor tvd=(TagVarDescriptor)task.getParameterTable().get(param_tag.getName(j));
818 TempDescriptor tmp=param_tag.getTemp(j);
819 int slot=fm.getTagInt(tmp);
820 output.println(slot+", "+state.getTagId(tvd.getTag()));
822 output.println("};");
824 // generate object queue for this parameter
825 String qname = this.objqueueprefix+i+"_"+task.getCoreSafeSymbol(num);
826 if(param_type.getClassDesc().getSymbol().equals("StartupObject")) {
827 this.startupcorenum = num;
829 if(qnames[param_type.getClassDesc().getId()] == null) {
830 qnames[param_type.getClassDesc().getId()] = new Vector();
832 qnames[param_type.getClassDesc().getId()].addElement(qname);
833 outtask.println("extern struct parameterwrapper " + qname + ";");
834 output.println("struct parameterwrapper " + qname + "={");
835 output.println(".objectset = 0,"); // objectset
836 output.println("/* number of DNF terms */ .numberofterms = "+dnfterms+","); // numberofterms
837 output.println(".intarray = parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+","); // intarray
840 output.println("/* number of tags */ .numbertags = "+param_tag.numTags()+",");
842 output.println("/* number of tags */ .numbertags = 0,");
843 output.println(".tagarray = parametertag_"+i+"_"+task.getCoreSafeSymbol(num)+","); // tagarray
844 output.println(".task = 0,"); // task
845 output.println(".slot = " + i + ","); // slot
847 output.println("};");
849 output.println("struct parameterdescriptor parameter_"+i+"_"+task.getCoreSafeSymbol(num)+"={");
850 output.println("/* type */"+param_type.getClassDesc().getId()+",");
851 output.println("/* number of DNF terms */"+dnfterms+",");
852 output.println("parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+","); // intarray
853 output.println("&" + qname + ","); // queue
854 //BUG, added next line to fix and else statement...test
855 //with any task program
857 output.println("/* number of tags */"+param_tag.numTags()+",");
859 output.println("/* number of tags */ 0,");
860 output.println("parametertag_"+i+"_"+task.getCoreSafeSymbol(num)); // tagarray
861 output.println("};");
864 /* parameter queues for this task*/
865 output.println("struct parameterwrapper * " + this.paramqarrayprefix + task.getCoreSafeSymbol(num)+"[] = {");
866 for (int i=0; i<task.numParameters(); i++) {
869 output.print("&" + this.objqueueprefix + i + "_" + task.getCoreSafeSymbol(num));
871 output.println("};");
873 output.println("struct parameterdescriptor * parameterdescriptors_"+task.getCoreSafeSymbol(num)+"[] = {");
874 for (int i=0; i<task.numParameters(); i++) {
877 output.print("¶meter_"+i+"_"+task.getCoreSafeSymbol(num));
879 output.println("};");
881 output.println("struct taskdescriptor " + this.taskprefix + task.getCoreSafeSymbol(num) + "={");
882 output.println("&"+task.getCoreSafeSymbol(num)+",");
883 output.println("/* number of parameters */" +task.numParameters() + ",");
884 int numtotal=task.numParameters()+fm.numTags();
885 output.println("/* number total parameters */" +numtotal + ",");
886 output.println("parameterdescriptors_"+task.getCoreSafeSymbol(num)+",");
887 output.println("\""+task.getSymbol()+"\"");
888 output.println("};");
893 /** This method generates header information for the task
894 * referenced by the Descriptor des. */
896 private void generateTaskHeader(FlatMethod fm,
899 PrintWriter output) {
901 ParamsObject objectparams=(ParamsObject)paramstable.get(lb!=null ? lb : des);
902 TaskDescriptor task=(TaskDescriptor) des;
904 int num = this.currentSchedule.getCoreNum();
905 //catch the constructor case
906 output.print("void ");
907 output.print(task.getCoreSafeSymbol(num)+"(");
909 boolean printcomma=false;
910 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
911 output.print("struct "+task.getCoreSafeSymbol(num)+"_params * "+paramsprefix);
915 /*if (state.DSM&&lb.isAtomic()) {
918 output.print("transrecord_t * trans");
922 if (!GENERATEPRECISEGC) {
924 output.println("void * parameterarray[]) {");
925 /* Unpack variables */
926 for(int i=0; i<objectparams.numPrimitives(); i++) {
927 TempDescriptor temp=objectparams.getPrimitive(i);
928 output.println("struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+"=parameterarray["+i+"];");
930 for(int i=0; i<fm.numTags(); i++) {
931 TempDescriptor temp=fm.getTag(i);
932 int offset=i+objectparams.numPrimitives();
933 output.println("struct ___TagDescriptor___ * "+temp.getSafeSymbol()+i+"___=parameterarray["+offset+"];"); // add i to fix bugs of duplicate definition of tags
936 if ((objectparams.numPrimitives()+fm.numTags())>maxtaskparams)
937 maxtaskparams=objectparams.numPrimitives()+fm.numTags();
938 } else output.println(") {");
941 protected void generateFlagOrAnd(FlatFlagActionNode ffan,
948 if (ffan.getTaskType()==FlatFlagActionNode.NEWOBJECT) {
949 output.println("flagorandinit("+super.generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+", 0x"+Integer.toHexString(andmask)+");");
951 int num = this.currentSchedule.getCoreNum();
952 ClassDescriptor cd = temp.getType().getClassDesc();
953 Vector<FlagState> initfstates = ffan.getInitFStates(cd);
954 for(int i = 0; i < initfstates.size(); ++i) {
955 FlagState tmpFState = initfstates.elementAt(i);
957 QueueInfo qinfo = outputqueues(tmpFState, num, output, false);
958 output.println("flagorand("+super.generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+
959 ", 0x"+Integer.toHexString(andmask)+", " + qinfo.qname +
960 ", " + qinfo.length + ");");
963 if(ffan.getTaskType()==FlatFlagActionNode.TASKEXIT) {
964 // generate codes for profiling, recording which task exit it is
965 output.println("#ifdef PROFILE");
966 output.println("setTaskExitIndex(" + ffan.getTaskExitIndex() + ");");
967 output.println("#endif");
972 protected void generateObjectDistribute(FlatFlagActionNode ffan,
976 PrintWriter output) {
977 ClassDescriptor cd = temp.getType().getClassDesc();
978 Vector<FlagState> initfstates = null;
979 Vector[] targetFStates = null;
980 if (ffan.getTaskType()==FlatFlagActionNode.NEWOBJECT) {
981 targetFStates = new Vector[1];
982 targetFStates[0] = ffan.getTargetFStates4NewObj(cd);
984 initfstates = ffan.getInitFStates(cd);
985 targetFStates = new Vector[initfstates.size()];
986 for(int i = 0; i < initfstates.size(); ++i) {
987 FlagState fs = initfstates.elementAt(i);
988 targetFStates[i] = ffan.getTargetFStates(fs);
990 if(!fs.isSetmask()) {
991 Hashtable flags=(Hashtable)flagorder.get(cd);
994 Iterator it_flags = fs.getFlags();
995 while(it_flags.hasNext()) {
996 FlagDescriptor fd = (FlagDescriptor)it_flags.next();
997 int flagid=1<<((Integer)flags.get(fd)).intValue();
1001 fs.setAndmask(andmask);
1002 fs.setCheckmask(checkmask);
1003 fs.setSetmask(true);
1007 boolean isolate = true; // check if this flagstate can associate to some task with multiple params which can
1008 // reside on multiple cores
1009 if((this.currentSchedule == null) && (fm.getMethod().getClassDesc().getSymbol().equals("ServerSocket"))) {
1010 // ServerSocket object will always reside on current core
1011 for(int j = 0; j < targetFStates.length; ++j) {
1012 if(initfstates != null) {
1013 FlagState fs = initfstates.elementAt(j);
1014 output.println("if(" + generateTempFlagName(fm, temp, lb) + "&(0x" + Integer.toHexString(fs.getAndmask())
1015 + ")==(0x" + Integer.toHexString(fs.getCheckmask()) + ")) {");
1017 Vector<FlagState> tmpfstates = (Vector<FlagState>)targetFStates[j];
1018 for(int i = 0; i < tmpfstates.size(); ++i) {
1019 FlagState tmpFState = tmpfstates.elementAt(i);
1021 // may have bugs here
1022 output.println("/* reside on this core*");
1023 output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1025 if(initfstates != null) {
1026 output.println("}");
1032 int num = this.currentSchedule.getCoreNum();
1033 Hashtable<FlagState, Queue<Integer>> targetCoreTbl = this.currentSchedule.getTargetCoreTable();
1034 for(int j = 0; j < targetFStates.length; ++j) {
1035 FlagState fs = null;
1036 if(initfstates != null) {
1037 fs = initfstates.elementAt(j);
1038 output.println("if((" + generateTempFlagName(fm, temp, lb) + "&(0x" + Integer.toHexString(fs.getAndmask())
1039 + "))==(0x" + Integer.toHexString(fs.getCheckmask()) + ")) {");
1041 Vector<FlagState> tmpfstates = (Vector<FlagState>)targetFStates[j];
1042 for(int i = 0; i < tmpfstates.size(); ++i) {
1043 FlagState tmpFState = tmpfstates.elementAt(i);
1045 if(this.currentSchedule.getAllyCoreTable() == null) {
1048 isolate = (this.currentSchedule.getAllyCoreTable().get(tmpFState) == null) ||
1049 (this.currentSchedule.getAllyCoreTable().get(tmpFState).size() == 0);
1052 Vector<Integer> sendto = new Vector<Integer>();
1053 Queue<Integer> queue = null;
1054 if(targetCoreTbl != null) {
1055 queue = targetCoreTbl.get(tmpFState);
1057 if((queue != null) &&
1058 ((queue.size() != 1) ||
1059 ((queue.size() == 1) && (queue.element().intValue() != num)))) {
1060 // this object may be transferred to other cores
1061 String queuename = (String) this.fsate2qnames[num].get(tmpFState);
1062 String queueins = queuename + "ins";
1064 Object[] cores = queue.toArray();
1066 Integer targetcore = (Integer)cores[0];
1067 if(queue.size() > 1) {
1068 index = queueins + ".index";
1070 if(queue.size() > 1) {
1071 output.println("switch(" + queueins + ".index % " + queueins + ".length) {");
1072 for(int k = 0; k < cores.length; ++k) {
1073 output.println("case " + k + ":");
1074 targetcore = (Integer)cores[k];
1075 if(targetcore.intValue() == num) {
1076 output.println("/* reside on this core*/");
1078 output.println("{");
1079 QueueInfo qinfo = outputqueues(tmpFState, num, output, true);
1080 output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", " + qinfo.qname +
1081 ", " + qinfo.length + ");");
1082 output.println("}");
1086 output.println("/* possibly needed by multi-parameter tasks on this core*//*");
1087 output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1088 }*/ // deleted 09/07/06, multi-param tasks are pinned to one core now
1092 // Is it possible to decide the actual queues?
1093 output.println("/* possibly needed by multi-parameter tasks on this core*//*");
1094 output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1095 }*/ // deleted 09/07/06, multi-param tasks are pinned to one core now
1096 output.println("/* transfer to core " + targetcore.toString() + "*/");
1097 output.println("{");
1098 // enqueue this object and its destinations for later process
1099 // all the possible queues
1100 QueueInfo qinfo = null;
1101 TranObjInfo tmpinfo = new TranObjInfo();
1102 tmpinfo.name = super.generateTemp(fm, temp, lb);
1103 tmpinfo.targetcore = targetcore;
1104 FlagState targetFS = this.currentSchedule.getTargetFState(tmpFState);
1105 if(targetFS != null) {
1106 tmpinfo.fs = targetFS;
1108 tmpinfo.fs = tmpFState;
1110 qinfo = outputtransqueues(tmpinfo.fs, targetcore, output);
1111 output.println("tmpObjInfo = RUNMALLOC(sizeof(struct transObjInfo));");
1112 output.println("tmpObjInfo->objptr = (void *)" + tmpinfo.name + ";");
1113 output.println("tmpObjInfo->targetcore = "+targetcore.toString()+";");
1114 output.println("tmpObjInfo->queues = " + qinfo.qname + ";");
1115 output.println("tmpObjInfo->length = " + qinfo.length + ";");
1116 output.println("addNewItem(totransobjqueue, (void*)tmpObjInfo);");
1117 output.println("}");
1119 output.println("break;");
1121 output.println("}");
1125 // Is it possible to decide the actual queues?
1126 output.println("/* possibly needed by multi-parameter tasks on this core*//*");
1127 output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1128 }*/ // deleted 09/07/06, multi-param tasks are pinned to one core now
1129 output.println("/* transfer to core " + targetcore.toString() + "*/");
1130 output.println("{");
1131 // enqueue this object and its destinations for later process
1132 // all the possible queues
1133 QueueInfo qinfo = null;
1134 TranObjInfo tmpinfo = new TranObjInfo();
1135 tmpinfo.name = super.generateTemp(fm, temp, lb);
1136 tmpinfo.targetcore = targetcore;
1137 FlagState targetFS = this.currentSchedule.getTargetFState(tmpFState);
1138 if(targetFS != null) {
1139 tmpinfo.fs = targetFS;
1141 tmpinfo.fs = tmpFState;
1143 qinfo = outputtransqueues(tmpinfo.fs, targetcore, output);
1144 output.println("tmpObjInfo = RUNMALLOC(sizeof(struct transObjInfo));");
1145 output.println("tmpObjInfo->objptr = (void *)" + tmpinfo.name + ";");
1146 output.println("tmpObjInfo->targetcore = "+targetcore.toString()+";");
1147 output.println("tmpObjInfo->queues = " + qinfo.qname + ";");
1148 output.println("tmpObjInfo->length = " + qinfo.length + ";");
1149 output.println("addNewItem(totransobjqueue, (void*)tmpObjInfo);");
1150 output.println("}");
1152 output.println("/* increase index*/");
1153 output.println("++" + queueins + ".index;");
1155 // this object will reside on current core
1156 output.println("/* reside on this core*/");
1158 output.println("{");
1159 QueueInfo qinfo = outputqueues(tmpFState, num, output, true);
1160 output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", " + qinfo.qname +
1161 ", " + qinfo.length + ");");
1162 output.println("}");
1166 output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1167 }*/ // deleted 09/07/06, multi-param tasks are pinned to one core now
1170 // codes for multi-params tasks
1172 // flagstate associated with some multi-params tasks
1173 // need to be send to other cores
1174 Vector<Integer> targetcores = this.currentSchedule.getAllyCores(tmpFState);
1175 output.println("/* send the shared object to possible queues on other cores*/");
1176 // TODO, temporary solution, send to mostly the first two
1177 int upperbound = targetcores.size() > 2? 2: targetcores.size();
1178 for(int k = 0; k < upperbound; ++k) {
1180 // add the information of exactly which queue
1181 int targetcore = targetcores.elementAt(k).intValue();
1182 if(!sendto.contains(targetcore)) {
1183 // previously not sended to this target core
1184 // enqueue this object and its destinations for later process
1185 output.println("{");
1186 // all the possible queues
1187 QueueInfo qinfo = null;
1188 TranObjInfo tmpinfo = new TranObjInfo();
1189 tmpinfo.name = super.generateTemp(fm, temp, lb);
1190 tmpinfo.targetcore = targetcore;
1191 FlagState targetFS = this.currentSchedule.getTargetFState(tmpFState);
1192 if(targetFS != null) {
1193 tmpinfo.fs = targetFS;
1195 tmpinfo.fs = tmpFState;
1197 qinfo = outputtransqueues(tmpinfo.fs, targetcore, output);
1198 output.println("tmpObjInfo = RUNMALLOC(sizeof(struct transObjInfo));");
1199 output.println("tmpObjInfo->objptr = (void *)" + tmpinfo.name + ";");
1200 output.println("tmpObjInfo->targetcore = "+targetcore+";");
1201 output.println("tmpObjInfo->queues = " + qinfo.qname + ";");
1202 output.println("tmpObjInfo->length = " + qinfo.length + ";");
1203 output.println("addNewItem(totransobjqueue, (void*)tmpObjInfo);");
1204 output.println("}");
1205 sendto.addElement(targetcore);
1211 if(initfstates != null) {
1212 output.println("}");
1217 private QueueInfo outputqueues(FlagState tmpFState,
1220 boolean isEnqueue) {
1222 QueueInfo qinfo = new QueueInfo();
1223 qinfo.qname = "queues_" + tmpFState.getLabel() + "_" + tmpFState.getiuid();
1224 output.println("struct parameterwrapper * " + qinfo.qname + "[] = {");
1225 Iterator it_edges = tmpFState.getEdgeVector().iterator();
1226 Vector<TaskDescriptor> residetasks = this.currentSchedule.getTasks();
1227 Vector<TaskDescriptor> tasks = new Vector<TaskDescriptor>();
1228 Vector<Integer> indexes = new Vector<Integer>();
1229 boolean comma = false;
1231 while(it_edges.hasNext()) {
1232 FEdge fe = (FEdge)it_edges.next();
1233 TaskDescriptor td = fe.getTask();
1234 int paraindex = fe.getIndex();
1235 if((!isEnqueue) || (isEnqueue && residetasks.contains(td))) {
1236 if((!tasks.contains(td)) ||
1237 ((tasks.contains(td)) && (paraindex != indexes.elementAt(tasks.indexOf(td)).intValue()))) {
1238 tasks.addElement(td);
1239 indexes.addElement(paraindex);
1241 output.println(",");
1245 output.print("&" + this.objqueueprefix + paraindex + "_" + td.getCoreSafeSymbol(num));
1250 output.println("};");
1254 private QueueInfo outputtransqueues(FlagState tmpFState,
1256 PrintWriter output) {
1258 QueueInfo qinfo = new QueueInfo();
1259 qinfo.qname = "queues_" + tmpFState.getLabel() + "_" + tmpFState.getiuid();
1260 output.println("int " + qinfo.qname + "_clone[] = {");
1261 Iterator it_edges = tmpFState.getEdgeVector().iterator();
1262 Vector<TaskDescriptor> residetasks = this.scheduling.get(targetcore).getTasks();
1263 Vector<TaskDescriptor> tasks = new Vector<TaskDescriptor>();
1264 Vector<Integer> indexes = new Vector<Integer>();
1265 boolean comma = false;
1267 while(it_edges.hasNext()) {
1268 FEdge fe = (FEdge)it_edges.next();
1269 TaskDescriptor td = fe.getTask();
1270 int paraindex = fe.getIndex();
1271 if(residetasks.contains(td)) {
1272 if((!tasks.contains(td)) ||
1273 ((tasks.contains(td)) && (paraindex != indexes.elementAt(tasks.indexOf(td)).intValue()))) {
1274 tasks.addElement(td);
1275 indexes.addElement(paraindex);
1277 output.println(",");
1281 output.print(residetasks.indexOf(td) + ", ");
1282 output.print(paraindex);
1287 output.println("};");
1288 output.println("int * " + qinfo.qname + " = RUNMALLOC(sizeof(int) * " + qinfo.length * 2 + ");");
1289 output.println("memcpy(" + qinfo.qname + ", (int *)" + qinfo.qname + "_clone, sizeof(int) * " + qinfo.length * 2 + ");");
1293 private class QueueInfo {
1295 public String qname;
1298 private String generateTempFlagName(FlatMethod fm,
1300 LocalityBinding lb) {
1301 MethodDescriptor md=fm.getMethod();
1302 TaskDescriptor task=fm.getTask();
1303 TempObject objecttemps=(TempObject) tempstable.get(lb!=null ? lb : md!=null ? md : task);
1305 if (objecttemps.isLocalPrim(td)||objecttemps.isParamPrim(td)) {
1306 return td.getSafeSymbol() + "_oldflag";
1309 if (objecttemps.isLocalPtr(td)) {
1310 return localsprefix+"_"+td.getSafeSymbol() + "_oldflag";
1313 if (objecttemps.isParamPtr(td)) {
1314 return paramsprefix+"_"+td.getSafeSymbol() + "_oldflag";
1319 protected void outputTransCode(PrintWriter output) {
1320 output.println("while(0 == isEmpty(totransobjqueue)) {");
1321 output.println(" struct transObjInfo * totransobj = (struct transObjInfo *)(getItem(totransobjqueue));");
1322 output.println(" transferObject(totransobj);");
1323 output.println(" RUNFREE(totransobj->queues);");
1324 output.println(" RUNFREE(totransobj);");
1325 output.println("}");
1326 output.println("freeQueue(totransobjqueue);");
1329 protected void outputAliasLockCode(FlatMethod fm,
1331 PrintWriter output) {
1332 if(this.m_oa == null) {
1335 TaskDescriptor td = fm.getTask();
1336 Object[] allocSites = this.m_oa.getFlaggedAllocationSitesReachableFromTask(td).toArray();
1337 Vector<Vector<Integer>> aliasSets = new Vector<Vector<Integer>>();
1338 Vector<Vector<FlatNew>> aliasFNSets = new Vector<Vector<FlatNew>>();
1339 Hashtable<Integer, Vector<FlatNew>> aliasFNTbl4Para = new Hashtable<Integer, Vector<FlatNew>>();
1340 Hashtable<FlatNew, Vector<FlatNew>> aliasFNTbl = new Hashtable<FlatNew, Vector<FlatNew>>();
1341 Set<HeapRegionNode> common;
1342 for( int i = 0; i < fm.numParameters(); ++i ) {
1343 // for the ith parameter check for aliases to all
1344 // higher numbered parameters
1345 aliasSets.add(null);
1346 for( int j = i + 1; j < fm.numParameters(); ++j ) {
1347 common = this.m_oa.createsPotentialAliases(td, i, j);
1348 if(!common.isEmpty()) {
1349 // ith parameter and jth parameter has alias, create lock to protect them
1350 if(aliasSets.elementAt(i) == null) {
1351 aliasSets.setElementAt(new Vector<Integer>(), i);
1353 aliasSets.elementAt(i).add(j);
1357 // for the ith parameter, check for aliases against
1358 // the set of allocation sites reachable from this
1360 aliasFNSets.add(null);
1361 for(int j = 0; j < allocSites.length; j++) {
1362 AllocationSite as = (AllocationSite)allocSites[j];
1363 common = this.m_oa.createsPotentialAliases(td, i, as);
1364 if( !common.isEmpty() ) {
1365 // ith parameter and allocationsite as has alias
1366 if(aliasFNSets.elementAt(i) == null) {
1367 aliasFNSets.setElementAt(new Vector<FlatNew>(), i);
1369 aliasFNSets.elementAt(i).add(as.getFlatNew());
1374 // for each allocation site check for aliases with
1375 // other allocation sites in the context of execution
1377 for( int i = 0; i < allocSites.length; ++i ) {
1378 AllocationSite as1 = (AllocationSite)allocSites[i];
1379 for(int j = i + 1; j < allocSites.length; j++) {
1380 AllocationSite as2 = (AllocationSite)allocSites[j];
1382 common = this.m_oa.createsPotentialAliases(td, as1, as2);
1383 if( !common.isEmpty() ) {
1384 // as1 and as2 has alias
1385 if(!aliasFNTbl.containsKey(as1.getFlatNew())) {
1386 aliasFNTbl.put(as1.getFlatNew(), new Vector<FlatNew>());
1388 if(!aliasFNTbl.get(as1.getFlatNew()).contains(as2.getFlatNew())) {
1389 aliasFNTbl.get(as1.getFlatNew()).add(as2.getFlatNew());
1395 // if FlatNew N1->N2->N3, we group N1, N2, N3 together
1396 Iterator<FlatNew> it = aliasFNTbl.keySet().iterator();
1397 Vector<FlatNew> visited = new Vector<FlatNew>();
1398 while(it.hasNext()) {
1399 FlatNew tmpfn = it.next();
1400 if(visited.contains(tmpfn)) {
1404 Queue<FlatNew> tovisit = new LinkedList<FlatNew>();
1405 Vector<FlatNew> tmpv = aliasFNTbl.get(tmpfn);
1410 for(int j = 0; j < tmpv.size(); j++) {
1411 tovisit.add(tmpv.elementAt(j));
1414 while(!tovisit.isEmpty()) {
1415 FlatNew fn = tovisit.poll();
1417 Vector<FlatNew> tmpset = aliasFNTbl.get(fn);
1418 if(tmpset != null) {
1419 // merge tmpset to the alias set of the ith parameter
1420 for(int j = 0; j < tmpset.size(); j++) {
1421 if(!tmpv.contains(tmpset.elementAt(j))) {
1422 tmpv.add(tmpset.elementAt(j));
1423 tovisit.add(tmpset.elementAt(j));
1426 aliasFNTbl.remove(fn);
1429 it = aliasFNTbl.keySet().iterator();
1432 // check alias between parameters and between parameter-flatnew
1433 for(int i = 0; i < aliasSets.size(); i++) {
1434 Queue<Integer> tovisit = new LinkedList<Integer>();
1435 Vector<Integer> tmpv = aliasSets.elementAt(i);
1440 for(int j = 0; j < tmpv.size(); j++) {
1441 tovisit.add(tmpv.elementAt(j));
1444 while(!tovisit.isEmpty()) {
1445 int index = tovisit.poll().intValue();
1446 Vector<Integer> tmpset = aliasSets.elementAt(index);
1447 if(tmpset != null) {
1448 // merge tmpset to the alias set of the ith parameter
1449 for(int j = 0; j < tmpset.size(); j++) {
1450 if(!tmpv.contains(tmpset.elementAt(j))) {
1451 tmpv.add(tmpset.elementAt(j));
1452 tovisit.add(tmpset.elementAt(j));
1455 aliasSets.setElementAt(null, index);
1458 Vector<FlatNew> tmpFNSet = aliasFNSets.elementAt(index);
1459 if(tmpFNSet != null) {
1460 // merge tmpFNSet to the aliasFNSet of the ith parameter
1461 if(aliasFNSets.elementAt(i) == null) {
1462 aliasFNSets.setElementAt(tmpFNSet, i);
1464 Vector<FlatNew> tmpFNv = aliasFNSets.elementAt(i);
1465 for(int j = 0; j < tmpFNSet.size(); j++) {
1466 if(!tmpFNv.contains(tmpFNSet.elementAt(j))) {
1467 tmpFNv.add(tmpFNSet.elementAt(j));
1471 aliasFNSets.setElementAt(null, index);
1477 int numparalock = 0;
1478 Vector<Vector<Integer>> tmpaliasSets = new Vector<Vector<Integer>>();
1479 for(int i = 0; i < aliasSets.size(); i++) {
1480 Vector<Integer> tmpv = aliasSets.elementAt(i);
1483 tmpaliasSets.add(tmpv);
1487 Vector<FlatNew> tmpFNv = aliasFNSets.elementAt(i);
1488 if(tmpFNv != null) {
1489 aliasFNTbl4Para.put(i, tmpFNv);
1495 numparalock = numlock;
1498 this.m_aliasSets = tmpaliasSets;
1499 tmpaliasSets.clear();
1500 tmpaliasSets = null;
1501 aliasFNSets.clear();
1503 this.m_aliasFNTbl4Para = aliasFNTbl4Para;
1504 this.m_aliasFNTbl = aliasFNTbl;
1505 numlock += this.m_aliasFNTbl.size();
1509 output.println("int aliaslocks[" + numlock + "];");
1510 output.println("int tmpi = 0;");
1511 // associate locks with parameters
1513 for(int i = 0; i < this.m_aliasSets.size(); i++) {
1514 Vector<Integer> toadd = this.m_aliasSets.elementAt(i);
1516 output.print("int tmplen_" + lockindex + " = 0;");
1517 output.println("void * tmpptrs_" + lockindex + "[] = {");
1518 for(int j = 0; j < toadd.size(); j++) {
1519 int para = toadd.elementAt(j).intValue();
1520 output.print(super.generateTemp(fm, fm.getParameter(para), lb));
1521 if(j < toadd.size() - 1) {
1524 output.println("};");
1527 output.println("aliaslocks[tmpi++] = getAliasLock(tmpptrs_" + lockindex + ", tmplen_" + lockindex + ", lockRedirectTbl);");
1529 for(int j = 0; j < toadd.size(); j++) {
1530 int para = toadd.elementAt(j).intValue();
1531 output.println("addAliasLock(" + super.generateTemp(fm, fm.getParameter(para), lb) + ", aliaslocks[" + i + "]);");
1533 // check if this lock is also associated with any FlatNew nodes
1534 if(this.m_aliasFNTbl4Para.containsKey(toadd.elementAt(0))) {
1535 if(this.m_aliaslocksTbl4FN == null) {
1536 this.m_aliaslocksTbl4FN = new Hashtable<FlatNew, Vector<Integer>>();
1538 Vector<FlatNew> tmpv = this.m_aliasFNTbl4Para.get(toadd.elementAt(0));
1539 for(int j = 0; j < tmpv.size(); j++) {
1540 FlatNew fn = tmpv.elementAt(j);
1541 if(!this.m_aliaslocksTbl4FN.containsKey(fn)) {
1542 this.m_aliaslocksTbl4FN.put(fn, new Vector<Integer>());
1544 this.m_aliaslocksTbl4FN.get(fn).add(i);
1546 this.m_aliasFNTbl4Para.remove(toadd.elementAt(0));
1551 Object[] key = this.m_aliasFNTbl4Para.keySet().toArray();
1552 for(int i = 0; i < key.length; i++) {
1553 int para = ((Integer)key[i]).intValue();
1555 output.println("void * tmpptrs_" + lockindex + "[] = {" + super.generateTemp(fm, fm.getParameter(para), lb) + "};");
1556 output.println("aliaslocks[tmpi++] = getAliasLock(tmpptrs_" + lockindex + ", 1, lockRedirectTbl);");
1558 output.println("addAliasLock(" + super.generateTemp(fm, fm.getParameter(para), lb) + ", aliaslocks[" + lockindex + "]);");
1559 Vector<FlatNew> tmpv = this.m_aliasFNTbl4Para.get(para);
1560 for(int j = 0; j < tmpv.size(); j++) {
1561 FlatNew fn = tmpv.elementAt(j);
1562 if(this.m_aliaslocksTbl4FN == null) {
1563 this.m_aliaslocksTbl4FN = new Hashtable<FlatNew, Vector<Integer>>();
1565 if(!this.m_aliaslocksTbl4FN.containsKey(fn)) {
1566 this.m_aliaslocksTbl4FN.put(fn, new Vector<Integer>());
1568 this.m_aliaslocksTbl4FN.get(fn).add(lockindex);
1573 // check m_aliasFNTbl for locks associated with FlatNew nodes
1574 Object[] FNkey = this.m_aliasFNTbl.keySet().toArray();
1575 for(int i = 0; i < FNkey.length; i++) {
1576 FlatNew fn = (FlatNew)FNkey[i];
1577 Vector<FlatNew> tmpv = this.m_aliasFNTbl.get(fn);
1579 output.println("aliaslocks[tmpi++] = (int)(RUNMALLOC(sizeof(int)));");
1581 if(this.m_aliaslocksTbl4FN == null) {
1582 this.m_aliaslocksTbl4FN = new Hashtable<FlatNew, Vector<Integer>>();
1584 if(!this.m_aliaslocksTbl4FN.containsKey(fn)) {
1585 this.m_aliaslocksTbl4FN.put(fn, new Vector<Integer>());
1587 this.m_aliaslocksTbl4FN.get(fn).add(lockindex);
1588 for(int j = 0; j < tmpv.size(); j++) {
1589 FlatNew tfn = tmpv.elementAt(j);
1590 if(!this.m_aliaslocksTbl4FN.containsKey(tfn)) {
1591 this.m_aliaslocksTbl4FN.put(tfn, new Vector<Integer>());
1593 this.m_aliaslocksTbl4FN.get(tfn).add(lockindex);
1600 protected void generateFlatReturnNode(FlatMethod fm,
1603 PrintWriter output) {
1604 if (frn.getReturnTemp()!=null) {
1605 if (frn.getReturnTemp().getType().isPtr())
1606 output.println("return (struct "+fm.getMethod().getReturnType().getSafeSymbol()+"*)"+generateTemp(fm, frn.getReturnTemp(), lb)+";");
1608 output.println("return "+generateTemp(fm, frn.getReturnTemp(), lb)+";");
1610 if(fm.getTask() != null) {
1611 output.println("#ifdef CACHEFLUSH");
1612 output.println("BAMBOO_START_CRITICAL_SECTION();");
1613 output.println("#ifdef DEBUG");
1614 output.println("BAMBOO_DEBUGPRINT(0xec00);");
1615 output.println("#endif");
1616 output.println("BAMBOO_CACHE_FLUSH_ALL();");
1617 output.println("#ifdef DEBUG");
1618 output.println("BAMBOO_DEBUGPRINT(0xecff);");
1619 output.println("#endif");
1620 output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
1621 output.println("#endif");
1622 outputTransCode(output);
1624 output.println("return;");
1628 protected void generateFlatNew(FlatMethod fm,
1631 PrintWriter output) {
1632 if (state.DSM && locality.getAtomic(lb).get(fn).intValue() > 0
1633 && !fn.isGlobal()) {
1634 // Stash pointer in case of GC
1635 String revertptr = super.generateTemp(fm, reverttable.get(lb), lb);
1636 output.println(revertptr + "=trans->revertlist;");
1638 if (fn.getType().isArray()) {
1639 int arrayid = state.getArrayNumber(fn.getType())
1640 + state.numClasses();
1641 if (fn.isGlobal()) {
1642 output.println(super.generateTemp(fm, fn.getDst(), lb)
1643 + "=allocate_newarrayglobal(trans, " + arrayid + ", "
1644 + super.generateTemp(fm, fn.getSize(), lb) + ");");
1645 } else if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1646 output.println(super.generateTemp(fm, fn.getDst(), lb)
1647 + "=allocate_newarray(&" + localsprefix + ", "
1648 + arrayid + ", " + super.generateTemp(fm, fn.getSize(), lb)
1651 output.println(super.generateTemp(fm, fn.getDst(), lb)
1652 + "=allocate_newarray(" + arrayid + ", "
1653 + super.generateTemp(fm, fn.getSize(), lb) + ");");
1656 if (fn.isGlobal()) {
1657 output.println(super.generateTemp(fm, fn.getDst(), lb)
1658 + "=allocate_newglobal(trans, "
1659 + fn.getType().getClassDesc().getId() + ");");
1660 } else if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1661 output.println(super.generateTemp(fm, fn.getDst(), lb)
1662 + "=allocate_new(&" + localsprefix + ", "
1663 + fn.getType().getClassDesc().getId() + ");");
1665 output.println(super.generateTemp(fm, fn.getDst(), lb)
1667 + fn.getType().getClassDesc().getId() + ");");
1670 if (state.DSM && locality.getAtomic(lb).get(fn).intValue() > 0
1671 && !fn.isGlobal()) {
1672 String revertptr = super.generateTemp(fm, reverttable.get(lb), lb);
1673 output.println("trans->revertlist=" + revertptr + ";");
1675 // create alias lock if necessary
1676 if((this.m_aliaslocksTbl4FN != null) && (this.m_aliaslocksTbl4FN.containsKey(fn))) {
1677 Vector<Integer> tmpv = this.m_aliaslocksTbl4FN.get(fn);
1678 for(int i = 0; i < tmpv.size(); i++) {
1679 output.println("addAliasLock(" + super.generateTemp(fm, fn.getDst(), lb) + ", aliaslocks[" + tmpv.elementAt(i).intValue() + "]);");
1682 // generate codes for profiling, recording how many new objects are created
1683 if(!fn.getType().isArray() &&
1684 (fn.getType().getClassDesc() != null)
1685 && (fn.getType().getClassDesc().hasFlags())) {
1686 output.println("#ifdef PROFILE");
1687 output.println("addNewObjInfo(\"" + fn.getType().getClassDesc().getSymbol() + "\");");
1688 output.println("#endif");
1694 public int targetcore;
1695 public FlagState fs;
1698 private boolean contains(Vector<TranObjInfo> sendto,
1700 if(sendto.size() == 0) {
1703 for(int i = 0; i < sendto.size(); i++) {
1704 TranObjInfo tmp = sendto.elementAt(i);
1705 if(!tmp.name.equals(t.name)) {
1708 if(tmp.targetcore != t.targetcore) {
1711 if(tmp.fs != t.fs) {