From b45849b72c165a7585cd46e12dfdf0da5511bfc9 Mon Sep 17 00:00:00 2001 From: jzhou Date: Fri, 18 Feb 2011 01:45:00 +0000 Subject: [PATCH] Bug fixes for MGC --- Robust/src/IR/Flat/BuildCode.java | 222 +++++++++++++++++---- Robust/src/IR/Flat/BuildCodeMGC.java | 3 - Robust/src/IR/Flat/BuildCodeMultiCore.java | 4 +- Robust/src/IR/Flat/BuildFlat.java | 33 ++- Robust/src/IR/Tree/BuildIR.java | 12 +- Robust/src/IR/Tree/MethodInvokeNode.java | 4 + Robust/src/IR/Tree/NameNode.java | 14 +- Robust/src/IR/Tree/SemanticCheck.java | 54 ++++- Robust/src/IR/TypeDescriptor.java | 47 +++-- Robust/src/IR/TypeUtil.java | 11 +- Robust/src/Tests/EnumTest.java | 2 +- Robust/src/buildscript | 10 +- 12 files changed, 321 insertions(+), 95 deletions(-) diff --git a/Robust/src/IR/Flat/BuildCode.java b/Robust/src/IR/Flat/BuildCode.java index a85621ae..26290be7 100644 --- a/Robust/src/IR/Flat/BuildCode.java +++ b/Robust/src/IR/Flat/BuildCode.java @@ -66,6 +66,9 @@ public class BuildCode { DiscoverConflicts recorddc; DCWrapper delaycomp; CallGraph callgraph; + Hashtable printedfieldstbl; + Hashtable> cd2fieldstbl; + Hashtable> cd2shadowfields; public BuildCode(State st, Hashtable temptovar, TypeUtil typeutil, SafetyAnalysis sa, PrefetchAnalysis pa) { @@ -111,6 +114,9 @@ public class BuildCode { recorddc=new DiscoverConflicts(locality, st, typeanalysis, delaycomp.getCannotDelayMap(), true, true, null); recorddc.doAnalysis(); } + printedfieldstbl = new Hashtable(); + cd2fieldstbl = new Hashtable>(); + cd2shadowfields = new Hashtable>(); } /** The buildCode method outputs C code for all the methods. The Flat @@ -658,22 +664,22 @@ public class BuildCode { outstructs.println("#define STRINGARRAYTYPE "+ (state.getArrayNumber( - (new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass))).makeArray(state))+state.numClasses())); + (new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass))).makeArray(state, true))+state.numClasses())); outstructs.println("#define OBJECTARRAYTYPE "+ (state.getArrayNumber( - (new TypeDescriptor(typeutil.getClass(TypeUtil.ObjectClass))).makeArray(state))+state.numClasses())); + (new TypeDescriptor(typeutil.getClass(TypeUtil.ObjectClass))).makeArray(state, true))+state.numClasses())); outstructs.println("#define STRINGTYPE "+typeutil.getClass(TypeUtil.StringClass).getId()); outstructs.println("#define CHARARRAYTYPE "+ - (state.getArrayNumber((new TypeDescriptor(TypeDescriptor.CHAR)).makeArray(state))+state.numClasses())); + (state.getArrayNumber((new TypeDescriptor(TypeDescriptor.CHAR)).makeArray(state, true))+state.numClasses())); outstructs.println("#define BYTEARRAYTYPE "+ - (state.getArrayNumber((new TypeDescriptor(TypeDescriptor.BYTE)).makeArray(state))+state.numClasses())); + (state.getArrayNumber((new TypeDescriptor(TypeDescriptor.BYTE)).makeArray(state, true))+state.numClasses())); outstructs.println("#define BYTEARRAYARRAYTYPE "+ - (state.getArrayNumber((new TypeDescriptor(TypeDescriptor.BYTE)).makeArray(state).makeArray(state))+state.numClasses())); + (state.getArrayNumber((new TypeDescriptor(TypeDescriptor.BYTE)).makeArray(state, true).makeArray(state, true))+state.numClasses())); outstructs.println("#define NUMCLASSES "+state.numClasses()); int totalClassSize = state.numClasses() + state.numArrays(); @@ -682,7 +688,7 @@ public class BuildCode { outstructs.println("#define STARTUPTYPE "+typeutil.getClass(TypeUtil.StartupClass).getId()); outstructs.println("#define TAGTYPE "+typeutil.getClass(TypeUtil.TagClass).getId()); outstructs.println("#define TAGARRAYTYPE "+ - (state.getArrayNumber(new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass)).makeArray(state))+state.numClasses())); + (state.getArrayNumber(new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass)).makeArray(state, true))+state.numClasses())); } } @@ -762,6 +768,7 @@ public class BuildCode { } printClassStruct(typeutil.getClass(TypeUtil.ObjectClass), outclassdefs, outglobaldefs); + printedfieldstbl.clear(); if (state.STMARRAY) { outclassdefs.println(" int lowindex;"); @@ -821,6 +828,7 @@ public class BuildCode { } } printClassStruct(typeutil.getClass(TypeUtil.ObjectClass), outclassdefs, outglobaldefs); + printedfieldstbl.clear(); outclassdefs.println("};\n"); } @@ -1491,25 +1499,69 @@ public class BuildCode { /** Force consistent field ordering between inherited classes. */ private void printClassStruct(ClassDescriptor cn, PrintWriter classdefout, PrintWriter globaldefout) { - + ClassDescriptor sp=cn.getSuperDesc(); if (sp!=null) - printClassStruct(sp, classdefout, globaldefout); + printClassStruct(sp, classdefout, /*globaldefout*/null); + + // TODO: what about here are multiple inherited fields with the same name? + SymbolTable sitbl = cn.getSuperInterfaceTable(); + Iterator it_sifs = sitbl.getDescriptorsIterator(); + if(state.MGC) { + while(it_sifs.hasNext()) { + ClassDescriptor si = (ClassDescriptor)it_sifs.next(); + printClassStruct(si, classdefout, /*globaldefout*/null); + } + } + + Vector shadow_fields = null; if (!fieldorder.containsKey(cn)) { Vector fields=new Vector(); fieldorder.put(cn,fields); + shadow_fields = new Vector(); + cd2shadowfields.put(cn, shadow_fields); + Vector fieldvec=cn.getFieldVec(); for(int i=0;i(); classdefout.println("};\n"); if (state.DSM||state.SINGLETM) { @@ -1682,7 +1781,11 @@ public class BuildCode { output.println(" void * next;"); for(int i=0; i"+fd.getSafeSymbol()+"=&(global_defs_p->"+cn.getSafeSymbol()+fd.getSafeSymbol()+");"); + // decide the exact class/interface that defines the field + ClassDescriptor fdcn = + cd2fieldstbl.get(cn).get(fd.getSymbol()); + + // static field + output.println(generateTemp(fm,fm.getParameter(0),lb)+"->"+fd.getSafeSymbol()+"=&(global_defs_p->"+fdcn.getSafeSymbol()+fd.getSafeSymbol()+");"); } } } @@ -3009,9 +3121,6 @@ public class BuildCode { // its static blocks have been executed output.println("#ifdef MGC_STATIC_INIT_CHECK"); output.println("if(global_defs_p->" + cn.getSafeSymbol()+"static_block_exe_flag == 0) {"); - if(cn.getNumStaticFields() != 0) { - // TODO add static field initialization here - } if(cn.getNumStaticBlocks() != 0) { MethodDescriptor t_md = (MethodDescriptor)cn.getMethodTable().get("staticblocks"); output.println(" "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"();"); @@ -3084,7 +3193,9 @@ public class BuildCode { } else { //yes output.print("(("); - if (md.getReturnType().isClass()||md.getReturnType().isArray()) + if (state.MGC && md.getReturnType().isClass() && md.getReturnType().getClassDesc().isEnum()) { + output.print("int "); + } else if (md.getReturnType().isClass()||md.getReturnType().isArray()) output.print("struct " + md.getReturnType().getSafeSymbol()+" * "); else output.print(md.getReturnType().getSafeSymbol()+" "); @@ -3105,7 +3216,9 @@ public class BuildCode { if (printcomma) output.print(", "); printcomma=true; - if (temp.getType().isClass()||temp.getType().isArray()) + if (state.MGC && temp.getType().isClass() && temp.getType().getClassDesc().isEnum()) { + output.print("int "); + } else if (temp.getType().isClass()||temp.getType().isArray()) output.print("struct " + temp.getType().getSafeSymbol()+" * "); else output.print(temp.getType().getSafeSymbol()); @@ -3136,7 +3249,9 @@ public class BuildCode { } if (needcomma) output.print(","); - if (ptd.isClass()&&!ptd.isArray()) + if(state.MGC && ptd.isClass() && ptd.getClassDesc().isEnum()) { + // do nothing + } else if (ptd.isClass()&&!ptd.isArray()) output.print("(struct "+ptd.getSafeSymbol()+" *) "); output.print(generateTemp(fm,fc.getThis(),lb)); needcomma=true; @@ -3152,7 +3267,9 @@ public class BuildCode { output.print(", "); TypeDescriptor ptd=md.getParamType(i); - if (ptd.isClass()&&!ptd.isArray()) + if (state.MGC && ptd.isClass() && ptd.getClassDesc().isEnum()) { + // do nothing + } else if (ptd.isClass()&&!ptd.isArray()) output.print("(struct "+ptd.getSafeSymbol()+" *) "); output.print(generateTemp(fm, targ,lb)); needcomma=true; @@ -3283,13 +3400,16 @@ public class BuildCode { } } // redirect to the global_defs_p structure - if((ffn.getField().isStatic()) || (ffn.getSrc().getType().isStatic())) { + if((ffn.getField().isStatic()) || (ffn.getSrc().getType().isClassNameRef())) { + // decide the exact class/interface that defines the field + ClassDescriptor fdcn = + cd2fieldstbl.get(ffn.getSrc().getType().getClassDesc()).get(ffn.getField().getSymbol()); + // reference to the static field with Class name - output.println(generateTemp(fm, ffn.getDst(),lb)+"=global_defs_p->"+ ffn.getSrc().getType().getClassDesc().getSafeSymbol()+ffn.getField().getSafeSymbol()+";"); + output.println(generateTemp(fm, ffn.getDst(),lb)+"=global_defs_p->"+ fdcn.getSafeSymbol()+ffn.getField().getSafeSymbol()+";"); } else { output.println(generateTemp(fm, ffn.getDst(),lb)+"=*"+ generateTemp(fm,ffn.getSrc(),lb)+"->"+ ffn.getField().getSafeSymbol()+";"); } - //output.println(generateTemp(fm, ffn.getDst(),lb)+"=global_defs_p->"+ffn.getSrc().getType().getClassDesc().getSafeSymbol()+"->"+ ffn.getField().getSafeSymbol()+";"); } else if (ffn.getField().isEnum()) { // an Enum value, directly replace the field access as int output.println(generateTemp(fm, ffn.getDst(), lb) + "=" + ffn.getField().enumValue() + ";"); @@ -3432,9 +3552,13 @@ public class BuildCode { } } // redirect to the global_defs_p structure - if(fsfn.getDst().getType().isStatic()) { + if(fsfn.getDst().getType().isClassNameRef()) { // reference to the static field with Class name - output.println("global_defs_p->" + fsfn.getDst().getType().getClassDesc().getSafeSymbol() + fsfn.getField().getSafeSymbol()+"="+ generateTemp(fm,fsfn.getSrc(),lb)+";"); + // decide the exact class/interface that defines the field + ClassDescriptor fdcn = + cd2fieldstbl.get(fsfn.getDst().getType().getClassDesc()).get(fsfn.getField().getSymbol()); + + output.println("global_defs_p->" + fdcn.getSafeSymbol() + fsfn.getField().getSafeSymbol()+"="+ generateTemp(fm,fsfn.getSrc(),lb)+";"); } else { output.println("*"+generateTemp(fm, fsfn.getDst(),lb)+"->"+ fsfn.getField().getSafeSymbol()+"="+ generateTemp(fm,fsfn.getSrc(),lb)+";"); } @@ -3451,7 +3575,9 @@ public class BuildCode { TypeDescriptor elementtype=fen.getSrc().getType().dereference(); String type=""; - if (elementtype.isArray()||elementtype.isClass()) + if (state.MGC && elementtype.isClass() && elementtype.getClassDesc().isEnum()) { + type="int "; + } else if (elementtype.isArray()||elementtype.isClass()) type="void *"; else type=elementtype.getSafeSymbol()+" "; @@ -3516,7 +3642,9 @@ public class BuildCode { TypeDescriptor elementtype=fsen.getDst().getType().dereference(); String type=""; - if (elementtype.isArray()||elementtype.isClass()) + if (state.MGC && elementtype.isClass() && elementtype.getClassDesc().isEnum()) { + type="int "; + } else if (elementtype.isArray()||elementtype.isClass() || (state.MGC && elementtype.isNull())) type="void *"; else type=elementtype.getSafeSymbol()+" "; @@ -3724,6 +3852,8 @@ public class BuildCode { /* TODO: Do type check here */ if (fcn.getType().isArray()) { output.println(generateTemp(fm,fcn.getDst(),lb)+"=(struct ArrayObject *)"+generateTemp(fm,fcn.getSrc(),lb)+";"); + } else if (state.MGC && fcn.getType().isClass() && fcn.getType().getClassDesc().isEnum()) { + output.println(generateTemp(fm,fcn.getDst(),lb)+"=(int)"+generateTemp(fm,fcn.getSrc(),lb)+";"); } else if (fcn.getType().isClass()) output.println(generateTemp(fm,fcn.getDst(),lb)+"=(struct "+fcn.getType().getSafeSymbol()+" *)"+generateTemp(fm,fcn.getSrc(),lb)+";"); else @@ -3822,7 +3952,9 @@ public class BuildCode { ClassDescriptor cn=md!=null ? md.getClassDesc() : null; if (md!=null&&md.getReturnType()!=null) { - if (md.getReturnType().isClass()||md.getReturnType().isArray()) + if (state.MGC && md.getReturnType().isClass() && md.getReturnType().getClassDesc().isEnum()) { + output.print("int "); + } else if (md.getReturnType().isClass()||md.getReturnType().isArray()) output.print("struct " + md.getReturnType().getSafeSymbol()+" * "); else output.print(md.getReturnType().getSafeSymbol()+" "); @@ -3856,7 +3988,9 @@ public class BuildCode { if (printcomma) output.print(", "); printcomma=true; - if (temp.getType().isClass()||temp.getType().isArray()) + if(state.MGC && temp.getType().isClass() && temp.getType().getClassDesc().isEnum()) { + output.print("int " + temp.getSafeSymbol()); + } else if (temp.getType().isClass()||temp.getType().isArray()) output.print("struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()); else output.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol()); @@ -3868,7 +4002,11 @@ public class BuildCode { /* Unpack variables */ for(int i=0; i sifv = cd.getSuperInterface(); for(int i = 0; i < sifv.size(); i++) { ClassDescriptor superif = getClass(sifv.elementAt(i)); + if(!superif.isInterface()) { + throw new Error("Error! Class " + cd.getSymbol() + " implements non-interface " + superif.getSymbol()); + } cd.addSuperInterfaces(superif); cd.getFieldTable().addParentIF(superif.getFieldTable()); cd.getMethodTable().addParentIF(superif.getMethodTable()); @@ -64,7 +70,7 @@ public class SemanticCheck { checkMethod(cd,md); hasConstructor |= md.isConstructor(); } - if(!hasConstructor) { + if((!hasConstructor) && (!cd.isEnum())) { // add a default constructor for this class MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC), cd.getSymbol(), false); @@ -603,7 +609,7 @@ public class SemanticCheck { fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname); if(state.MGC) { // TODO add version for normal Java later - if(ltd.isStatic()) { + if(ltd.isClassNameRef()) { // the field access is using a class name directly if(ltd.getClassDesc().isEnum()) { int value = ltd.getClassDesc().getEnumConstant(fieldname); @@ -890,8 +896,8 @@ public class SemanticCheck { } } if(out_type != null) { - out_type = out_type.makeArray(state); - out_type.setStatic(); + out_type = out_type.makeArray(state, false); + //out_type.setStatic(); } ain.setType(out_type); } @@ -1228,7 +1234,12 @@ NextMethod: continue; for(int i=0; i=BYTE)&&(type<=DOUBLE)); } + public boolean isEnum() { + if(this.type != CLASS) { + return false; + } else if(this.class_desc != null){ + return this.class_desc.isEnum(); + } + return false; + } + public boolean isClass() { return type==CLASS; } @@ -267,7 +280,7 @@ public class TypeDescriptor extends Descriptor { this.type=CLASS; this.class_desc=null; this.arraycount=0; - this.isStatic =false; + this.isClassNameRef =false; } public TypeDescriptor(String st) { @@ -275,7 +288,7 @@ public class TypeDescriptor extends Descriptor { this.type=CLASS; this.class_desc=null; this.arraycount=0; - this.isStatic =false; + this.isClassNameRef =false; } public ClassDescriptor getClassDesc() { @@ -287,14 +300,14 @@ public class TypeDescriptor extends Descriptor { this.type=CLASS; this.class_desc=cd; this.arraycount=0; - this.isStatic =false; + this.isClassNameRef =false; } public TypeDescriptor(int t) { super(decodeInt(t)); this.type=t; this.arraycount=0; - this.isStatic =false; + this.isClassNameRef =false; } public String toString() { diff --git a/Robust/src/IR/TypeUtil.java b/Robust/src/IR/TypeUtil.java index 1eda9aa4..3cd80b86 100644 --- a/Robust/src/IR/TypeUtil.java +++ b/Robust/src/IR/TypeUtil.java @@ -149,8 +149,15 @@ public class TypeUtil { if (md1.numParameters()!=md2.numParameters()) throw new Error(); for(int i=0; i