7 public class NaiveGenerator {
10 java.io.PrintWriter output = null;
12 public NaiveGenerator(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();
22 generate_implicit_checks();
27 private void generate_tokentable() {
29 CodeWriter cr = new CodeWriter() {
32 public void indent() { indent++; }
33 public void unindent() { indent--; assert indent >= 0; }
34 private void doindent() {
35 for (int i = 0; i < indent; i++) {
39 public void outputline(String s) {
43 public void output(String s) { throw new IRException(); }
44 public SymbolTable getSymbolTable() { throw new IRException(); }
47 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
50 cr.outputline("// Token values");
53 while (tokens.hasNext()) {
54 Object token = tokens.next();
55 cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());
62 private void generate_hashtables() {
64 CodeWriter cr = new CodeWriter() {
67 public void indent() { indent++; }
68 public void unindent() { indent--; assert indent >= 0; }
69 private void doindent() {
70 for (int i = 0; i < indent; i++) {
74 public void outputline(String s) {
78 public void output(String s) { throw new IRException(); }
79 public SymbolTable getSymbolTable() { throw new IRException(); }
82 cr.outputline("int __Success = 1;");
84 cr.outputline("// creating hashtables ");
86 /* build all the hashtables */
87 Hashtable hashtables = new Hashtable();
90 Iterator sets = state.stSets.descriptors();
92 /* first pass create all the hash tables */
93 while (sets.hasNext()) {
94 SetDescriptor set = (SetDescriptor) sets.next();
95 cr.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash = new SimpleHash();");
98 /* second pass build relationships between hashtables */
99 sets = state.stSets.descriptors();
101 while (sets.hasNext()) {
102 SetDescriptor set = (SetDescriptor) sets.next();
103 Iterator subsets = set.subsets();
105 while (subsets.hasNext()) {
106 SetDescriptor subset = (SetDescriptor) subsets.next();
107 cr.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
111 /* build relations */
112 Iterator relations = state.stRelations.descriptors();
114 /* first pass create all the hash tables */
115 while (relations.hasNext()) {
116 RelationDescriptor relation = (RelationDescriptor) relations.next();
117 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash = new SimpleHash();");
118 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
125 private void generate_rules() {
127 /* first we must sort the rules */
128 GraphNode.DFS.depthFirstSearch(state.rulenodes.values());
130 TreeSet topologicalsort = new TreeSet(new Comparator() {
131 public boolean equals(Object obj) { return false; }
132 public int compare(Object o1, Object o2) {
133 GraphNode g1 = (GraphNode) o1;
134 GraphNode g2 = (GraphNode) o2;
135 return g2.getFinishingTime() - g1.getFinishingTime();
139 topologicalsort.addAll(state.rulenodes.values());
141 /* build all the rules */
142 Iterator rules = topologicalsort.iterator();
144 while (rules.hasNext()) {
146 GraphNode rulenode = (GraphNode) rules.next();
147 Rule rule = (Rule) rulenode.getOwner();
151 final SymbolTable st = rule.getSymbolTable();
153 CodeWriter cr = new CodeWriter() {
154 boolean linestarted = false;
156 public void indent() { indent++; }
157 public void unindent() { indent--; assert indent >= 0; }
158 private void doindent() {
159 for (int i = 0; i < indent; i++) {
164 public void outputline(String s) {
171 public void output(String s) {
178 public SymbolTable getSymbolTable() { return st; }
181 cr.outputline("// build " + rule.getLabel());
185 ListIterator quantifiers = rule.quantifiers();
187 while (quantifiers.hasNext()) {
188 Quantifier quantifier = (Quantifier) quantifiers.next();
189 quantifier.generate_open(cr);
194 rule.getGuardExpr().prettyPrint(cr);
197 /* now we have to generate the guard test */
199 VarDescriptor guardval = VarDescriptor.makeNew();
200 rule.getGuardExpr().generate(cr, guardval);
202 cr.outputline("if (" + guardval.getSafeSymbol() + ") {");
206 /* now we have to generate the inclusion code */
207 rule.getInclusion().generate(cr);
213 while (quantifiers.hasPrevious()) {
214 Quantifier quantifier = (Quantifier) quantifiers.previous();
227 private void generate_implicit_checks() {
229 // #STOPPED# : stop... modify relationdescriptor with flag to symbolize that
230 // static analysis has determined the relation to be type safe... for sets that
231 // are not type safe do these tests in the
232 // relation inclusion...
236 // check to make sure all relations are well typed
237 // for all relations (need to check reverse as well i guess)
238 // make sure that each element is in the corresponding set for the domain and range
241 CodeWriter cr = new CodeWriter() {
242 boolean linestarted = false;
244 public void indent() { indent++; }
245 public void unindent() { indent--; assert indent >= 0; }
246 private void doindent() {
247 for (int i = 0; i < indent; i++) {
252 public void outputline(String s) {
259 public void output(String s) {
266 public SymbolTable getSymbolTable() { throw new IRException(); }
269 /* build relations */
270 Iterator relations = state.stRelations.descriptors();
272 /* first pass create all the hash tables */
273 while (relations.hasNext()) {
274 RelationDescriptor relation = (RelationDescriptor) relations.next();
276 if (relation.testUsage(RelationDescriptor.IMAGE)) {
277 VarDescriptor x = VarDescriptor.makeNew("x");
278 VarDescriptor y = VarDescriptor.makeNew("y");
280 /* start iteration */
281 cr.outputline("for (SimpleIterator* " + x.getSafeSymbol() + "_iterator = " + relation.getSafeSymbol() + "_hash->iterator(); " + x.getSafeSymbol() + "_iterator->hasNext(); ) {");
283 cr.outputline("int " + y.getSafeSymbol() + " = (" + x.getSafeSymbol() + "_iterator->next();");
284 cr.outputline("int " + x.getSafeSymbol() + " = (" + x.getSafeSymbol() + "_iterator->key();");
286 SetDescriptor domain = relation.getDomain();
287 SetDescriptor range = relation.getRange();
289 // #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
292 cr.outputline("if (!domain.get(x)) { remove x from hashtable, remove current iterator }");
294 } else if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
295 throw new IRException("unsupported");
305 //output.println("check multiplicity");
309 private void generate_checks() {
311 /* do constraint checks */
312 Vector constraints = state.vConstraints;
314 for (int i = 0; i < constraints.size(); i++) {
316 Constraint constraint = (Constraint) constraints.elementAt(i);
320 final SymbolTable st = constraint.getSymbolTable();
322 CodeWriter cr = new CodeWriter() {
323 boolean linestarted = false;
325 public void indent() { indent++; }
326 public void unindent() { indent--; assert indent >= 0; }
327 private void doindent() {
328 for (int i = 0; i < indent; i++) {
333 public void outputline(String s) {
340 public void output(String s) {
347 public SymbolTable getSymbolTable() { return st; }
350 cr.outputline("// checking " + constraint.getLabel());
354 ListIterator quantifiers = constraint.quantifiers();
356 while (quantifiers.hasNext()) {
357 Quantifier quantifier = (Quantifier) quantifiers.next();
358 quantifier.generate_open(cr);
361 /* now we have to generate the guard test */
363 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
364 constraint.getLogicStatement().generate(cr, constraintboolean);
366 cr.outputline("if (!" + constraintboolean.getSafeSymbol() + ") {");
370 cr.outputline("__Success = 0;");
371 cr.outputline("printf(\"fail. \");");
377 while (quantifiers.hasPrevious()) {
378 Quantifier quantifier = (Quantifier) quantifiers.previous();
391 output.println("if (__Success) { printf(\"all tests passed\"); }");