reorganize multicore version runtime codes for easy support of new platforms
[IRC.git] / Robust / src / IR / Flat / BuildCodeMultiCore.java
1 package IR.Flat;
2
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;
10 import java.util.Set;
11 import java.util.Vector;
12
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;
23 import IR.Descriptor;
24 import IR.FlagDescriptor;
25 import IR.MethodDescriptor;
26 import IR.State;
27 import IR.TagVarDescriptor;
28 import IR.TaskDescriptor;
29 import IR.TypeDescriptor;
30 import IR.TypeUtil;
31 import IR.VarDescriptor;
32 import IR.Tree.DNFFlag;
33 import IR.Tree.DNFFlagAtom;
34 import IR.Tree.FlagExpressionNode;
35 import IR.Tree.TagExpressionList;
36
37 public class BuildCodeMultiCore extends BuildCode {
38   private Vector<Schedule> scheduling;
39   int coreNum;
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
50
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;
56
57   public BuildCodeMultiCore(State st, 
58                             Hashtable temptovar, 
59                             TypeUtil typeutil, 
60                             SafetyAnalysis sa, 
61                             Vector<Schedule> scheduling, 
62                             int coreNum, 
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;
70
71     // sometimes there are extra cores then needed in scheduling
72     // TODO
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();
79     }
80
81     this.m_oa = null;
82     this.m_aliasSets = null;
83     this.m_aliasFNTbl4Para = null;
84     this.m_aliasFNTbl = null;
85     this.m_aliaslocksTbl4FN = null;
86   }
87
88   public void setOwnershipAnalysis(OwnershipAnalysis m_oa) {
89     this.m_oa = m_oa;
90   }
91
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;
103
104     try {
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);
110       if (state.TASK) {
111         outtask=new PrintWriter(new FileOutputStream(PREFIX+"task.h"), true);
112         outtaskdefs=new PrintWriter(new FileOutputStream(PREFIX+"taskdefs.c"), true);
113         /* optional
114            if (state.OPTIONAL){
115             outoptionalarrays=new PrintWriter(new FileOutputStream(PREFIX+"optionalarrays.c"), true);
116             optionalheaders=new PrintWriter(new FileOutputStream(PREFIX+"optionalstruct.h"), true);
117            } */
118       }
119       /*if (state.structfile!=null) {
120           outrepairstructs=new PrintWriter(new FileOutputStream(PREFIX+state.structfile+".struct"), true);
121          }*/
122     } catch (Exception e) {
123       e.printStackTrace();
124       System.exit(-1);
125     }
126
127     /* Build the virtual dispatch tables */
128     super.buildVirtualTables(outvirtual);
129
130     /* Output includes */
131     outmethodheader.println("#ifndef METHODHEADERS_H");
132     outmethodheader.println("#define METHODHEADERS_H");
133     outmethodheader.println("#include \"structdefs.h\"");
134     /*if (state.DSM)
135         outmethodheader.println("#include \"dstm.h\"");*/
136
137     /* Output Structures */
138     super.outputStructs(outstructs);
139
140     // Output the C class declarations
141     // These could mutually reference each other
142     super.outputClassDeclarations(outclassdefs);
143
144     // Output function prototypes and structures for parameters
145     Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
146     int numclasses = 0;
147     while(it.hasNext()) {
148       ++numclasses;
149       ClassDescriptor cn=(ClassDescriptor)it.next();
150       super.generateCallStructs(cn, outclassdefs, outstructs, outmethodheader);
151     }
152     outclassdefs.close();
153
154     if (state.TASK) {
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();
160         super.mapFlags(cn);
161       }
162       /* Generate Tasks */
163       generateTaskStructs(outstructs, outmethodheader);
164
165       /* Outputs generic task structures if this is a task
166          program */
167       outputTaskTypes(outtask);
168     }
169
170     /* Build the actual methods */
171     super.outputMethods(outmethod);
172
173     if (state.TASK) {
174       Iterator[] taskits = new Iterator[this.coreNum];
175       for(int i = 0; i < taskits.length; ++i) {
176         taskits[i] = null;
177       }
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);
184       }
185
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) {
191         if(needcomma) {
192           outtaskdefs.println(",");
193         } else {
194           needcomma = true;
195         }
196         outtaskdefs.println("/* object queue array for core " + i + "*/");
197         outtaskdefs.print("{");
198         comma = false;
199         for(int j = 0; j < numclasses; ++j) {
200           if(comma) {
201             outtaskdefs.println(",");
202           } else {
203             comma = true;
204           }
205           outtaskdefs.print(this.objqarrayprefix + j + "_core" + i);
206         }
207         outtaskdefs.print("}");
208       }
209       outtaskdefs.println("};");
210       needcomma = false;
211       outtaskdefs.println("int numqueues[][NUMCLASSES] = {");
212       for(int i = 0; i < numqueues.length; ++i) {
213         if(needcomma) {
214           outtaskdefs.println(",");
215         } else {
216           needcomma = true;
217         }
218         int[] tmparray = numqueues[i];
219         comma = false;
220         outtaskdefs.print("{");
221         for(int j = 0; j < tmparray.length; ++j) {
222           if(comma) {
223             outtaskdefs.print(",");
224           } else {
225             comma = true;
226           }
227           outtaskdefs.print(tmparray[j]);
228         }
229         outtaskdefs.print("}");
230       }
231       outtaskdefs.println("};");
232
233       /* parameter queue arrays for all the tasks*/
234       outtaskdefs.println("struct parameterwrapper *** paramqueues[] = {");
235       needcomma = false;
236       for(int i = 0; i < this.coreNum ; ++i) {
237         if(needcomma) {
238           outtaskdefs.println(",");
239         } else {
240           needcomma = true;
241         }
242         outtaskdefs.println("/* parameter queue array for core " + i + "*/");
243         outtaskdefs.print(this.coreqarrayprefix + i);
244       }
245       outtaskdefs.println("};");
246
247       for(int i = 0; i < taskits.length; ++i) {
248         outtaskdefs.println("struct taskdescriptor * " + this.taskarrayprefix + i + "[]={");
249         Iterator taskit = taskits[i];
250         if(taskit != null) {
251           boolean first=true;
252           while(taskit.hasNext()) {
253             TaskDescriptor td=(TaskDescriptor)taskit.next();
254             if (first)
255               first=false;
256             else
257               outtaskdefs.println(",");
258             outtaskdefs.print("&" + this.taskprefix +td.getCoreSafeSymbol(i));
259           }
260         }
261         outtaskdefs.println();
262         outtaskdefs.println("};");
263       }
264       outtaskdefs.println("struct taskdescriptor ** taskarray[]= {");
265       comma = false;
266       for(int i = 0; i < taskits.length; ++i) {
267         if (comma)
268           outtaskdefs.println(",");
269         else
270           comma = true;
271         outtaskdefs.print(this.taskarrayprefix + i);
272       }
273       outtaskdefs.println("};");
274
275       outtaskdefs.print("int numtasks[]= {");
276       comma = false;
277       for(int i = 0; i < taskits.length; ++i) {
278         if (comma)
279           outtaskdefs.print(",");
280         else
281           comma=true;
282         outtaskdefs.print(numtasks[i]);
283       }
284       outtaskdefs.println("};");
285       outtaskdefs.println("int corenum=0;");
286
287       outtaskdefs.close();
288       outtask.println("#endif");
289       outtask.close();
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);
301           //}
302
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();
307        } */
308
309     /* Output structure definitions for repair tool */
310     /*if (state.structfile!=null) {
311         buildRepairStructs(outrepairstructs);
312         outrepairstructs.close();
313        }*/
314
315     /* Close files */
316     outmethodheader.println("#endif");
317     outmethodheader.close();
318     outmethod.close();
319     outstructs.println("#endif");
320     outstructs.close();
321   }
322
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 */
326
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();
334
335       while(taskit.hasNext()) {
336         /* Classify parameters */
337         TaskDescriptor task=taskit.next();
338         FlatMethod fm=state.getMethodFlat(task);
339         super.generateTempStructs(fm, null);
340
341         ParamsObject objectparams=(ParamsObject) paramstable.get(task);
342         TempObject objecttemps=(TempObject) tempstable.get(task);
343
344         /* Output parameter structure */
345         if (GENERATEPRECISEGC) {
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()+";");
352           }
353
354           output.println("};\n");
355           if ((objectparams.numPointers()+fm.numTags())>maxtaskparams) {
356             maxtaskparams=objectparams.numPointers()+fm.numTags();
357           }
358         }
359
360         /* Output temp structure */
361         if (GENERATEPRECISEGC) {
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()+";");
372             else
373               output.println("  struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
374           }
375           output.println("};\n");
376         }
377
378         /* Output task declaration */
379         headersout.print("void " + task.getCoreSafeSymbol(num)+"(");
380
381         if (GENERATEPRECISEGC) {
382           headersout.print("struct "+task.getCoreSafeSymbol(num)+"_params * "+paramsprefix);
383         } else
384           headersout.print("void * parameterarray[]");
385         headersout.println(");\n");
386       }
387     }
388
389   }
390
391   /* This method outputs code for each task. */
392
393   private void outputTaskCode(PrintWriter outtaskdefs, 
394                               PrintWriter outmethod, 
395                               PrintWriter outtask, 
396                               Iterator[] taskits, 
397                               int[] numtasks,
398                               int[][] numqueues) {
399     /* Compile task based program */
400     outtaskdefs.println("#include \"task.h\"");
401     outtaskdefs.println("#include \"methodheaders.h\"");
402
403     /* Output object transfer queues into method.c*/
404     generateObjectTransQueues(outmethod);
405
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) {
410       qnames[i] = null;
411     }
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);
418     }
419
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 + "[] = {");
426       comma = false;
427       Vector tmpvector = qnames[i];
428       if(tmpvector != null) {
429         for(int j = 0; j < tmpvector.size(); ++j) {
430           if(comma) {
431             outtaskdefs.println(",");
432           } else {
433             comma = true;
434           }
435           outtaskdefs.print("&" + tmpvector.elementAt(j));
436         }
437         numqueues[num][i] = tmpvector.size();
438       } else {
439         numqueues[num][i] = 0;
440       }
441       outtaskdefs.println("};");
442     }
443
444     /* All the queues for tasks residing on this core*/
445     comma = false;
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()) {
450       if (comma) {
451         outtaskdefs.println(",");
452       } else {
453         comma = true;
454       }
455       TaskDescriptor td=taskit.next();
456       outtaskdefs.print(this.paramqarrayprefix + td.getCoreSafeSymbol(num));
457     }
458     outtaskdefs.println("};");
459
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();
464   }
465
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");
478     outtask.println();
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("};");
495     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("};");
507     outtask.println();
508     outtask.println("extern struct parameterwrapper ** objectqueues[][NUMCLASSES];");
509     outtask.println("extern int numqueues[][NUMCLASSES];");
510     outtask.println();
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("};");
519     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("};");
527     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[];");
532     outtask.println();
533   }
534
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;
540       }
541     }
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();
549       output.println();
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) {
564           if(j > 0) {
565             output.print(", ");
566           }
567           output.print(((Integer)targetcores[j]).intValue());
568         }
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 + "};");
574       }
575     }
576     output.println();
577   }
578
579   private void generateTaskMethod(FlatMethod fm, 
580                                   LocalityBinding lb, 
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();
587
588     //ParamsObject objectparams=(ParamsObject)paramstable.get(lb!=null?lb:task);
589     generateTaskHeader(fm, lb, task,output);
590     TempObject objecttemp=(TempObject) tempstable.get(lb!=null ? lb : task);
591     /*if (state.DSM&&lb.getHasAtomic()) {
592         output.println("transrecord_t * trans;");
593        }*/
594
595     if (GENERATEPRECISEGC) {
596       output.print("   struct "+task.getCoreSafeSymbol(num)+"_locals "+localsprefix+"={");
597
598       output.print(objecttemp.numPointers()+",");
599       output.print(paramsprefix);
600       for(int j=0; j<objecttemp.numPointers(); j++)
601         output.print(", NULL");
602       output.println("};");
603     }
604
605     for(int i=0; i<objecttemp.numPrimitives(); i++) {
606       TempDescriptor td=objecttemp.getPrimitive(i);
607       TypeDescriptor type=td.getType();
608       if (type.isNull())
609         output.println("   void * "+td.getSafeSymbol()+";");
610       else if (type.isClass()||type.isArray())
611         output.println("   struct "+type.getSafeSymbol()+" * "+td.getSafeSymbol()+";");
612       else
613         output.println("   "+type.getSafeSymbol()+" "+td.getSafeSymbol()+";");
614     }
615
616     for(int i = 0; i < fm.numParameters(); ++i) {
617       TempDescriptor temp = fm.getParameter(i);
618       output.println("   int "+generateTempFlagName(fm, temp, lb)+" = "+super.generateTemp(fm, temp, lb)+
619                      "->flag;");
620     }
621
622     /* Assign labels to FlatNode's if necessary.*/
623
624     Hashtable<FlatNode, Integer> nodetolabel=super.assignLabels(fm);
625
626     /* Check to see if we need to do a GC if this is a
627      * multi-threaded program...*/
628
629     /*if ((state.THREAD||state.DSM)&&GENERATEPRECISEGC) {
630         if (state.DSM&&lb.isAtomic())
631             output.println("checkcollect2(&"+localsprefix+",trans);");
632         else
633             output.println("checkcollect(&"+localsprefix+");");
634        }*/
635
636     /* Create queues to store objects need to be transferred to other cores and their destination*/
637     output.println("   struct Queue * totransobjqueue = createQueue();");
638     output.println("   struct transObjInfo * tmpObjInfo = NULL;");
639
640     this.m_aliasSets = null;
641     this.m_aliasFNTbl4Para = null;
642     this.m_aliasFNTbl = null;
643     this.m_aliaslocksTbl4FN = null;
644     outputAliasLockCode(fm, lb, output);
645
646     /* generate print information for RAW version */
647     output.println("#ifdef MULTICORE");
648     output.println("{");
649     output.println("int tmpsum = 0;");
650     output.println("char * taskname = \"" + task.getSymbol() + "\";");
651     output.println("int tmplen = " + task.getSymbol().length() + ";");
652     output.println("int tmpindex = 1;");
653     output.println("for(;tmpindex < tmplen; tmpindex++) {");
654     output.println("   tmpsum = tmpsum * 10 + *(taskname + tmpindex) - '0';");
655     output.println("}");
656     output.println("#ifdef RAWPATH");
657         output.println("BAMBOO_DEBUGPRINT(0xAAAA);");
658     output.println("BAMBOO_DEBUGPRINT_REG(tmpsum);");
659         output.println("BAMBOO_DEBUGPRINT(BAMBOO_GET_EXE_TIME());"); 
660     output.println("#endif");
661     output.println("#ifdef DEBUG");
662     output.println("BAMBOO_DEBUGPRINT(0xAAAA);");
663     output.println("BAMBOO_DEBUGPRINT_REG(tmpsum);");
664     output.println("#endif");
665     output.println("}");
666     output.println("#endif");
667
668     for(int i = 0; i < fm.numParameters(); ++i) {
669       TempDescriptor temp = fm.getParameter(i);
670       output.println("   ++" + super.generateTemp(fm, temp, lb)+"->version;");
671     }
672
673     /* Do the actual code generation */
674     FlatNode current_node=null;
675     HashSet tovisit=new HashSet();
676     HashSet visited=new HashSet();
677     tovisit.add(fm.getNext(0));
678     while(current_node!=null||!tovisit.isEmpty()) {
679       if (current_node==null) {
680         current_node=(FlatNode)tovisit.iterator().next();
681         tovisit.remove(current_node);
682       }
683       visited.add(current_node);
684       if (nodetolabel.containsKey(current_node))
685         output.println("L"+nodetolabel.get(current_node)+":");
686       /*if (state.INSTRUCTIONFAILURE) {
687           if (state.THREAD||state.DSM) {
688               output.println("if ((++instructioncount)>failurecount) {instructioncount=0;injectinstructionfailure();}");
689           }
690           else
691               output.println("if ((--instructioncount)==0) injectinstructionfailure();");
692          }*/
693       if (current_node.numNext()==0) {
694         output.print("   ");
695         super.generateFlatNode(fm, lb, current_node, output);
696         if (current_node.kind()!=FKind.FlatReturnNode) {
697           //output.println("   flushAll();");
698           output.println("#ifdef CACHEFLUSH");
699           output.println("BAMBOO_START_CRITICAL_SECTION();");
700           output.println("#ifdef DEBUG");
701           output.println("BAMBOO_DEBUGPRINT(0xec00);");
702           output.println("#endif");
703           output.println("BAMBOO_CACHE_FLUSH_ALL();");
704           output.println("#ifdef DEBUG");
705           output.println("BAMBOO_DEBUGPRINT(0xecff);");
706           output.println("#endif");
707           output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
708           output.println("#endif");
709           outputTransCode(output);
710           output.println("   return;");
711         }
712         current_node=null;
713       } else if(current_node.numNext()==1) {
714         output.print("   ");
715         super.generateFlatNode(fm, lb, current_node, output);
716         FlatNode nextnode=current_node.getNext(0);
717         if (visited.contains(nextnode)) {
718           output.println("goto L"+nodetolabel.get(nextnode)+";");
719           current_node=null;
720         } else
721           current_node=nextnode;
722       } else if (current_node.numNext()==2) {
723         /* Branch */
724         output.print("   ");
725         super.generateFlatCondBranch(fm, lb, (FlatCondBranch)current_node, "L"+nodetolabel.get(current_node.getNext(1)), output);
726         if (!visited.contains(current_node.getNext(1)))
727           tovisit.add(current_node.getNext(1));
728         if (visited.contains(current_node.getNext(0))) {
729           output.println("goto L"+nodetolabel.get(current_node.getNext(0))+";");
730           current_node=null;
731         } else
732           current_node=current_node.getNext(0);
733       } else throw new Error();
734     }
735
736     output.println("}\n\n");
737   }
738
739   /** This method outputs TaskDescriptor information */
740   private void generateTaskDescriptor(PrintWriter output, 
741                                       PrintWriter outtask, 
742                                       FlatMethod fm, 
743                                       TaskDescriptor task, 
744                                       Vector[] qnames) {
745     int num = this.currentSchedule.getCoreNum();
746
747     output.println("/* TaskDescriptor information for task " + task.getSymbol() + " on core " + num + "*/");
748
749     for (int i=0; i<task.numParameters(); i++) {
750       VarDescriptor param_var=task.getParameter(i);
751       TypeDescriptor param_type=task.getParamType(i);
752       FlagExpressionNode param_flag=task.getFlag(param_var);
753       TagExpressionList param_tag=task.getTag(param_var);
754
755       int dnfterms;
756       if (param_flag==null) {
757         output.println("int parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+"[]={");
758         output.println("0x0, 0x0 };");
759         dnfterms=1;
760       } else {
761         DNFFlag dflag=param_flag.getDNF();
762         dnfterms=dflag.size();
763
764         Hashtable flags=(Hashtable)flagorder.get(param_type.getClassDesc());
765         output.println("int parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+"[]={");
766         for(int j=0; j<dflag.size(); j++) {
767           if (j!=0)
768             output.println(",");
769           Vector term=dflag.get(j);
770           int andmask=0;
771           int checkmask=0;
772           for(int k=0; k<term.size(); k++) {
773             DNFFlagAtom dfa=(DNFFlagAtom)term.get(k);
774             FlagDescriptor fd=dfa.getFlag();
775             boolean negated=dfa.getNegated();
776             int flagid=1<<((Integer)flags.get(fd)).intValue();
777             andmask|=flagid;
778             if (!negated)
779               checkmask|=flagid;
780           }
781           output.print("0x"+Integer.toHexString(andmask)+", 0x"+Integer.toHexString(checkmask));
782         }
783         output.println("};");
784       }
785
786       output.println("int parametertag_"+i+"_"+task.getCoreSafeSymbol(num)+"[]={");
787       //BUG...added next line to fix, test with any task program
788       if (param_tag!=null)
789         for(int j=0; j<param_tag.numTags(); j++) {
790           if (j!=0)
791             output.println(",");
792           /* for each tag we need */
793           /* which slot it is */
794           /* what type it is */
795           TagVarDescriptor tvd=(TagVarDescriptor)task.getParameterTable().get(param_tag.getName(j));
796           TempDescriptor tmp=param_tag.getTemp(j);
797           int slot=fm.getTagInt(tmp);
798           output.println(slot+", "+state.getTagId(tvd.getTag()));
799         }
800       output.println("};");
801
802       // generate object queue for this parameter
803       String qname = this.objqueueprefix+i+"_"+task.getCoreSafeSymbol(num);
804       if(param_type.getClassDesc().getSymbol().equals("StartupObject")) {
805         this.startupcorenum = num;
806       }
807       if(qnames[param_type.getClassDesc().getId()] == null) {
808         qnames[param_type.getClassDesc().getId()] = new Vector();
809       }
810       qnames[param_type.getClassDesc().getId()].addElement(qname);
811       outtask.println("extern struct parameterwrapper " + qname + ";");
812       output.println("struct parameterwrapper " + qname + "={");
813       output.println(".objectset = 0,");      // objectset
814       output.println("/* number of DNF terms */ .numberofterms = "+dnfterms+",");     // numberofterms
815       output.println(".intarray = parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+",");    // intarray
816       // numbertags
817       if (param_tag!=null)
818         output.println("/* number of tags */ .numbertags = "+param_tag.numTags()+",");
819       else
820         output.println("/* number of tags */ .numbertags = 0,");
821       output.println(".tagarray = parametertag_"+i+"_"+task.getCoreSafeSymbol(num)+",");    // tagarray
822       output.println(".task = 0,");      // task
823       output.println(".slot = " + i + ",");    // slot
824       // iterators
825       output.println("};");
826
827       output.println("struct parameterdescriptor parameter_"+i+"_"+task.getCoreSafeSymbol(num)+"={");
828       output.println("/* type */"+param_type.getClassDesc().getId()+",");
829       output.println("/* number of DNF terms */"+dnfterms+",");
830       output.println("parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+",");    // intarray
831       output.println("&" + qname + ",");     // queue
832       //BUG, added next line to fix and else statement...test
833       //with any task program
834       if (param_tag!=null)
835         output.println("/* number of tags */"+param_tag.numTags()+",");
836       else
837         output.println("/* number of tags */ 0,");
838       output.println("parametertag_"+i+"_"+task.getCoreSafeSymbol(num));     // tagarray
839       output.println("};");
840     }
841
842     /* parameter queues for this task*/
843     output.println("struct parameterwrapper * " + this.paramqarrayprefix + task.getCoreSafeSymbol(num)+"[] = {");
844     for (int i=0; i<task.numParameters(); i++) {
845       if (i!=0)
846         output.println(",");
847       output.print("&" + this.objqueueprefix + i + "_" + task.getCoreSafeSymbol(num));
848     }
849     output.println("};");
850
851     output.println("struct parameterdescriptor * parameterdescriptors_"+task.getCoreSafeSymbol(num)+"[] = {");
852     for (int i=0; i<task.numParameters(); i++) {
853       if (i!=0)
854         output.println(",");
855       output.print("&parameter_"+i+"_"+task.getCoreSafeSymbol(num));
856     }
857     output.println("};");
858
859     output.println("struct taskdescriptor " + this.taskprefix + task.getCoreSafeSymbol(num) + "={");
860     output.println("&"+task.getCoreSafeSymbol(num)+",");
861     output.println("/* number of parameters */" +task.numParameters() + ",");
862     int numtotal=task.numParameters()+fm.numTags();
863     output.println("/* number total parameters */" +numtotal + ",");
864     output.println("parameterdescriptors_"+task.getCoreSafeSymbol(num)+",");
865     output.println("\""+task.getSymbol()+"\"");
866     output.println("};");
867
868     output.println();
869   }
870
871   /** This method generates header information for the task
872    *  referenced by the Descriptor des. */
873
874   private void generateTaskHeader(FlatMethod fm, 
875                                   LocalityBinding lb, 
876                                   Descriptor des, 
877                                   PrintWriter output) {
878     /* Print header */
879     ParamsObject objectparams=(ParamsObject)paramstable.get(lb!=null ? lb : des);
880     TaskDescriptor task=(TaskDescriptor) des;
881
882     int num = this.currentSchedule.getCoreNum();
883     //catch the constructor case
884     output.print("void ");
885     output.print(task.getCoreSafeSymbol(num)+"(");
886
887     boolean printcomma=false;
888     if (GENERATEPRECISEGC) {
889       output.print("struct "+task.getCoreSafeSymbol(num)+"_params * "+paramsprefix);
890       printcomma=true;
891     }
892
893     /*if (state.DSM&&lb.isAtomic()) {
894         if (printcomma)
895             output.print(", ");
896         output.print("transrecord_t * trans");
897         printcomma=true;
898        }*/
899
900     if (!GENERATEPRECISEGC) {
901       /* Imprecise Task */
902       output.println("void * parameterarray[]) {");
903       /* Unpack variables */
904       for(int i=0; i<objectparams.numPrimitives(); i++) {
905         TempDescriptor temp=objectparams.getPrimitive(i);
906         output.println("struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+"=parameterarray["+i+"];");
907       }
908       for(int i=0; i<fm.numTags(); i++) {
909         TempDescriptor temp=fm.getTag(i);
910         int offset=i+objectparams.numPrimitives();
911         output.println("struct ___TagDescriptor___ * "+temp.getSafeSymbol()+i+"___=parameterarray["+offset+"];");     // add i to fix bugs of duplicate definition of tags
912       }
913
914       if ((objectparams.numPrimitives()+fm.numTags())>maxtaskparams)
915         maxtaskparams=objectparams.numPrimitives()+fm.numTags();
916     } else output.println(") {");
917   }
918
919   protected void generateFlagOrAnd(FlatFlagActionNode ffan, 
920                                    FlatMethod fm, 
921                                    LocalityBinding lb, 
922                                    TempDescriptor temp,
923                                    PrintWriter output, 
924                                    int ormask, 
925                                    int andmask) {
926     if (ffan.getTaskType()==FlatFlagActionNode.NEWOBJECT) {
927       output.println("flagorandinit("+super.generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+", 0x"+Integer.toHexString(andmask)+");");
928     } else {
929       int num = this.currentSchedule.getCoreNum();
930       ClassDescriptor cd = temp.getType().getClassDesc();
931       Vector<FlagState> initfstates = ffan.getInitFStates(cd);
932       for(int i = 0; i < initfstates.size(); ++i) {
933         FlagState tmpFState = initfstates.elementAt(i);
934         output.println("{");
935         QueueInfo qinfo = outputqueues(tmpFState, num, output, false);
936         output.println("flagorand("+super.generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+
937                        ", 0x"+Integer.toHexString(andmask)+", " + qinfo.qname +
938                        ", " + qinfo.length + ");");
939         output.println("}");
940       }
941       if(ffan.getTaskType()==FlatFlagActionNode.TASKEXIT) {
942           // generate codes for profiling, recording which task exit it is
943           output.println("#ifdef PROFILE");
944           output.println("setTaskExitIndex(" + ffan.getTaskExitIndex() + ");");
945           output.println("#endif");
946       }
947     }
948   }
949
950   protected void generateObjectDistribute(FlatFlagActionNode ffan, 
951                                           FlatMethod fm, 
952                                           LocalityBinding lb, 
953                                           TempDescriptor temp,
954                                           PrintWriter output) {
955     ClassDescriptor cd = temp.getType().getClassDesc();
956     Vector<FlagState> initfstates = null;
957     Vector[] targetFStates = null;
958     if (ffan.getTaskType()==FlatFlagActionNode.NEWOBJECT) {
959       targetFStates = new Vector[1];
960       targetFStates[0] = ffan.getTargetFStates4NewObj(cd);
961     } else {
962       initfstates = ffan.getInitFStates(cd);
963       targetFStates = new Vector[initfstates.size()];
964       for(int i = 0; i < initfstates.size(); ++i) {
965         FlagState fs = initfstates.elementAt(i);
966         targetFStates[i] = ffan.getTargetFStates(fs);
967
968         if(!fs.isSetmask()) {
969           Hashtable flags=(Hashtable)flagorder.get(cd);
970           int andmask=0;
971           int checkmask=0;
972           Iterator it_flags = fs.getFlags();
973           while(it_flags.hasNext()) {
974             FlagDescriptor fd = (FlagDescriptor)it_flags.next();
975             int flagid=1<<((Integer)flags.get(fd)).intValue();
976             andmask|=flagid;
977             checkmask|=flagid;
978           }
979           fs.setAndmask(andmask);
980           fs.setCheckmask(checkmask);
981           fs.setSetmask(true);
982         }
983       }
984     }
985     boolean isolate = true;     // check if this flagstate can associate to some task with multiple params which can
986                                 // reside on multiple cores
987     if((this.currentSchedule == null) && (fm.getMethod().getClassDesc().getSymbol().equals("ServerSocket"))) {
988       // ServerSocket object will always reside on current core
989       for(int j = 0; j < targetFStates.length; ++j) {
990         if(initfstates != null) {
991           FlagState fs = initfstates.elementAt(j);
992           output.println("if(" + generateTempFlagName(fm, temp, lb) + "&(0x" + Integer.toHexString(fs.getAndmask())
993                          + ")==(0x" + Integer.toHexString(fs.getCheckmask()) + ")) {");
994         }
995         Vector<FlagState> tmpfstates = (Vector<FlagState>)targetFStates[j];
996         for(int i = 0; i < tmpfstates.size(); ++i) {
997           FlagState tmpFState = tmpfstates.elementAt(i);
998           // TODO
999           // may have bugs here
1000           output.println("/* reside on this core*");
1001           output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1002         }
1003         if(initfstates != null) {
1004           output.println("}");
1005         }
1006       }
1007       return;
1008     }
1009
1010     int num = this.currentSchedule.getCoreNum();
1011     Hashtable<FlagState, Queue<Integer>> targetCoreTbl = this.currentSchedule.getTargetCoreTable();
1012     for(int j = 0; j < targetFStates.length; ++j) {
1013       FlagState fs = null;
1014       if(initfstates != null) {
1015         fs = initfstates.elementAt(j);
1016         output.println("if((" + generateTempFlagName(fm, temp, lb) + "&(0x" + Integer.toHexString(fs.getAndmask())
1017                        + "))==(0x" + Integer.toHexString(fs.getCheckmask()) + ")) {");
1018       }
1019       Vector<FlagState> tmpfstates = (Vector<FlagState>)targetFStates[j];
1020       for(int i = 0; i < tmpfstates.size(); ++i) {
1021         FlagState tmpFState = tmpfstates.elementAt(i);
1022
1023         if(this.currentSchedule.getAllyCoreTable() == null) {
1024           isolate = true;
1025         } else {
1026           isolate = (this.currentSchedule.getAllyCoreTable().get(tmpFState) == null) ||
1027                     (this.currentSchedule.getAllyCoreTable().get(tmpFState).size() == 0);
1028         }
1029         if(!isolate) {
1030           // indentify this object as a shared object
1031           // isolate flag is initially set as 1, once this flag is set as 0, it is never reset to 1, i.e. once an object
1032           // is shared, it maybe shared all the time afterwards
1033           output.println("if(" + super.generateTemp(fm, temp, lb) + "->isolate == 1) {");
1034           output.println("  " + super.generateTemp(fm, temp, lb) + "->isolate = 0;");
1035           output.println("  " + super.generateTemp(fm, temp, lb) + "->original = (struct ___Object___ *)" + super.generateTemp(fm, temp, lb) + ";");
1036           output.println("}");
1037         }
1038
1039         Vector<TranObjInfo> sendto = new Vector<TranObjInfo>();
1040         Queue<Integer> queue = null;
1041         if(targetCoreTbl != null) {
1042           queue = targetCoreTbl.get(tmpFState);
1043         }
1044         if((queue != null) &&
1045            ((queue.size() != 1) ||
1046             ((queue.size() == 1) && (queue.element().intValue() != num)))) {
1047           // this object may be transferred to other cores
1048           String queuename = (String) this.fsate2qnames[num].get(tmpFState);
1049           String queueins = queuename + "ins";
1050
1051           Object[] cores = queue.toArray();
1052           String index = "0";
1053           Integer targetcore = (Integer)cores[0];
1054           if(queue.size() > 1) {
1055             index = queueins + ".index";
1056           }
1057           if(queue.size() > 1) {
1058             output.println("switch(" + queueins + ".index % " + queueins + ".length) {");
1059             for(int k = 0; k < cores.length; ++k) {
1060               output.println("case " + k + ":");
1061               targetcore = (Integer)cores[k];
1062               if(targetcore.intValue() == num) {
1063                 output.println("/* reside on this core*/");
1064                 if(isolate) {
1065                   output.println("{");
1066                   QueueInfo qinfo = outputqueues(tmpFState, num, output, true);
1067                   output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", " + qinfo.qname +
1068                                  ", " + qinfo.length + ");");
1069                   output.println("}");
1070                 } else {
1071                   // TODO
1072                   // really needed?
1073                   output.println("/* possibly needed by multi-parameter tasks on this core*/");
1074                   output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1075                 }
1076               } else {
1077                 if(!isolate) {
1078                   // TODO
1079                   // Is it possible to decide the actual queues?
1080                   output.println("/* possibly needed by multi-parameter tasks on this core*/");
1081                   output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1082                 }
1083                 output.println("/* transfer to core " + targetcore.toString() + "*/");
1084                 output.println("{");
1085                 // enqueue this object and its destinations for later process
1086                 // all the possible queues
1087                 QueueInfo qinfo = null;
1088                 TranObjInfo tmpinfo = new TranObjInfo();
1089                 tmpinfo.name = super.generateTemp(fm, temp, lb);
1090                 tmpinfo.targetcore = targetcore;
1091                 FlagState targetFS = this.currentSchedule.getTargetFState(tmpFState);
1092                 if(targetFS != null) {
1093                   tmpinfo.fs = targetFS;
1094                 } else {
1095                   tmpinfo.fs = tmpFState;
1096                 }
1097                 if(!contains(sendto, tmpinfo)) {
1098                   qinfo = outputtransqueues(tmpinfo.fs, targetcore, output);
1099                   output.println("tmpObjInfo = RUNMALLOC(sizeof(struct transObjInfo));");
1100                   output.println("tmpObjInfo->objptr = (void *)" + tmpinfo.name + ";");
1101                   output.println("tmpObjInfo->targetcore = "+targetcore.toString()+";");
1102                   output.println("tmpObjInfo->queues = " + qinfo.qname + ";");
1103                   output.println("tmpObjInfo->length = " + qinfo.length + ";");
1104                   output.println("addNewItem(totransobjqueue, (void*)tmpObjInfo);");
1105                   sendto.add(tmpinfo);
1106                 }
1107                 output.println("}");
1108               }
1109               output.println("break;");
1110             }
1111             output.println("}");
1112           } else {
1113             if(!isolate) {
1114               // TODO
1115               // Is it possible to decide the actual queues?
1116               output.println("/* possibly needed by multi-parameter tasks on this core*/");
1117               output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1118             }
1119             output.println("/* transfer to core " + targetcore.toString() + "*/");
1120             output.println("{");
1121             // enqueue this object and its destinations for later process
1122             // all the possible queues
1123             QueueInfo qinfo = null;
1124             TranObjInfo tmpinfo = new TranObjInfo();
1125             tmpinfo.name = super.generateTemp(fm, temp, lb);
1126             tmpinfo.targetcore = targetcore;
1127             FlagState targetFS = this.currentSchedule.getTargetFState(tmpFState);
1128             if(targetFS != null) {
1129               tmpinfo.fs = targetFS;
1130             } else {
1131               tmpinfo.fs = tmpFState;
1132             }
1133             if(!contains(sendto, tmpinfo)) {
1134               qinfo = outputtransqueues(tmpinfo.fs, targetcore, output);
1135               output.println("tmpObjInfo = RUNMALLOC(sizeof(struct transObjInfo));");
1136               output.println("tmpObjInfo->objptr = (void *)" + tmpinfo.name + ";");
1137               output.println("tmpObjInfo->targetcore = "+targetcore.toString()+";");
1138               output.println("tmpObjInfo->queues = " + qinfo.qname + ";");
1139               output.println("tmpObjInfo->length = " + qinfo.length + ";");
1140               output.println("addNewItem(totransobjqueue, (void*)tmpObjInfo);");
1141               sendto.add(tmpinfo);
1142             }
1143             output.println("}");
1144           }
1145           output.println("/* increase index*/");
1146           output.println("++" + queueins + ".index;");
1147         } else {
1148           // this object will reside on current core
1149           output.println("/* reside on this core*/");
1150           if(isolate) {
1151             output.println("{");
1152             QueueInfo qinfo = outputqueues(tmpFState, num, output, true);
1153             output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", " + qinfo.qname +
1154                            ", " + qinfo.length + ");");
1155             output.println("}");
1156           } else {
1157             // TODO
1158             // really needed?
1159             output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1160           }
1161         }
1162
1163         // codes for multi-params tasks
1164         if(!isolate) {
1165           // flagstate associated with some multi-params tasks
1166           // need to be send to other cores
1167           Vector<Integer> targetcores = this.currentSchedule.getAllyCores(tmpFState);
1168           output.println("/* send the shared object to possible queues on other cores*/");
1169           for(int k = 0; k < targetcores.size(); ++k) {
1170             // TODO
1171             // add the information of exactly which queue
1172             //if(!sendto.contains(targetcores.elementAt(i))) {
1173             // previously not sended to this target core
1174             // enqueue this object and its destinations for later process
1175             output.println("{");
1176             // all the possible queues
1177             QueueInfo qinfo = null;
1178             TranObjInfo tmpinfo = new TranObjInfo();
1179             tmpinfo.name = super.generateTemp(fm, temp, lb);
1180             tmpinfo.targetcore = targetcores.elementAt(i);
1181             FlagState targetFS = this.currentSchedule.getTargetFState(tmpFState);
1182             if(targetFS != null) {
1183               tmpinfo.fs = targetFS;
1184             } else {
1185               tmpinfo.fs = tmpFState;
1186             }
1187             if(!contains(sendto, tmpinfo)) {
1188               qinfo = outputtransqueues(tmpinfo.fs, targetcores.elementAt(i), output);
1189               output.println("tmpObjInfo = RUNMALLOC(sizeof(struct transObjInfo));");
1190               output.println("tmpObjInfo->objptr = (void *)" + tmpinfo.name + ";");
1191               output.println("tmpObjInfo->targetcore = "+targetcores.elementAt(i).toString()+";");
1192               output.println("tmpObjInfo->queues = " + qinfo.qname + ";");
1193               output.println("tmpObjInfo->length = " + qinfo.length + ";");
1194               output.println("addNewItem(totransobjqueue, (void*)tmpObjInfo);");
1195               sendto.add(tmpinfo);
1196             }
1197             output.println("}");
1198             //}
1199           }
1200         }
1201       }
1202
1203       if(initfstates != null) {
1204         output.println("}");
1205       }
1206     }
1207   }
1208
1209   private QueueInfo outputqueues(FlagState tmpFState, 
1210                                  int num, 
1211                                  PrintWriter output, 
1212                                  boolean isEnqueue) {
1213     // queue array
1214     QueueInfo qinfo = new QueueInfo();
1215     qinfo.qname  = "queues_" + tmpFState.getLabel() + "_" + tmpFState.getiuid();
1216     output.println("struct parameterwrapper * " + qinfo.qname + "[] = {");
1217     Iterator it_edges = tmpFState.getEdgeVector().iterator();
1218     Vector<TaskDescriptor> residetasks = this.currentSchedule.getTasks();
1219     Vector<TaskDescriptor> tasks = new Vector<TaskDescriptor>();
1220     Vector<Integer> indexes = new Vector<Integer>();
1221     boolean comma = false;
1222     qinfo.length = 0;
1223     while(it_edges.hasNext()) {
1224       FEdge fe = (FEdge)it_edges.next();
1225       TaskDescriptor td = fe.getTask();
1226       int paraindex = fe.getIndex();
1227       if((!isEnqueue) || (isEnqueue && residetasks.contains(td))) {
1228         if((!tasks.contains(td)) ||
1229            ((tasks.contains(td)) && (paraindex != indexes.elementAt(tasks.indexOf(td)).intValue()))) {
1230           tasks.addElement(td);
1231           indexes.addElement(paraindex);
1232           if(comma) {
1233             output.println(",");
1234           } else {
1235             comma = true;
1236           }
1237           output.print("&" + this.objqueueprefix + paraindex + "_" + td.getCoreSafeSymbol(num));
1238           ++qinfo.length;
1239         }
1240       }
1241     }
1242     output.println("};");
1243     return qinfo;
1244   }
1245
1246   private QueueInfo outputtransqueues(FlagState tmpFState, 
1247                                       int targetcore, 
1248                                       PrintWriter output) {
1249     // queue array
1250     QueueInfo qinfo = new QueueInfo();
1251     qinfo.qname  = "queues_" + tmpFState.getLabel() + "_" + tmpFState.getiuid();
1252     output.println("int " + qinfo.qname + "_clone[] = {");
1253     Iterator it_edges = tmpFState.getEdgeVector().iterator();
1254     Vector<TaskDescriptor> residetasks = this.scheduling.get(targetcore).getTasks();
1255     Vector<TaskDescriptor> tasks = new Vector<TaskDescriptor>();
1256     Vector<Integer> indexes = new Vector<Integer>();
1257     boolean comma = false;
1258     qinfo.length = 0;
1259     while(it_edges.hasNext()) {
1260       FEdge fe = (FEdge)it_edges.next();
1261       TaskDescriptor td = fe.getTask();
1262       int paraindex = fe.getIndex();
1263       if(residetasks.contains(td)) {
1264         if((!tasks.contains(td)) ||
1265            ((tasks.contains(td)) && (paraindex != indexes.elementAt(tasks.indexOf(td)).intValue()))) {
1266           tasks.addElement(td);
1267           indexes.addElement(paraindex);
1268           if(comma) {
1269             output.println(",");
1270           } else {
1271             comma = true;
1272           }
1273           output.print(residetasks.indexOf(td) + ", ");
1274           output.print(paraindex);
1275           ++qinfo.length;
1276         }
1277       }
1278     }
1279     output.println("};");
1280     output.println("int * " + qinfo.qname + " = RUNMALLOC(sizeof(int) * " + qinfo.length * 2 + ");");
1281     output.println("memcpy(" + qinfo.qname + ", (int *)" + qinfo.qname + "_clone, sizeof(int) * " + qinfo.length * 2 + ");");
1282     return qinfo;
1283   }
1284
1285   private class QueueInfo {
1286     public int length;
1287     public String qname;
1288   }
1289
1290   private String generateTempFlagName(FlatMethod fm, 
1291                                       TempDescriptor td, 
1292                                       LocalityBinding lb) {
1293     MethodDescriptor md=fm.getMethod();
1294     TaskDescriptor task=fm.getTask();
1295     TempObject objecttemps=(TempObject) tempstable.get(lb!=null ? lb : md!=null ? md : task);
1296
1297     if (objecttemps.isLocalPrim(td)||objecttemps.isParamPrim(td)) {
1298       return td.getSafeSymbol() + "_oldflag";
1299     }
1300
1301     if (objecttemps.isLocalPtr(td)) {
1302       return localsprefix+"_"+td.getSafeSymbol() + "_oldflag";
1303     }
1304
1305     if (objecttemps.isParamPtr(td)) {
1306       return paramsprefix+"_"+td.getSafeSymbol() + "_oldflag";
1307     }
1308     throw new Error();
1309   }
1310
1311   protected void outputTransCode(PrintWriter output) {
1312     output.println("while(0 == isEmpty(totransobjqueue)) {");
1313     output.println("   struct transObjInfo * totransobj = (struct transObjInfo *)(getItem(totransobjqueue));");
1314     output.println("   transferObject(totransobj);");
1315     output.println("   RUNFREE(totransobj->queues);");
1316     output.println("   RUNFREE(totransobj);");
1317     output.println("}");
1318     output.println("freeQueue(totransobjqueue);");
1319   }
1320
1321   protected void outputAliasLockCode(FlatMethod fm, 
1322                                      LocalityBinding lb, 
1323                                      PrintWriter output) {
1324     if(this.m_oa == null) {
1325       return;
1326     }
1327     TaskDescriptor td = fm.getTask();
1328     Object[] allocSites = this.m_oa.getFlaggedAllocationSitesReachableFromTask(td).toArray();
1329     Vector<Vector<Integer>> aliasSets = new Vector<Vector<Integer>>();
1330     Vector<Vector<FlatNew>> aliasFNSets = new Vector<Vector<FlatNew>>();
1331     Hashtable<Integer, Vector<FlatNew>> aliasFNTbl4Para = new Hashtable<Integer, Vector<FlatNew>>();
1332     Hashtable<FlatNew, Vector<FlatNew>> aliasFNTbl = new Hashtable<FlatNew, Vector<FlatNew>>();
1333     Set<HeapRegionNode> common;
1334     for( int i = 0; i < fm.numParameters(); ++i ) {
1335       // for the ith parameter check for aliases to all
1336       // higher numbered parameters
1337       aliasSets.add(null);
1338       for( int j = i + 1; j < fm.numParameters(); ++j ) {
1339         common = this.m_oa.createsPotentialAliases(td, i, j);
1340         if(!common.isEmpty()) {
1341           // ith parameter and jth parameter has alias, create lock to protect them
1342           if(aliasSets.elementAt(i) == null) {
1343             aliasSets.setElementAt(new Vector<Integer>(), i);
1344           }
1345           aliasSets.elementAt(i).add(j);
1346         }
1347       }
1348
1349       // for the ith parameter, check for aliases against
1350       // the set of allocation sites reachable from this
1351       // task context
1352       aliasFNSets.add(null);
1353       for(int j = 0; j < allocSites.length; j++) {
1354         AllocationSite as = (AllocationSite)allocSites[j];
1355         common = this.m_oa.createsPotentialAliases(td, i, as);
1356         if( !common.isEmpty() ) {
1357           // ith parameter and allocationsite as has alias
1358           if(aliasFNSets.elementAt(i) == null) {
1359             aliasFNSets.setElementAt(new Vector<FlatNew>(), i);
1360           }
1361           aliasFNSets.elementAt(i).add(as.getFlatNew());
1362         }
1363       }
1364     }
1365
1366     // for each allocation site check for aliases with
1367     // other allocation sites in the context of execution
1368     // of this task
1369     for( int i = 0; i < allocSites.length; ++i ) {
1370       AllocationSite as1 = (AllocationSite)allocSites[i];
1371       for(int j = i + 1; j < allocSites.length; j++) {
1372         AllocationSite as2 = (AllocationSite)allocSites[j];
1373
1374         common = this.m_oa.createsPotentialAliases(td, as1, as2);
1375         if( !common.isEmpty() ) {
1376           // as1 and as2 has alias
1377           if(!aliasFNTbl.containsKey(as1.getFlatNew())) {
1378             aliasFNTbl.put(as1.getFlatNew(), new Vector<FlatNew>());
1379           }
1380           if(!aliasFNTbl.get(as1.getFlatNew()).contains(as2.getFlatNew())) {
1381             aliasFNTbl.get(as1.getFlatNew()).add(as2.getFlatNew());
1382           }
1383         }
1384       }
1385     }
1386
1387     // if FlatNew N1->N2->N3, we group N1, N2, N3 together
1388     Iterator<FlatNew> it = aliasFNTbl.keySet().iterator();
1389     Vector<FlatNew> visited = new Vector<FlatNew>();
1390     while(it.hasNext()) {
1391       FlatNew tmpfn = it.next();
1392       if(visited.contains(tmpfn)) {
1393         continue;
1394       }
1395       visited.add(tmpfn);
1396       Queue<FlatNew> tovisit = new LinkedList<FlatNew>();
1397       Vector<FlatNew> tmpv = aliasFNTbl.get(tmpfn);
1398       if(tmpv == null) {
1399         continue;
1400       }
1401
1402       for(int j = 0; j < tmpv.size(); j++) {
1403         tovisit.add(tmpv.elementAt(j));
1404       }
1405
1406       while(!tovisit.isEmpty()) {
1407         FlatNew fn = tovisit.poll();
1408         visited.add(fn);
1409         Vector<FlatNew> tmpset = aliasFNTbl.get(fn);
1410         if(tmpset != null) {
1411           // merge tmpset to the alias set of the ith parameter
1412           for(int j = 0; j < tmpset.size(); j++) {
1413             if(!tmpv.contains(tmpset.elementAt(j))) {
1414               tmpv.add(tmpset.elementAt(j));
1415               tovisit.add(tmpset.elementAt(j));
1416             }
1417           }
1418           aliasFNTbl.remove(fn);
1419         }
1420       }
1421       it = aliasFNTbl.keySet().iterator();
1422     }
1423
1424     // check alias between parameters and between parameter-flatnew
1425     for(int i = 0; i < aliasSets.size(); i++) {
1426       Queue<Integer> tovisit = new LinkedList<Integer>();
1427       Vector<Integer> tmpv = aliasSets.elementAt(i);
1428       if(tmpv == null) {
1429         continue;
1430       }
1431
1432       for(int j = 0; j < tmpv.size(); j++) {
1433         tovisit.add(tmpv.elementAt(j));
1434       }
1435
1436       while(!tovisit.isEmpty()) {
1437         int index = tovisit.poll().intValue();
1438         Vector<Integer> tmpset = aliasSets.elementAt(index);
1439         if(tmpset != null) {
1440           // merge tmpset to the alias set of the ith parameter
1441           for(int j = 0; j < tmpset.size(); j++) {
1442             if(!tmpv.contains(tmpset.elementAt(j))) {
1443               tmpv.add(tmpset.elementAt(j));
1444               tovisit.add(tmpset.elementAt(j));
1445             }
1446           }
1447           aliasSets.setElementAt(null, index);
1448         }
1449
1450         Vector<FlatNew> tmpFNSet = aliasFNSets.elementAt(index);
1451         if(tmpFNSet != null) {
1452           // merge tmpFNSet to the aliasFNSet of the ith parameter
1453           if(aliasFNSets.elementAt(i) == null) {
1454             aliasFNSets.setElementAt(tmpFNSet, i);
1455           } else {
1456             Vector<FlatNew> tmpFNv = aliasFNSets.elementAt(i);
1457             for(int j = 0; j < tmpFNSet.size(); j++) {
1458               if(!tmpFNv.contains(tmpFNSet.elementAt(j))) {
1459                 tmpFNv.add(tmpFNSet.elementAt(j));
1460               }
1461             }
1462           }
1463           aliasFNSets.setElementAt(null, index);
1464         }
1465       }
1466     }
1467
1468     int numlock = 0;
1469     int numparalock = 0;
1470     Vector<Vector<Integer>> tmpaliasSets = new Vector<Vector<Integer>>();
1471     for(int i = 0; i < aliasSets.size(); i++) {
1472       Vector<Integer> tmpv = aliasSets.elementAt(i);
1473       if(tmpv != null) {
1474         tmpv.add(0, i);
1475         tmpaliasSets.add(tmpv);
1476         numlock++;
1477       }
1478
1479       Vector<FlatNew> tmpFNv = aliasFNSets.elementAt(i);
1480       if(tmpFNv != null) {
1481         aliasFNTbl4Para.put(i, tmpFNv);
1482         if(tmpv == null) {
1483           numlock++;
1484         }
1485       }
1486     }
1487     numparalock = numlock;
1488     aliasSets.clear();
1489     aliasSets = null;
1490     this.m_aliasSets = tmpaliasSets;
1491     tmpaliasSets.clear();
1492     tmpaliasSets = null;
1493     aliasFNSets.clear();
1494     aliasFNSets = null;
1495     this.m_aliasFNTbl4Para = aliasFNTbl4Para;
1496     this.m_aliasFNTbl = aliasFNTbl;
1497     numlock += this.m_aliasFNTbl.size();
1498
1499     // create locks
1500     if(numlock > 0) {
1501       output.println("int aliaslocks[" + numlock + "];");
1502       output.println("int tmpi = 0;");      
1503       // associate locks with parameters
1504       int lockindex = 0;
1505       for(int i = 0; i < this.m_aliasSets.size(); i++) {
1506         Vector<Integer> toadd = this.m_aliasSets.elementAt(i);
1507         
1508         output.print("int tmplen_" + lockindex + " = 0;");
1509         output.println("void * tmpptrs_" + lockindex + "[] = {");
1510         for(int j = 0; j < toadd.size(); j++) {
1511             int para = toadd.elementAt(j).intValue();
1512             output.print(super.generateTemp(fm, fm.getParameter(para), lb));
1513             if(j < toadd.size() - 1) {
1514                 output.print(", ");
1515             } else {
1516                 output.println("};");
1517             }
1518         }
1519         output.println("aliaslocks[tmpi++] = getAliasLock(tmpptrs_" + lockindex + ", tmplen_" + lockindex + ", lockRedirectTbl);");
1520         
1521         for(int j = 0; j < toadd.size(); j++) {
1522           int para = toadd.elementAt(j).intValue();
1523           output.println("addAliasLock("  + super.generateTemp(fm, fm.getParameter(para), lb) + ", aliaslocks[" + i + "]);");
1524         }
1525         // check if this lock is also associated with any FlatNew nodes
1526         if(this.m_aliasFNTbl4Para.containsKey(toadd.elementAt(0))) {
1527           if(this.m_aliaslocksTbl4FN == null) {
1528             this.m_aliaslocksTbl4FN = new Hashtable<FlatNew, Vector<Integer>>();
1529           }
1530           Vector<FlatNew> tmpv = this.m_aliasFNTbl4Para.get(toadd.elementAt(0));
1531           for(int j = 0; j < tmpv.size(); j++) {
1532             FlatNew fn = tmpv.elementAt(j);
1533             if(!this.m_aliaslocksTbl4FN.containsKey(fn)) {
1534               this.m_aliaslocksTbl4FN.put(fn, new Vector<Integer>());
1535             }
1536             this.m_aliaslocksTbl4FN.get(fn).add(i);
1537           }
1538           this.m_aliasFNTbl4Para.remove(toadd.elementAt(0));
1539         }
1540         lockindex++;
1541       }
1542       
1543       Object[] key = this.m_aliasFNTbl4Para.keySet().toArray();
1544       for(int i = 0; i < key.length; i++) {
1545         int para = ((Integer)key[i]).intValue();
1546
1547         output.println("void * tmpptrs_" + lockindex + "[] = {" + super.generateTemp(fm, fm.getParameter(para), lb) + "};");
1548         output.println("aliaslocks[tmpi++] = getAliasLock(tmpptrs_" + lockindex + ", 1, lockRedirectTbl);");
1549         
1550         output.println("addAliasLock(" + super.generateTemp(fm, fm.getParameter(para), lb) + ", aliaslocks[" + lockindex + "]);");
1551         Vector<FlatNew> tmpv = this.m_aliasFNTbl4Para.get(para);
1552         for(int j = 0; j < tmpv.size(); j++) {
1553           FlatNew fn = tmpv.elementAt(j);
1554           if(this.m_aliaslocksTbl4FN == null) {
1555             this.m_aliaslocksTbl4FN = new Hashtable<FlatNew, Vector<Integer>>();
1556           }
1557           if(!this.m_aliaslocksTbl4FN.containsKey(fn)) {
1558             this.m_aliaslocksTbl4FN.put(fn, new Vector<Integer>());
1559           }
1560           this.m_aliaslocksTbl4FN.get(fn).add(lockindex);
1561         }
1562         lockindex++;
1563       }
1564       
1565       // check m_aliasFNTbl for locks associated with FlatNew nodes
1566       Object[] FNkey = this.m_aliasFNTbl.keySet().toArray();
1567       for(int i = 0; i < FNkey.length; i++) {
1568         FlatNew fn = (FlatNew)FNkey[i];
1569         Vector<FlatNew> tmpv = this.m_aliasFNTbl.get(fn);
1570         
1571         output.println("aliaslocks[tmpi++] = (int)(RUNMALLOC(sizeof(int)));");
1572         
1573         if(this.m_aliaslocksTbl4FN == null) {
1574           this.m_aliaslocksTbl4FN = new Hashtable<FlatNew, Vector<Integer>>();
1575         }
1576         if(!this.m_aliaslocksTbl4FN.containsKey(fn)) {
1577           this.m_aliaslocksTbl4FN.put(fn, new Vector<Integer>());
1578         }
1579         this.m_aliaslocksTbl4FN.get(fn).add(lockindex);
1580         for(int j = 0; j < tmpv.size(); j++) {
1581           FlatNew tfn = tmpv.elementAt(j);
1582           if(!this.m_aliaslocksTbl4FN.containsKey(tfn)) {
1583             this.m_aliaslocksTbl4FN.put(tfn, new Vector<Integer>());
1584           }
1585           this.m_aliaslocksTbl4FN.get(tfn).add(lockindex);
1586         }
1587         lockindex++;
1588       }
1589     }
1590   }
1591
1592   protected void generateFlatReturnNode(FlatMethod fm, 
1593                                         LocalityBinding lb, 
1594                                         FlatReturnNode frn, 
1595                                         PrintWriter output) {
1596     if (frn.getReturnTemp()!=null) {
1597       if (frn.getReturnTemp().getType().isPtr())
1598         output.println("return (struct "+fm.getMethod().getReturnType().getSafeSymbol()+"*)"+generateTemp(fm, frn.getReturnTemp(), lb)+";");
1599       else
1600         output.println("return "+generateTemp(fm, frn.getReturnTemp(), lb)+";");
1601     } else {
1602       if(fm.getTask() != null) {
1603         output.println("#ifdef CACHEFLUSH");
1604         output.println("BAMBOO_START_CRITICAL_SECTION();");
1605         output.println("#ifdef DEBUG");
1606         output.println("BAMBOO_DEBUGPRINT(0xec00);");
1607         output.println("#endif");
1608         output.println("BAMBOO_CACHE_FLUSH_ALL();");
1609         output.println("#ifdef DEBUG");
1610         output.println("BAMBOO_DEBUGPRINT(0xecff);");
1611         output.println("#endif");
1612         output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
1613         output.println("#endif");
1614         outputTransCode(output);
1615       }
1616       output.println("return;");
1617     }
1618   }
1619
1620   protected void generateFlatNew(FlatMethod fm, 
1621                                  LocalityBinding lb, 
1622                                  FlatNew fn,
1623                                  PrintWriter output) {
1624     if (state.DSM && locality.getAtomic(lb).get(fn).intValue() > 0
1625         && !fn.isGlobal()) {
1626       // Stash pointer in case of GC
1627       String revertptr = super.generateTemp(fm, reverttable.get(lb), lb);
1628       output.println(revertptr + "=trans->revertlist;");
1629     }
1630     if (fn.getType().isArray()) {
1631       int arrayid = state.getArrayNumber(fn.getType())
1632                     + state.numClasses();
1633       if (fn.isGlobal()) {
1634         output.println(super.generateTemp(fm, fn.getDst(), lb)
1635                        + "=allocate_newarrayglobal(trans, " + arrayid + ", "
1636                        + super.generateTemp(fm, fn.getSize(), lb) + ");");
1637       } else if (GENERATEPRECISEGC) {
1638         output.println(super.generateTemp(fm, fn.getDst(), lb)
1639                        + "=allocate_newarray(&" + localsprefix + ", "
1640                        + arrayid + ", " + super.generateTemp(fm, fn.getSize(), lb)
1641                        + ");");
1642       } else {
1643         output.println(super.generateTemp(fm, fn.getDst(), lb)
1644                        + "=allocate_newarray(" + arrayid + ", "
1645                        + super.generateTemp(fm, fn.getSize(), lb) + ");");
1646       }
1647     } else {
1648       if (fn.isGlobal()) {
1649         output.println(super.generateTemp(fm, fn.getDst(), lb)
1650                        + "=allocate_newglobal(trans, "
1651                        + fn.getType().getClassDesc().getId() + ");");
1652       } else if (GENERATEPRECISEGC) {
1653         output.println(super.generateTemp(fm, fn.getDst(), lb)
1654                        + "=allocate_new(&" + localsprefix + ", "
1655                        + fn.getType().getClassDesc().getId() + ");");
1656       } else {
1657         output.println(super.generateTemp(fm, fn.getDst(), lb)
1658                        + "=allocate_new("
1659                        + fn.getType().getClassDesc().getId() + ");");
1660       }
1661     }
1662     if (state.DSM && locality.getAtomic(lb).get(fn).intValue() > 0
1663         && !fn.isGlobal()) {
1664       String revertptr = super.generateTemp(fm, reverttable.get(lb), lb);
1665       output.println("trans->revertlist=" + revertptr + ";");
1666     }
1667     // create alias lock if necessary
1668     if((this.m_aliaslocksTbl4FN != null) && (this.m_aliaslocksTbl4FN.containsKey(fn))) {
1669       Vector<Integer> tmpv = this.m_aliaslocksTbl4FN.get(fn);
1670       for(int i = 0; i < tmpv.size(); i++) {
1671         output.println("addAliasLock(" + super.generateTemp(fm, fn.getDst(), lb) + ", aliaslocks[" + tmpv.elementAt(i).intValue() + "]);");
1672       }
1673     }
1674     // generate codes for profiling, recording how many new objects are created
1675     if(!fn.getType().isArray() && 
1676             (fn.getType().getClassDesc() != null) 
1677             && (fn.getType().getClassDesc().hasFlags())) {
1678         output.println("#ifdef PROFILE");
1679         output.println("addNewObjInfo(\"" + fn.getType().getClassDesc().getSymbol() + "\");");
1680         output.println("#endif");
1681     }
1682   }
1683
1684   class TranObjInfo {
1685     public String name;
1686     public int targetcore;
1687     public FlagState fs;
1688   }
1689
1690   private boolean contains(Vector<TranObjInfo> sendto, 
1691                            TranObjInfo t) {
1692     if(sendto.size() == 0) {
1693       return false;
1694     }
1695     for(int i = 0; i < sendto.size(); i++) {
1696       TranObjInfo tmp = sendto.elementAt(i);
1697       if(!tmp.name.equals(t.name)) {
1698         return false;
1699       }
1700       if(tmp.targetcore != t.targetcore) {
1701         return false;
1702       }
1703       if(tmp.fs != t.fs) {
1704         return false;
1705       }
1706     }
1707     return true;
1708   }
1709 }