a4e6d801654d56d2e54de76a00659671342909bc
[repair.git] / Repair / RepairCompiler / MCC / IR / Updates.java
1 package MCC.IR;
2
3 class Updates {
4     static public final int EXPR=0;
5     static public final int POSITION=1;
6     static public final int ABSTRACT=2;
7     int type=-1;
8     int rightposition;
9     Expr rightexpr;
10     Expr leftexpr;
11     Opcode opcode;
12     boolean negate=false;
13
14     public String toString() {
15         if (type==EXPR)
16             return leftexpr.name()+opcode.toString()+rightexpr.name();
17         else if (type==POSITION)
18             return leftexpr.name()+opcode.toString()+"Position("+String.valueOf(rightposition)+")";
19         else if (type==ABSTRACT) {
20             if (negate) return "!"+leftexpr.name();
21             else return leftexpr.name();
22         } else throw new Error("Unrecognized type");
23     }
24
25     public Updates(Expr lexpr, Expr rexpr, Opcode op, boolean negate) {
26         if (!lexpr.isValue())
27             System.out.println("Building invalid update");
28         leftexpr=lexpr;
29         type=Updates.EXPR;
30         if (negate) {
31         /* remove negation through opcode translation */
32             if (op==Opcode.GT)
33                 op=Opcode.LE;
34             else if (op==Opcode.GE)
35                 op=Opcode.LT;
36             else if (op==Opcode.EQ)
37                 op=Opcode.NE;
38             else if (op==Opcode.NE)
39                 op=Opcode.EQ;
40             else if (op==Opcode.LT)
41                 op=Opcode.GE;
42             else if (op==Opcode.LE)
43                 op=Opcode.GT;
44         }
45         opcode=op;
46         rightexpr=rexpr;
47     }
48
49     boolean isGlobal() {
50         if (leftexpr instanceof VarExpr)
51             return true;
52         else return false;
53     }
54
55     VarDescriptor getVar() {
56         if (isGlobal()) {
57             return ((VarExpr)leftexpr).getVar();
58         } else if (isField()) {
59             Expr e=leftexpr;
60             for(;e instanceof DotExpr;e=((DotExpr)e).getExpr()) ;
61             return ((VarExpr)e).getVar();
62         } else {
63             System.out.println(toString());
64             throw new Error("Unrecognized Update");
65         }
66     }
67
68     Descriptor getDescriptor() {
69         if (isGlobal()) {
70             return ((VarExpr)leftexpr).getVar();
71         } else if (isField()) {
72             return ((DotExpr)leftexpr).getField();
73         } else {
74             System.out.println(toString());
75             throw new Error("Unrecognized Update");
76         }
77     }
78
79     boolean isField() {
80         if (leftexpr instanceof DotExpr) {
81             assert ((DotExpr)leftexpr).getIndex()==null;
82             return true;
83         } else
84             return false;
85     }
86     
87     boolean isExpr() {
88         return type==Updates.EXPR;
89     }
90
91     
92     Opcode getOpcode() {
93         return opcode;
94     }
95
96     public Updates(Expr lexpr, Expr rexpr) {
97         if (!lexpr.isValue())
98             System.out.println("Building invalid update");
99         leftexpr=lexpr;
100         rightexpr=rexpr;
101         type=Updates.EXPR;
102         opcode=Opcode.EQ;
103     }
104
105     public Updates(Expr lexpr, int rpos) {
106         if (!lexpr.isValue())
107             System.out.println("Building invalid update");
108         leftexpr=lexpr;
109         rightposition=rpos;
110         type=Updates.POSITION;
111         opcode=Opcode.EQ;
112     }
113
114     boolean isAbstract() {
115         return type==Updates.ABSTRACT;
116     }
117
118     public Updates(Expr lexpr,boolean negates) {
119         leftexpr=lexpr;
120         type=Updates.ABSTRACT;
121         negate=negates;
122         opcode=null;
123     }
124
125     public int getType() {
126         return type;
127     }
128     public Expr getLeftExpr() {
129         return leftexpr;
130     }
131     public int getRightPos() {
132         assert type==Updates.POSITION;
133         return rightposition;
134     }
135     public Expr getRightExpr() {
136         assert type==Updates.EXPR;
137         return rightexpr;
138     }
139 }