Make SPECjbb compile again
[IRC.git] / Robust / src / IR / TypeUtil.java
index 72d8405105a3aab469b97530f4d4d5082c9bee7c..8170472080016cfa6f5cdcccaffb6399cfee04ef 100644 (file)
@@ -7,56 +7,86 @@ import java.io.File;
 import Main.Main;
 
 public class TypeUtil {
-  public static final String StringClass="String";
-  public static final String ObjectClass="Object";
-  public static final String StartupClass="StartupObject";
-  public static final String TagClass="TagDescriptor";
-  public static final String ThreadClass="Thread";
-  public static final String TaskClass="Task";
+  public static String StringClass;
+  public static String ObjectClass;
+  public static String StartupClass;
+  public static String TagClass;
+  public static String ThreadClass;
+  public static String TaskClass;
   State state;
   Hashtable supertable;
   Hashtable subclasstable;
   BuildIR bir;
-  
+
   // for interfaces
-  Hashtable superIFtbl;
+  Hashtable<ClassDescriptor, Set<ClassDescriptor>> superIFtbl;
 
   public TypeUtil(State state, BuildIR bir) {
     this.state=state;
     this.bir=bir;
+    if (state.JNI) {
+      StringClass="java.lang.String";
+      ObjectClass="java.lang.Object";
+      StartupClass="StartupObject";
+      TagClass="TagDescriptor";
+      ThreadClass="java.lang.Thread";
+      TaskClass="Task";
+    } else {
+      StringClass="String";
+      ObjectClass="Object";
+      StartupClass="StartupObject";
+      TagClass="TagDescriptor";
+      ThreadClass="Thread";
+      TaskClass="Task";
+    }
     createTables();
   }
 
   public void addNewClass(String cl, Set todo) {
-    for(int i=0;i<state.classpath.size();i++) {
-      String path=(String)state.classpath.get(i);
-      File f=new File(path, cl+".java");
+    //search through the default locations for the file.
+    if(state.MGC) {
+      // do not consider package or import when compiling MGC version
+      cl = (cl.lastIndexOf('.')==-1)?cl:cl.substring(cl.lastIndexOf('.')+1);
+    }
+    for (int i = 0; i < state.classpath.size(); i++) {
+      String path = (String) state.classpath.get(i);
+      File f = new File(path, cl.replace('.', '/') + ".java");
       if (f.exists()) {
-       try {
-         ParseNode pn=Main.readSourceFile(state, f.getCanonicalPath());
-         bir.buildtree(pn, todo);
-         return;
-       } catch (Exception e) {
-         throw new Error(e);
-       }
+        try {
+          ParseNode pn = Main.readSourceFile(state, f.getCanonicalPath());
+          bir.buildtree(pn, todo, f.getCanonicalPath());
+          return;
+        } catch (Exception e) {
+          throw new Error(e);
+        }
       }
     }
-    throw new Error("Couldn't find class "+cl);
+    throw new Error("Couldn't find class " + cl);
   }
 
 
 
   public ClassDescriptor getClass(String classname) {
-    ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
+    String cl = classname;
+    if(state.MGC) {
+      // do not consider package or import when compiling MGC version
+      cl = (cl.lastIndexOf('.')==-1)?cl:cl.substring(cl.lastIndexOf('.')+1);
+    }
+    ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(cl);
     return cd;
   }
 
   public ClassDescriptor getClass(String classname, HashSet todo) {
-    ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
+    String cl = classname;
+    if(state.MGC) {
+      // do not consider package or import when compiling MGC version
+      cl = (cl.lastIndexOf('.')==-1)?cl:cl.substring(cl.lastIndexOf('.')+1);
+    }
+    ClassDescriptor cd=(ClassDescriptor)state.getClassSymbolTable().get(cl);
     if (cd==null) {
       //have to find class
-      addNewClass(classname, todo);
-      cd=(ClassDescriptor)state.getClassSymbolTable().get(classname);
+      addNewClass(cl, todo);
+      cd=(ClassDescriptor)state.getClassSymbolTable().get(cl);
 
       System.out.println("Build class:"+cd);
       todo.add(cd);
@@ -64,11 +94,11 @@ public class TypeUtil {
     if (!supertable.containsKey(cd)) {
       String superc=cd.getSuper();
       if (superc!=null) {
-       ClassDescriptor cd_super=getClass(superc, todo);
-       supertable.put(cd,cd_super);
+        ClassDescriptor cd_super=getClass(superc, todo);
+        supertable.put(cd,cd_super);
       }
     }
-    if (!this.superIFtbl.containsKey(cd)) {
+    if (!superIFtbl.containsKey(cd)) {
       // add inherited interfaces
       superIFtbl.put(cd,new HashSet());
       HashSet hs=(HashSet)superIFtbl.get(cd);
@@ -84,7 +114,7 @@ public class TypeUtil {
 
   private void createTables() {
     supertable=new Hashtable();
-    superIFtbl = new Hashtable();
+    superIFtbl = new Hashtable<ClassDescriptor,Set<ClassDescriptor>>();
   }
 
   public ClassDescriptor getMainClass() {
@@ -93,22 +123,33 @@ public class TypeUtil {
 
   public MethodDescriptor getRun() {
     ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
-    for(Iterator methodit=cd.getMethodTable().getSet("run").iterator(); methodit.hasNext();) {
+    for(Iterator methodit=cd.getMethodTable().getSet("run").iterator(); methodit.hasNext(); ) {
       MethodDescriptor md=(MethodDescriptor) methodit.next();
       if (md.numParameters()!=0||md.getModifiers().isStatic())
-       continue;
+        continue;
       return md;
     }
     throw new Error("Can't find Thread.run");
   }
-  
+
+  public MethodDescriptor getStaticStart() {
+    ClassDescriptor cd=getClass(TypeUtil.ThreadClass);
+    for(Iterator methodit=cd.getMethodTable().getSet("staticStart").iterator(); methodit.hasNext(); ) {
+      MethodDescriptor md=(MethodDescriptor) methodit.next();
+      if (md.numParameters()!=1||!md.getModifiers().isStatic()||!md.getParamType(0).isClass()||md.getParamType(0).getClassDesc()!=cd)
+        continue;
+      return md;
+    }
+    throw new Error("Can't find Thread.run");
+  }
+
   public MethodDescriptor getExecute() {
     ClassDescriptor cd = getClass(TypeUtil.TaskClass);
 
     if(cd == null && state.DSMTASK)
       throw new Error("Task.java is not included");
 
-    for(Iterator methodit = cd.getMethodTable().getSet("execute").iterator(); methodit.hasNext();) {
+    for(Iterator methodit = cd.getMethodTable().getSet("execute").iterator(); methodit.hasNext(); ) {
       MethodDescriptor md = (MethodDescriptor) methodit.next();
       if (md.numParameters()!=0 || md.getModifiers().isStatic())
         continue;
@@ -121,20 +162,20 @@ public class TypeUtil {
   public MethodDescriptor getMain() {
     ClassDescriptor cd=getMainClass();
     Set mainset=cd.getMethodTable().getSet("main");
-    for(Iterator mainit=mainset.iterator(); mainit.hasNext();) {
+
+    for(Iterator mainit=mainset.iterator(); mainit.hasNext(); ) {
       MethodDescriptor md=(MethodDescriptor)mainit.next();
       if (md.numParameters()!=1)
-       continue;
+        continue;
       Descriptor pd=md.getParameter(0);
-      TypeDescriptor tpd=(pd instanceof TagVarDescriptor) ? ((TagVarDescriptor)pd).getType() : ((VarDescriptor)pd)
+      TypeDescriptor tpd=(pd instanceof TagVarDescriptor)?((TagVarDescriptor)pd).getType():((VarDescriptor)pd)
                           .getType();
       if (tpd.getArrayCount()!=1)
-       continue;
-      if (!tpd.getSymbol().equals("String"))
-       continue;
-
+        continue;
+      if (!tpd.getSymbol().equals(StringClass))
+        continue;
       if (!md.getModifiers().isStatic())
-       throw new Error("Error: Non static main");
+        throw new Error("Error: Non static main");
       return md;
     }
     throw new Error(cd+" has no main");
@@ -149,15 +190,22 @@ public class TypeUtil {
     if (md1.numParameters()!=md2.numParameters())
       throw new Error();
     for(int i=0; i<md1.numParameters(); i++) {
-      if (!this.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
-       return false;
+      if (!this.isSuperorType(md2.getParamType(i), md1.getParamType(i))) {
+        if(((!md1.getParamType(i).isArray() &&
+             (md1.getParamType(i).isInt() || md1.getParamType(i).isLong() || md1.getParamType(i).isDouble() || md1.getParamType(i).isFloat()))
+            && md2.getParamType(i).isClass() && md2.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
+          // primitive parameters vs Object
+        } else {
+          return false;
+        }
+      }
     }
     if (md1.getReturnType()==null||md2.getReturnType()==null) {
-       if (md1.getReturnType()!=md2.getReturnType())
-           return false;
+      if (md1.getReturnType()!=md2.getReturnType())
+        return false;
     } else
-       if (!this.isSuperorType(md2.getReturnType(), md1.getReturnType()))
-           return false;
+    if (!this.isSuperorType(md2.getReturnType(), md1.getReturnType()))
+      return false;
 
     if (!this.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
       return false;
@@ -169,25 +217,25 @@ public class TypeUtil {
     Set methoddescriptorset=cd.getMethodTable().getSet(name);
     MethodDescriptor bestmd=null;
 NextMethod:
-    for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
+    for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext(); ) {
       MethodDescriptor currmd=(MethodDescriptor)methodit.next();
       /* Need correct number of parameters */
       if (types.length!=currmd.numParameters())
-       continue;
+        continue;
       for(int i=0; i<types.length; i++) {
-       if (!this.isSuperorType(currmd.getParamType(i),types[i]))
-         continue NextMethod;
+        if (!this.isSuperorType(currmd.getParamType(i),types[i]))
+          continue NextMethod;
       }
       /* Method okay so far */
       if (bestmd==null)
-       bestmd=currmd;
+        bestmd=currmd;
       else {
-       if (isMoreSpecific(currmd,bestmd)) {
-         bestmd=currmd;
-       } else if (!isMoreSpecific(bestmd, currmd))
-         throw new Error("No method is most specific");
+        if (isMoreSpecific(currmd,bestmd)) {
+          bestmd=currmd;
+        } else if (!isMoreSpecific(bestmd, currmd))
+          throw new Error("No method is most specific");
 
-       /* Is this more specific than bestmd */
+        /* Is this more specific than bestmd */
       }
     }
     if (bestmd==null)
@@ -198,22 +246,22 @@ NextMethod:
 
   public void createFullTable() {
     subclasstable=new Hashtable();
-    //subIFclasstable = new Hashtable();
     HashSet tovisit=new HashSet();
     HashSet visited=new HashSet();
 
     Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
     while(classit.hasNext()) {
+      tovisit.clear();
+      visited.clear();
       ClassDescriptor cd=(ClassDescriptor)classit.next();
       ClassDescriptor tmp=cd.getSuperDesc();
-      
-      if(state.MGC) {
-        // TODO add version for normal Java later
-        // check tmp's interface ancestors
+
+      // check cd's interface ancestors
+      {
         Iterator it_sifs = cd.getSuperInterfaces();
         while(it_sifs.hasNext()) {
           ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
-          if(!tovisit.contains(cdt)){
+          if(!tovisit.contains(cdt)) {
             tovisit.add(cdt);
           }
         }
@@ -224,42 +272,37 @@ NextMethod:
           subclasstable.put(tmp,new HashSet());
         HashSet hs=(HashSet)subclasstable.get(tmp);
         hs.add(cd);
-        if(state.MGC) {
-          // TODO add version for normal Java later
-          // check tmp's interface ancestors
-          Iterator it_sifs = tmp.getSuperInterfaces();
-          while(it_sifs.hasNext()) {
-            ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
-            if(!tovisit.contains(cdt)){
-              tovisit.add(cdt);
-            }
+        // check tmp's interface ancestors
+        Iterator it_sifs = tmp.getSuperInterfaces();
+        while(it_sifs.hasNext()) {
+          ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
+          if(!tovisit.contains(cdt)) {
+            tovisit.add(cdt);
           }
         }
+
         tmp=tmp.getSuperDesc();
       }
-      
-      if(state.MGC) {
-        // TODO add version for normal Java later
-        while(!tovisit.isEmpty()) {
-          ClassDescriptor sif = (ClassDescriptor)tovisit.iterator().next();
-          tovisit.remove(sif);
-          
-          if(!visited.contains(sif)) {
-            if(!this.subclasstable.containsKey(sif)) {
-              this.subclasstable.put(sif, new HashSet());
-            }
-            HashSet hs = (HashSet)this.subclasstable/*subIFclasstable*/.get(sif);
-            hs.add(cd);
-            
-            Iterator it_sifs = sif.getSuperInterfaces();
-            while(it_sifs.hasNext()) {
-              ClassDescriptor siftmp = (ClassDescriptor)it_sifs.next();
-              if(!tovisit.contains(siftmp)){
-                tovisit.add(siftmp);
-              }
+
+      while(!tovisit.isEmpty()) {
+        ClassDescriptor sif = (ClassDescriptor)tovisit.iterator().next();
+        tovisit.remove(sif);
+
+        if(!visited.contains(sif)) {
+          if(!this.subclasstable.containsKey(sif)) {
+            this.subclasstable.put(sif, new HashSet());
+          }
+          HashSet hs = (HashSet) this.subclasstable.get(sif);
+          hs.add(cd);
+
+          Iterator it_sifs = sif.getSuperInterfaces();
+          while(it_sifs.hasNext()) {
+            ClassDescriptor siftmp = (ClassDescriptor)it_sifs.next();
+            if(!tovisit.contains(siftmp)) {
+              tovisit.add(siftmp);
             }
-            visited.add(sif);
           }
+          visited.add(sif);
         }
       }
     }
@@ -272,9 +315,9 @@ NextMethod:
   public ClassDescriptor getSuper(ClassDescriptor cd) {
     return (ClassDescriptor)supertable.get(cd);
   }
-  
-  public Set getSuperIFs(ClassDescriptor cd) {
-    return (Set)this.superIFtbl.get(cd);
+
+  public Set<ClassDescriptor> getSuperIFs(ClassDescriptor cd) {
+    return superIFtbl.get(cd);
   }
 
   public boolean isCastable(TypeDescriptor original, TypeDescriptor casttype) {
@@ -309,22 +352,22 @@ NextMethod:
     if (cd2.isArray()||possiblesuper.isArray()) {
       // Object is super class of all arrays
       if (possiblesuper.getSymbol().equals(ObjectClass)&&!possiblesuper.isArray())
-       return true;
+        return true;
 
       // If we have the same dimensionality of arrays & both are classes, we can default to the normal test
       if (cd2.isClass()&&possiblesuper.isClass()
           &&(possiblesuper.getArrayCount()==cd2.getArrayCount())&&
           isSuperorType(possiblesuper.getClassDesc(), cd2.getClassDesc()))
-       return true;
+        return true;
 
       // Object is superclass of all array classes
       if (possiblesuper.getSymbol().equals(ObjectClass)&&cd2.isClass()
           &&(possiblesuper.getArrayCount()<cd2.getArrayCount()))
-       return true;
+        return true;
 
       //Allow arraytype=null statements
       if (possiblesuper.isArray()&&cd2.isNull())
-       return true;
+        return true;
 
       return false;
     }
@@ -343,28 +386,34 @@ NextMethod:
       if (cd2.isByte()&&(possiblesuper.isByte()||possiblesuper.isShort()||
                          possiblesuper.isInt()||possiblesuper.isLong()||
                          possiblesuper.isFloat()||possiblesuper.isDouble()))
-       return true;
+        return true;
       if (cd2.isShort()&&(possiblesuper.isShort()||
                           possiblesuper.isInt()||possiblesuper.isLong()||
                           possiblesuper.isFloat()||possiblesuper.isDouble()))
-       return true;
+        return true;
       if (cd2.isChar()&&(possiblesuper.isChar()||
                          possiblesuper.isInt()||possiblesuper.isLong()||
                          possiblesuper.isFloat()||possiblesuper.isDouble()))
-       return true;
+        return true;
       if (cd2.isInt()&&(possiblesuper.isInt()||possiblesuper.isLong()||
-                        possiblesuper.isFloat()||possiblesuper.isDouble()))
-       return true;
+                        possiblesuper.isFloat()||possiblesuper.isDouble()
+                        ||possiblesuper.isEnum()))
+        return true;
+      if (cd2.isEnum()&&(possiblesuper.isInt()||possiblesuper.isLong()||
+                         possiblesuper.isFloat()||possiblesuper.isDouble()))
+        return true;
+      if(cd2.isEnum()&&possiblesuper.isEnum()&&cd2.class_desc.equals(possiblesuper.class_desc))
+        return true;
       if (cd2.isLong()&&(possiblesuper.isLong()||
                          possiblesuper.isFloat()||possiblesuper.isDouble()))
-       return true;
+        return true;
       if (cd2.isFloat()&&(possiblesuper.isFloat()||possiblesuper.isDouble()))
-       return true;
+        return true;
       if (cd2.isDouble()&&possiblesuper.isDouble())
 
-       return true;
+        return true;
       if (cd2.isBoolean()&&possiblesuper.isBoolean())
-       return true;
+        return true;
 
       return false;
     } else if (possiblesuper.isPrimitive()&&(!possiblesuper.isArray())&&
@@ -378,17 +427,17 @@ NextMethod:
   }
 
   public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2) {
-    if( isSuperorType( td1, td2 ) ) {
+    if( isSuperorType(td1, td2) ) {
       return td2;
     }
-    if( isSuperorType( td2, td1 ) ) {
+    if( isSuperorType(td2, td1) ) {
       return td1;
     }
-    throw new Error( td1+" and "+td2+" have no superclass relationship" );
+    throw new Error(td1+" and "+td2+" have no superclass relationship");
   }
 
   public TypeDescriptor mostSpecific(TypeDescriptor td1, TypeDescriptor td2, TypeDescriptor td3) {
-    return mostSpecific( td1, mostSpecific( td2, td3 ) );
+    return mostSpecific(td1, mostSpecific(td2, td3) );
   }
 
   public boolean isSuperorType(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
@@ -401,16 +450,15 @@ NextMethod:
   private boolean isSuper(ClassDescriptor possiblesuper, ClassDescriptor cd2) {
     HashSet tovisit=new HashSet();
     HashSet visited=new HashSet();
-    
-    if(state.MGC) {
-      // TODO add version for normal Java later
+
+    {
       // check cd2's interface ancestors
-      Iterator it_sifs = getSuperIFs(cd2).iterator();
+      Iterator<ClassDescriptor> it_sifs = getSuperIFs(cd2).iterator();
       while(it_sifs.hasNext()) {
-        ClassDescriptor cd = (ClassDescriptor)it_sifs.next();
+        ClassDescriptor cd = it_sifs.next();
         if(cd == possiblesuper) {
           return true;
-        } else if(!tovisit.contains(cd)){
+        } else if(!tovisit.contains(cd)) {
           tovisit.add(cd);
         }
       }
@@ -419,45 +467,38 @@ NextMethod:
     while(cd2!=null) {
       cd2=getSuper(cd2);
       if (cd2==possiblesuper)
-       return true;
-      
-      if(state.MGC) {
-        // TODO add version for normal Java later
-        // check cd2's interface ancestors
-        if(cd2 != null) {
-          Iterator it_sifs = getSuperIFs(cd2).iterator();
-          while(it_sifs.hasNext()) {
-            ClassDescriptor cd = (ClassDescriptor)it_sifs.next();
-            if(cd == possiblesuper) {
-              return true;
-            } else if(!tovisit.contains(cd)){
-              tovisit.add(cd);
-            }
+        return true;
+
+      // check cd2's interface ancestors
+      if(cd2 != null) {
+        Iterator it_sifs = getSuperIFs(cd2).iterator();
+        while(it_sifs.hasNext()) {
+          ClassDescriptor cd = (ClassDescriptor)it_sifs.next();
+          if(cd == possiblesuper) {
+            return true;
+          } else if(!tovisit.contains(cd)) {
+            tovisit.add(cd);
           }
         }
       }
     }
-    
-    if(state.MGC) {
-      // TODO add version for normal Java later
-      while(!tovisit.isEmpty()) {
-        ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
-        tovisit.remove(cd);
-        
-        if(!visited.contains(cd)) {
-          Iterator it_sifs = getSuperIFs(cd).iterator();
-          while(it_sifs.hasNext()) {
-            ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
-            if(cdt == possiblesuper) {
-              return true;
-            } else if(!tovisit.contains(cdt)){
-              tovisit.add(cdt);
-            }
+
+    while(!tovisit.isEmpty()) {
+      ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
+      tovisit.remove(cd);
+
+      if(!visited.contains(cd)) {
+        Iterator it_sifs = getSuperIFs(cd).iterator();
+        while(it_sifs.hasNext()) {
+          ClassDescriptor cdt = (ClassDescriptor)it_sifs.next();
+          if(cdt == possiblesuper) {
+            return true;
+          } else if(!tovisit.contains(cdt)) {
+            tovisit.add(cdt);
           }
-          visited.add(cd);
         }
+        visited.add(cd);
       }
-        
     }
     return false;
   }