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