66b79f52141e0e1d3b578dbf012ae12eeeda7820
[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("for (SimpleIterator* " + x.getSafeSymbol() + "_iterator = " + relation.getSafeSymbol() + "_hash->iterator(); " + x.getSafeSymbol() + "_iterator->hasNext(); )");
37         writer.startblock();
38         writer.outputline(y.getType().getGenerateType() + " " + y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") " + x.getSafeSymbol() + "_iterator->next();");        
39         // #ATTN#: key is called second because next() forwards ptr and key does not! 
40         writer.outputline(x.getType().getGenerateType() + " " + x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") " + x.getSafeSymbol() + "_iterator->key();");
41     }
42
43     public void generate_open(CodeWriter writer, String type,int number, String left,String right) {
44         VarDescriptor tmp=VarDescriptor.makeNew("flag");
45         writer.outputline("SimpleIterator* " + x.getSafeSymbol() + "_iterator = " + relation.getSafeSymbol() + "_hash->iterator();");
46         writer.outputline("int "+tmp.getSafeSymbol()+"=0;");
47         writer.outputline("if ("+type+"=="+number+")");
48         writer.outputline(tmp.getSafeSymbol()+"=1;");
49         
50         writer.outputline("while("+tmp.getSafeSymbol()+"||(("+type+"!="+number+")&&"+x.getSafeSymbol() + "_iterator->hasNext()))");
51         writer.startblock();
52         writer.outputline(x.getType().getGenerateType() + " " + x.getSafeSymbol() + ";");
53         writer.outputline(y.getType().getGenerateType() + " " + y.getSafeSymbol() + ";");
54         writer.outputline("if ("+type+"=="+number+")");
55         writer.startblock();
56         writer.outputline(tmp.getSafeSymbol()+"=0;");
57         writer.outputline(x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") " + left + ";");
58         writer.outputline(y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") " + right + ";");
59         writer.endblock();
60         writer.outputline("else");
61         writer.startblock();
62         writer.outputline(y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") " + x.getSafeSymbol() + "_iterator->next();");        
63         writer.outputline(x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") " + x.getSafeSymbol() + "_iterator->key();");
64         writer.endblock();
65     }
66
67     public int generate_worklistload(CodeWriter writer, int offset) {        
68         String varx = x.getSafeSymbol();
69         String vary = y.getSafeSymbol();
70         writer.outputline("int " + varx + " = wi->word" + offset + "; // r1"); 
71         writer.outputline("int " + vary + " = wi->word" + (offset + 1) + "; //r2"); 
72         return offset + 2;       
73     }
74
75     public int generate_workliststore(CodeWriter writer, int offset) {        
76         String varx = x.getSafeSymbol();
77         String vary = y.getSafeSymbol();
78         writer.outputline("wi->word" + offset + " = " + varx + "; // r1");
79         writer.outputline("wi->word" + (offset+1) + " = " + vary + "; // r2");
80         return offset + 2;       
81     }
82
83
84 }