import java.io.*;
import java.util.*;
import MCC.State;
+import MCC.Compiler;
public class RepairGenerator {
State state;
removed=t.removedset;
togenerate=new HashSet();
togenerate.addAll(termination.conjunctions);
- togenerate.removeAll(removed);
+ if (Compiler.REPAIR)
+ togenerate.removeAll(removed);
GraphNode.computeclosure(togenerate,removed);
cost=new Cost();
sources=new Sources(state);
String strepairtable="repairtable";
String stleft="left";
String stright="right";
+ String stnew="newvalue";
private void generate_updates() {
int count=0;
CodeWriter cr = new StandardCodeWriter(outputaux) {
public SymbolTable getSymbolTable() { return st; }
};
- un.generate(cr, false, stleft,stright,this);
+ un.generate(cr, false, false, stleft,stright, null,this);
craux.outputline("if (maybe) printf(\"REALLY BAD\");");
craux.endblock();
break;
- case MultUpdateNode.REMOVE:
+ case MultUpdateNode.REMOVE: {
Rule r=un.getRule();
String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
for(int j=0;j<r.numQuantifiers();j++) {
CodeWriter cr2 = new StandardCodeWriter(outputaux) {
public SymbolTable getSymbolTable() { return st2; }
};
- un.generate(cr2, true, null,null,this);
+ un.generate(cr2, true, false, null,null, null,this);
craux.outputline("if (maybe) printf(\"REALLY BAD\");");
craux.endblock();
+ }
+ break;
+ case MultUpdateNode.MODIFY: {
+ Rule r=un.getRule();
+ String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
+ for(int j=0;j<r.numQuantifiers();j++) {
+ Quantifier q=r.getQuantifier(j);
+ if (q instanceof SetQuantifier) {
+ SetQuantifier sq=(SetQuantifier) q;
+ methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
+ } else if (q instanceof RelationQuantifier) {
+ RelationQuantifier rq=(RelationQuantifier) q;
+
+ methodcall+=", "+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
+ methodcall+=", "+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
+ } else if (q instanceof ForQuantifier) {
+ ForQuantifier fq=(ForQuantifier) q;
+ methodcall+=", int "+fq.getVar().getSafeSymbol();
+ }
+ }
+ methodcall+=", "+stleft+", "+stright+", "+stnew;
+ methodcall+=")";
+ crhead.outputline(methodcall+";");
+ craux.outputline(methodcall);
+ craux.startblock();
+ craux.outputline("int maybe=0;");
+ final SymbolTable st2 = un.getRule().getSymbolTable();
+ CodeWriter cr2 = new StandardCodeWriter(outputaux) {
+ public SymbolTable getSymbolTable() { return st2; }
+ };
+ un.generate(cr2, false, true, stleft, stright, stnew, this);
+ craux.outputline("if (maybe) printf(\"REALLY BAD\");");
+ craux.endblock();
+ }
break;
- case MultUpdateNode.MODIFY:
+
default:
throw new Error("Nonimplement Update");
}
rule.getGuardExpr().prettyPrint(cr);
cr.outputline("");
-
+
/* now we have to generate the guard test */
-
+
VarDescriptor guardval = VarDescriptor.makeNew();
rule.getGuardExpr().generate(cr, guardval);
cr.outputline("if (" + guardval.getSafeSymbol() + ")");
cr.startblock();
-
+
/* now we have to generate the inclusion code */
currentrule=rule;
rule.getInclusion().generate(cr);
for (int j=0;j<rule.numQuantifiers();j++) {
cr.endblock();
}
-
+
// close startblocks generated by DotExpr memory checks
//DotExpr.generate_memory_endblocks(cr);
-
+
cr.endblock(); // end else-if WORKLIST ladder
elseladder = "else if";
Vector constraints = state.vConstraints;
for (int i = 0; i < constraints.size(); i++) {
-
Constraint constraint = (Constraint) constraints.elementAt(i);
-
+
{
-
- final SymbolTable st = constraint.getSymbolTable();
-
- CodeWriter cr = new StandardCodeWriter(outputaux) {
+ final SymbolTable st = constraint.getSymbolTable();
+ CodeWriter cr = new StandardCodeWriter(outputaux) {
public SymbolTable getSymbolTable() { return st; }
};
-
- cr.outputline("// checking " + escape(constraint.toString()));
+ cr.outputline("// checking " + escape(constraint.toString()));
cr.startblock();
ListIterator quantifiers = constraint.quantifiers();
cr.outputline("if (maybe)");
cr.startblock();
- cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \");");
+ cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \\n\");");
cr.outputline("exit(1);");
cr.endblock();
cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
cr.startblock();
- if (DEBUG)
- cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \");");
-
+ if (!Compiler.REPAIR)
+ cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
+ else {
/* Do repairs */
/* Build new repair table */
cr.outputline("if ("+repairtable.getSafeSymbol()+")");
cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
cr.outputline("goto rebuild;"); /* Rebuild model and all */
-
+ }
cr.endblock();
-
+
while (quantifiers.hasPrevious()) {
Quantifier quantifier = (Quantifier) quantifiers.previous();
cr.endblock();
return mun;
}
- public void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
+ /** Generates abstract (and concrete) repair for a comparison */
+
+ private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
+ MultUpdateNode munmodify=getmultupdatenode(conj,dpred,AbstractRepair.MODIFYRELATION);
MultUpdateNode munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
MultUpdateNode munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
}
}
/* Do concrete repairs */
- /* Start with scheduling removal */
- for(int i=0;i<state.vRules.size();i++) {
- Rule r=(Rule)state.vRules.get(i);
- if (r.getInclusion().getTargetDescriptors().contains(rd)) {
- for(int j=0;j<munremove.numUpdates();j++) {
- UpdateNode un=munremove.getUpdate(i);
- if (un.getRule()==r) {
- /* Update for rule r */
- String name=(String)updatenames.get(un);
- cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
+ if (munmodify!=null) {
+ for(int i=0;i<state.vRules.size();i++) {
+ Rule r=(Rule)state.vRules.get(i);
+ if (r.getInclusion().getTargetDescriptors().contains(rd)) {
+ for(int j=0;j<munmodify.numUpdates();j++) {
+ UpdateNode un=munmodify.getUpdate(i);
+ if (un.getRule()==r) {
+ /* Update for rule r */
+ String name=(String)updatenames.get(un);
+ cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
+ }
}
}
}
- }
- /* Now do addition */
- UpdateNode un=munadd.getUpdate(0);
- String name=(String)updatenames.get(un);
- if (!inverted) {
- cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
+
} else {
- cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
+ /* Start with scheduling removal */
+ for(int i=0;i<state.vRules.size();i++) {
+ Rule r=(Rule)state.vRules.get(i);
+ if (r.getInclusion().getTargetDescriptors().contains(rd)) {
+ for(int j=0;j<munremove.numUpdates();j++) {
+ UpdateNode un=munremove.getUpdate(i);
+ if (un.getRule()==r) {
+ /* Update for rule r */
+ String name=(String)updatenames.get(un);
+ cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
+ }
+ }
+ }
+ }
+ /* Now do addition */
+ UpdateNode un=munadd.getUpdate(0);
+ String name=(String)updatenames.get(un);
+ if (!inverted) {
+ cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
+ } else {
+ cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
+ }
}
}
cr.outputline(repairtable.getSafeSymbol()+"->containsrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
cr.startblock(); {
/* Have update to call into */
- VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
+ VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
+ cr.outputline("int "+mdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->getrelation2("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
+
String parttype="";
for(int i=0;i<currentrule.numQuantifiers();i++) {
if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
else
parttype=parttype+", int";
}
- cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")=");
- cr.outputline("(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
+ VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
+ VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
+
String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
for(int i=0;i<currentrule.numQuantifiers();i++) {
Quantifier q=currentrule.getQuantifier(i);
methodcall+=","+fq.getVar().getSafeSymbol();
}
}
- methodcall+=");";
- cr.outputline(methodcall);
+
+
+
+ cr.outputline("void *"+tmpptr.getSafeSymbol()+"=");
+ cr.outputline("(void *) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
+ cr.outputline("if ("+mdfyptr.getSafeSymbol()+")");
+ {
+ cr.startblock();
+ cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+"int,int,int)="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+"int,int,int)) "+tmpptr.getSafeSymbol());
+ cr.outputline(methodcall+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
+ cr.endblock();
+ }
+ cr.outputline("else ");
+ {
+ cr.startblock();
+ cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol());
+ cr.outputline(methodcall+");");
+ cr.endblock();
+ }
cr.outputline("goto rebuild;");
}
cr.endblock();
import java.util.*;
import java.io.*;
import MCC.State;
+import MCC.Compiler;
public class Termination {
HashSet conjunctions;
abstractadd=new Hashtable();
abstractremove=new Hashtable();
conjtonodemap=new Hashtable();
+ if (!Compiler.REPAIR)
+ return;
generateconjunctionnodes();
generatescopenodes();
if (possiblerules.size()==0)
return;
int[] count=new int[possiblerules.size()];
- while(remains(count,possiblerules)) {
+ while(remains(count,possiblerules,true)) {
MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.REMOVE);
TermNode tn=new TermNode(mun);
GraphNode gn2=new GraphNode("UpdateRem"+removefromcount,tn);
gn.addEdge(e);
updatenodes.add(gn2);
}
- increment(count,possiblerules);
+ increment(count,possiblerules,true);
}
}
- static void increment(int count[], Vector rules) {
+ static void increment(int count[], Vector rules,boolean isremove) {
count[0]++;
for(int i=0;i<(rules.size()-1);i++) {
- if (count[i]>=(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size()))) {
+ int num=isremove?(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size())):((Rule)rules.get(i)).getDNFGuardExpr().size();
+ if (count[i]>=num) {
count[i+1]++;
count[i]=0;
} else break;
}
}
- static boolean remains(int count[], Vector rules) {
+ static boolean remains(int count[], Vector rules, boolean isremove) {
for(int i=0;i<rules.size();i++) {
- if (count[i]>=(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size()))) {
+ int num=isremove?(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size())):((Rule)rules.get(i)).getDNFGuardExpr().size();
+ if (count[i]>=num) {
return false;
}
}
return true;
}
+
/** This method generates data structure updates to implement the
* abstract atomic modification specified by ar. */
-
+ int modifycount=0;
void generatemodifyrelation(GraphNode gn, AbstractRepair ar) {
-
+ RelationDescriptor rd=(RelationDescriptor)ar.getDescriptor();
+ ExprPredicate exprPredicate=(ExprPredicate)ar.getPredicate().getPredicate();
+ boolean inverted=exprPredicate.inverted();
+ int leftindex=0;
+ int rightindex=1;
+ if (inverted)
+ leftindex=2;
+ else
+ rightindex=2;
+
+
+ Vector possiblerules=new Vector();
+ for(int i=0;i<state.vRules.size();i++) {
+ Rule r=(Rule) state.vRules.get(i);
+ if ((r.getInclusion() instanceof RelationInclusion)&&
+ (rd==((RelationInclusion)r.getInclusion()).getRelation()))
+ possiblerules.add(r);
+ }
+ if (possiblerules.size()==0)
+ return;
+ int[] count=new int[possiblerules.size()];
+ while(remains(count,possiblerules,false)) {
+ MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.MODIFY);
+ TermNode tn=new TermNode(mun);
+ GraphNode gn2=new GraphNode("UpdateMod"+removefromcount,tn);
+
+ boolean goodflag=true;
+ for(int i=0;i<possiblerules.size();i++) {
+ Rule r=(Rule)possiblerules.get(i);
+ UpdateNode un=new UpdateNode(r);
+ /* No Need to construct bindings on modify */
+
+ int c=count[i];
+ if (!processconjunction(un,r.getDNFGuardExpr().get(c))) {
+ goodflag=false;break;
+ }
+ RelationInclusion ri=(RelationInclusion)r.getInclusion();
+ if (!(ri.getLeftExpr() instanceof VarExpr)) {
+ Updates up=new Updates(ri.getLeftExpr(),leftindex);
+ un.addUpdate(up);
+ } else {
+ VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
+ if (vd.isGlobal()) {
+ Updates up=new Updates(ri.getLeftExpr(),leftindex);
+ un.addUpdate(up);
+ } else if (inverted)
+ goodflag=false;
+ }
+ if (!(ri.getRightExpr() instanceof VarExpr)) {
+ Updates up=new Updates(ri.getRightExpr(),rightindex);
+ un.addUpdate(up);
+ } else {
+ VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
+ if (vd.isGlobal()) {
+ Updates up=new Updates(ri.getRightExpr(),rightindex);
+ un.addUpdate(up);
+ } else if (!inverted)
+ goodflag=false;
+ }
+
+ if (!un.checkupdates()) {
+ goodflag=false;
+ break;
+ }
+ mun.addUpdate(un);
+ }
+ if (goodflag) {
+ GraphNode.Edge e=new GraphNode.Edge("abstract"+modifycount,gn2);
+ modifycount++;
+ gn.addEdge(e);
+ updatenodes.add(gn2);
+ }
+ increment(count,possiblerules,false);
+ }
}
}
} else {
System.out.println(e.getClass().getName());
- throw new Error("Error #213");
+ throw new Error("Unrecognized Expr");
}
}
return okay;
// ************************************************************
-RepairHashNode::RepairHashNode(int setrelation, int rule, int lvalue, int rvalue, int data){
+RepairHashNode::RepairHashNode(int setrelation, int rule, int lvalue, int rvalue, int data, int data2){
this->setrelation = setrelation;
this->lvalue=lvalue;
this->rvalue=rvalue;
this->data = data;
+ this->data2 = data2;
this->next = 0;
this->lnext=0;
this->rule=rule;
}
int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data) {
+ return addrelation(relation,rule,lvalue,rvalue,data, 0);
+}
+
+int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data, int data2) {
unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
RepairHashNode **ptr = &bucket[hashkey];
(*ptr)->rule==rule &&
(*ptr)->lvalue==lvalue &&
(*ptr)->rvalue==rvalue &&
- (*ptr)->data == data) {
+ (*ptr)->data == data &&
+ (*ptr)->data2 == data2) {
return 0;
}
ptr = &((*ptr)->next);
}
- *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data);
+ *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data,data2);
(*ptr)->lnext=nodelist;
nodelist=*ptr;
numelements++;
return getrelation(setv||SETFLAG, rule, value,0);
}
+int RepairHash::getrelation2(int relation, int rule, int lvalue,int rvalue) {
+ unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
+
+ RepairHashNode **ptr = &bucket[hashkey];
+
+ /* check that this key/object pair isn't already here */
+ // TBD can be optimized for set v. relation */
+ while (*ptr) {
+ if ((*ptr)->setrelation == relation &&
+ (*ptr)->rule==rule &&
+ (*ptr)->lvalue==lvalue &&
+ (*ptr)->rvalue==rvalue) {
+ return (*ptr)->data2;
+ }
+ ptr = &((*ptr)->next);
+ }
+ return 0;
+}
int RepairHash::getrelation(int relation, int rule, int lvalue,int rvalue) {
unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;