48959afc7e426a9fee455214ce83c0cef10fdf27
[repair.git] / Repair / RepairCompiler / MCC / IR / RelationQuantifier.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class RelationQuantifier extends Quantifier {
6
7     RelationDescriptor relation;
8     VarDescriptor x, y; // y = x.relation
9
10     public RelationQuantifier() {}
11
12     public void setRelation(RelationDescriptor rd) {
13         relation = rd; 
14     }
15
16     public RelationDescriptor getRelation() {
17         return relation;
18     }
19
20     public void setTuple(VarDescriptor x, VarDescriptor y) {
21         this.x = x;
22         this.y = y;
23     }
24
25     public Set getRequiredDescriptors() {
26         HashSet v = new HashSet();
27         v.add(relation);
28         return v;
29     }
30
31     public String toString() {
32         return "relation quantifier <" + x.getSymbol() + "," + y.getSymbol() + "> in " + relation.getSymbol();
33     }
34
35     public void generate_open(CodeWriter writer) {
36         writer.outputline("SimpleIterator "+x.getSafeSymbol()+"_iterator;");
37         writer.outputline("for ("+relation.getSafeSymbol() + "_hash->iterator("+x.getSafeSymbol()+"_iterator); " + x.getSafeSymbol() + "_iterator.hasNext(); )");
38         writer.startblock();
39         writer.outputline(y.getType().getGenerateType() + " " + y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") " + x.getSafeSymbol() + "_iterator.next();");        
40         // #ATTN#: key is called second because next() forwards ptr and key does not! 
41         writer.outputline(x.getType().getGenerateType() + " " + x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") " + x.getSafeSymbol() + "_iterator.key();");
42     }
43
44     public void generate_open(CodeWriter writer, String type,int number, String left,String right) {
45         VarDescriptor tmp=VarDescriptor.makeNew("flag");
46         writer.outputline("SimpleIterator* " + x.getSafeSymbol() + "_iterator = " + relation.getSafeSymbol() + "_hash->iterator();");
47         writer.outputline("int "+tmp.getSafeSymbol()+"=0;");
48         writer.outputline("if ("+type+"=="+number+")");
49         writer.outputline(tmp.getSafeSymbol()+"=1;");
50         
51         writer.outputline("while("+tmp.getSafeSymbol()+"||(("+type+"!="+number+")&&"+x.getSafeSymbol() + "_iterator->hasNext()))");
52         writer.startblock();
53         writer.outputline(x.getType().getGenerateType() + " " + x.getSafeSymbol() + ";");
54         writer.outputline(y.getType().getGenerateType() + " " + y.getSafeSymbol() + ";");
55         writer.outputline("if ("+type+"=="+number+")");
56         writer.startblock();
57         writer.outputline(tmp.getSafeSymbol()+"=0;");
58         writer.outputline(x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") " + left + ";");
59         writer.outputline(y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") " + right + ";");
60         writer.endblock();
61         writer.outputline("else");
62         writer.startblock();
63         writer.outputline(y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") " + x.getSafeSymbol() + "_iterator->next();");        
64         writer.outputline(x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") " + x.getSafeSymbol() + "_iterator->key();");
65         writer.endblock();
66     }
67
68     public int generate_worklistload(CodeWriter writer, int offset) {        
69         String varx = x.getSafeSymbol();
70         String vary = y.getSafeSymbol();
71         writer.outputline("int " + varx + " = wi->word" + offset + "; // r1"); 
72         writer.outputline("int " + vary + " = wi->word" + (offset + 1) + "; //r2"); 
73         return offset + 2;       
74     }
75
76     public int generate_workliststore(CodeWriter writer, int offset) {        
77         String varx = x.getSafeSymbol();
78         String vary = y.getSafeSymbol();
79         writer.outputline("wi->word" + offset + " = " + varx + "; // r1");
80         writer.outputline("wi->word" + (offset+1) + " = " + vary + "; // r2");
81         return offset + 2;       
82     }
83
84
85 }