correct
[repair.git] / Repair / RepairCompiler / MCC / IR / NaiveGenerator.java
index 9543ae0529879d1f5d17f02be3fd7db1b5e94b02..505217e4b9f8cff674e316c82c71df32df7f485d 100755 (executable)
@@ -8,14 +8,14 @@ public class NaiveGenerator {
 
     State state;
     java.io.PrintWriter output = null;
-            
+
     public NaiveGenerator(State state) {
         this.state = state;
     }
 
     public void generate(java.io.OutputStream output) {
-        this.output = new java.io.PrintWriter(output, true); 
-        
+        this.output = new java.io.PrintWriter(output, true);
+
         generate_tokentable();
         generate_hashtables();
         generate_rules();
@@ -26,33 +26,16 @@ public class NaiveGenerator {
 
     private void generate_tokentable() {
 
-        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(); }
-            };
-        
-        Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();        
+        CodeWriter cr = new StandardCodeWriter(output);
+        Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
 
         cr.outputline("");
-        cr.outputline("// Token values");
+        cr.outputline("/* Token values*/");
         cr.outputline("");
 
         while (tokens.hasNext()) {
             Object token = tokens.next();
-            cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());            
+            cr.outputline("/* " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString()+"*/");
         }
 
         cr.outputline("");
@@ -61,62 +44,50 @@ public class NaiveGenerator {
 
     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(); }
-            };
-
-        cr.outputline("int __Success = 1;");
-        
-        cr.outputline("// creating hashtables ");
-        
+        CodeWriter cr = new StandardCodeWriter(output);
+        cr.outputline("int __Success = 1;\n");
+        cr.outputline("/* creating hashtables */");
+
         /* build all the hashtables */
         Hashtable hashtables = new Hashtable();
 
         /* build sets */
         Iterator sets = state.stSets.descriptors();
-        
+
         /* first pass create all the hash tables */
         while (sets.hasNext()) {
             SetDescriptor set = (SetDescriptor) sets.next();
             cr.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash = new SimpleHash();");
-        } 
+        }
 
         /* second pass build relationships between hashtables */
         sets = state.stSets.descriptors();
-        
+
         while (sets.hasNext()) {
             SetDescriptor set = (SetDescriptor) sets.next();
             Iterator subsets = set.subsets();
-            
+
             while (subsets.hasNext()) {
-                SetDescriptor subset = (SetDescriptor) subsets.next();                
+                SetDescriptor subset = (SetDescriptor) subsets.next();
                 cr.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
             }
-        } 
+        }
 
         /* build relations */
         Iterator relations = state.stRelations.descriptors();
-        
+
         /* first pass create all the hash tables */
         while (relations.hasNext()) {
             RelationDescriptor relation = (RelationDescriptor) relations.next();
-            cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash = new SimpleHash();");
-            cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv = 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("");
@@ -135,89 +106,63 @@ public class NaiveGenerator {
                     return g2.getFinishingTime() - g1.getFinishingTime();
                 }
             });
-        
-        topologicalsort.addAll(state.rulenodes.values());        
+
+        topologicalsort.addAll(state.rulenodes.values());
 
         /* build all the rules */
         Iterator rules = topologicalsort.iterator();
-                
+
         while (rules.hasNext()) {
 
             GraphNode rulenode = (GraphNode) rules.next();
-            Rule rule = (Rule) rulenode.getOwner();            
+            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; }
-                    };
-                
-                cr.outputline("// build " + rule.getLabel());
-                cr.outputline("{");
-                cr.indent();
+                CodeWriter cr = new StandardCodeWriter(output);
+                cr.pushSymbolTable(rule.getSymbolTable());
+
+                cr.outputline("/* build " + rule.getLabel()+"*/");
+                cr.startblock();
 
                 ListIterator quantifiers = rule.quantifiers();
 
                 while (quantifiers.hasNext()) {
-                    Quantifier quantifier = (Quantifier) quantifiers.next();                   
+                    Quantifier quantifier = (Quantifier) quantifiers.next();
                     quantifier.generate_open(cr);
-                }            
-                        
+                }
+
                 /* pretty print! */
-                cr.output("//");
+                cr.output("/*");
                 rule.getGuardExpr().prettyPrint(cr);
-                cr.outputline("");
+                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.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("");
             }
@@ -226,84 +171,12 @@ public class NaiveGenerator {
 
     private void generate_implicit_checks() {
 
-        // #STOPPED# : stop... modify relationdescriptor with flag to symbolize that
-        // static analysis has determined the relation to be type safe... for sets that 
-        // are not type safe do these tests in the 
-        // relation inclusion...
-
         /* do post checks */
 
-        // check to make sure all relations are well typed
-        // for all relations (need to check reverse as well i guess) 
-        // make sure that each element is in the corresponding set for the domain and range
-        // use iterators
-        
-        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() { throw new IRException(); }
-            };
-          
-        /* build relations */
-        Iterator relations = state.stRelations.descriptors();
-
-        /* first pass create all the hash tables */
-        while (relations.hasNext()) {
-            RelationDescriptor relation = (RelationDescriptor) relations.next();
-
-            if (relation.testUsage(RelationDescriptor.IMAGE)) {
-                VarDescriptor x = VarDescriptor.makeNew("x");
-                VarDescriptor y = VarDescriptor.makeNew("y");
-
-                /* start iteration */
-                cr.outputline("for (SimpleIterator* " + x.getSafeSymbol() + "_iterator = " + relation.getSafeSymbol() + "_hash->iterator(); " + x.getSafeSymbol() + "_iterator->hasNext(); ) {");
-                cr.indent();
-                cr.outputline("int " + y.getSafeSymbol() + " = (" + x.getSafeSymbol() + "_iterator->next();");        
-                cr.outputline("int " + x.getSafeSymbol() + " = (" + x.getSafeSymbol() + "_iterator->key();");
-
-                SetDescriptor domain = relation.getDomain();
-                SetDescriptor range = relation.getRange();
-
-                // #TBD#: decide if this is bad and go back and do checks in relationinclusion.. (have flag set by static analysis which determines whether or not these tests need to be made or 
-                // not
-
-                cr.outputline("if (!domain.get(x)) { remove x from hashtable, remove current iterator }");
-                
-            } else if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
-                throw new IRException("unsupported");
-            }
-            
-           
-        } 
-
-        cr.outputline("");
-        cr.outputline("");
+        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() {
@@ -313,82 +186,58 @@ public class NaiveGenerator {
 
         for (int i = 0; i < constraints.size(); i++) {
 
-            Constraint constraint = (Constraint) constraints.elementAt(i); 
+            Constraint constraint = (Constraint) constraints.elementAt(i);
 
             {
 
-                final SymbolTable st = constraint.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; }
-                    };
-                
-                cr.outputline("// checking " + constraint.getLabel());
-                cr.outputline("{");
-                cr.indent();
+                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 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 (!" + constraintboolean.getSafeSymbol() + ") {");
-
-                cr.indent();
 
+                cr.outputline("if (maybe)");
+                cr.startblock();
                 cr.outputline("__Success = 0;");
-                cr.outputline("printf(\"fail. \");");
+                cr.outputline("printf(\"maybe fail " + constraint.getNum() + ". \");");
+                cr.outputline("exit(1);");
+                cr.endblock();
 
-                cr.unindent();
+                cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
+                cr.startblock();
 
-                cr.outputline("}");
+                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.unindent();                    
-                    cr.outputline("}");
+                    cr.endblock();
                 }
 
-                cr.unindent();
-                cr.outputline("}");
+                cr.endblock();
                 cr.outputline("");
                 cr.outputline("");
             }
-            
+
         }
 
-        output.println("if (__Success) { printf(\"all tests passed\"); }");
-    }    
+        output.println("/*if (__Success) { printf(\"all tests passed\"); }*/");
+    }
 
 }