added dsmdebug file for debugging using macros and setting flags in buildscript
[IRC.git] / Robust / src / IR / Flat / BuildCode.java
index 352a648d76d29073dc4e9d25c821f155edd4ca03..8781ff4e3ca832db40545acb02f3d86ce2a20c59 100644 (file)
@@ -6,6 +6,7 @@ import IR.Tree.TagExpressionList;
 import IR.*;
 import java.util.*;
 import java.io.*;
+
 import Util.Relation;
 import Analysis.TaskStateAnalysis.FlagState;
 import Analysis.TaskStateAnalysis.FlagComparator;
@@ -33,11 +34,10 @@ public class BuildCode {
     public static boolean GENERATEPRECISEGC=false;
     public static String PREFIX="";
     public static String arraytype="ArrayObject";
-    public static int count = 0;
     public static int flagcount = 0;
     Virtual virtualcalls;
     TypeUtil typeutil;
-    private int maxtaskparams=0;
+    protected int maxtaskparams=0;
     private int maxcount=0;
     ClassDescriptor[] cdarray;
     TypeDescriptor[] arraytable;
@@ -45,17 +45,19 @@ public class BuildCode {
     Hashtable<TempDescriptor, TempDescriptor> backuptable;
     Hashtable<LocalityBinding, TempDescriptor> reverttable;
     SafetyAnalysis sa;
+    PrefetchAnalysis pa;
 
-    public BuildCode(State st, Hashtable temptovar, TypeUtil typeutil, SafetyAnalysis sa) {
-       this(st, temptovar, typeutil, null, sa);
+    public BuildCode(State st, Hashtable temptovar, TypeUtil typeutil, SafetyAnalysis sa, PrefetchAnalysis pa) {
+       this(st, temptovar, typeutil, null, sa, pa);
     }
 
-    public BuildCode(State st, Hashtable temptovar, TypeUtil typeutil, LocalityAnalysis locality) {
-       this(st, temptovar, typeutil, locality, null);
+    public BuildCode(State st, Hashtable temptovar, TypeUtil typeutil, LocalityAnalysis locality, PrefetchAnalysis pa) {
+       this(st, temptovar, typeutil, locality, null, pa);
     }
 
-    public BuildCode(State st, Hashtable temptovar, TypeUtil typeutil, LocalityAnalysis locality, SafetyAnalysis sa) {
+    public BuildCode(State st, Hashtable temptovar, TypeUtil typeutil, LocalityAnalysis locality, SafetyAnalysis sa, PrefetchAnalysis pa) {
        this.sa=sa;
+       this.pa=pa;
        state=st;
        this.temptovar=temptovar;
        paramstable=new Hashtable();
@@ -192,6 +194,9 @@ public class BuildCode {
     private void outputMainMethod(PrintWriter outmethod) {
        outmethod.println("int main(int argc, const char *argv[]) {");
        outmethod.println("  int i;");
+    outmethod.println("#ifdef TRANSSTATS \n");
+    outmethod.println("handle();\n");
+    outmethod.println("#endif\n");
        if (state.THREAD||state.DSM) {
            outmethod.println("initializethreads();");
        }
@@ -261,8 +266,17 @@ public class BuildCode {
                outmethod.println("pthread_exit(NULL);");
        }
 
-
+    outmethod.println("#ifdef TRANSSTATS \n");
+    outmethod.println("printf(\"******  Transaction Stats   ******\\n\");");
+    outmethod.println("printf(\"numTransAbort= %d\\n\", numTransAbort);");
+    outmethod.println("printf(\"numTransCommit= %d\\n\", numTransCommit);");
+    outmethod.println("printf(\"nchashSearch= %d\\n\", nchashSearch);");
+    outmethod.println("printf(\"nmhashSearch= %d\\n\", nmhashSearch);");
+    outmethod.println("printf(\"nprehashSearch= %d\\n\", nprehashSearch);");
+    outmethod.println("printf(\"nRemoteReadSend= %d\\n\", nRemoteSend);");
+    outmethod.println("#endif\n");
        outmethod.println("}");
+
     }
 
     /* This method outputs code for each task. */
@@ -301,13 +315,17 @@ public class BuildCode {
      * objets and array that stores supertype and then the code for
      * the Java methods.. */
 
-    private void outputMethods(PrintWriter outmethod) {
+    protected void outputMethods(PrintWriter outmethod) {
        outmethod.println("#include \"methodheaders.h\"");
        outmethod.println("#include \"virtualtable.h\"");
-       outmethod.println("#include <runtime.h>");
+       outmethod.println("#include \"runtime.h\"");
        if (state.DSM) {
+           outmethod.println("#include \"addPrefetchEnhance.h\"");
            outmethod.println("#include \"localobjects.h\"");
        }
+       if(state.MULTICORE) {
+           outmethod.println("#include \"task.h\"");
+       }
        if (state.THREAD||state.DSM)
            outmethod.println("#include <thread.h>");
        if (state.main!=null) {
@@ -352,7 +370,7 @@ public class BuildCode {
        } 
     }
 
-    private void outputStructs(PrintWriter outstructs) {
+    protected void outputStructs(PrintWriter outstructs) {
        outstructs.println("#ifndef STRUCTDEFS_H");
        outstructs.println("#define STRUCTDEFS_H");
        outstructs.println("#include \"classdefs.h\"");
@@ -394,7 +412,7 @@ public class BuildCode {
        }
     }
 
-    private void outputClassDeclarations(PrintWriter outclassdefs) {
+    protected void outputClassDeclarations(PrintWriter outclassdefs) {
        if (state.THREAD||state.DSM)
            outclassdefs.println("#include <pthread.h>");
        if(state.OPTIONAL)
@@ -417,7 +435,13 @@ public class BuildCode {
        }
        if (state.TASK) {
            outclassdefs.println("  int flag;");
-           outclassdefs.println("  void * flagptr;");
+           if(!state.MULTICORE) {
+               outclassdefs.println("  void * flagptr;");
+           } else {
+               outclassdefs.println("  int isolate;"); // indicate if this object is shared or not
+               outclassdefs.println("  int version;");
+               outclassdefs.println("  struct ___Object___ * original;");
+           }
            if(state.OPTIONAL){
                outclassdefs.println("  int numfses;");
                outclassdefs.println("  int * fses;");
@@ -450,7 +474,7 @@ public class BuildCode {
        outtask.println("struct taskdescriptor {");
        outtask.println("void * taskptr;");
        outtask.println("int numParameters;");
-       outtask.println("int numTotal;");
+       outtask.println("  int numTotal;");
        outtask.println("struct parameterdescriptor **descriptorarray;");
        outtask.println("char * name;");
        outtask.println("};");
@@ -468,7 +492,9 @@ public class BuildCode {
            outrepairstructs.println("  int __type__;");
            if (state.TASK) {
                outrepairstructs.println("  int __flag__;");
-               outrepairstructs.println("  int __flagptr__;");
+               if(!state.MULTICORE) {
+                   outrepairstructs.println("  int __flagptr__;");
+               }
            }
            printRepairStruct(cn, outrepairstructs);
            outrepairstructs.println("}\n");
@@ -513,7 +539,7 @@ public class BuildCode {
     }
 
     /** This method outputs TaskDescriptor information */
-    void generateTaskDescriptor(PrintWriter output, FlatMethod fm, TaskDescriptor task) {
+    private void generateTaskDescriptor(PrintWriter output, FlatMethod fm, TaskDescriptor task) {
        for (int i=0;i<task.numParameters();i++) {
            VarDescriptor param_var=task.getParameter(i);
            TypeDescriptor param_type=task.getParamType(i);
@@ -604,8 +630,8 @@ public class BuildCode {
 
     /** The buildVirtualTables method outputs the virtual dispatch
      * tables for methods. */
-
-    private void buildVirtualTables(PrintWriter outvirtual) {
+    
+    protected void buildVirtualTables(PrintWriter outvirtual) {
        Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
        while(classit.hasNext()) {
            ClassDescriptor cd=(ClassDescriptor)classit.next();
@@ -701,7 +727,19 @@ public class BuildCode {
      * information. */
 
     private void generateSizeArray(PrintWriter outclassdefs) {
-       outclassdefs.print("int classsize[]={");
+      outclassdefs.print("extern struct prefetchCountStats * evalPrefetch;\n");
+      outclassdefs.print("#ifdef TRANSSTATS \n");
+      outclassdefs.print("extern int numTransAbort;\n");
+      outclassdefs.print("extern int numTransCommit;\n");
+      outclassdefs.print("extern int nchashSearch;\n");
+      outclassdefs.print("extern int nmhashSearch;\n");
+      outclassdefs.print("extern int nprehashSearch;\n");
+      outclassdefs.print("extern int nRemoteSend;\n");
+      outclassdefs.print("extern void handle();\n");
+      outclassdefs.print("#endif\n");
+      outclassdefs.print("int numprefetchsites = " + pa.prefetchsiteid + ";\n");
+
+      outclassdefs.print("int classsize[]={");
        Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
        cdarray=new ClassDescriptor[state.numClasses()];
        while(it.hasNext()) {
@@ -743,7 +781,7 @@ public class BuildCode {
      * These objects tell the compiler which temps need to be
      * allocated.  */
 
-    private void generateTempStructs(FlatMethod fm, LocalityBinding lb) {
+    protected void generateTempStructs(FlatMethod fm, LocalityBinding lb) {
        MethodDescriptor md=fm.getMethod();
        TaskDescriptor task=fm.getTask();
        Set<TempDescriptor> saveset=lb!=null?locality.getTempSet(lb):null;
@@ -924,14 +962,14 @@ public class BuildCode {
        if (!fieldorder.containsKey(cn)) {
            Vector fields=new Vector();
            fieldorder.put(cn,fields);
-           if (sp==null) {
+           if (sp==null&&!state.TASK) {
                fields.add(cn.getFieldTable().get("cachedCode"));
            }
            Iterator fieldit=cn.getFields();
            while(fieldit.hasNext()) {
                FieldDescriptor fd=(FieldDescriptor)fieldit.next();
                if ((sp==null||!sp.getFieldTable().contains(fd.getSymbol()))&&
-                   (!fd.getSymbol().equals("cachedCode")))
+                   (!fd.getSymbol().equals("cachedCode")||state.TASK))
                    fields.add(fd);
            }
        }
@@ -950,7 +988,7 @@ public class BuildCode {
     /* Map flags to integers consistently between inherited
      * classes. */
 
-    private void mapFlags(ClassDescriptor cn) {
+    protected void mapFlags(ClassDescriptor cn) {
        ClassDescriptor sp=cn.getSuperDesc();
        if (sp!=null)
            mapFlags(sp);
@@ -984,7 +1022,7 @@ public class BuildCode {
      * passed in (when PRECISE GC is enabled) and (2) function
      * prototypes for the methods */
 
-    private void generateCallStructs(ClassDescriptor cn, PrintWriter classdefout, PrintWriter output, PrintWriter headersout) {
+    protected void generateCallStructs(ClassDescriptor cn, PrintWriter classdefout, PrintWriter output, PrintWriter headersout) {
        /* Output class structure */
        classdefout.println("struct "+cn.getSafeSymbol()+" {");
        classdefout.println("  int type;");
@@ -996,7 +1034,13 @@ public class BuildCode {
 
        if (state.TASK) {
            classdefout.println("  int flag;");
-           classdefout.println("  void * flagptr;");
+           if((!state.MULTICORE) || (cn.getSymbol().equals("TagDescriptor"))) {
+               classdefout.println("  void * flagptr;");
+           } else if (state.MULTICORE){
+               classdefout.println("  int isolate;"); // indicate if this object is shared or not
+               classdefout.println("  int version;");
+               classdefout.println("  struct ___Object___ * original;");
+           }
            if (state.OPTIONAL){
                classdefout.println("  int numfses;");
                classdefout.println("  int * fses;");
@@ -1115,7 +1159,7 @@ public class BuildCode {
            printcomma=true;
        }
        
-       if (state.DSM&&lb.isAtomic()) {
+       if (state.DSM&&lb.isAtomic()&&!md.getModifiers().isNative()) {
            if (printcomma)
                headersout.print(", ");
            headersout.print("transrecord_t * trans");
@@ -1313,7 +1357,7 @@ public class BuildCode {
 
     /** This method assigns labels to FlatNodes */
 
-    private Hashtable<FlatNode, Integer> assignLabels(FlatMethod fm) {
+    protected Hashtable<FlatNode, Integer> assignLabels(FlatMethod fm) {
        HashSet tovisit=new HashSet();
        HashSet visited=new HashSet();
        int labelindex=0;
@@ -1346,7 +1390,7 @@ public class BuildCode {
 
 
     /** Generate text string that corresponds to the TempDescriptor td. */
-    private String generateTemp(FlatMethod fm, TempDescriptor td, LocalityBinding lb) {
+    protected String generateTemp(FlatMethod fm, TempDescriptor td, LocalityBinding lb) {
        MethodDescriptor md=fm.getMethod();
        TaskDescriptor task=fm.getTask();
        TempObject objecttemps=(TempObject) tempstable.get(lb!=null?lb:md!=null?md:task);
@@ -1365,7 +1409,7 @@ public class BuildCode {
        throw new Error();
     }
 
-    private void generateFlatNode(FlatMethod fm, LocalityBinding lb, FlatNode fn, PrintWriter output) {
+    protected void generateFlatNode(FlatMethod fm, LocalityBinding lb, FlatNode fn, PrintWriter output) {
        switch(fn.kind()) {
        case FKind.FlatAtomicEnterNode:
            generateFlatAtomicEnterNode(fm, lb, (FlatAtomicEnterNode) fn, output);
@@ -1428,320 +1472,178 @@ public class BuildCode {
            generateFlatFlagActionNode(fm, lb, (FlatFlagActionNode) fn, output);
            return;
        case FKind.FlatPrefetchNode:
-           if (!state.excprefetch.contains(fm.getMethod().getClassMethodName()))
-               generateFlatPrefetchNode(fm,lb, (FlatPrefetchNode) fn, output);
+           generateFlatPrefetchNode(fm,lb, (FlatPrefetchNode) fn, output);
            return;
        }
        throw new Error();
     }
  
     public void generateFlatPrefetchNode(FlatMethod fm, LocalityBinding lb, FlatPrefetchNode fpn, PrintWriter output) {
-           short[] arrayfields = null;
-           Vector fieldoffset = new Vector();
+       if (state.PREFETCH) {
+           Vector oids = new Vector();
+           Vector fieldoffset = new Vector();
            Vector endoffset = new Vector();
-           Vector oids = new Vector();
-           short offsetcount = 0;
-           int tuplecount = 0;  //Keeps track of number of prefetch tuples that need to be generated
-           int i,j;
-          
-           if (state.PREFETCH) {
-                   output.println("/* prefetch */");
-                   output.println("; /* empty statement to avoid compiler error */");
-                   Iterator it = fpn.hspp.iterator();
-                   String oidlist = new String();
-                   while(it.hasNext()) {
-                           PrefetchPair pp = (PrefetchPair) it.next();
-                           Integer statusbase = locality.getNodePreTempInfo(lb,fpn).get(pp.base);
-                           /* Find prefetches that can generate oid */
-                           if(statusbase == LocalityAnalysis.GLOBAL) {
-                                   if(locality.getAtomic(lb).get(fpn).intValue()>0) { /* Inside transaction */
-                                           generateInsideTransCode(fm,lb,output, pp,oids,fieldoffset,endoffset,tuplecount);
-                                   } else {/* Outside Transaction */
-                                           generateOutsideTransCode(fm,lb,pp,oids,fieldoffset,endoffset,tuplecount);
-                                   }
-                                   tuplecount++;
-                           } else if(statusbase == LocalityAnalysis.LOCAL) {
-                                   generateLocalTransCode(fm,lb,pp,oids,fieldoffset,endoffset,tuplecount);
-                           } else {
-                                   continue;
-                           }
-                   }
-
-                   /*Create C code for numtuples */
-                   output.println("   int numtuples_" + count + " = " + tuplecount + ";");
-                   /*Create C code for oid array */
-                   output.print("   unsigned int oidarray_" + count + "[] = {");
-                   boolean needcomma=false;
-                   it = oids.iterator();
-                   while(it.hasNext()) {
-                           if (needcomma)
-                                   output.print(", ");
-                           output.print(it.next());
-                           needcomma=true;
-                   }
-                   output.println("};");
-                   /*Create C code for endoffset values */
-                   output.print("   unsigned short endoffsetarry_" + count +"[] = {");
-                   needcomma=false;
-                   it = endoffset.iterator();
-                   while(it.hasNext()) {
-                           if (needcomma)
-                                   output.print(", ");
-                           output.print(it.next());
-                           needcomma=true;
-                   }
-                   output.println("};");
-                   /*Create C code for Field Offset Values */
-                   output.print("   short fieldarry_" + count +"[] = {");
-                   needcomma=false;
-                   it = fieldoffset.iterator();
-                   while(it.hasNext()) {
-                           if (needcomma)
-                                   output.print(", ");
-                           output.print(it.next());
-                           needcomma=true;
-                   }
-                   output.println("};");
-                   /* make the prefetch call to Runtime */
-                   if(tuplecount > 0) {
-                           output.println("   prefetch((int) numtuples_"+count+ ", oidarray_"+count+ ", endoffsetarry_"+
-                                           count+", fieldarry_"+count+");");
+           int tuplecount = 0;  //Keeps track of number of prefetch tuples that need to be generated
+           for(Iterator it = fpn.hspp.iterator();it.hasNext();) {
+               PrefetchPair pp = (PrefetchPair) it.next();
+               Integer statusbase = locality.getNodePreTempInfo(lb,fpn).get(pp.base);
+               /* Find prefetches that can generate oid */
+               if(statusbase == LocalityAnalysis.GLOBAL) {
+                   generateTransCode(fm, lb, pp, oids, fieldoffset, endoffset, tuplecount, locality.getAtomic(lb).get(fpn).intValue()>0, false);
+                   tuplecount++;
+               } else if (statusbase == LocalityAnalysis.LOCAL) {
+                   generateTransCode(fm,lb,pp,oids,fieldoffset,endoffset,tuplecount,false,true);
+               } else {
+                   continue;
+               }
+           }
+           if (tuplecount==0)
+               return;
+           output.println("{");
+           output.println("/* prefetch */");
+        output.println("/* prefetchid_" + fpn.siteid + " */");
+           output.println("void * prefptr;");
+           output.println("int tmpindex;");
+
+        output.println("if((evalPrefetch["+fpn.siteid+"].operMode) || (evalPrefetch["+fpn.siteid+"].retrycount <= 0)) {");
+           /*Create C code for oid array */
+           output.print("   unsigned int oidarray_[] = {");
+           boolean needcomma=false;
+           for (Iterator it = oids.iterator();it.hasNext();) {
+               if (needcomma)
+                   output.print(", ");
+               output.print(it.next());
+               needcomma=true;
+           }
+           output.println("};");
+           
+           /*Create C code for endoffset values */
+           output.print("   unsigned short endoffsetarry_[] = {");
+           needcomma=false;
+           for (Iterator it = endoffset.iterator();it.hasNext();) {
+               if (needcomma)
+                   output.print(", ");
+               output.print(it.next());
+               needcomma=true;
+           }
+           output.println("};");
+           
+           /*Create C code for Field Offset Values */
+           output.print("   short fieldarry_[] = {");
+           needcomma=false;
+           for (Iterator it = fieldoffset.iterator();it.hasNext();) {
+               if (needcomma)
+                   output.print(", ");
+               output.print(it.next());
+               needcomma=true;
+           }
+           output.println("};");
+           /* make the prefetch call to Runtime */
+        output.println("   if(!evalPrefetch["+fpn.siteid+"].operMode) {"); 
+           output.println("     evalPrefetch["+fpn.siteid+"].retrycount = RETRYINTERVAL;");
+           output.println("   }");
+           output.println("   prefetch("+fpn.siteid+" ,"+tuplecount+", oidarray_, endoffsetarry_, fieldarry_);");
+           output.println(" } else {");
+           output.println("   evalPrefetch["+fpn.siteid+"].retrycount--;");
+           output.println(" }");
+           output.println("}");
+       }   
+    }   
+    
+    public void generateTransCode(FlatMethod fm, LocalityBinding lb,PrefetchPair pp, Vector oids, Vector fieldoffset, Vector endoffset, int tuplecount, boolean inside, boolean localbase) {
+       short offsetcount = 0;
+       int breakindex=0;
+       if (inside) {
+           breakindex=1;
+       } else if (localbase) {
+           for(;breakindex<pp.desc.size();breakindex++) {
+               Descriptor desc=pp.getDescAt(breakindex);
+               if (desc instanceof FieldDescriptor) {
+                   FieldDescriptor fd=(FieldDescriptor)desc;
+                   if (fd.isGlobal()) {
+                       break;
                    }
-                   count++;
-           }   
+               }
+           }
+           breakindex++;
+       }
 
-           /* Free heap memory */
-           fieldoffset = null;
-           endoffset = null;
-           oids = null;
-    }   
+       if (breakindex>pp.desc.size()) //all local
+           return;
 
-    public void generateInsideTransCode(FlatMethod fm, LocalityBinding lb,PrintWriter output,PrefetchPair pp,Vector oids, Vector fieldoffset,Vector endoffset, int tuplecount){
-           int i,j;
-           short offsetcount = 0;
-           String test = new String();
-
-           Object newdesc = pp.desc.get(0);
-           if(newdesc instanceof FieldDescriptor) {
-                   FieldDescriptor fd = (FieldDescriptor)newdesc;
-                   String oid = new String("(unsigned int) (" + 
-                                   generateTemp(fm, pp.base, lb) + " != NULL ? " + 
-                                   generateTemp(fm, pp.base, lb) + "->" + ((FieldDescriptor)fd).getSafeSymbol() +
-                                   " : NULL)");
-                   oids.add(oid);
+       TypeDescriptor lasttype=pp.base.getType();
+       String basestr=generateTemp(fm, pp.base, lb);
+       String teststr="";
+       boolean maybenull=fm.getMethod().isStatic()||
+           !pp.base.equals(fm.getParameter(0));
+                           
+       for(int i=0;i<breakindex;i++) {
+           String indexcheck="";
+
+           Descriptor desc=pp.getDescAt(i);
+           if (desc instanceof FieldDescriptor) {
+               FieldDescriptor fd=(FieldDescriptor)desc;
+               if (maybenull) {
+                   if (!teststr.equals(""))
+                       teststr+="&&";
+                   teststr+="((prefptr="+basestr+")!=NULL)";
+                   basestr="((struct "+lasttype.getSafeSymbol()+" *)prefptr)->"+fd.getSafeSymbol();
+               } else {
+                   basestr=basestr+"->"+fd.getSafeSymbol();
+                   maybenull=true;
+               }
+               lasttype=fd.getType();
            } else {
-                   IndexDescriptor id = (IndexDescriptor)newdesc;
-                   String tstlbl = new String();
-                   for(i=0; i<id.tddesc.size(); i++) {
-                           tstlbl += generateTemp(fm, id.getTempDescAt(i), lb) + "+";
-                   }
-                   tstlbl += id.offset.toString();
-
-                   TypeDescriptor elementtype = pp.base.getType().dereference();
-                   String type="";
-                   if (elementtype.isArray()||elementtype.isClass())
-                           type="void *";
-                   else 
-                           type=elementtype.getSafeSymbol()+" ";
-
-                   String oid = new String("(unsigned int) (" + generateTemp(fm, pp.base, lb) + " != NULL ? " + 
-                                   "((" + tstlbl+"< 0 || "+tstlbl+" >= "+ generateTemp(fm, pp.base, lb) + "->___length___) ? 0 :"+
-                                   "((" + type + "*)(((char *) &("+ generateTemp(fm, pp.base, lb)+ "->___length___))+sizeof(int)))["+tstlbl+"]) : 0)");
-                   oids.add(oid);
-           }
-
-           for(i = 1; i < pp.desc.size(); i++) {
-                   TypeDescriptor newtd;
-                   ClassDescriptor cd;
-                   String newfieldoffset;
-                   Object desc = pp.getDescAt(i);
-                   offsetcount++;
-                   if(desc instanceof FieldDescriptor) {
-                           Object prevdesc = pp.getDescAt(i-1);
-                           if(prevdesc instanceof IndexDescriptor){
-                                   if((i-1) == 0) {
-                                           newtd = pp.base.getType(); 
-                                           cd = newtd.getClassDesc();
-                                   } else {
-                                           //FIXME currently handles one dimensional arrays
-                                           newtd = ((FieldDescriptor)pp.getDescAt(i-2)).getType();
-                                           cd = newtd.getClassDesc();
-                                   }
-                                   newfieldoffset = new String("(unsigned int)(&(((struct "+ cd.getSafeSymbol() +" *)0)->"+ 
-                                                   ((FieldDescriptor)desc).getSafeSymbol()+ "))");
-                           } else {
-                                   newtd = ((FieldDescriptor)pp.getDescAt(i-1)).getType();
-                                   newfieldoffset = new String("(unsigned int)(&(((struct "+ newtd.getSafeSymbol()+" *)0)->"+ 
-                                           ((FieldDescriptor)desc).getSafeSymbol()+ "))");
-                           }
-                           fieldoffset.add(newfieldoffset);
-                   } else {
-                           String tstlbl = new String();
-                           for(j = 0; j < ((IndexDescriptor)desc).tddesc.size(); j++) {
-                                   tstlbl += generateTemp(fm, ((IndexDescriptor)desc).getTempDescAt(j), lb) + "+";
-                           }
-                           tstlbl += ((IndexDescriptor)desc).offset.toString();
-                           newfieldoffset = new String(tstlbl);
-                           fieldoffset.add(newfieldoffset);
-                   }
-           }
-           if(tuplecount > 0) {
-                   int tmp = (int) ((Short)(endoffset.get(tuplecount-1))).shortValue() + (int) offsetcount;
-                   short endoffsetval = (short) tmp;
-                   endoffset.add(endoffsetval);
-           }else {
-                   endoffset.add(offsetcount);
-           }
-           flagcount++;
-    }
+               IndexDescriptor id=(IndexDescriptor)desc;
+               indexcheck="((tmpindex=";
+               for(int j=0;j<id.tddesc.size();j++) {
+                   indexcheck+=generateTemp(fm, id.getTempDescAt(j), lb)+"+";
+               }
+               indexcheck+=id.offset+")>=0)&&(tmpindex<((struct ArrayObject *)prefptr)->___length___)";
 
-    public void generateOutsideTransCode(FlatMethod fm, LocalityBinding lb,PrefetchPair pp, Vector oids, Vector fieldoffset, Vector endoffset, int tuplecount) {
-           int i,j;
-           short offsetcount = 0;
-
-           String oid = new String(" (unsigned int) (" + generateTemp(fm, pp.base, lb)+ ")");
-           oids.add(oid);
-           for(i = 0; i < pp.desc.size(); i++) {
-                   TypeDescriptor newtd;
-                   ClassDescriptor cd;
-                   String newfieldoffset;
-                   Object desc = pp.getDescAt(i);
-                   offsetcount++;
-                   if(desc instanceof FieldDescriptor) {
-                           if(i == 0){
-                                   newtd = pp.base.getType(); 
-                                   newfieldoffset = new String("(unsigned int)(&(((struct "+ newtd.getSafeSymbol()+" *)0)->"+ 
-                                                   ((FieldDescriptor)desc).getSafeSymbol()+ "))");
-                           } else {
-                                   Object prevdesc = pp.getDescAt(i-1);
-                                   if(prevdesc instanceof IndexDescriptor){
-                                           if((i-1) == 0) {
-                                                   newtd = pp.base.getType(); 
-                                                   cd = newtd.getClassDesc();
-                                           } else {
-                                                   //FIXME currently handles one dimensional arrays
-                                                   newtd = ((FieldDescriptor)pp.getDescAt(i-2)).getType();
-                                                   cd = newtd.getClassDesc();
-                                           }
-                                           newfieldoffset = new String("(unsigned int)(&(((struct "+ cd.getSafeSymbol() +" *)0)->"+ 
-                                                           ((FieldDescriptor)desc).getSafeSymbol()+ "))");
-                                   } else {
-                                           newtd = ((FieldDescriptor)pp.getDescAt(i-1)).getType();
-                                           newfieldoffset = new String("(unsigned int)(&(((struct "+ newtd.getSafeSymbol()+" *)0)->"+ 
-                                                           ((FieldDescriptor)desc).getSafeSymbol()+ "))");
-                                   }
-                           }
-                   } else {
-                           String tstlbl = new String();
-                           for(j = 0; j < ((IndexDescriptor)desc).tddesc.size(); j++) {
-                                   tstlbl += generateTemp(fm, ((IndexDescriptor)desc).getTempDescAt(j), lb) + "+";
-                           }
-                           tstlbl += ((IndexDescriptor)desc).offset.toString();
-                           newfieldoffset = new String(tstlbl);
-                   }
-                   fieldoffset.add(newfieldoffset);
+               if (!teststr.equals(""))
+                   teststr+="&&";
+               teststr+="((prefptr="+basestr+")!= NULL) &&"+indexcheck;
+               basestr="((void **)(((char *) &(((struct ArrayObject *)prefptr)->___length___))+sizeof(int)))[tmpindex]";
+               maybenull=true;
+               lasttype=lasttype.dereference();
            }
-           if(tuplecount > 0) {
-                   int tmp = (int) ((Short)(endoffset.get(tuplecount-1))).shortValue() + (int) offsetcount;
-                   short endoffsetval = (short) tmp;
-                   endoffset.add(endoffsetval);
-           }else {
-                   endoffset.add(offsetcount);
+       }
+       
+       String oid;
+       if (teststr.equals("")) {
+           oid="((unsigned int)"+basestr+")";
+       } else {
+           oid="((unsigned int)(("+teststr+")?"+basestr+":NULL))";
+       }
+       oids.add(oid);
+       
+       for(int i = breakindex; i < pp.desc.size(); i++) {
+           String newfieldoffset;
+           Object desc = pp.getDescAt(i);
+           if(desc instanceof FieldDescriptor) {
+               FieldDescriptor fd=(FieldDescriptor)desc;
+               newfieldoffset = new String("(unsigned int)(&(((struct "+ lasttype.getSafeSymbol()+" *)0)->"+ fd.getSafeSymbol()+ "))");
+               lasttype=fd.getType();
+           } else {
+               newfieldoffset = "";
+               IndexDescriptor id=(IndexDescriptor)desc;
+               for(int j = 0; j < id.tddesc.size(); j++) {
+                   newfieldoffset += generateTemp(fm, id.getTempDescAt(j), lb) + "+";
+               }
+               newfieldoffset += id.offset.toString();
+               lasttype=lasttype.dereference();
            }
-    }
+           fieldoffset.add(newfieldoffset);
+       }
 
-    public void generateLocalTransCode(FlatMethod fm, LocalityBinding lb,PrefetchPair pp,Vector oids, Vector fieldoffset,Vector endoffset, int tuplecount) {
-           int i, j, k;
-           short offsetcount = 0;
-
-           Vector prefix = new Vector();
-           prefix.add(generateTemp(fm,pp.base,lb));
-           String tstlbl = new String("(" + prefix.get(0) + " != NULL ");
-           for (i = 0; i < pp.desc.size(); i++) {
-                   Object newdesc = pp.desc.get(i);
-                   if(newdesc instanceof FieldDescriptor) {
-                           FieldDescriptor fd = (FieldDescriptor) newdesc;
-                           if(fd.isGlobal()){
-                                   /* Field descriptor is global */
-                                   String oid = new String(" (unsigned int) (");
-                                   tstlbl += ") ? ";
-                                   for(j = 0; j < prefix.size(); j++) {
-                                           tstlbl += prefix.get(j) + "->";
-                                   }
-                                   tstlbl += fd.getSafeSymbol() + ": NULL";
-                                   oid += tstlbl+ " )";
-                                   oids.add(oid);
-                                   for(j = i+1; j < pp.desc.size(); j++) {
-                                           TypeDescriptor newtd;
-                                           String newfieldoffset;
-                                           Object desc = pp.getDescAt(j);
-                                           offsetcount++;
-                                           if(desc instanceof FieldDescriptor) {
-                                                   Object prevdesc = pp.getDescAt(j-1);
-                                                   if(prevdesc instanceof IndexDescriptor){
-                                                           //FIXME currently handles one dimensional arrays
-                                                           newtd = ((FieldDescriptor)pp.getDescAt(j-2)).getType();
-                                                   } else {
-                                                           newtd = ((FieldDescriptor) pp.getDescAt(j-1)).getType();
-                                                   }
-                                                   newfieldoffset = new String("(unsigned int)(&(((struct "+ newtd.getSafeSymbol()+" *)0)->"+ 
-                                                                   ((FieldDescriptor)desc).getSafeSymbol()+ "))");
-                                           } else {
-                                                   String indexlbl = new String();
-                                                   for(k = 0; k < ((IndexDescriptor)desc).tddesc.size(); k++) {
-                                                           indexlbl += generateTemp(fm, ((IndexDescriptor)desc).getTempDescAt(k), lb) + "+";
-                                                   }
-                                                   indexlbl += ((IndexDescriptor)desc).offset.toString();
-                                                   newfieldoffset = new String(indexlbl);
-                                           }
-                                           fieldoffset.add(newfieldoffset);
-                                   }
-                                   if(tuplecount > 0) {
-                                           int tmp = (int) ((Short)(endoffset.get(tuplecount-1))).shortValue() + (int) offsetcount;
-                                           short endoffsetval = (short) tmp;
-                                           endoffset.add(endoffsetval);
-                                   }else {
-                                           endoffset.add(offsetcount);
-                                   }
-                                   tuplecount++;
-                                   break; //break from outer for loop
-                           } else {
-                                   tstlbl += "&& ";
-                                   for(j = 0; j < prefix.size(); j++) {
-                                           tstlbl += prefix.get(j) + "->";
-                                   }
-                                   prefix.add(fd.getSafeSymbol());
-                                   tstlbl += fd.getSafeSymbol() + " != NULL";
-                           }
-                   } else { /* if Index descriptor */
-                           String indexstring = new String();
-                           IndexDescriptor id = (IndexDescriptor) newdesc;
-                           if(i == 0) {
-                                   indexstring = generateTemp(fm, pp.base, lb);
-                           } else {
-                                   indexstring = ((FieldDescriptor)pp.getDescAt(i-1)).getSafeSymbol();
-                           }
-                           tstlbl += "&& ";
-                           for(j = 0; j < prefix.size(); j++) {
-                                   tstlbl += prefix.get(j) + "[";
-                           }
-                           indexstring += "[";
-                           for(j = 0; j < id.tddesc.size(); j++) {
-                                   tstlbl += generateTemp(fm, id.getTempDescAt(j), lb) + "+"; 
-                                   indexstring += generateTemp(fm,id.getTempDescAt(j), lb) + "+";
-                           }
-                           tstlbl += id.offset.toString()+ "]";
-                           indexstring += id.offset.toString()+ "]";
-                           prefix. removeElementAt(prefix.size() -1);
-                           prefix.add(indexstring);
-                           tstlbl += " != NULL";
-                   }
-           }
+       int base=(tuplecount>0)?((Short)endoffset.get(tuplecount-1)).intValue():0;
+       base+=pp.desc.size()-breakindex;
+       endoffset.add(new Short((short)base));
     }
 
+
+  
     public void generateFlatGlobalConvNode(FlatMethod fm, LocalityBinding lb, FlatGlobalConvNode fgcn, PrintWriter output) {
            if (lb!=fgcn.getLocality())
                    return;
@@ -1849,10 +1751,16 @@ public class BuildCode {
            
            output.print(objectparams.numPointers());
            output.print(", & "+localsprefix);
-           if (fc.getThis()!=null) {
+           if (md.getThis()!=null) {
                output.print(", ");
                output.print("(struct "+md.getThis().getType().getSafeSymbol() +" *)"+ generateTemp(fm,fc.getThis(),lb));
            }
+           if (fc.getThis()!=null&&md.getThis()==null) {
+               System.out.println("WARNING!!!!!!!!!!!!");
+               System.out.println("Source code calls static method"+md+"on an object in "+fm.getMethod()+"!");         
+           }
+           
+
            for(int i=0;i<fc.numArgs();i++) {
                Descriptor var=md.getParameter(i);
                TempDescriptor paramtemp=(TempDescriptor)temptovar.get(var);
@@ -1913,6 +1821,14 @@ public class BuildCode {
                    output.print(temp.getType().getSafeSymbol());
            }
 
+           if (state.DSM&&locality.getBinding(lb,fc).isAtomic()&&!fc.getMethod().getModifiers().isNative()) {
+               LocalityBinding fclb=locality.getBinding(lb, fc);
+               if (printcomma)
+                   output.print(", ");
+               output.print("transrecord_t *");
+               printcomma=true;
+           }
+
            if (state.DSM) {
                LocalityBinding fclb=locality.getBinding(lb, fc);
                output.print("))virtualtable["+generateTemp(fm,fc.getThis(),lb)+"->type*"+maxcount+"+"+virtualcalls.getLocalityNumber(fclb)+"])");
@@ -1927,7 +1843,7 @@ public class BuildCode {
            needcomma=true;
        }
 
-       if (state.DSM&&locality.getBinding(lb,fc).isAtomic()) {
+       if (state.DSM&&locality.getBinding(lb,fc).isAtomic()&&!fc.getMethod().getModifiers().isNative()) {
            if (needcomma)
                output.print(",");
            output.print("trans");
@@ -2016,7 +1932,9 @@ public class BuildCode {
            } else if (status==LocalityAnalysis.EITHER) {
                //Code is reading from a null pointer
                output.println("if ("+generateTemp(fm, ffn.getSrc(),lb)+") {");
+               output.println("#ifndef RAW");
                output.println("printf(\"BIG ERROR\\n\");exit(-1);}");
+               output.println("#endif");
                //This should throw a suitable null pointer error
                output.println(generateTemp(fm, ffn.getDst(),lb)+"="+ generateTemp(fm,ffn.getSrc(),lb)+"->"+ ffn.getField().getSafeSymbol()+";");
            } else
@@ -2068,7 +1986,9 @@ public class BuildCode {
            } else if (statusdst.equals(LocalityAnalysis.EITHER)) {
                //writing to a null...bad
                output.println("if ("+dst+") {");
+               output.println("#ifndef RAW");
                output.println("printf(\"BIG ERROR 2\\n\");exit(-1);}");
+               output.println("#endif");
                if (srcglobal)
                    output.println(dst+"->"+ fsfn.getField().getSafeSymbol()+"=srcoid;");
                else
@@ -2111,7 +2031,9 @@ public class BuildCode {
            } else if (status==LocalityAnalysis.EITHER) {
                //Code is reading from a null pointer
                output.println("if ("+generateTemp(fm, fen.getSrc(),lb)+") {");
+               output.println("#ifndef RAW");
                output.println("printf(\"BIG ERROR\\n\");exit(-1);}");
+               output.println("#endif");
                //This should throw a suitable null pointer error
                output.println(generateTemp(fm, fen.getDst(),lb)+"=(("+ type+"*)(((char *) &("+ generateTemp(fm,fen.getSrc(),lb)+"->___length___))+sizeof(int)))["+generateTemp(fm, fen.getIndex(),lb)+"];");
            } else
@@ -2283,17 +2205,18 @@ public class BuildCode {
            output.println(generateTemp(fm, fln.getDst(),lb)+"="+fln.getValue()+";");
     }
 
-    private void generateFlatReturnNode(FlatMethod fm, LocalityBinding lb, FlatReturnNode frn, PrintWriter output) {
+    protected void generateFlatReturnNode(FlatMethod fm, LocalityBinding lb, FlatReturnNode frn, PrintWriter output) {
        if (frn.getReturnTemp()!=null) {
            if (frn.getReturnTemp().getType().isPtr())
                output.println("return (struct "+fm.getMethod().getReturnType().getSafeSymbol()+"*)"+generateTemp(fm, frn.getReturnTemp(), lb)+";");
            else
                output.println("return "+generateTemp(fm, frn.getReturnTemp(), lb)+";");
-       } else
+       } else {
            output.println("return;");
+       }
     }
 
-    private void generateFlatCondBranch(FlatMethod fm, LocalityBinding lb, FlatCondBranch fcb, String label, PrintWriter output) {
+    protected void generateFlatCondBranch(FlatMethod fm, LocalityBinding lb, FlatCondBranch fcb, String label, PrintWriter output) {
        output.println("if (!"+generateTemp(fm, fcb.getTest(),lb)+") goto "+label+";");
     }
 
@@ -2483,14 +2406,24 @@ public class BuildCode {
                ormask=((Integer)flagortable.get(temp)).intValue();
            if (flagandtable.containsKey(temp))
                andmask=((Integer)flagandtable.get(temp)).intValue();
-           if (ffan.getTaskType()==FlatFlagActionNode.NEWOBJECT) {
-               output.println("flagorandinit("+generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+", 0x"+Integer.toHexString(andmask)+");");
-           } else {
-               output.println("flagorand("+generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+", 0x"+Integer.toHexString(andmask)+");");
-           }
+           generateFlagOrAnd(ffan, fm, lb, temp, output, ormask, andmask);
+           generateObjectDistribute(ffan, fm, lb, temp, output);
+       }
+    }
+    
+    protected void generateFlagOrAnd(FlatFlagActionNode ffan, FlatMethod fm, LocalityBinding lb, TempDescriptor temp, 
+                                    PrintWriter output, int ormask, int andmask) {
+       if (ffan.getTaskType()==FlatFlagActionNode.NEWOBJECT) {
+           output.println("flagorandinit("+generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+", 0x"+Integer.toHexString(andmask)+");");
+       } else {
+           output.println("flagorand("+generateTemp(fm, temp, lb)+", 0x"+Integer.toHexString(ormask)+", 0x"+Integer.toHexString(andmask)+");");
        }
     }
 
+    protected void generateObjectDistribute(FlatFlagActionNode ffan, FlatMethod fm, LocalityBinding lb, TempDescriptor temp, PrintWriter output) {
+       output.println("enqueueObject("+generateTemp(fm, temp, lb)+");");
+    }
+    
     void generateOptionalHeader(PrintWriter headers) {
 
         //GENERATE HEADERS
@@ -2891,6 +2824,9 @@ public class BuildCode {
        
        return l;
     }
+    
+    protected void outputTransCode(PrintWriter output) {
+    }
 }