// for interfaces
Vector<String> superinterfaces;
SymbolTable superIFdesc;
- private boolean isInterface;
+ private int interfaceid;
// for inner classes
boolean isInnerClass=false;
fields=new SymbolTable();
fieldvec=new Vector();
methods=new SymbolTable();
- this.isInterface = isInterface;
- classid=UIDCount++;
+ if(isInterface) {
+ this.classid = -2;
+ this.interfaceid = -1;
+ } else {
+ classid=UIDCount++;
+ }
this.packagename=packagename;
superinterfaces = new Vector<String>();
superIFdesc = new SymbolTable();
}
public int getId() {
+ if(this.isInterface()) {
+ return this.interfaceid;
+ }
return classid;
}
}
public boolean isInterface() {
- return this.isInterface;
+ return (this.classid == -2);
+ }
+
+ public void setInterfaceId(int id) {
+ this.interfaceid = id;
}
public boolean isStatic() {
protected int maxtaskparams=0;
protected int maxcount=0;
ClassDescriptor[] cdarray;
+ ClassDescriptor[] ifarray;
TypeDescriptor[] arraytable;
SafetyAnalysis sa;
CallGraph callgraph;
fieldorder=new Hashtable();
flagorder=new Hashtable();
this.typeutil=typeutil;
+ checkMethods2Gen();
virtualcalls=new Virtual(state, null);
printedfieldstbl = new Hashtable<String, ClassDescriptor>();
}
postCodeGenCleanUp();
}
+ /* This method goes though the call graph and check which methods are really
+ * invoked and should be generated
+ */
+ protected void checkMethods2Gen() {
+ MethodDescriptor md=typeutil.getMain();
+
+ if(md != null) {
+ // check the methods to be generated
+ state.setGenAllMethods(false);
+ } else {
+ // generate all methods
+ return;
+ }
+ this.state.addMethod2gen(md);
+
+ Iterator it_classes = this.state.getClassSymbolTable().getDescriptorsIterator();
+ while(it_classes.hasNext()) {
+ ClassDescriptor cd = (ClassDescriptor)it_classes.next();
+ Iterator it_methods = cd.getMethodTable().getDescriptorsIterator();
+ while(it_methods.hasNext()) {
+ md = (MethodDescriptor)it_methods.next();
+ if(md.isStaticBlock() || md.getModifiers().isNative() || this.callgraph.getCallerSet(md).size() > 0
+ || (cd.getSymbol().equals("Thread") && md.getSymbol().equals("staticStart"))) {
+ this.state.addMethod2gen(md);
+ }
+ }
+ }
+ }
/* This method goes though the call graph and tag those methods that are
while(methodit.hasNext()) {
/* Classify parameters */
MethodDescriptor md=(MethodDescriptor)methodit.next();
+ Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
+ boolean foundmatch = false;
+ for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
+ MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+ if (md.matches(matchmd)) {
+ foundmatch=true;
+ break;
+ }
+ }
+ if(!foundmatch) {
+ continue;
+ }
FlatMethod fm=state.getMethodFlat(md);
if (!md.getModifiers().isNative()) {
generateFlatMethod(fm, outmethod);
(state.getArrayNumber((new TypeDescriptor(TypeDescriptor.BYTE)).makeArray(state).makeArray(state))+state.numClasses()));
outstructs.println("#define NUMCLASSES "+state.numClasses());
- int totalClassSize = state.numClasses() + state.numArrays();
+ int totalClassSize = state.numClasses() + state.numArrays() + state.numInterfaces();
outstructs.println("#define TOTALNUMCLASSANDARRAY "+ totalClassSize);
if (state.TASK) {
outstructs.println("#define STARTUPTYPE "+typeutil.getClass(TypeUtil.StartupClass).getId());
classit=state.getClassSymbolTable().getDescriptorsIterator();
while(classit.hasNext()) {
ClassDescriptor cd=(ClassDescriptor)classit.next();
+ if(cd.isInterface()) {
+ continue;
+ }
fillinRow(cd, virtualtable, cd.getId());
}
MethodDescriptor md=(MethodDescriptor)it.next();
if (md.isStatic()||md.getReturnType()==null)
continue;
- Vector<Integer> numvec = virtualcalls.getMethodNumber(md);
- for(int i = 0; i < numvec.size(); i++) {
- int methodnum = numvec.elementAt(i).intValue();
- virtualtable[rownum][methodnum]=md;
+ Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
+ boolean foundmatch = false;
+ for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
+ MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+ if (md.matches(matchmd)) {
+ foundmatch=true;
+ break;
+ }
+ }
+ if(!foundmatch) {
+ continue;
}
+ int methodnum = virtualcalls.getMethodNumber(md);
+ virtualtable[rownum][methodnum]=md;
}
}
Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
cdarray=new ClassDescriptor[state.numClasses()];
+ ifarray = new ClassDescriptor[state.numInterfaces()];
cdarray[0] = null;
int interfaceid = 0;
while(it.hasNext()) {
ClassDescriptor cd=(ClassDescriptor)it.next();
- cdarray[cd.getId()] = cd;
+ if(cd.isInterface()) {
+ ifarray[cd.getId()] = cd;
+ } else {
+ cdarray[cd.getId()] = cd;
+ }
}
arraytable=new TypeDescriptor[state.numArrays()];
TypeDescriptor arraytd=arraytable[i];
outclassdefs.println(arraytd.toPrettyString() +" "+(i+state.numClasses()));
}
+
+ for(int i=0; i<state.numInterfaces(); i++) {
+ ClassDescriptor ifcd = ifarray[i];
+ outclassdefs.println(ifcd +" "+(i+state.numClasses()+state.numArrays()));
+ }
outclassdefs.println("*/");
outclassdefs.print("sizeof("+tdelement.getSafeSymbol()+")");
needcomma=true;
}
+
+ for(int i=0; i<state.numInterfaces(); i++) {
+ if (needcomma)
+ outclassdefs.print(", ");
+ outclassdefs.print("sizeof(struct "+ifarray[i].getSafeSymbol()+")");
+ needcomma=true;
+ }
outclassdefs.println("};");
for(int i=0; i<state.numClasses(); i++) {
ClassDescriptor cd=cdarray[i];
ClassDescriptor supercd=i>0 ? cd.getSuperDesc() : null;
+ if(supercd != null && supercd.isInterface()) {
+ throw new Error("Super class can not be interfaces");
+ }
if (needcomma)
outclassdefs.print(", ");
if (supercd==null)
outclassdefs.print(type);
needcomma=true;
}
+
+ for(int i=0; i<state.numInterfaces(); i++) {
+ ClassDescriptor cd=ifarray[i];
+ ClassDescriptor supercd=cd.getSuperDesc();
+ if(supercd != null && supercd.isInterface()) {
+ throw new Error("Super class can not be interfaces");
+ }
+ if (needcomma)
+ outclassdefs.print(", ");
+ if (supercd==null)
+ outclassdefs.print("-1");
+ else
+ outclassdefs.print(supercd.getId());
+ needcomma=true;
+ }
outclassdefs.println("};");
if(ncomma) {
output.print(",");
}
- output.print(((ClassDescriptor)it_sifs.next()).getId());
+ output.print(((ClassDescriptor)it_sifs.next()).getId()+state.numClasses()+state.numArrays());
}
output.println("};");
output.println("};");
}
+ for(int i=0; i<state.numInterfaces(); i++) {
+ ClassDescriptor cn=ifarray[i];
+ if(cn == null) {
+ continue;
+ }
+ output.print("int supertypes" + cn.getSafeSymbol() + "[] = {");
+ boolean ncomma = false;
+ int snum = 0;
+ if((cn != null) && (cn.getSuperDesc() != null)) {
+ snum++;
+ }
+ Iterator it_sifs = cn != null? cn.getSuperInterfaces() : null;
+ while(it_sifs != null && it_sifs.hasNext()) {
+ snum++;
+ it_sifs.next();
+ }
+ output.print(snum);
+ ncomma = true;
+ if ((cn != null) && (cn.getSuperDesc()!=null)) {
+ if(ncomma) {
+ output.print(",");
+ }
+ ClassDescriptor cdsuper=cn.getSuperDesc();
+ output.print(cdsuper.getId());
+ }
+ it_sifs = cn != null? cn.getSuperInterfaces() : null;
+ while(it_sifs != null && it_sifs.hasNext()) {
+ if(ncomma) {
+ output.print(",");
+ }
+ output.print(((ClassDescriptor)it_sifs.next()).getId()+state.numClasses()+state.numArrays());
+ }
+
+ output.println("};");
+ }
+
output.println("int* supertypes[]={");
boolean needcomma=false;
for(int i=0; i<state.numClasses(); i++) {
needcomma = true;
output.print("supertypes___arraytype___" + (i+state.numClasses()));
}
+
+ for(int i=0; i<state.numInterfaces(); i++) {
+ ClassDescriptor cn=ifarray[i];
+ if (needcomma)
+ output.println(",");
+ needcomma=true;
+ output.print("supertypes" + cn.getSafeSymbol());
+ }
output.println("};");
}
protected void generateCallStructsMethods(ClassDescriptor cn, PrintWriter output, PrintWriter headersout) {
for(Iterator methodit=cn.getMethods(); methodit.hasNext(); ) {
MethodDescriptor md=(MethodDescriptor)methodit.next();
- generateMethod(cn, md, headersout, output);
+ Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
+ boolean foundmatch = false;
+ for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
+ MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+ if (md.matches(matchmd)) {
+ foundmatch=true;
+ break;
+ }
+ }
+ if(foundmatch) {
+ generateMethod(cn, md, headersout, output);
+ }
}
}
int otype;
if (fion.getType().isArray()) {
type=state.getArrayNumber(fion.getType())+state.numClasses();
+ } else if (fion.getType().getClassDesc().isInterface()) {
+ type=fion.getType().getClassDesc().getId()+state.numClasses()+state.numArrays();
} else {
type=fion.getType().getClassDesc().getId();
}
if (fion.getSrc().getType().isArray()) {
otype=state.getArrayNumber(fion.getSrc().getType())+state.numClasses();
+ } else if (fion.getSrc().getType().getClassDesc().isInterface()) {
+ otype=fion.getSrc().getType().getClassDesc().getId()+state.numClasses()+state.numArrays();
} else {
otype=fion.getSrc().getType().getClassDesc().getId();
}
}
- output.print("))virtualtable["+generateTemp(fm,fc.getThis())+"->type*"+maxcount+"+"+virtualcalls.getMethodNumber(md).elementAt(0)+"])");
+ output.print("))virtualtable["+generateTemp(fm,fc.getThis())+"->type*"+maxcount+"+"+virtualcalls.getMethodNumber(md)+"])");
}
output.print("(");
// Output function prototypes and structures for parameters
Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
- int numclasses = this.state.numClasses();
while(it.hasNext()) {
ClassDescriptor cn=(ClassDescriptor)it.next();
super.generateCallStructs(cn, outclassdefs, outstructs, outmethodheader, outglobaldefs, outglobaldefsprim);
/* Record maximum number of task parameters */
//outstructs.println("#define MAXTASKPARAMS "+maxtaskparams);
/* Record maximum number of all types, i.e. length of classsize[] */
- outstructs.println("#define NUMTYPES "+(state.numClasses() + state.numArrays()));
+ outstructs.println("#define NUMTYPES "+(state.numClasses() + state.numArrays() + state.numInterfaces()));
/* Record number of total cores */
outstructs.println("#define NUMCORES "+this.tcoreNum);
/* Record number of active cores */
outmethod.println(" }");
}
- } // else TODO normal java version
-
+ } // else TODO normal java version
}
}
/* Record maximum number of task parameters */
outstructs.println("#define MAXTASKPARAMS "+maxtaskparams);
/* Record maximum number of all types, i.e. length of classsize[] */
- outstructs.println("#define NUMTYPES "+(state.numClasses() + state.numArrays()));
+ outstructs.println("#define NUMTYPES "+(state.numClasses() + state.numArrays() + state.numInterfaces()));
/* Record number of total cores */
outstructs.println("#define NUMCORES "+this.tcoreNum);
/* Record number of active cores */
this.sclasses=new SymbolTable();
this.treemethodmap=new Hashtable();
this.flatmethodmap=new Hashtable();
+ this.genAllMethods = true;
+ this.methods2gen = new SymbolTable();
this.parsetrees=new HashSet();
this.arraytypes=new HashSet();
this.arraytonumber=new Hashtable();
public Set parsetrees;
public Hashtable treemethodmap;
public Hashtable flatmethodmap;
+ SymbolTable methods2gen;
+ boolean genAllMethods;
private HashSet arraytypes;
public Hashtable arraytonumber;
private int numclasses=1; // start from 1 instead of 0 for multicore gc
+ private int numinterfaces = 0;
private int numtasks=0;
private int numstaticblocks=0;
private int arraycount=0;
if (classes.contains(tdn.getSymbol()))
throw new Error("Class "+tdn.getSymbol()+" defined twice");
classes.add(tdn);
- numclasses++;
+ if(tdn.isInterface()) {
+ numinterfaces++;
+ } else {
+ numclasses++;
+ }
if((tdn.numstaticfields != 0) || (tdn.numstaticblocks != 0)) {
sclasses.add(tdn);
}
}
+ public void setGenAllMethods(boolean flag) {
+ this.genAllMethods = flag;
+ }
+
+ public void addMethod2gen(MethodDescriptor md) {
+ if(this.genAllMethods) {
+ throw new Error("The state.genAllMethods is TRUE, do not need to check methods to genenrate");
+ }
+ this.methods2gen.add(md);
+ }
+
+ public SymbolTable getMethod2gen() {
+ return this.methods2gen;
+ }
+
public int numClasses() {
return numclasses;
}
+ public int numInterfaces() {
+ return numinterfaces;
+ }
+
public int numStaticBlocks() {
return numstaticblocks;
}
public void buildtree(ParseNode pn, Set toanalyze) {
parseFile(pn, toanalyze);
+
+ // numering the interfaces
+ int if_num = 0;
+ Iterator it_classes = state.getClassSymbolTable().getValueSet().iterator();
+ while(it_classes.hasNext()) {
+ ClassDescriptor cd = (ClassDescriptor)it_classes.next();
+ if(cd.isInterface()) {
+ cd.setInterfaceId(if_num++);
+ }
+ }
}
Vector singleimports;
package IR;
import java.util.*;
+
import Analysis.Locality.LocalityBinding;
import Analysis.Locality.LocalityAnalysis;
public class Virtual {
State state;
LocalityAnalysis locality;
- Hashtable<MethodDescriptor, Vector<Integer>> methodnumber;
+ Hashtable<MethodDescriptor, Integer> methodnumber;
Hashtable<ClassDescriptor, Integer> classmethodcount;
Hashtable<LocalityBinding, Integer> localitynumber;
// for interfaces
int if_starts;
+ SymbolTable if_methods;
- public Vector<Integer> getMethodNumber(MethodDescriptor md) {
+ public Integer getMethodNumber(MethodDescriptor md) {
return methodnumber.get(md);
}
this.state=state;
this.locality=locality;
this.if_starts = 0;
+ this.if_methods = new SymbolTable();
classmethodcount=new Hashtable<ClassDescriptor, Integer>();
if (state.DSM||state.SINGLETM)
localitynumber=new Hashtable<LocalityBinding, Integer>();
else
- methodnumber=new Hashtable<MethodDescriptor, Vector<Integer>>();
+ methodnumber=new Hashtable<MethodDescriptor, Integer>();
doAnalysis();
}
if(!cd.isInterface()) {
return 0;
}
- int start = 0;
+ int mnum = 0;
if (classmethodcount.containsKey(cd))
return classmethodcount.get(cd).intValue();
// check the inherited interfaces
Iterator it_sifs = cd.getSuperInterfaces();
while(it_sifs.hasNext()) {
ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
- start += numberMethodsIF(superif);
+ mnum += numberMethodsIF(superif);
}
for(Iterator it=cd.getMethods(); it.hasNext();) {
MethodDescriptor md=(MethodDescriptor)it.next();
if (md.isStatic()||md.getReturnType()==null)
continue;
- boolean foundmatch=false;
- // check if there is a matched method in inherited interfaces
- it_sifs = cd.getSuperInterfaces();
- while(it_sifs.hasNext() && !foundmatch) {
- ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
- Set possiblematches_if=superif.getMethodTable().getSet(md.getSymbol());
- for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
- MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
- if (md.matches(matchmd)) {
- Vector<Integer> num=methodnumber.get(matchmd);
- if(!methodnumber.containsKey(md)) {
- methodnumber.put(md, new Vector<Integer>());
- }
- Vector<Integer> toadd = methodnumber.get(md);
- toadd.addAll(num);
- foundmatch=true;
- }
+ Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
+ boolean foundmatch = false;
+ for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
+ MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+ if (md.matches(matchmd)) {
+ foundmatch=true;
+ break;
}
}
if(!foundmatch) {
- Vector<Integer> vec = new Vector<Integer>();
- vec.add(new Integer(if_starts++));
- methodnumber.put(md, vec);
- start++;
+ continue;
+ }
+ foundmatch=false;
+ // check if there is a matched method that has been assigned method num
+ Set possiblematches_if = if_methods.getSet(md.getSymbol());
+ for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
+ MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+ if (md.matches(matchmd)) {
+ int num=methodnumber.get(matchmd);
+ methodnumber.put(md, new Integer(num));
+ foundmatch=true;
+ break;
+ }
+ }
+ if(!foundmatch) {
+ methodnumber.put(md, new Integer(if_starts++));
+ if_methods.add(md);
+ mnum++;
}
}
- classmethodcount.put(cd, new Integer(start));
- return start;
+ classmethodcount.put(cd, new Integer(mnum));
+ return mnum;
}
private int numberMethods(ClassDescriptor cd) {
MethodDescriptor md=(MethodDescriptor)it.next();
if (md.isStatic()||md.getReturnType()==null)
continue;
- boolean foundmatch=false;
- if (superdesc!=null) {
+ Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
+ boolean foundmatch = false;
+ for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
+ MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+ if (md.matches(matchmd)) {
+ foundmatch=true;
+ break;
+ }
+ }
+ if(!foundmatch) {
+ continue;
+ }
+ foundmatch=false;
+ // check if there is a matched method in methods defined in interfaces
+ Set possiblematches_if=if_methods.getSet(md.getSymbol());
+ for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
+ MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+ if (md.matches(matchmd)) {
+ int num = methodnumber.get(matchmd);
+ methodnumber.put(md, new Integer(num));
+ foundmatch=true;
+ break;
+ }
+ }
+ if (!foundmatch && superdesc!=null) {
Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
if (md.matches(matchmd)) {
- Vector<Integer> num = methodnumber.get(matchmd);
- methodnumber.put(md, num);
+ int num = methodnumber.get(matchmd);
+ methodnumber.put(md, new Integer(num));
foundmatch=true;
break;
}
}
}
- // check if there is a matched method in inherited interfaces
- Iterator it_sifs = cd.getSuperInterfaces();
- while(it_sifs.hasNext()) {
- ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
- Set possiblematches_if=superif.getMethodTable().getSet(md.getSymbol());
- for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
- MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
- if (md.matches(matchmd)) {
- Vector<Integer> num = methodnumber.get(matchmd);
- if(!methodnumber.containsKey(md)) {
- methodnumber.put(md, new Vector<Integer>());
- }
- Vector<Integer> toadd = methodnumber.get(md);
- toadd.addAll(num);
- foundmatch=true;
- }
- }
- }
if (!foundmatch) {
- Vector<Integer> vec = new Vector<Integer>();
- vec.add(new Integer(start++));
- methodnumber.put(md, vec);
+ methodnumber.put(md, new Integer(start++));
mnum++;
}
}