From b1d7ca35bb16de7b7ae53ee00d30618380c91cc0 Mon Sep 17 00:00:00 2001 From: jzhou Date: Sat, 12 Feb 2011 00:43:15 +0000 Subject: [PATCH] some bug fix --- Robust/src/IR/Flat/BuildCode.java | 5 +- Robust/src/IR/Flat/BuildCodeMGC.java | 16 --- Robust/src/IR/Flat/BuildFlat.java | 9 +- Robust/src/IR/SymbolTable.java | 26 +++- Robust/src/IR/Tree/BuildIR.java | 24 +++- Robust/src/IR/Tree/OpNode.java | 8 +- Robust/src/IR/Tree/SemanticCheck.java | 163 ++++++++++++++++++++------ Robust/src/IR/TypeDescriptor.java | 2 - Robust/src/Parse/java14.cup | 2 +- Robust/src/buildscript | 8 +- 10 files changed, 185 insertions(+), 78 deletions(-) diff --git a/Robust/src/IR/Flat/BuildCode.java b/Robust/src/IR/Flat/BuildCode.java index fee74315..a85621ae 100644 --- a/Robust/src/IR/Flat/BuildCode.java +++ b/Robust/src/IR/Flat/BuildCode.java @@ -3271,9 +3271,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()+"();"); @@ -3286,7 +3283,7 @@ public class BuildCode { } } // redirect to the global_defs_p structure - if(ffn.getSrc().getType().isStatic()) { + if((ffn.getField().isStatic()) || (ffn.getSrc().getType().isStatic())) { // 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()+";"); } else { diff --git a/Robust/src/IR/Flat/BuildCodeMGC.java b/Robust/src/IR/Flat/BuildCodeMGC.java index 9ffcc8d1..0784bece 100644 --- a/Robust/src/IR/Flat/BuildCodeMGC.java +++ b/Robust/src/IR/Flat/BuildCodeMGC.java @@ -4,30 +4,14 @@ import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Hashtable; import java.util.Iterator; -import java.util.Set; -import Analysis.Locality.LocalityBinding; import Analysis.Prefetch.*; import Analysis.TaskStateAnalysis.SafetyAnalysis; import IR.ClassDescriptor; -import IR.Descriptor; -import IR.FlagDescriptor; import IR.MethodDescriptor; import IR.State; import IR.SymbolTable; -import IR.TagVarDescriptor; -import IR.TaskDescriptor; -import IR.TypeDescriptor; import IR.TypeUtil; -import IR.VarDescriptor; -import IR.Tree.DNFFlag; -import IR.Tree.DNFFlagAtom; -import IR.Tree.ExpressionNode; -import IR.Tree.FlagEffect; -import IR.Tree.FlagEffects; -import IR.Tree.FlagExpressionNode; -import IR.Tree.TagEffect; -import IR.Tree.TagExpressionList; public class BuildCodeMGC extends BuildCode { int coreNum; diff --git a/Robust/src/IR/Flat/BuildFlat.java b/Robust/src/IR/Flat/BuildFlat.java index 207dae3c..7cbb6100 100644 --- a/Robust/src/IR/Flat/BuildFlat.java +++ b/Robust/src/IR/Flat/BuildFlat.java @@ -856,7 +856,14 @@ public class BuildFlat { /* Hack - use subtree instead */ return flattenExpressionNode(nn.getExpression(),out_temp); } else if (nn.getField()!=null) { - TempDescriptor tmp=getTempforVar(nn.getVar()); + TempDescriptor tmp= null; + if(state.MGC && (nn.getClassDesc() != null)) { + // this is a static field + tmp = new TempDescriptor(nn.getClassDesc().getSymbol(), nn.getClassType()); + + } else { + tmp=getTempforVar(nn.getVar()); + } FlatFieldNode ffn=new FlatFieldNode(nn.getField(), tmp, out_temp); return new NodePair(ffn,ffn); } else { diff --git a/Robust/src/IR/SymbolTable.java b/Robust/src/IR/SymbolTable.java index 6532671f..1a1f4fd9 100644 --- a/Robust/src/IR/SymbolTable.java +++ b/Robust/src/IR/SymbolTable.java @@ -44,6 +44,13 @@ public class SymbolTable { hs=parent.getPSet(name); else hs=new HashSet(); + if(this.parentIFs != null) { + for(int i = 0; i < this.parentIFs.size(); i++) { + if(this.parentIFs.elementAt(i).contains(name)) { + hs.addAll((HashSet)(this.parentIFs.elementAt(i).getPSet(name))); + } + } + } if (table.containsKey(name)) { hs.addAll((HashSet)table.get(name)); } @@ -64,11 +71,20 @@ public class SymbolTable { public Descriptor get(String name) { Descriptor d = getFromSameScope(name); - if (d == null && parent != null) { - return parent.get(name); - } else { - return d; + if (d == null){ + if(parent != null) { + d = parent.get(name); + } + if((d == null) && (this.parentIFs != null)) { + for(int i = 0; i < this.parentIFs.size(); i++) { + d = this.parentIFs.elementAt(i).get(name); + if(d != null) { + return d; + } + } + } } + return d; } public Descriptor getFromSameScope(String name) { @@ -79,7 +95,7 @@ public class SymbolTable { return null; } - + public Enumeration getNames() { return table.keys(); } diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index e29eaa9e..7d7121e5 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -647,7 +647,7 @@ public class BuildIR { // for static field, the initializer should be considered as a // static block boolean isfirst = false; - MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().get("staticblocks"); + MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks"); if(md == null) { // the first static block for this class Modifiers m_i=new Modifiers(); @@ -995,12 +995,12 @@ public class BuildIR { // Each class maintains one MethodDecscriptor which combines all its // static blocks in their declaration order boolean isfirst = false; - MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().get("staticblocks"); + MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks"); if(md == null) { // the first static block for this class - Modifiers m=new Modifiers(); - m.addModifier(Modifiers.STATIC); - md = new MethodDescriptor(m, "staticblocks", false); + Modifiers m_i=new Modifiers(); + m_i.addModifier(Modifiers.STATIC); + md = new MethodDescriptor(m_i, "staticblocks", false); md.setAsStaticBlock(); isfirst = true; } @@ -1150,7 +1150,7 @@ public class BuildIR { } } else if (isNode(pn, "throwstatement")) { // TODO Simply return here - blockstatements.add(new ReturnNode()); + //blockstatements.add(new ReturnNode()); } else if (isNode(pn,"taskexit")) { Vector vfe=null; if (pn.getChild("flag_effects_list")!=null) @@ -1192,14 +1192,26 @@ public class BuildIR { BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild()); ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild()); BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild()); + if(condition == null) { + // no condition clause, make a 'true' expression as the condition + condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true)); + } blockstatements.add(new LoopNode(init,condition,update,body)); } else if (isNode(pn,"whilestatement")) { ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild()); BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild()); + if(condition == null) { + // no condition clause, make a 'true' expression as the condition + condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true)); + } blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP)); } else if (isNode(pn,"dowhilestatement")) { ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild()); BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild()); + if(condition == null) { + // no condition clause, make a 'true' expression as the condition + condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true)); + } blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP)); } else if (isNode(pn,"sese")) { ParseNode pnID=pn.getChild("identifier"); diff --git a/Robust/src/IR/Tree/OpNode.java b/Robust/src/IR/Tree/OpNode.java index d2a9dc81..e367665e 100644 --- a/Robust/src/IR/Tree/OpNode.java +++ b/Robust/src/IR/Tree/OpNode.java @@ -77,6 +77,10 @@ public class OpNode extends ExpressionNode { eval = Long.valueOf(l.longValue() > 0 ? 0 : 1); else if (this.op.getOp() == Operation.COMP) eval = Long.valueOf((long)(~l.longValue())); + else if (this.op.getOp() == Operation.UNARYMINUS) + eval = Long.valueOf(-l.longValue() ); + else if (this.op.getOp() == Operation.UNARYPLUS) + eval = Long.valueOf(+l.longValue()); else { Long r = this.right.evaluate(); if(r != null) { @@ -118,10 +122,6 @@ public class OpNode extends ExpressionNode { eval = Long.valueOf(l.longValue() / r.longValue()); else if (this.op.getOp() == Operation.MOD) eval = Long.valueOf(l.longValue() % r.longValue()); - else if (this.op.getOp() == Operation.UNARYPLUS) - eval = Long.valueOf(+l.longValue()); - else if (this.op.getOp() == Operation.UNARYMINUS) - eval = Long.valueOf(-l.longValue() ); else if (this.op.getOp() == Operation.ASSIGN) eval = Long.valueOf(r.longValue()); } diff --git a/Robust/src/IR/Tree/SemanticCheck.java b/Robust/src/IR/Tree/SemanticCheck.java index 69205423..8c1b5cb0 100644 --- a/Robust/src/IR/Tree/SemanticCheck.java +++ b/Robust/src/IR/Tree/SemanticCheck.java @@ -38,18 +38,18 @@ public class SemanticCheck { cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable()); cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable()); cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable()); - if(state.MGC) { - // TODO add version for normal Java later - // Link together Field, Method tables do classes inherit these from - // their ancestor interfaces - Vector sifv = cd.getSuperInterface(); - for(int i = 0; i < sifv.size(); i++) { - ClassDescriptor superif = getClass(sifv.elementAt(i)); - cd.addSuperInterfaces(superif); - cd.getFieldTable().addParentIF(superif.getFieldTable()); - cd.getMethodTable().addParentIF(superif.getMethodTable()); } - } + if(state.MGC) { + // TODO add version for normal Java later + // Link together Field, Method tables do classes inherit these from + // their ancestor interfaces + Vector sifv = cd.getSuperInterface(); + for(int i = 0; i < sifv.size(); i++) { + ClassDescriptor superif = getClass(sifv.elementAt(i)); + cd.addSuperInterfaces(superif); + cd.getFieldTable().addParentIF(superif.getFieldTable()); + cd.getMethodTable().addParentIF(superif.getMethodTable()); + } } /* Check to see that fields are well typed */ @@ -117,6 +117,10 @@ public class SemanticCheck { return; /* Done */ else if (td.isClass()) { String name=td.toString(); + int index = name.lastIndexOf('.'); + if(index != -1) { + name = name.substring(index+1); + } ClassDescriptor field_cd=getClass(name); if (field_cd==null) throw new Error("Undefined class "+name); @@ -394,8 +398,11 @@ public class SemanticCheck { void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) { if (loopstack.empty()) throw new Error("continue/break outside of loop"); - LoopNode ln=(LoopNode)loopstack.peek(); - cbn.setLoop(ln); + Object o = loopstack.peek(); + if(o instanceof LoopNode) { + LoopNode ln=(LoopNode)o; + cbn.setLoop(ln); + } } void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) { @@ -455,7 +462,9 @@ public class SemanticCheck { if(defaultb > 1) { throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol()); } else { + loopstack.push(sbn); checkBlockNode(md, nametable, sbn.getSwitchBlockStatement()); + loopstack.pop(); return (defaultb > 0); } } @@ -469,6 +478,14 @@ public class SemanticCheck { case Kind.LiteralNode: checkLiteralNode(md,nametable,(LiteralNode)en,td); return; + + case Kind.NameNode: + checkNameNode(md,nametable,(NameNode)en,td); + return; + + case Kind.OpNode: + checkOpNode(md, nametable, (OpNode)en, td); + return; } throw new Error(); } @@ -587,6 +604,7 @@ public class SemanticCheck { if(state.MGC) { // TODO add version for normal Java later if(ltd.isStatic()) { + // the field access is using a class name directly if(ltd.getClassDesc().isEnum()) { int value = ltd.getClassDesc().getEnumConstant(fieldname); if(-1 == value) { @@ -699,6 +717,11 @@ public class SemanticCheck { checkExpressionNode(md,nametable,en,td); } else { String varname=nd.toString(); + if(varname.equals("this")) { + // "this" + nn.setVar((VarDescriptor)nametable.get("this")); + return; + } Descriptor d=(Descriptor)nametable.get(varname); if (d==null) { if(state.MGC) { @@ -729,6 +752,21 @@ public class SemanticCheck { return; } } else { + // check if the var is a static field of the class + if(md instanceof MethodDescriptor) { + cd = ((MethodDescriptor)md).getClassDesc(); + FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname); + if((fd != null) && (fd.isStatic())) { + nn.setField(fd); + nn.setClassDesc(cd); + if (td!=null) + if (!typeutil.isSuperorType(td,nn.getType())) + throw new Error("Field node returns "+nn.getType()+", but need "+td); + return; + } else if(fd != null) { + throw new Error("Name "+varname+" should not be used in " + md); + } + } cd=getClass(varname); if(cd != null) { // this is a class name @@ -853,6 +891,7 @@ public class SemanticCheck { } if(out_type != null) { out_type = out_type.makeArray(state); + out_type.setStatic(); } ain.setType(out_type); } @@ -901,20 +940,37 @@ public class SemanticCheck { if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) { TypeDescriptor dt = an.getDest().getType(); TypeDescriptor st = an.getSrc().getType(); - Long l = an.getSrc().evaluate(); - if((st.isByte() || st.isShort() || st.isChar() || st.isInt()) - && (l != null) - && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) { - long lnvalue = l.longValue(); - if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128))) - || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768))) - || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0))) - || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648))) - || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) { - throw new Error("Field node returns "+st+", but need "+dt+" in "+md); + if(an.getSrc().kind() == Kind.ArrayInitializerNode) { + if(dt.getArrayCount() != st.getArrayCount()) { + throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0)); + } else { + do { + dt = dt.dereference(); + st = st.dereference(); + } while(dt.isArray()); + if((st.isByte() || st.isShort() || st.isChar() || st.isInt()) + && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) { + return; + } else { + throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0)); + } } } else { - throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0)); + Long l = an.getSrc().evaluate(); + if((st.isByte() || st.isShort() || st.isChar() || st.isInt()) + && (l != null) + && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) { + long lnvalue = l.longValue(); + if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128))) + || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768))) + || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0))) + || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648))) + || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) { + throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0)); + } + } else { + throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0)); + } } } } @@ -1073,6 +1129,12 @@ NextMethod: /*Typecheck subexpressions and get types for expressions*/ + boolean isstatic = false; + if(state.MGC) { + if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) { + isstatic = true; + } + } TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()]; for(int i=0; i