} else return llower;
}
+
public boolean isSafe() {
if (right==null)
return left.isSafe();
return left.isSafe()&&right.isSafe();
}
+ public boolean isInvariant(Set vars) {
+ return left.isInvariant(vars)&&((right==null)||right.isInvariant(vars));
+ }
+
+ public Set findInvariants(Set vars) {
+ if (isInt(this)) {
+ /* Don't hoist ints */
+ return new HashSet();
+ } else if (isInvariant(vars)) {
+ Set s=new HashSet();
+ s.add(this);
+ return s;
+ } else {
+ Set ls=left.findInvariants(vars);
+ if (right!=null)
+ ls.addAll(right.findInvariants(vars));
+ return ls;
+ }
+ }
+
public Set getfunctions() {
Set leftfunctions=left.getfunctions();
- Set rightfunctions=right.getfunctions();
+ Set rightfunctions=null;
+ if (right!=null) rightfunctions=right.getfunctions();
if (leftfunctions!=null&&rightfunctions!=null) {
HashSet functions=new HashSet();
functions.addAll(leftfunctions);
return rightfunctions;
}
- public void findmatch(Descriptor d, Set s) {
- left.findmatch(d,s);
- if (right!=null)
- right.findmatch(d,s);
- }
-
public static boolean isInt(Expr e) {
if (e==null)
return false;
if (opcode==Opcode.RND)
return "Round("+left.name()+")";
String name=left.name()+opcode.toString();
- if (right!=null)
+ if (right!=null) {
name+=right.name();
+ name="("+name+")";
+ }
return name;
}
return opcode;
}
-
-
-
public boolean equals(Map remap, Expr e) {
if (e==null||!(e instanceof OpExpr))
return false;
return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
}
+ public void findmatch(Descriptor d, Set s) {
+ left.findmatch(d,s);
+ if (right!=null)
+ right.findmatch(d,s);
+ }
+
public Set useDescriptor(Descriptor d) {
HashSet newset=new HashSet();
newset.addAll(left.useDescriptor(d));
newset.addAll(right.useDescriptor(d));
return newset;
}
-
+
public int[] getRepairs(boolean negated, Termination t) {
if (left instanceof RelationExpr)
return new int[] {AbstractRepair.MODIFYRELATION};
AbstractRepair.REMOVEFROMRELATION};
}
} else if (op==Opcode.GE||op==Opcode.GT) {
- return new int[]{AbstractRepair.ADDTORELATION};
+ return new int[]{AbstractRepair.ADDTORELATION};
} else if (op==Opcode.LE||op==Opcode.LT) {
if ((op==Opcode.LT&&maxsize!=-1&&maxsize<size)||(op==Opcode.LE&&maxsize!=-1&&maxsize<=size))
return new int[0];
} else {
if (op==Opcode.EQ) {
if (size==0)
- return new int[] {AbstractRepair.REMOVEFROMSET};
+ return new int[] {AbstractRepair.REMOVEFROMSET};
else {
if (maxsize<=size&&maxsize!=-1)
return new int[] {AbstractRepair.ADDTOSET};
AbstractRepair.REMOVEFROMSET};
}
} else if (op==Opcode.GE||op==Opcode.GT) {
- return new int[] {AbstractRepair.ADDTOSET};
+ return new int[] {AbstractRepair.ADDTOSET};
} else if (op==Opcode.LE||op==Opcode.LT) {
if ((op==Opcode.LT&&maxsize<size&&maxsize!=-1)||(op==Opcode.LE&&maxsize<=size&&maxsize!=-1))
return new int[0];
}
throw new Error("BAD");
}
-
+
public Descriptor getDescriptor() {
return left.getDescriptor();
}
public Set getRequiredDescriptors() {
Set v = left.getRequiredDescriptors();
-
+
if (right != null) {
v.addAll(right.getRequiredDescriptors());
}
return v;
- }
+ }
public void generate(CodeWriter writer, VarDescriptor dest) {
VarDescriptor ld = VarDescriptor.makeNew("leftop");
- left.generate(writer, ld);
+ /* Check for loop invariant hoisting. */
+ if (writer.getInvariantValue()!=null&&
+ writer.getInvariantValue().isInvariant(this)) {
+ writer.addDeclaration("int",dest.getSafeSymbol());
+ writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
+ writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
+ return;
+ }
+
+ left.generate(writer, ld);
VarDescriptor rd = null;
VarDescriptor lm=VarDescriptor.makeNew("lm");
VarDescriptor rm=VarDescriptor.makeNew("rm");
if (right != null) {
if ((opcode==Opcode.OR)||
(opcode==Opcode.AND)) {
- writer.outputline("int "+lm.getSafeSymbol()+"=maybe;");
- writer.outputline("int maybe=0;");
+ writer.addDeclaration("int",lm.getSafeSymbol());
+ writer.outputline(lm.getSafeSymbol()+"=maybe;");
+ writer.outputline("maybe=0;");
}
rd = VarDescriptor.makeNew("rightop");
String code;
if (opcode == Opcode.RND) {
- writer.outputline("int " +dest.getSafeSymbol() + " = (" +
+ writer.addDeclaration("int",dest.getSafeSymbol());
+ writer.outputline(dest.getSafeSymbol() + " = (" +
ld.getSafeSymbol() + ">>3)<<3; ");
writer.outputline("if ("+ld.getSafeSymbol()+" % 8) "+dest.getSafeSymbol()+"+=8;");
} else if (opcode == Opcode.NOP) {
- writer.outputline("int " +dest.getSafeSymbol() + " = " +
+ writer.addDeclaration("int", dest.getSafeSymbol());
+ writer.outputline(dest.getSafeSymbol() + " = " +
ld.getSafeSymbol() +"; ");
} else if (opcode == Opcode.AND) {
- writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
+ writer.addDeclaration("int",rm.getSafeSymbol());
+ writer.outputline(rm.getSafeSymbol()+"=maybe;");
writer.outputline("maybe = (" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
+ writer.addDeclaration("int",dest.getSafeSymbol());
writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";");
} else if (opcode == Opcode.OR) {
- writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
+ writer.addDeclaration("int",rm.getSafeSymbol());
+ writer.outputline(rm.getSafeSymbol()+"=maybe;");
writer.outputline("maybe = (!" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (!" + rd.getSafeSymbol() +
" && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
- writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() +
- ";");
+ writer.addDeclaration("int",dest.getSafeSymbol());
+ writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() + ";");
} else if (opcode != Opcode.NOT) { /* two operands */
assert rd != null;
- writer.outputline("int " + dest.getSafeSymbol() + " = " +
- ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
+ writer.addDeclaration("int", dest.getSafeSymbol());
+ writer.outputline(dest.getSafeSymbol() + " = " +
+ ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
} else if (opcode == Opcode.NOT) {
- writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
+ writer.addDeclaration("int", dest.getSafeSymbol());
+ writer.outputline(dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
}
}
public void prettyPrint(PrettyPrinter pp) {
pp.output("(");
if (opcode == Opcode.NOT) {
- pp.output("!");
+ pp.output("!(");
left.prettyPrint(pp);
+ pp.output(")");
} else if (opcode == Opcode.NOP) {
left.prettyPrint(pp);
} else if (opcode == Opcode.RND) {
pp.output("RND ");
left.prettyPrint(pp);
- } else {
+ } else {
left.prettyPrint(pp);
pp.output(" " + opcode.toString() + " ");
assert right != null;
boolean ok = true;
- // #ATTN#: if we want node.next != literal(0) to represent a null check than we need to allow ptr arithmetic
- // either that or we use a isvalid clause to check for null
-
- /*
- if (lt != ReservedTypeDescriptor.INT) {
- sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
- ok = false;
- }
-
- if (right != null) {
- if (rt != ReservedTypeDescriptor.INT) {
- sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
- ok = false;
- }
- }
- */
-
if (!ok) {
return null;
}