7 public class WorklistGenerator {
10 java.io.PrintWriter output = null;
12 public WorklistGenerator(State state) {
16 public void generate(java.io.OutputStream output) {
17 this.output = new java.io.PrintWriter(output, true);
19 generate_tokentable();
20 generate_hashtables();
23 generate_implicit_checks();
29 private void generate_tokentable() {
31 CodeWriter cr = new StandardCodeWriter(output);
32 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
35 cr.outputline("// Token values");
38 while (tokens.hasNext()) {
39 Object token = tokens.next();
40 cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());
47 private void generate_hashtables() {
49 CodeWriter cr = new StandardCodeWriter(output);
50 cr.outputline("int __Success = 1;\n");
51 cr.outputline("// creating hashtables ");
53 /* build all the hashtables */
54 Hashtable hashtables = new Hashtable();
57 Iterator sets = state.stSets.descriptors();
59 /* first pass create all the hash tables */
60 while (sets.hasNext()) {
61 SetDescriptor set = (SetDescriptor) sets.next();
62 cr.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash = new SimpleHash();");
65 /* second pass build relationships between hashtables */
66 sets = state.stSets.descriptors();
68 while (sets.hasNext()) {
69 SetDescriptor set = (SetDescriptor) sets.next();
70 Iterator subsets = set.subsets();
72 while (subsets.hasNext()) {
73 SetDescriptor subset = (SetDescriptor) subsets.next();
74 cr.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
79 Iterator relations = state.stRelations.descriptors();
81 /* first pass create all the hash tables */
82 while (relations.hasNext()) {
83 RelationDescriptor relation = (RelationDescriptor) relations.next();
85 if (relation.testUsage(RelationDescriptor.IMAGE)) {
86 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash = new SimpleHash();");
89 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
90 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
98 private void generate_worklist() {
100 CodeWriter cr = new StandardCodeWriter(output);
101 cr.outputline("WORKLIST = new SimpleList();");
105 private void generate_teardown() {
107 CodeWriter cr = new StandardCodeWriter(output);
108 cr.outputline("WORKLIST->reset();");
109 cr.outputline("while (WORKLIST->hasMoreElements())");
111 cr.outputline("free ((WORKITEM *) WORKLIST->nextElement());");
113 cr.outputline("delete WORKLIST;");
117 private void generate_rules() {
119 /* first we must sort the rules */
120 Iterator allrules = state.vRules.iterator();
122 Vector emptyrules = new Vector(); // rules with no quantifiers
123 Vector worklistrules = new Vector(); // the rest of the rules
125 while (allrules.hasNext()) {
126 Rule rule = (Rule) allrules.next();
127 ListIterator quantifiers = rule.quantifiers();
129 boolean noquantifiers = true;
130 while (quantifiers.hasNext()) {
131 Quantifier quantifier = (Quantifier) quantifiers.next();
132 if (quantifier instanceof ForQuantifier) {
133 // ok, because integers exist already!
136 noquantifiers = false;
142 emptyrules.add(rule);
144 worklistrules.add(rule);
148 Iterator iterator_er = emptyrules.iterator();
149 while (iterator_er.hasNext()) {
151 Rule rule = (Rule) iterator_er.next();
154 final SymbolTable st = rule.getSymbolTable();
155 CodeWriter cr = new StandardCodeWriter(output) {
156 public SymbolTable getSymbolTable() { return st; }
159 cr.outputline("// build " + rule.getLabel());
162 ListIterator quantifiers = rule.quantifiers();
164 while (quantifiers.hasNext()) {
165 Quantifier quantifier = (Quantifier) quantifiers.next();
166 quantifier.generate_open(cr);
171 rule.getGuardExpr().prettyPrint(cr);
174 /* now we have to generate the guard test */
176 VarDescriptor guardval = VarDescriptor.makeNew();
177 rule.getGuardExpr().generate(cr, guardval);
179 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
182 /* now we have to generate the inclusion code */
183 rule.getInclusion().generate(cr);
186 while (quantifiers.hasPrevious()) {
187 Quantifier quantifier = (Quantifier) quantifiers.previous();
197 CodeWriter cr2 = new StandardCodeWriter(output);
199 cr2.outputline("WORKLIST->reset();");
200 cr2.outputline("while (WORKLIST->hasMoreElements())");
202 cr2.outputline("WORKITEM *wi = (WORKITEM *) WORKLIST->nextElement();");
204 String elseladder = "if";
206 Iterator iterator_rules = worklistrules.iterator();
207 while (iterator_rules.hasNext()) {
209 Rule rule = (Rule) iterator_rules.next();
210 int dispatchid = rule.getNum();
213 final SymbolTable st = rule.getSymbolTable();
214 CodeWriter cr = new StandardCodeWriter(output) {
215 public SymbolTable getSymbolTable() { return st; }
219 cr.outputline(elseladder + " (wi->id == " + dispatchid + ")");
222 cr.outputline("// build " + rule.getLabel());
224 ListIterator quantifiers = rule.quantifiers();
227 while (quantifiers.hasNext()) {
228 Quantifier quantifier = (Quantifier) quantifiers.next();
229 count = quantifier.generate_worklistload(cr, count );
234 rule.getGuardExpr().prettyPrint(cr);
237 /* now we have to generate the guard test */
239 VarDescriptor guardval = VarDescriptor.makeNew();
240 rule.getGuardExpr().generate(cr, guardval);
242 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
245 /* now we have to generate the inclusion code */
246 rule.getInclusion().generate(cr);
249 // close startblocks generated by DotExpr memory checks
250 //DotExpr.generate_memory_endblocks(cr);
252 cr.endblock(); // end else-if WORKLIST ladder
254 elseladder = "else if";
258 cr2.outputline("else");
260 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
261 cr2.outputline("exit(1);");
264 // end block created for worklist
269 private void generate_implicit_checks() {
273 CodeWriter cr = new StandardCodeWriter(output);
275 // #TBD#: these should be implicit checks added to the set of constraints
276 //output.println("check multiplicity");
279 private void generate_checks() {
281 /* do constraint checks */
282 Vector constraints = state.vConstraints;
284 for (int i = 0; i < constraints.size(); i++) {
286 Constraint constraint = (Constraint) constraints.elementAt(i);
290 final SymbolTable st = constraint.getSymbolTable();
292 CodeWriter cr = new StandardCodeWriter(output) {
293 public SymbolTable getSymbolTable() { return st; }
296 cr.outputline("// checking " + constraint.getLabel());
299 ListIterator quantifiers = constraint.quantifiers();
301 while (quantifiers.hasNext()) {
302 Quantifier quantifier = (Quantifier) quantifiers.next();
303 quantifier.generate_open(cr);
306 cr.outputline("int maybe = 0;");
308 /* now we have to generate the guard test */
310 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
311 constraint.getLogicStatement().generate(cr, constraintboolean);
313 cr.outputline("if (maybe)");
315 cr.outputline("__Success = 0;");
316 cr.outputline("printf(\"maybe fail " + (i+1) + ". \");");
317 cr.outputline("exit(1);");
320 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
323 cr.outputline("__Success = 0;");
324 cr.outputline("printf(\"fail " + (i+1) + ". \");");
325 cr.outputline("exit(1);");
328 while (quantifiers.hasPrevious()) {
329 Quantifier quantifier = (Quantifier) quantifiers.previous();
340 output.println("// if (__Success) { printf(\"all tests passed\"); }");