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