bug fixes in multicore gc
[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) || (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()+";");
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) || (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()+";");
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) || (this.state.MULTICOREGC)) {
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
591     TempObject objecttemp=(TempObject) tempstable.get(lb!=null ? lb : task);
592     /*if (state.DSM&&lb.getHasAtomic()) {
593         output.println("transrecord_t * trans;");
594        }*/
595
596     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
597       output.print("   struct "+task.getCoreSafeSymbol(num)+"_locals "+localsprefix+"={");
598
599       output.print(objecttemp.numPointers()+",");
600       output.print(paramsprefix);
601       for(int j=0; j<objecttemp.numPointers(); j++)
602         output.print(", NULL");
603       output.println("};");
604     }
605
606     for(int i=0; i<objecttemp.numPrimitives(); i++) {
607       TempDescriptor td=objecttemp.getPrimitive(i);
608       TypeDescriptor type=td.getType();
609       if (type.isNull())
610         output.println("   void * "+td.getSafeSymbol()+";");
611       else if (type.isClass()||type.isArray())
612         output.println("   struct "+type.getSafeSymbol()+" * "+td.getSafeSymbol()+";");
613       else
614         output.println("   "+type.getSafeSymbol()+" "+td.getSafeSymbol()+";");
615     }
616
617     for(int i = 0; i < fm.numParameters(); ++i) {
618       TempDescriptor temp = fm.getParameter(i);
619       output.println("   int "+generateTempFlagName(fm, temp, lb)+" = "+super.generateTemp(fm, temp, lb)+
620                      "->flag;");
621     }
622
623     /* Assign labels to FlatNode's if necessary.*/
624
625     Hashtable<FlatNode, Integer> nodetolabel=super.assignLabels(fm);
626
627     /* Check to see if we need to do a GC if this is a
628      * multi-threaded program...*/
629     if(this.state.MULTICOREGC) {
630       output.println("if(gcflag) gc("+localsprefixaddr+");");
631     }
632
633     /*if ((state.THREAD||state.DSM)&&GENERATEPRECISEGC) {
634         if (state.DSM&&lb.isAtomic())
635             output.println("checkcollect2(&"+localsprefix+",trans);");
636         else
637             output.println("checkcollect(&"+localsprefix+");");
638        }*/
639
640     /* Create queues to store objects need to be transferred to other cores and their destination*/
641     //output.println("   struct Queue * totransobjqueue = createQueue();");
642     output.println("   clearQueue(totransobjqueue);");
643     output.println("   struct transObjInfo * tmpObjInfo = NULL;");
644
645     this.m_aliasSets = null;
646     this.m_aliasFNTbl4Para = null;
647     this.m_aliasFNTbl = null;
648     this.m_aliaslocksTbl4FN = null;
649     outputAliasLockCode(fm, lb, output);
650
651     /* generate print information for RAW version */
652     output.println("#ifdef MULTICORE");
653         if(this.state.RAW) {
654                 output.println("{");
655                 output.println("int tmpsum = 0;");
656                 output.println("char * taskname = \"" + task.getSymbol() + "\";");
657                 output.println("int tmplen = " + task.getSymbol().length() + ";");
658                 output.println("int tmpindex = 1;");
659                 output.println("for(;tmpindex < tmplen; tmpindex++) {");
660                 output.println("   tmpsum = tmpsum * 10 + *(taskname + tmpindex) - '0';");
661                 output.println("}");
662         }
663     output.println("#ifdef RAWPATH");
664         if(this.state.RAW) {
665                 output.println("BAMBOO_DEBUGPRINT(0xAAAA);");
666                 output.println("BAMBOO_DEBUGPRINT_REG(tmpsum);"); 
667         } else {
668                 output.println("BAMBOO_START_CRITICAL_SECTION();");
669                 output.println("tprintf(\"Process %x(%d): task %s\\n\", corenum, corenum, \"" + task.getSymbol() + "\");");
670                 output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
671         }
672         //output.println("BAMBOO_DEBUGPRINT(BAMBOO_GET_EXE_TIME());");
673     output.println("#endif");
674     output.println("#ifdef DEBUG");
675         if(this.state.RAW) {
676                 output.println("BAMBOO_DEBUGPRINT(0xAAAA);");
677                 output.println("BAMBOO_DEBUGPRINT_REG(tmpsum);");
678         } else {
679                 output.println("BAMBOO_START_CRITICAL_SECTION();");
680                 output.println("tprintf(\"Process %x(%d): task %s\\n\", corenum, corenum, \"" + task.getSymbol() + "\");");
681                 output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
682         }
683     output.println("#endif");
684         if(this.state.RAW) {
685                 output.println("}");
686         }
687         output.println("#endif");
688
689     for(int i = 0; i < fm.numParameters(); ++i) {
690       TempDescriptor temp = fm.getParameter(i);
691       output.println("   ++" + super.generateTemp(fm, temp, lb)+"->version;");
692     }
693
694     /* Do the actual code generation */
695     FlatNode current_node=null;
696     HashSet tovisit=new HashSet();
697     HashSet visited=new HashSet();
698     tovisit.add(fm.getNext(0));
699     while(current_node!=null||!tovisit.isEmpty()) {
700       if (current_node==null) {
701         current_node=(FlatNode)tovisit.iterator().next();
702         tovisit.remove(current_node);
703       }
704       visited.add(current_node);
705       if (nodetolabel.containsKey(current_node))
706         output.println("L"+nodetolabel.get(current_node)+":");
707       /*if (state.INSTRUCTIONFAILURE) {
708           if (state.THREAD||state.DSM) {
709               output.println("if ((++instructioncount)>failurecount) {instructioncount=0;injectinstructionfailure();}");
710           }
711           else
712               output.println("if ((--instructioncount)==0) injectinstructionfailure();");
713          }*/
714       if (current_node.numNext()==0) {
715         output.print("   ");
716         super.generateFlatNode(fm, lb, current_node, output);
717         if (current_node.kind()!=FKind.FlatReturnNode) {
718           //output.println("   flushAll();");
719           output.println("#ifdef CACHEFLUSH");
720           output.println("BAMBOO_START_CRITICAL_SECTION();");
721           output.println("#ifdef DEBUG");
722           output.println("BAMBOO_DEBUGPRINT(0xec00);");
723           output.println("#endif");
724           output.println("BAMBOO_CACHE_FLUSH_ALL();");
725           output.println("#ifdef DEBUG");
726           output.println("BAMBOO_DEBUGPRINT(0xecff);");
727           output.println("#endif");
728           output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
729           output.println("#endif");
730           outputTransCode(output);
731           output.println("   return;");
732         }
733         current_node=null;
734       } else if(current_node.numNext()==1) {
735         output.print("   ");
736         super.generateFlatNode(fm, lb, current_node, output);
737         FlatNode nextnode=current_node.getNext(0);
738         if (visited.contains(nextnode)) {
739           output.println("goto L"+nodetolabel.get(nextnode)+";");
740           current_node=null;
741         } else
742           current_node=nextnode;
743       } else if (current_node.numNext()==2) {
744         /* Branch */
745         output.print("   ");
746         super.generateFlatCondBranch(fm, lb, (FlatCondBranch)current_node, "L"+nodetolabel.get(current_node.getNext(1)), output);
747         if (!visited.contains(current_node.getNext(1)))
748           tovisit.add(current_node.getNext(1));
749         if (visited.contains(current_node.getNext(0))) {
750           output.println("goto L"+nodetolabel.get(current_node.getNext(0))+";");
751           current_node=null;
752         } else
753           current_node=current_node.getNext(0);
754       } else throw new Error();
755     }
756
757     output.println("}\n\n");
758   }
759
760   /** This method outputs TaskDescriptor information */
761   private void generateTaskDescriptor(PrintWriter output, 
762                                       PrintWriter outtask, 
763                                       FlatMethod fm, 
764                                       TaskDescriptor task, 
765                                       Vector[] qnames) {
766     int num = this.currentSchedule.getCoreNum();
767
768     output.println("/* TaskDescriptor information for task " + task.getSymbol() + " on core " + num + "*/");
769
770     for (int i=0; i<task.numParameters(); i++) {
771       VarDescriptor param_var=task.getParameter(i);
772       TypeDescriptor param_type=task.getParamType(i);
773       FlagExpressionNode param_flag=task.getFlag(param_var);
774       TagExpressionList param_tag=task.getTag(param_var);
775
776       int dnfterms;
777       if (param_flag==null) {
778         output.println("int parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+"[]={");
779         output.println("0x0, 0x0 };");
780         dnfterms=1;
781       } else {
782         DNFFlag dflag=param_flag.getDNF();
783         dnfterms=dflag.size();
784
785         Hashtable flags=(Hashtable)flagorder.get(param_type.getClassDesc());
786         output.println("int parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+"[]={");
787         for(int j=0; j<dflag.size(); j++) {
788           if (j!=0)
789             output.println(",");
790           Vector term=dflag.get(j);
791           int andmask=0;
792           int checkmask=0;
793           for(int k=0; k<term.size(); k++) {
794             DNFFlagAtom dfa=(DNFFlagAtom)term.get(k);
795             FlagDescriptor fd=dfa.getFlag();
796             boolean negated=dfa.getNegated();
797             int flagid=1<<((Integer)flags.get(fd)).intValue();
798             andmask|=flagid;
799             if (!negated)
800               checkmask|=flagid;
801           }
802           output.print("0x"+Integer.toHexString(andmask)+", 0x"+Integer.toHexString(checkmask));
803         }
804         output.println("};");
805       }
806
807       output.println("int parametertag_"+i+"_"+task.getCoreSafeSymbol(num)+"[]={");
808       //BUG...added next line to fix, test with any task program
809       if (param_tag!=null)
810         for(int j=0; j<param_tag.numTags(); j++) {
811           if (j!=0)
812             output.println(",");
813           /* for each tag we need */
814           /* which slot it is */
815           /* what type it is */
816           TagVarDescriptor tvd=(TagVarDescriptor)task.getParameterTable().get(param_tag.getName(j));
817           TempDescriptor tmp=param_tag.getTemp(j);
818           int slot=fm.getTagInt(tmp);
819           output.println(slot+", "+state.getTagId(tvd.getTag()));
820         }
821       output.println("};");
822
823       // generate object queue for this parameter
824       String qname = this.objqueueprefix+i+"_"+task.getCoreSafeSymbol(num);
825       if(param_type.getClassDesc().getSymbol().equals("StartupObject")) {
826         this.startupcorenum = num;
827       }
828       if(qnames[param_type.getClassDesc().getId()] == null) {
829         qnames[param_type.getClassDesc().getId()] = new Vector();
830       }
831       qnames[param_type.getClassDesc().getId()].addElement(qname);
832       outtask.println("extern struct parameterwrapper " + qname + ";");
833       output.println("struct parameterwrapper " + qname + "={");
834       output.println(".objectset = 0,");      // objectset
835       output.println("/* number of DNF terms */ .numberofterms = "+dnfterms+",");     // numberofterms
836       output.println(".intarray = parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+",");    // intarray
837       // numbertags
838       if (param_tag!=null)
839         output.println("/* number of tags */ .numbertags = "+param_tag.numTags()+",");
840       else
841         output.println("/* number of tags */ .numbertags = 0,");
842       output.println(".tagarray = parametertag_"+i+"_"+task.getCoreSafeSymbol(num)+",");    // tagarray
843       output.println(".task = 0,");      // task
844       output.println(".slot = " + i + ",");    // slot
845       // iterators
846       output.println("};");
847
848       output.println("struct parameterdescriptor parameter_"+i+"_"+task.getCoreSafeSymbol(num)+"={");
849       output.println("/* type */"+param_type.getClassDesc().getId()+",");
850       output.println("/* number of DNF terms */"+dnfterms+",");
851       output.println("parameterdnf_"+i+"_"+task.getCoreSafeSymbol(num)+",");    // intarray
852       output.println("&" + qname + ",");     // queue
853       //BUG, added next line to fix and else statement...test
854       //with any task program
855       if (param_tag!=null)
856         output.println("/* number of tags */"+param_tag.numTags()+",");
857       else
858         output.println("/* number of tags */ 0,");
859       output.println("parametertag_"+i+"_"+task.getCoreSafeSymbol(num));     // tagarray
860       output.println("};");
861     }
862
863     /* parameter queues for this task*/
864     output.println("struct parameterwrapper * " + this.paramqarrayprefix + task.getCoreSafeSymbol(num)+"[] = {");
865     for (int i=0; i<task.numParameters(); i++) {
866       if (i!=0)
867         output.println(",");
868       output.print("&" + this.objqueueprefix + i + "_" + task.getCoreSafeSymbol(num));
869     }
870     output.println("};");
871
872     output.println("struct parameterdescriptor * parameterdescriptors_"+task.getCoreSafeSymbol(num)+"[] = {");
873     for (int i=0; i<task.numParameters(); i++) {
874       if (i!=0)
875         output.println(",");
876       output.print("&parameter_"+i+"_"+task.getCoreSafeSymbol(num));
877     }
878     output.println("};");
879
880     output.println("struct taskdescriptor " + this.taskprefix + task.getCoreSafeSymbol(num) + "={");
881     output.println("&"+task.getCoreSafeSymbol(num)+",");
882     output.println("/* number of parameters */" +task.numParameters() + ",");
883     int numtotal=task.numParameters()+fm.numTags();
884     output.println("/* number total parameters */" +numtotal + ",");
885     output.println("parameterdescriptors_"+task.getCoreSafeSymbol(num)+",");
886     output.println("\""+task.getSymbol()+"\"");
887     output.println("};");
888
889     output.println();
890   }
891
892   /** This method generates header information for the task
893    *  referenced by the Descriptor des. */
894
895   private void generateTaskHeader(FlatMethod fm, 
896                                   LocalityBinding lb, 
897                                   Descriptor des, 
898                                   PrintWriter output) {
899     /* Print header */
900     ParamsObject objectparams=(ParamsObject)paramstable.get(lb!=null ? lb : des);
901     TaskDescriptor task=(TaskDescriptor) des;
902
903     int num = this.currentSchedule.getCoreNum();
904     //catch the constructor case
905     output.print("void ");
906     output.print(task.getCoreSafeSymbol(num)+"(");
907
908     boolean printcomma=false;
909     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
910       output.print("struct "+task.getCoreSafeSymbol(num)+"_params * "+paramsprefix);
911       printcomma=true;
912     }
913
914     /*if (state.DSM&&lb.isAtomic()) {
915         if (printcomma)
916             output.print(", ");
917         output.print("transrecord_t * trans");
918         printcomma=true;
919        }*/
920
921     if (!GENERATEPRECISEGC && !this.state.MULTICOREGC) {
922       /* Imprecise Task */
923       output.println("void * parameterarray[]) {");
924       /* Unpack variables */
925       for(int i=0; i<objectparams.numPrimitives(); i++) {
926         TempDescriptor temp=objectparams.getPrimitive(i);
927         output.println("struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+"=parameterarray["+i+"];");
928       }
929       for(int i=0; i<fm.numTags(); i++) {
930         TempDescriptor temp=fm.getTag(i);
931         int offset=i+objectparams.numPrimitives();
932         output.println("struct ___TagDescriptor___ * "+temp.getSafeSymbol()+i+"___=parameterarray["+offset+"];");     // add i to fix bugs of duplicate definition of tags
933       }
934
935       if ((objectparams.numPrimitives()+fm.numTags())>maxtaskparams)
936         maxtaskparams=objectparams.numPrimitives()+fm.numTags();
937     } else output.println(") {");
938   }
939
940   protected void generateFlagOrAnd(FlatFlagActionNode ffan, 
941                                    FlatMethod fm, 
942                                    LocalityBinding lb, 
943                                    TempDescriptor temp,
944                                    PrintWriter output, 
945                                    int ormask, 
946                                    int andmask) {
947     if (ffan.getTaskType()==FlatFlagActionNode.NEWOBJECT) {
948       output.println("flagorandinit("+super.generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+", 0x"+Integer.toHexString(andmask)+");");
949     } else {
950       int num = this.currentSchedule.getCoreNum();
951       ClassDescriptor cd = temp.getType().getClassDesc();
952       Vector<FlagState> initfstates = ffan.getInitFStates(cd);
953       for(int i = 0; i < initfstates.size(); ++i) {
954         FlagState tmpFState = initfstates.elementAt(i);
955         output.println("{");
956         QueueInfo qinfo = outputqueues(tmpFState, num, output, false);
957         output.println("flagorand("+super.generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+
958                        ", 0x"+Integer.toHexString(andmask)+", " + qinfo.qname +
959                        ", " + qinfo.length + ");");
960         output.println("}");
961       }
962       if(ffan.getTaskType()==FlatFlagActionNode.TASKEXIT) {
963           // generate codes for profiling, recording which task exit it is
964           output.println("#ifdef PROFILE");
965           output.println("setTaskExitIndex(" + ffan.getTaskExitIndex() + ");");
966           output.println("#endif");
967       }
968     }
969   }
970
971   protected void generateObjectDistribute(FlatFlagActionNode ffan, 
972                                               FlatMethod fm, 
973                                               LocalityBinding lb, 
974                                               TempDescriptor temp,
975                                           PrintWriter output) {
976     ClassDescriptor cd = temp.getType().getClassDesc();
977     Vector<FlagState> initfstates = null;
978     Vector[] targetFStates = null;
979     if (ffan.getTaskType()==FlatFlagActionNode.NEWOBJECT) {
980       targetFStates = new Vector[1];
981       targetFStates[0] = ffan.getTargetFStates4NewObj(cd);
982     } else {
983       initfstates = ffan.getInitFStates(cd);
984       targetFStates = new Vector[initfstates.size()];
985       for(int i = 0; i < initfstates.size(); ++i) {
986         FlagState fs = initfstates.elementAt(i);
987         targetFStates[i] = ffan.getTargetFStates(fs);
988
989         if(!fs.isSetmask()) {
990           Hashtable flags=(Hashtable)flagorder.get(cd);
991           int andmask=0;
992           int checkmask=0;
993           Iterator it_flags = fs.getFlags();
994           while(it_flags.hasNext()) {
995             FlagDescriptor fd = (FlagDescriptor)it_flags.next();
996             int flagid=1<<((Integer)flags.get(fd)).intValue();
997             andmask|=flagid;
998             checkmask|=flagid;
999           }
1000           fs.setAndmask(andmask);
1001           fs.setCheckmask(checkmask);
1002           fs.setSetmask(true);
1003         }
1004       }
1005     }
1006     boolean isolate = true;     // check if this flagstate can associate to some task with multiple params which can
1007                                 // reside on multiple cores
1008     if((this.currentSchedule == null) && (fm.getMethod().getClassDesc().getSymbol().equals("ServerSocket"))) {
1009       // ServerSocket object will always reside on current core
1010       for(int j = 0; j < targetFStates.length; ++j) {
1011         if(initfstates != null) {
1012           FlagState fs = initfstates.elementAt(j);
1013           output.println("if(" + generateTempFlagName(fm, temp, lb) + "&(0x" + Integer.toHexString(fs.getAndmask())
1014                          + ")==(0x" + Integer.toHexString(fs.getCheckmask()) + ")) {");
1015         }
1016         Vector<FlagState> tmpfstates = (Vector<FlagState>)targetFStates[j];
1017         for(int i = 0; i < tmpfstates.size(); ++i) {
1018           FlagState tmpFState = tmpfstates.elementAt(i);
1019           // TODO
1020           // may have bugs here
1021           output.println("/* reside on this core*");
1022           output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1023         }
1024         if(initfstates != null) {
1025           output.println("}");
1026         }
1027       }
1028       return;
1029     }
1030
1031     int num = this.currentSchedule.getCoreNum();
1032     Hashtable<FlagState, Queue<Integer>> targetCoreTbl = this.currentSchedule.getTargetCoreTable();
1033     for(int j = 0; j < targetFStates.length; ++j) {
1034       FlagState fs = null;
1035       if(initfstates != null) {
1036         fs = initfstates.elementAt(j);
1037         output.println("if((" + generateTempFlagName(fm, temp, lb) + "&(0x" + Integer.toHexString(fs.getAndmask())
1038                        + "))==(0x" + Integer.toHexString(fs.getCheckmask()) + ")) {");
1039       }
1040       Vector<FlagState> tmpfstates = (Vector<FlagState>)targetFStates[j];
1041       for(int i = 0; i < tmpfstates.size(); ++i) {
1042         FlagState tmpFState = tmpfstates.elementAt(i);
1043
1044         if(this.currentSchedule.getAllyCoreTable() == null) {
1045           isolate = true;
1046         } else {
1047           isolate = (this.currentSchedule.getAllyCoreTable().get(tmpFState) == null) ||
1048                     (this.currentSchedule.getAllyCoreTable().get(tmpFState).size() == 0);
1049         }
1050
1051         Vector<Integer> sendto = new Vector<Integer>();
1052         Queue<Integer> queue = null;
1053         if(targetCoreTbl != null) {
1054           queue = targetCoreTbl.get(tmpFState);
1055         }
1056         if((queue != null) &&
1057            ((queue.size() != 1) ||
1058             ((queue.size() == 1) && (queue.element().intValue() != num)))) {
1059           // this object may be transferred to other cores
1060           String queuename = (String) this.fsate2qnames[num].get(tmpFState);
1061           String queueins = queuename + "ins";
1062
1063           Object[] cores = queue.toArray();
1064           String index = "0";
1065           Integer targetcore = (Integer)cores[0];
1066           if(queue.size() > 1) {
1067             index = queueins + ".index";
1068           }
1069           if(queue.size() > 1) {
1070             output.println("switch(" + queueins + ".index % " + queueins + ".length) {");
1071             for(int k = 0; k < cores.length; ++k) {
1072               output.println("case " + k + ":");
1073               targetcore = (Integer)cores[k];
1074               if(targetcore.intValue() == num) {
1075                 output.println("/* reside on this core*/");
1076                 if(isolate) {
1077                   output.println("{");
1078                   QueueInfo qinfo = outputqueues(tmpFState, num, output, true);
1079                   output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", " + qinfo.qname +
1080                                  ", " + qinfo.length + ");");
1081                   output.println("}");
1082                 } /*else {
1083                   // TODO
1084                   // really needed?
1085                   output.println("/* possibly needed by multi-parameter tasks on this core*//*");
1086                   output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1087                 }*/  // deleted 09/07/06, multi-param tasks are pinned to one core now
1088               } else {
1089                 /*if(!isolate) {
1090                   // TODO
1091                   // Is it possible to decide the actual queues?
1092                   output.println("/* possibly needed by multi-parameter tasks on this core*//*");
1093                   output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1094                 }*/ // deleted 09/07/06, multi-param tasks are pinned to one core now
1095                 output.println("/* transfer to core " + targetcore.toString() + "*/");
1096                 output.println("{");
1097                 // enqueue this object and its destinations for later process
1098                 // all the possible queues
1099                 QueueInfo qinfo = null;
1100                 TranObjInfo tmpinfo = new TranObjInfo();
1101                 tmpinfo.name = super.generateTemp(fm, temp, lb);
1102                 tmpinfo.targetcore = targetcore;
1103                 FlagState targetFS = this.currentSchedule.getTargetFState(tmpFState);
1104                 if(targetFS != null) {
1105                   tmpinfo.fs = targetFS;
1106                 } else {
1107                   tmpinfo.fs = tmpFState;
1108                 }
1109                   qinfo = outputtransqueues(tmpinfo.fs, targetcore, output);
1110                   output.println("tmpObjInfo = RUNMALLOC(sizeof(struct transObjInfo));");
1111                   output.println("tmpObjInfo->objptr = (void *)" + tmpinfo.name + ";");
1112                   output.println("tmpObjInfo->targetcore = "+targetcore.toString()+";");
1113                   output.println("tmpObjInfo->queues = " + qinfo.qname + ";");
1114                   output.println("tmpObjInfo->length = " + qinfo.length + ";");
1115                   output.println("addNewItem(totransobjqueue, (void*)tmpObjInfo);");
1116                 output.println("}");
1117               }
1118               output.println("break;");
1119             }
1120             output.println("}");
1121           } else {
1122             /*if(!isolate) {
1123               // TODO
1124               // Is it possible to decide the actual queues?
1125               output.println("/* possibly needed by multi-parameter tasks on this core*//*");
1126               output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1127             }*/ // deleted 09/07/06, multi-param tasks are pinned to one core now
1128             output.println("/* transfer to core " + targetcore.toString() + "*/");
1129             output.println("{");
1130             // enqueue this object and its destinations for later process
1131             // all the possible queues
1132             QueueInfo qinfo = null;
1133             TranObjInfo tmpinfo = new TranObjInfo();
1134             tmpinfo.name = super.generateTemp(fm, temp, lb);
1135             tmpinfo.targetcore = targetcore;
1136             FlagState targetFS = this.currentSchedule.getTargetFState(tmpFState);
1137             if(targetFS != null) {
1138               tmpinfo.fs = targetFS;
1139             } else {
1140               tmpinfo.fs = tmpFState;
1141             }
1142               qinfo = outputtransqueues(tmpinfo.fs, targetcore, output);
1143               output.println("tmpObjInfo = RUNMALLOC(sizeof(struct transObjInfo));");
1144               output.println("tmpObjInfo->objptr = (void *)" + tmpinfo.name + ";");
1145               output.println("tmpObjInfo->targetcore = "+targetcore.toString()+";");
1146               output.println("tmpObjInfo->queues = " + qinfo.qname + ";");
1147               output.println("tmpObjInfo->length = " + qinfo.length + ";");
1148               output.println("addNewItem(totransobjqueue, (void*)tmpObjInfo);");
1149             output.println("}");
1150           }
1151           output.println("/* increase index*/");
1152           output.println("++" + queueins + ".index;");
1153         } else {
1154           // this object will reside on current core
1155           output.println("/* reside on this core*/");
1156           if(isolate) {
1157             output.println("{");
1158             QueueInfo qinfo = outputqueues(tmpFState, num, output, true);
1159             output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", " + qinfo.qname +
1160                            ", " + qinfo.length + ");");
1161             output.println("}");
1162           } /*else {
1163             // TODO
1164             // really needed?
1165             output.println("enqueueObject("+super.generateTemp(fm, temp, lb)+", NULL, 0);");
1166           }*/ // deleted 09/07/06, multi-param tasks are pinned to one core now
1167         }
1168
1169         // codes for multi-params tasks
1170         if(!isolate) {
1171           // flagstate associated with some multi-params tasks
1172           // need to be send to other cores
1173           Vector<Integer> targetcores = this.currentSchedule.getAllyCores(tmpFState);
1174           output.println("/* send the shared object to possible queues on other cores*/");
1175           // TODO, temporary solution, send to mostly the first two 
1176           int upperbound = targetcores.size() > 2? 2: targetcores.size();
1177           for(int k = 0; k < upperbound; ++k) {
1178             // TODO
1179             // add the information of exactly which queue
1180             int targetcore = targetcores.elementAt(k).intValue();
1181             if(!sendto.contains(targetcore)) {
1182             // previously not sended to this target core
1183             // enqueue this object and its destinations for later process
1184             output.println("{");
1185             // all the possible queues
1186             QueueInfo qinfo = null;
1187             TranObjInfo tmpinfo = new TranObjInfo();
1188             tmpinfo.name = super.generateTemp(fm, temp, lb);
1189             tmpinfo.targetcore = targetcore;
1190             FlagState targetFS = this.currentSchedule.getTargetFState(tmpFState);
1191             if(targetFS != null) {
1192               tmpinfo.fs = targetFS;
1193             } else {
1194               tmpinfo.fs = tmpFState;
1195             }
1196               qinfo = outputtransqueues(tmpinfo.fs, targetcore, output);
1197               output.println("tmpObjInfo = RUNMALLOC(sizeof(struct transObjInfo));");
1198               output.println("tmpObjInfo->objptr = (void *)" + tmpinfo.name + ";");
1199               output.println("tmpObjInfo->targetcore = "+targetcore+";");
1200               output.println("tmpObjInfo->queues = " + qinfo.qname + ";");
1201               output.println("tmpObjInfo->length = " + qinfo.length + ";");
1202               output.println("addNewItem(totransobjqueue, (void*)tmpObjInfo);");
1203               output.println("}");
1204               sendto.addElement(targetcore);
1205             }
1206           }
1207         }
1208       }
1209
1210       if(initfstates != null) {
1211         output.println("}");
1212       }
1213     }
1214   }
1215
1216   private QueueInfo outputqueues(FlagState tmpFState, 
1217                                  int num, 
1218                                  PrintWriter output, 
1219                                  boolean isEnqueue) {
1220     // queue array
1221     QueueInfo qinfo = new QueueInfo();
1222     qinfo.qname  = "queues_" + tmpFState.getLabel() + "_" + tmpFState.getiuid();
1223     output.println("struct parameterwrapper * " + qinfo.qname + "[] = {");
1224     Iterator it_edges = tmpFState.getEdgeVector().iterator();
1225     Vector<TaskDescriptor> residetasks = this.currentSchedule.getTasks();
1226     Vector<TaskDescriptor> tasks = new Vector<TaskDescriptor>();
1227     Vector<Integer> indexes = new Vector<Integer>();
1228     boolean comma = false;
1229     qinfo.length = 0;
1230     while(it_edges.hasNext()) {
1231       FEdge fe = (FEdge)it_edges.next();
1232       TaskDescriptor td = fe.getTask();
1233       int paraindex = fe.getIndex();
1234       if((!isEnqueue) || (isEnqueue && residetasks.contains(td))) {
1235         if((!tasks.contains(td)) ||
1236            ((tasks.contains(td)) && (paraindex != indexes.elementAt(tasks.indexOf(td)).intValue()))) {
1237           tasks.addElement(td);
1238           indexes.addElement(paraindex);
1239           if(comma) {
1240             output.println(",");
1241           } else {
1242             comma = true;
1243           }
1244           output.print("&" + this.objqueueprefix + paraindex + "_" + td.getCoreSafeSymbol(num));
1245           ++qinfo.length;
1246         }
1247       }
1248     }
1249     output.println("};");
1250     return qinfo;
1251   }
1252
1253   private QueueInfo outputtransqueues(FlagState tmpFState, 
1254                                       int targetcore, 
1255                                       PrintWriter output) {
1256     // queue array
1257     QueueInfo qinfo = new QueueInfo();
1258     qinfo.qname  = "queues_" + tmpFState.getLabel() + "_" + tmpFState.getiuid();
1259     output.println("int " + qinfo.qname + "_clone[] = {");
1260     Iterator it_edges = tmpFState.getEdgeVector().iterator();
1261     Vector<TaskDescriptor> residetasks = this.scheduling.get(targetcore).getTasks();
1262     Vector<TaskDescriptor> tasks = new Vector<TaskDescriptor>();
1263     Vector<Integer> indexes = new Vector<Integer>();
1264     boolean comma = false;
1265     qinfo.length = 0;
1266     while(it_edges.hasNext()) {
1267       FEdge fe = (FEdge)it_edges.next();
1268       TaskDescriptor td = fe.getTask();
1269       int paraindex = fe.getIndex();
1270       if(residetasks.contains(td)) {
1271         if((!tasks.contains(td)) ||
1272            ((tasks.contains(td)) && (paraindex != indexes.elementAt(tasks.indexOf(td)).intValue()))) {
1273           tasks.addElement(td);
1274           indexes.addElement(paraindex);
1275           if(comma) {
1276             output.println(",");
1277           } else {
1278             comma = true;
1279           }
1280           output.print(residetasks.indexOf(td) + ", ");
1281           output.print(paraindex);
1282           ++qinfo.length;
1283         }
1284       }
1285     }
1286     output.println("};");
1287     output.println("int * " + qinfo.qname + " = RUNMALLOC(sizeof(int) * " + qinfo.length * 2 + ");");
1288     output.println("memcpy(" + qinfo.qname + ", (int *)" + qinfo.qname + "_clone, sizeof(int) * " + qinfo.length * 2 + ");");
1289     return qinfo;
1290   }
1291
1292   private class QueueInfo {
1293     public int length;
1294     public String qname;
1295   }
1296
1297   private String generateTempFlagName(FlatMethod fm, 
1298                                       TempDescriptor td, 
1299                                       LocalityBinding lb) {
1300     MethodDescriptor md=fm.getMethod();
1301     TaskDescriptor task=fm.getTask();
1302     TempObject objecttemps=(TempObject) tempstable.get(lb!=null ? lb : md!=null ? md : task);
1303
1304     if (objecttemps.isLocalPrim(td)||objecttemps.isParamPrim(td)) {
1305       return td.getSafeSymbol() + "_oldflag";
1306     }
1307
1308     if (objecttemps.isLocalPtr(td)) {
1309       return localsprefix+"_"+td.getSafeSymbol() + "_oldflag";
1310     }
1311
1312     if (objecttemps.isParamPtr(td)) {
1313       return paramsprefix+"_"+td.getSafeSymbol() + "_oldflag";
1314     }
1315     throw new Error();
1316   }
1317
1318   protected void outputTransCode(PrintWriter output) {
1319     output.println("while(0 == isEmpty(totransobjqueue)) {");
1320     output.println("   struct transObjInfo * totransobj = (struct transObjInfo *)(getItem(totransobjqueue));");
1321     output.println("   transferObject(totransobj);");
1322     output.println("   RUNFREE(totransobj->queues);");
1323     output.println("   RUNFREE(totransobj);");
1324     output.println("}");
1325     //output.println("freeQueue(totransobjqueue);");
1326   }
1327
1328   protected void outputAliasLockCode(FlatMethod fm, 
1329                                          LocalityBinding lb, 
1330                                          PrintWriter output) {
1331     if(this.m_oa == null) {
1332       return;
1333     }
1334     TaskDescriptor td = fm.getTask();
1335     Object[] allocSites = this.m_oa.getFlaggedAllocationSitesReachableFromTask(td).toArray();
1336     Vector<Vector<Integer>> aliasSets = new Vector<Vector<Integer>>();
1337     Vector<Vector<FlatNew>> aliasFNSets = new Vector<Vector<FlatNew>>();
1338     Hashtable<Integer, Vector<FlatNew>> aliasFNTbl4Para = new Hashtable<Integer, Vector<FlatNew>>();
1339     Hashtable<FlatNew, Vector<FlatNew>> aliasFNTbl = new Hashtable<FlatNew, Vector<FlatNew>>();
1340     Set<HeapRegionNode> common;
1341     for( int i = 0; i < fm.numParameters(); ++i ) {
1342       // for the ith parameter check for aliases to all
1343       // higher numbered parameters
1344       aliasSets.add(null);
1345       for( int j = i + 1; j < fm.numParameters(); ++j ) {
1346         common = this.m_oa.createsPotentialAliases(td, i, j);
1347         if(!common.isEmpty()) {
1348           // ith parameter and jth parameter has alias, create lock to protect them
1349           if(aliasSets.elementAt(i) == null) {
1350             aliasSets.setElementAt(new Vector<Integer>(), i);
1351           }
1352           aliasSets.elementAt(i).add(j);
1353         }
1354       }
1355
1356       // for the ith parameter, check for aliases against
1357       // the set of allocation sites reachable from this
1358       // task context
1359       aliasFNSets.add(null);
1360       for(int j = 0; j < allocSites.length; j++) {
1361         AllocationSite as = (AllocationSite)allocSites[j];
1362         common = this.m_oa.createsPotentialAliases(td, i, as);
1363         if( !common.isEmpty() ) {
1364           // ith parameter and allocationsite as has alias
1365           if(aliasFNSets.elementAt(i) == null) {
1366             aliasFNSets.setElementAt(new Vector<FlatNew>(), i);
1367           }
1368           aliasFNSets.elementAt(i).add(as.getFlatNew());
1369         }
1370       }
1371     }
1372
1373     // for each allocation site check for aliases with
1374     // other allocation sites in the context of execution
1375     // of this task
1376     for( int i = 0; i < allocSites.length; ++i ) {
1377       AllocationSite as1 = (AllocationSite)allocSites[i];
1378       for(int j = i + 1; j < allocSites.length; j++) {
1379         AllocationSite as2 = (AllocationSite)allocSites[j];
1380
1381         common = this.m_oa.createsPotentialAliases(td, as1, as2);
1382         if( !common.isEmpty() ) {
1383           // as1 and as2 has alias
1384           if(!aliasFNTbl.containsKey(as1.getFlatNew())) {
1385             aliasFNTbl.put(as1.getFlatNew(), new Vector<FlatNew>());
1386           }
1387           if(!aliasFNTbl.get(as1.getFlatNew()).contains(as2.getFlatNew())) {
1388             aliasFNTbl.get(as1.getFlatNew()).add(as2.getFlatNew());
1389           }
1390         }
1391       }
1392     }
1393
1394     // if FlatNew N1->N2->N3, we group N1, N2, N3 together
1395     Iterator<FlatNew> it = aliasFNTbl.keySet().iterator();
1396     Vector<FlatNew> visited = new Vector<FlatNew>();
1397     while(it.hasNext()) {
1398       FlatNew tmpfn = it.next();
1399       if(visited.contains(tmpfn)) {
1400         continue;
1401       }
1402       visited.add(tmpfn);
1403       Queue<FlatNew> tovisit = new LinkedList<FlatNew>();
1404       Vector<FlatNew> tmpv = aliasFNTbl.get(tmpfn);
1405       if(tmpv == null) {
1406         continue;
1407       }
1408
1409       for(int j = 0; j < tmpv.size(); j++) {
1410         tovisit.add(tmpv.elementAt(j));
1411       }
1412
1413       while(!tovisit.isEmpty()) {
1414         FlatNew fn = tovisit.poll();
1415         visited.add(fn);
1416         Vector<FlatNew> tmpset = aliasFNTbl.get(fn);
1417         if(tmpset != null) {
1418           // merge tmpset to the alias set of the ith parameter
1419           for(int j = 0; j < tmpset.size(); j++) {
1420             if(!tmpv.contains(tmpset.elementAt(j))) {
1421               tmpv.add(tmpset.elementAt(j));
1422               tovisit.add(tmpset.elementAt(j));
1423             }
1424           }
1425           aliasFNTbl.remove(fn);
1426         }
1427       }
1428       it = aliasFNTbl.keySet().iterator();
1429     }
1430
1431     // check alias between parameters and between parameter-flatnew
1432     for(int i = 0; i < aliasSets.size(); i++) {
1433       Queue<Integer> tovisit = new LinkedList<Integer>();
1434       Vector<Integer> tmpv = aliasSets.elementAt(i);
1435       if(tmpv == null) {
1436         continue;
1437       }
1438
1439       for(int j = 0; j < tmpv.size(); j++) {
1440         tovisit.add(tmpv.elementAt(j));
1441       }
1442
1443       while(!tovisit.isEmpty()) {
1444         int index = tovisit.poll().intValue();
1445         Vector<Integer> tmpset = aliasSets.elementAt(index);
1446         if(tmpset != null) {
1447           // merge tmpset to the alias set of the ith parameter
1448           for(int j = 0; j < tmpset.size(); j++) {
1449             if(!tmpv.contains(tmpset.elementAt(j))) {
1450               tmpv.add(tmpset.elementAt(j));
1451               tovisit.add(tmpset.elementAt(j));
1452             }
1453           }
1454           aliasSets.setElementAt(null, index);
1455         }
1456
1457         Vector<FlatNew> tmpFNSet = aliasFNSets.elementAt(index);
1458         if(tmpFNSet != null) {
1459           // merge tmpFNSet to the aliasFNSet of the ith parameter
1460           if(aliasFNSets.elementAt(i) == null) {
1461             aliasFNSets.setElementAt(tmpFNSet, i);
1462           } else {
1463             Vector<FlatNew> tmpFNv = aliasFNSets.elementAt(i);
1464             for(int j = 0; j < tmpFNSet.size(); j++) {
1465               if(!tmpFNv.contains(tmpFNSet.elementAt(j))) {
1466                 tmpFNv.add(tmpFNSet.elementAt(j));
1467               }
1468             }
1469           }
1470           aliasFNSets.setElementAt(null, index);
1471         }
1472       }
1473     }
1474
1475     int numlock = 0;
1476     int numparalock = 0;
1477     Vector<Vector<Integer>> tmpaliasSets = new Vector<Vector<Integer>>();
1478     for(int i = 0; i < aliasSets.size(); i++) {
1479       Vector<Integer> tmpv = aliasSets.elementAt(i);
1480       if(tmpv != null) {
1481         tmpv.add(0, i);
1482         tmpaliasSets.add(tmpv);
1483         numlock++;
1484       }
1485
1486       Vector<FlatNew> tmpFNv = aliasFNSets.elementAt(i);
1487       if(tmpFNv != null) {
1488         aliasFNTbl4Para.put(i, tmpFNv);
1489         if(tmpv == null) {
1490           numlock++;
1491         }
1492       }
1493     }
1494     numparalock = numlock;
1495     aliasSets.clear();
1496     aliasSets = null;
1497     this.m_aliasSets = tmpaliasSets;
1498     tmpaliasSets.clear();
1499     tmpaliasSets = null;
1500     aliasFNSets.clear();
1501     aliasFNSets = null;
1502     this.m_aliasFNTbl4Para = aliasFNTbl4Para;
1503     this.m_aliasFNTbl = aliasFNTbl;
1504     numlock += this.m_aliasFNTbl.size();
1505
1506     // create locks
1507     if(numlock > 0) {
1508       output.println("int aliaslocks[" + numlock + "];");
1509       output.println("int tmpi = 0;");      
1510       // associate locks with parameters
1511       int lockindex = 0;
1512       for(int i = 0; i < this.m_aliasSets.size(); i++) {
1513         Vector<Integer> toadd = this.m_aliasSets.elementAt(i);
1514         
1515         output.print("int tmplen_" + lockindex + " = 0;");
1516         output.println("void * tmpptrs_" + lockindex + "[] = {");
1517         for(int j = 0; j < toadd.size(); j++) {
1518             int para = toadd.elementAt(j).intValue();
1519             output.print(super.generateTemp(fm, fm.getParameter(para), lb));
1520             if(j < toadd.size() - 1) {
1521                 output.print(", ");
1522             } else {
1523                 output.println("};");
1524             }
1525         }
1526         output.println("aliaslocks[tmpi++] = getAliasLock(tmpptrs_" + lockindex + ", tmplen_" + lockindex + ", lockRedirectTbl);");
1527         
1528         for(int j = 0; j < toadd.size(); j++) {
1529           int para = toadd.elementAt(j).intValue();
1530           output.println("addAliasLock("  + super.generateTemp(fm, fm.getParameter(para), lb) + ", aliaslocks[" + i + "]);");
1531         }
1532         // check if this lock is also associated with any FlatNew nodes
1533         if(this.m_aliasFNTbl4Para.containsKey(toadd.elementAt(0))) {
1534           if(this.m_aliaslocksTbl4FN == null) {
1535             this.m_aliaslocksTbl4FN = new Hashtable<FlatNew, Vector<Integer>>();
1536           }
1537           Vector<FlatNew> tmpv = this.m_aliasFNTbl4Para.get(toadd.elementAt(0));
1538           for(int j = 0; j < tmpv.size(); j++) {
1539             FlatNew fn = tmpv.elementAt(j);
1540             if(!this.m_aliaslocksTbl4FN.containsKey(fn)) {
1541               this.m_aliaslocksTbl4FN.put(fn, new Vector<Integer>());
1542             }
1543             this.m_aliaslocksTbl4FN.get(fn).add(i);
1544           }
1545           this.m_aliasFNTbl4Para.remove(toadd.elementAt(0));
1546         }
1547         lockindex++;
1548       }
1549       
1550       Object[] key = this.m_aliasFNTbl4Para.keySet().toArray();
1551       for(int i = 0; i < key.length; i++) {
1552         int para = ((Integer)key[i]).intValue();
1553
1554         output.println("void * tmpptrs_" + lockindex + "[] = {" + super.generateTemp(fm, fm.getParameter(para), lb) + "};");
1555         output.println("aliaslocks[tmpi++] = getAliasLock(tmpptrs_" + lockindex + ", 1, lockRedirectTbl);");
1556         
1557         output.println("addAliasLock(" + super.generateTemp(fm, fm.getParameter(para), lb) + ", aliaslocks[" + lockindex + "]);");
1558         Vector<FlatNew> tmpv = this.m_aliasFNTbl4Para.get(para);
1559         for(int j = 0; j < tmpv.size(); j++) {
1560           FlatNew fn = tmpv.elementAt(j);
1561           if(this.m_aliaslocksTbl4FN == null) {
1562             this.m_aliaslocksTbl4FN = new Hashtable<FlatNew, Vector<Integer>>();
1563           }
1564           if(!this.m_aliaslocksTbl4FN.containsKey(fn)) {
1565             this.m_aliaslocksTbl4FN.put(fn, new Vector<Integer>());
1566           }
1567           this.m_aliaslocksTbl4FN.get(fn).add(lockindex);
1568         }
1569         lockindex++;
1570       }
1571       
1572       // check m_aliasFNTbl for locks associated with FlatNew nodes
1573       Object[] FNkey = this.m_aliasFNTbl.keySet().toArray();
1574       for(int i = 0; i < FNkey.length; i++) {
1575         FlatNew fn = (FlatNew)FNkey[i];
1576         Vector<FlatNew> tmpv = this.m_aliasFNTbl.get(fn);
1577         
1578         output.println("aliaslocks[tmpi++] = (int)(RUNMALLOC(sizeof(int)));");
1579         
1580         if(this.m_aliaslocksTbl4FN == null) {
1581           this.m_aliaslocksTbl4FN = new Hashtable<FlatNew, Vector<Integer>>();
1582         }
1583         if(!this.m_aliaslocksTbl4FN.containsKey(fn)) {
1584           this.m_aliaslocksTbl4FN.put(fn, new Vector<Integer>());
1585         }
1586         this.m_aliaslocksTbl4FN.get(fn).add(lockindex);
1587         for(int j = 0; j < tmpv.size(); j++) {
1588           FlatNew tfn = tmpv.elementAt(j);
1589           if(!this.m_aliaslocksTbl4FN.containsKey(tfn)) {
1590             this.m_aliaslocksTbl4FN.put(tfn, new Vector<Integer>());
1591           }
1592           this.m_aliaslocksTbl4FN.get(tfn).add(lockindex);
1593         }
1594         lockindex++;
1595       }
1596     }
1597   }
1598
1599   protected void generateFlatReturnNode(FlatMethod fm, 
1600                                         LocalityBinding lb, 
1601                                         FlatReturnNode frn, 
1602                                         PrintWriter output) {
1603     if (frn.getReturnTemp()!=null) {
1604       if (frn.getReturnTemp().getType().isPtr())
1605         output.println("return (struct "+fm.getMethod().getReturnType().getSafeSymbol()+"*)"+generateTemp(fm, frn.getReturnTemp(), lb)+";");
1606       else
1607         output.println("return "+generateTemp(fm, frn.getReturnTemp(), lb)+";");
1608     } else {
1609       if(fm.getTask() != null) {
1610         output.println("#ifdef CACHEFLUSH");
1611         output.println("BAMBOO_START_CRITICAL_SECTION();");
1612         output.println("#ifdef DEBUG");
1613         output.println("BAMBOO_DEBUGPRINT(0xec00);");
1614         output.println("#endif");
1615         output.println("BAMBOO_CACHE_FLUSH_ALL();");
1616         output.println("#ifdef DEBUG");
1617         output.println("BAMBOO_DEBUGPRINT(0xecff);");
1618         output.println("#endif");
1619         output.println("BAMBOO_CLOSE_CRITICAL_SECTION();");
1620         output.println("#endif");
1621         outputTransCode(output);
1622       }
1623       output.println("return;");
1624     }
1625   }
1626
1627   protected void generateFlatNew(FlatMethod fm, 
1628                                  LocalityBinding lb, 
1629                                  FlatNew fn,
1630                                  PrintWriter output) {
1631     if (state.DSM && locality.getAtomic(lb).get(fn).intValue() > 0
1632         && !fn.isGlobal()) {
1633       // Stash pointer in case of GC
1634       String revertptr = super.generateTemp(fm, reverttable.get(lb), lb);
1635       output.println(revertptr + "=trans->revertlist;");
1636     }
1637     if (fn.getType().isArray()) {
1638       int arrayid = state.getArrayNumber(fn.getType())
1639                     + state.numClasses();
1640       if (fn.isGlobal()) {
1641         output.println(super.generateTemp(fm, fn.getDst(), lb)
1642                        + "=allocate_newarrayglobal(trans, " + arrayid + ", "
1643                        + super.generateTemp(fm, fn.getSize(), lb) + ");");
1644       } else if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1645         output.println(super.generateTemp(fm, fn.getDst(), lb)
1646                        + "=allocate_newarray(&" + localsprefix + ", "
1647                        + arrayid + ", " + super.generateTemp(fm, fn.getSize(), lb)
1648                        + ");");
1649       } else {
1650         output.println(super.generateTemp(fm, fn.getDst(), lb)
1651                        + "=allocate_newarray(" + arrayid + ", "
1652                        + super.generateTemp(fm, fn.getSize(), lb) + ");");
1653       }
1654     } else {
1655       if (fn.isGlobal()) {
1656         output.println(super.generateTemp(fm, fn.getDst(), lb)
1657                        + "=allocate_newglobal(trans, "
1658                        + fn.getType().getClassDesc().getId() + ");");
1659       } else if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1660         output.println(super.generateTemp(fm, fn.getDst(), lb)
1661                        + "=allocate_new(&" + localsprefix + ", "
1662                        + fn.getType().getClassDesc().getId() + ");");
1663       } else {
1664         output.println(super.generateTemp(fm, fn.getDst(), lb)
1665                        + "=allocate_new("
1666                        + fn.getType().getClassDesc().getId() + ");");
1667       }
1668     }
1669     if (state.DSM && locality.getAtomic(lb).get(fn).intValue() > 0
1670         && !fn.isGlobal()) {
1671       String revertptr = super.generateTemp(fm, reverttable.get(lb), lb);
1672       output.println("trans->revertlist=" + revertptr + ";");
1673     }
1674     // create alias lock if necessary
1675     if((this.m_aliaslocksTbl4FN != null) && (this.m_aliaslocksTbl4FN.containsKey(fn))) {
1676       Vector<Integer> tmpv = this.m_aliaslocksTbl4FN.get(fn);
1677       for(int i = 0; i < tmpv.size(); i++) {
1678         output.println("addAliasLock(" + super.generateTemp(fm, fn.getDst(), lb) + ", aliaslocks[" + tmpv.elementAt(i).intValue() + "]);");
1679       }
1680     }
1681     // generate codes for profiling, recording how many new objects are created
1682     if(!fn.getType().isArray() && 
1683             (fn.getType().getClassDesc() != null) 
1684             && (fn.getType().getClassDesc().hasFlags())) {
1685         output.println("#ifdef PROFILE");
1686         output.println("addNewObjInfo(\"" + fn.getType().getClassDesc().getSymbol() + "\");");
1687         output.println("#endif");
1688     }
1689   }
1690
1691   class TranObjInfo {
1692     public String name;
1693     public int targetcore;
1694     public FlagState fs;
1695   }
1696
1697   private boolean contains(Vector<TranObjInfo> sendto, 
1698                            TranObjInfo t) {
1699     if(sendto.size() == 0) {
1700       return false;
1701     }
1702     for(int i = 0; i < sendto.size(); i++) {
1703       TranObjInfo tmp = sendto.elementAt(i);
1704       if(!tmp.name.equals(t.name)) {
1705         return false;
1706       }
1707       if(tmp.targetcore != t.targetcore) {
1708         return false;
1709       }
1710       if(tmp.fs != t.fs) {
1711         return false;
1712       }
1713     }
1714     return true;
1715   }
1716 }