X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=Repair%2FRepairCompiler%2FMCC%2FIR%2FNaiveGenerator.java;h=08ed84d6325ddd44a83d3a90faed431f3d758cf5;hb=75670c5c42cf7f4ec4a0d681db1200317ec29284;hp=d4af8e591e1f5db43da310b041846645300bc8df;hpb=cbe945a51aa2e383c3ab5d14024a4a2edea4c84a;p=repair.git diff --git a/Repair/RepairCompiler/MCC/IR/NaiveGenerator.java b/Repair/RepairCompiler/MCC/IR/NaiveGenerator.java index d4af8e5..08ed84d 100755 --- a/Repair/RepairCompiler/MCC/IR/NaiveGenerator.java +++ b/Repair/RepairCompiler/MCC/IR/NaiveGenerator.java @@ -15,7 +15,8 @@ public class NaiveGenerator { public void generate(java.io.OutputStream output) { this.output = new java.io.PrintWriter(output, true); - + + generate_tokentable(); generate_hashtables(); generate_rules(); generate_implicit_checks(); @@ -23,26 +24,28 @@ public class NaiveGenerator { } + private void generate_tokentable() { + + CodeWriter cr = new StandardCodeWriter(output); + Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator(); + + cr.outputline(""); + cr.outputline("// Token values"); + cr.outputline(""); + + while (tokens.hasNext()) { + Object token = tokens.next(); + cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString()); + } + + cr.outputline(""); + cr.outputline(""); + } + private void generate_hashtables() { - CodeWriter cr = new CodeWriter() { - - int indent = 0; - public void indent() { indent++; } - public void unindent() { indent--; assert indent >= 0; } - private void doindent() { - for (int i = 0; i < indent; i++) { - output.print(" "); - } - } - public void outputline(String s) { - doindent(); - output.println(s); - } - public void output(String s) { throw new IRException(); } - public SymbolTable getSymbolTable() { throw new IRException(); } - }; - + CodeWriter cr = new StandardCodeWriter(output); + cr.outputline("int __Success = 1;\n"); cr.outputline("// creating hashtables "); /* build all the hashtables */ @@ -56,7 +59,7 @@ public class NaiveGenerator { SetDescriptor set = (SetDescriptor) sets.next(); cr.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash = new SimpleHash();"); } - + /* second pass build relationships between hashtables */ sets = state.stSets.descriptors(); @@ -76,12 +79,18 @@ public class NaiveGenerator { /* first pass create all the hash tables */ while (relations.hasNext()) { RelationDescriptor relation = (RelationDescriptor) relations.next(); - cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash = new SimpleHash();"); - } + + if (relation.testUsage(RelationDescriptor.IMAGE)) { + cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash = new SimpleHash();"); + } + + if (relation.testUsage(RelationDescriptor.INVIMAGE)) { + cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv = new SimpleHash();"); + } + } cr.outputline(""); cr.outputline(""); - } private void generate_rules() { @@ -108,41 +117,18 @@ public class NaiveGenerator { GraphNode rulenode = (GraphNode) rules.next(); Rule rule = (Rule) rulenode.getOwner(); + if (!state.vRules.contains(rule)) { + // this is no longer a top-level rule + continue; + } + { - final SymbolTable st = rule.getSymbolTable(); - - CodeWriter cr = new CodeWriter() { - boolean linestarted = false; - int indent = 0; - public void indent() { indent++; } - public void unindent() { indent--; assert indent >= 0; } - private void doindent() { - for (int i = 0; i < indent; i++) { - output.print(" "); - } - linestarted = true; - } - public void outputline(String s) { - if (!linestarted) { - doindent(); - } - output.println(s); - linestarted = false; - } - public void output(String s) { - if (!linestarted) { - doindent(); - } - output.print(s); - output.flush(); - } - public SymbolTable getSymbolTable() { return st; } - }; + CodeWriter cr = new StandardCodeWriter(output); + cr.pushSymbolTable(rule.getSymbolTable()); cr.outputline("// build " + rule.getLabel()); - cr.outputline("{"); - cr.indent(); + cr.startblock(); ListIterator quantifiers = rule.quantifiers(); @@ -161,38 +147,36 @@ public class NaiveGenerator { VarDescriptor guardval = VarDescriptor.makeNew(); rule.getGuardExpr().generate(cr, guardval); - cr.outputline("if (" + guardval.getSafeSymbol() + ") {"); - - cr.indent(); + cr.outputline("if (" + guardval.getSafeSymbol() + ")"); + cr.startblock(); /* now we have to generate the inclusion code */ rule.getInclusion().generate(cr); + cr.endblock(); - cr.unindent(); - - cr.outputline("}"); + // close startblocks generated by DotExpr memory checks + //DotExpr.generate_memory_endblocks(cr); while (quantifiers.hasPrevious()) { Quantifier quantifier = (Quantifier) quantifiers.previous(); - cr.unindent(); - cr.outputline("}"); + cr.endblock(); } - cr.unindent(); - cr.outputline("}"); + cr.endblock(); cr.outputline(""); cr.outputline(""); } } - } private void generate_implicit_checks() { /* do post checks */ - //output.println("check to make sure all relations are well typed"); + + CodeWriter cr = new StandardCodeWriter(output); + + // #TBD#: these should be implicit checks added to the set of constraints //output.println("check multiplicity"); - } private void generate_checks() { @@ -201,10 +185,62 @@ public class NaiveGenerator { Vector constraints = state.vConstraints; for (int i = 0; i < constraints.size(); i++) { - //output.println("check constraint " + (i + 1)); + + Constraint constraint = (Constraint) constraints.elementAt(i); + + { + + CodeWriter cr = new StandardCodeWriter(output); + cr.pushSymbolTable(constraint.getSymbolTable()); + + cr.outputline("// checking " + constraint.getLabel()); + cr.startblock(); + + ListIterator quantifiers = constraint.quantifiers(); + + while (quantifiers.hasNext()) { + Quantifier quantifier = (Quantifier) quantifiers.next(); + quantifier.generate_open(cr); + } + + cr.outputline("int maybe = 0;"); + + /* now we have to generate the guard test */ + + VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean"); + constraint.getLogicStatement().generate(cr, constraintboolean); + + cr.outputline("if (maybe)"); + cr.startblock(); + cr.outputline("__Success = 0;"); + cr.outputline("printf(\"maybe fail " + constraint.getNum() + ". \");"); + cr.outputline("exit(1);"); + cr.endblock(); + + cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")"); + cr.startblock(); + + cr.outputline("__Success = 0;"); + cr.outputline("printf(\"fail " + constraint.getNum() + ". \");"); + cr.outputline("exit(1);"); + cr.endblock(); + + while (quantifiers.hasPrevious()) { + Quantifier quantifier = (Quantifier) quantifiers.previous(); + cr.endblock(); + } + + cr.endblock(); + cr.outputline(""); + cr.outputline(""); + } + } - //output.println("report problems"); + output.println("//if (__Success) { printf(\"all tests passed\"); }"); } } + + +