X-Git-Url: http://plrg.eecs.uci.edu/git/?p=repair.git;a=blobdiff_plain;f=Repair%2FRepairCompiler%2FMCC%2FIR%2FDotExpr.java;h=54133026fdc63f38a933a044e3796cea27d1f3db;hp=fbd217d5cbcc20d4bc100e9589a1019cf4c19be8;hb=87862c69c1cb47c83a858f0b6e52d9c0bc25913f;hpb=1c526e586ae3716fc1ada61e7af588c1dbff44e6 diff --git a/Repair/RepairCompiler/MCC/IR/DotExpr.java b/Repair/RepairCompiler/MCC/IR/DotExpr.java index fbd217d..5413302 100755 --- a/Repair/RepairCompiler/MCC/IR/DotExpr.java +++ b/Repair/RepairCompiler/MCC/IR/DotExpr.java @@ -1,30 +1,86 @@ package MCC.IR; import java.util.*; +import MCC.Compiler; public class DotExpr extends Expr { - + Expr left; String field; Expr index; - - static boolean DOMEMCHECKS=true; + + static boolean DOMEMCHECKS=false; static boolean DOTYPECHECKS=false; static boolean DONULL=false; - public void findmatch(Descriptor d, Set s) { - if (d==fd) + + public DotExpr(Expr left, String field, Expr index) { + this.left = left; + this.field = field; + this.index = index; + } + + public boolean isInvariant(Set vars) { + if (!left.isInvariant(vars)) + return false; + if (intindex!=null) + return intindex.isInvariant(vars); + else + return true; + } + + public Set findInvariants(Set vars) { + if (isInvariant(vars)) { + Set s=new HashSet(); s.add(this); - left.findmatch(d,s); - if (index!=null) - index.findmatch(d,s); + return s; + } else { + Set ls=left.findInvariants(vars); + if (intindex!=null) { + ls.addAll(intindex.findInvariants(vars)); + Expr indexbound=((ArrayDescriptor)this.fd).getIndexBound(); + ls.addAll(indexbound.findInvariants(vars)); + if ((!(intindex instanceof IntegerLiteralExpr))|| + ((IntegerLiteralExpr) intindex).getValue() != 0) { + FieldDescriptor fd=this.fd; + if (fd instanceof ArrayDescriptor) + fd=((ArrayDescriptor)fd).getField(); + Expr basesize = fd.getBaseSizeExpr(); + ls.addAll(basesize.findInvariants(vars)); + } + } + return ls; + } + } + + + public boolean isSafe() { + if (!left.isSafe()) + return false; + + FieldDescriptor tmpfd=fd; + + if (tmpfd.getPtr()) // Pointers cound be invalid + return false; + + if (tmpfd instanceof ArrayDescriptor) { + Expr arrayindex=((ArrayDescriptor)tmpfd).getIndexBound(); + if (index instanceof IntegerLiteralExpr&&arrayindex instanceof IntegerLiteralExpr) { + int indexvalue=((IntegerLiteralExpr)index).getValue(); + int arrayindexvalue=((IntegerLiteralExpr)arrayindex).getValue(); + if (indexvalue>=0&&indexvalue=0 &&"+indexvd.getSafeSymbol()+"<"+indexboundvd.getSafeSymbol()+")"); + writer.startblock(); + VarExpr indexve=new VarExpr(indexvd); + offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, indexve)); + + performedboundscheck=true; + } else + offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, intindex)); } } @@ -148,128 +254,97 @@ public class DotExpr extends Expr { public IRErrorReporter getErrorReporter() { throw new IRException("badness"); } public SymbolTable getSymbolTable() { return st; } }); - + if (td2 == null) { throw new IRException(); } else if (td2 != ReservedTypeDescriptor.INT) { throw new IRException(); } - + boolean dotypecheck = false; - if (offsetbits instanceof IntegerLiteralExpr) { - int offsetinbits = ((IntegerLiteralExpr) offsetbits).getValue(); - int offset = offsetinbits >> 3; /* offset in bytes */ - - if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) { - int shift = offsetinbits - (offset << 3); - int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue()); - - /* type var = ((*(int *) (base + offset)) >> shift) & mask */ - writer.outputline(getType().getGenerateType() + " "+dest.getSafeSymbol()+"=0;"); - writer.outputline("if ("+leftd.getSafeSymbol()+")"); - writer.outputline(dest.getSafeSymbol() + " = ((*(int *)" + - "(" + leftd.getSafeSymbol() + " + " + offset + ")) " + - " >> " + shift + ") & 0x" + Integer.toHexString(mask) + ";"); - writer.outputline("else maybe=1;"); - } else { /* a structure address or a ptr! */ - String ptr = fd.getPtr() ? "*(int *)" : ""; - /* type var = [*(int *)] (base + offset) */ - writer.outputline("int " + dest.getSafeSymbol()+"=0;"); - writer.outputline("if ("+leftd.getSafeSymbol()+")"); + VarDescriptor ob = VarDescriptor.makeNew("offsetinbits"); + writer.output("/* " + ob.getSafeSymbol() + " <-- "); + offsetbits.prettyPrint(writer); + writer.outputline("*/"); + offsetbits.generate(writer, ob); + writer.output("/* " + ob.getSafeSymbol() + " = "); + offsetbits.prettyPrint(writer); + writer.outputline("*/"); + + /* derive offset in bytes */ + VarDescriptor offset = VarDescriptor.makeNew("offset"); + writer.addDeclaration("int", offset.getSafeSymbol()); + writer.outputline(offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;"); + + if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) { + VarDescriptor shift = VarDescriptor.makeNew("shift"); + writer.addDeclaration("int", shift.getSafeSymbol()); + writer.outputline(shift.getSafeSymbol() + " = " + ob.getSafeSymbol() + + " - (" + offset.getSafeSymbol() + " << 3);"); + int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue()); + + /* type var = ((*(int *) (base + offset)) >> shift) & mask */ + writer.outputline("if ("+leftd.getSafeSymbol()+")"); + writer.outputline(dest.getSafeSymbol() + " = ((*(int *)" + + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")) " + + " >> " + shift.getSafeSymbol() + ") & 0x" + Integer.toHexString(mask) + ";"); + writer.outputline("else maybe=1;"); + } else { /* a structure address or a ptr */ + String ptr = fd.getPtr() ? "*(int *)" : ""; + /* type var = [*(int *)] (base + offset) */ + writer.outputline("if ("+leftd.getSafeSymbol()+")"); + writer.startblock(); + writer.outputline(dest.getSafeSymbol() + + " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ");"); + if (fd.getPtr()) { + writer.outputline("if ("+dest.getSafeSymbol()+")"); writer.startblock(); - writer.outputline(dest.getSafeSymbol() + - " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset + ");"); - if (fd.getPtr()) { - writer.outputline("if ("+dest.getSafeSymbol()+")"); - writer.startblock(); - VarDescriptor typevar=VarDescriptor.makeNew("typechecks"); - if (DOMEMCHECKS&&(!DOTYPECHECKS)) { - writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); - dotypecheck = true; - } else if (DOTYPECHECKS) { - writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); - } - writer.outputline("if (!"+typevar.getSafeSymbol()+")"); - writer.startblock(); - writer.outputline(dest.getSafeSymbol()+"=0;"); - if (DONULL) - writer.outputline(ptr + "(" + leftd.getSafeSymbol() + " + " + offset + ")=0;"); - writer.endblock(); - writer.endblock(); + VarDescriptor typevar=VarDescriptor.makeNew("typechecks"); + if (DOMEMCHECKS&&(!DOTYPECHECKS)) { + writer.addDeclaration("bool", typevar.getSafeSymbol()); + writer.outputline(typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); + dotypecheck = true; + } else if (DOTYPECHECKS) { + writer.addDeclaration("bool", typevar.getSafeSymbol()); + writer.outputline(typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); } - writer.endblock(); - writer.outputline("else maybe=1;"); - } - } else { /* offset in bits is an expression that must be generated */ - VarDescriptor ob = VarDescriptor.makeNew("offsetinbits"); - writer.output("// " + ob.getSafeSymbol() + " <-- "); - offsetbits.prettyPrint(writer); - writer.outputline(""); - offsetbits.generate(writer, ob); - writer.output("// " + ob.getSafeSymbol() + " = "); - offsetbits.prettyPrint(writer); - writer.outputline(""); - - /* derive offset in bytes */ - VarDescriptor offset = VarDescriptor.makeNew("offset"); - writer.outputline("int " + offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;"); - - if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) { - VarDescriptor shift = VarDescriptor.makeNew("shift"); - writer.outputline("int " + shift.getSafeSymbol() + " = " + ob.getSafeSymbol() + - " - (" + offset.getSafeSymbol() + " << 3);"); - int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue()); - - /* type var = ((*(int *) (base + offset)) >> shift) & mask */ - writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol()+"=0;"); - writer.outputline("if ("+leftd.getSafeSymbol()+")"); - writer.outputline(dest.getSafeSymbol() + - " = ((*(int *)" + - "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")) " + - " >> " + shift.getSafeSymbol() + ") & 0x" + Integer.toHexString(mask) + ";"); - writer.outputline("else maybe=1;"); - } else { /* a structure address or a ptr */ - String ptr = fd.getPtr() ? "*(int *)" : ""; - /* type var = [*(int *)] (base + offset) */ - writer.outputline("int " + dest.getSafeSymbol() +"=0;"); - writer.outputline("if ("+leftd.getSafeSymbol()+")"); - writer.startblock(); - writer.outputline(dest.getSafeSymbol() + - " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ");"); - if (fd.getPtr()) { - writer.outputline("if ("+dest.getSafeSymbol()+")"); - writer.startblock(); - VarDescriptor typevar=VarDescriptor.makeNew("typechecks"); - if (DOMEMCHECKS&&(!DOTYPECHECKS)) { - writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); - dotypecheck = true; - } else if (DOTYPECHECKS) { - writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); - } + + if (DOTYPECHECKS||DOMEMCHECKS) { writer.outputline("if (!"+typevar.getSafeSymbol()+")"); writer.startblock(); writer.outputline(dest.getSafeSymbol()+"=0;"); if (DONULL) writer.outputline(ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")=0;"); writer.endblock(); - writer.endblock(); } + writer.endblock(); - writer.outputline("else maybe=1;"); - } + } + writer.endblock(); + writer.outputline("else maybe=1;"); } + if (performedboundscheck) { + writer.endblock(); + writer.outputline(" else "); + writer.startblock(); + writer.outputline(dest.getSafeSymbol()+"=0;"); + writer.outputline("maybe=1;"); + if (!Compiler.REPAIR) + writer.outputline("printf(\"Array Index Out of Bounds\");"); + writer.endblock(); + } } private int bitmask(int bits) { int mask = 0; - + for (int i = 0; i < bits; i++) { mask <<= 1; mask += 1; } - return mask; + return mask; } public void prettyPrint(PrettyPrinter pp) { @@ -282,13 +357,20 @@ public class DotExpr extends Expr { } } - public boolean isValue() { + public boolean isValue(TypeDescriptor td) { FieldDescriptor tmpfd=fd; if (tmpfd instanceof ArrayDescriptor) tmpfd=((ArrayDescriptor)tmpfd).getField(); return (tmpfd.getPtr()||(tmpfd.getType() instanceof ReservedTypeDescriptor)); } + public boolean isPtr() { + FieldDescriptor tmpfd=fd; + if (tmpfd instanceof ArrayDescriptor) + tmpfd=((ArrayDescriptor)tmpfd).getField(); + return tmpfd.getPtr(); + } + boolean typechecked=false; public TypeDescriptor typecheck(SemanticAnalyzer sa) { if (typechecked) @@ -296,10 +378,10 @@ public class DotExpr extends Expr { else typechecked=true; TypeDescriptor lefttype = left.typecheck(sa); TypeDescriptor indextype = index == null ? null : index.typecheck(sa); - + { /* finished typechecking...so we can fill the fields in */ - StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType(); + StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType(); FieldDescriptor fd = struct.getField(field); LabelDescriptor ld = struct.getLabel(field); if (ld != null) { /* label */ @@ -310,10 +392,15 @@ public class DotExpr extends Expr { assert intindex == null; intindex = ld.getIndex(); } else { + if (fd==null) { + throw new Error("Null fd for: "+field); + } fieldtype = fd.getType(); intindex=index; } this.fd=fd; + if (fieldtype instanceof MissingTypeDescriptor) + throw new Error(fieldtype.getSymbol()+" type undefined!"); } if ((lefttype == null) || (index != null && indextype == null)) { @@ -327,7 +414,7 @@ public class DotExpr extends Expr { } } - if (lefttype instanceof StructureTypeDescriptor) { + if (lefttype instanceof StructureTypeDescriptor) { StructureTypeDescriptor struct = (StructureTypeDescriptor) lefttype; FieldDescriptor fd = struct.getField(field); LabelDescriptor ld = struct.getLabel(field); @@ -337,21 +424,21 @@ public class DotExpr extends Expr { if (indextype == null && fd instanceof ArrayDescriptor) { sa.getErrorReporter().report(null, "Must specify an index what accessing array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'"); - return null; + return null; } else if (indextype != null && !(fd instanceof ArrayDescriptor)) { sa.getErrorReporter().report(null, "Cannot specify an index when accessing non-array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'"); return null; } - + this.td = fd.getType(); } else if (ld != null) { /* label */ assert fd == null; - if (index != null) { + if (index != null) { sa.getErrorReporter().report(null, "A label cannot be accessed as an array"); return null; } - + this.td = ld.getType(); } else { sa.getErrorReporter().report(null, "No such field or label '" + field + "' in structure '" + struct.getSymbol() + "'"); @@ -372,4 +459,3 @@ public class DotExpr extends Expr { } } } -