5e3652b044c7b45ced723b3541aa856349f46b57
[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         leftexpr=lexpr;
27         type=Updates.EXPR;
28         if (negate) {
29         /* remove negation through opcode translation */
30             if (op==Opcode.GT)
31                 op=Opcode.LE;
32             else if (op==Opcode.GE)
33                 op=Opcode.LT;
34             else if (op==Opcode.EQ)
35                 op=Opcode.NE;
36             else if (op==Opcode.NE)
37                 op=Opcode.EQ;
38             else if (op==Opcode.LT)
39                 op=Opcode.GE;
40             else if (op==Opcode.LE)
41                 op=Opcode.GT;
42         }
43         opcode=op;
44         rightexpr=rexpr;
45     }
46
47     boolean isGlobal() {
48         if (leftexpr instanceof VarExpr)
49             return true;
50         else return false;
51     }
52
53     VarDescriptor getVar() {
54         if (isGlobal()) {
55             return ((VarExpr)leftexpr).getVar();
56         } else if (isField()) {
57             Expr e=leftexpr;
58             for(;e instanceof DotExpr;e=((DotExpr)e).getExpr()) ;
59             return ((VarExpr)e).getVar();
60         } else {
61             System.out.println(toString());
62             throw new Error("Unrecognized Update");
63         }
64     }
65
66     Descriptor getDescriptor() {
67         if (isGlobal()) {
68             return ((VarExpr)leftexpr).getVar();
69         } else if (isField()) {
70             return ((DotExpr)leftexpr).getField();
71         } else {
72             System.out.println(toString());
73             throw new Error("Unrecognized Update");
74         }
75     }
76
77     boolean isField() {
78         if (leftexpr instanceof DotExpr) {
79             assert ((DotExpr)leftexpr).getIndex()==null;
80             return true;
81         } else
82             return false;
83     }
84     
85     boolean isExpr() {
86         return type==Updates.EXPR;
87     }
88
89     
90     Opcode getOpcode() {
91         return opcode;
92     }
93
94     public Updates(Expr lexpr, Expr rexpr) {
95         leftexpr=lexpr;
96         rightexpr=rexpr;
97         type=Updates.EXPR;
98         opcode=Opcode.EQ;
99     }
100
101     public Updates(Expr lexpr, int rpos) {
102         leftexpr=lexpr;
103         rightposition=rpos;
104         type=Updates.POSITION;
105         opcode=Opcode.EQ;
106     }
107
108     boolean isAbstract() {
109         return type==Updates.ABSTRACT;
110     }
111
112     public Updates(Expr lexpr,boolean negates) {
113         leftexpr=lexpr;
114         type=Updates.ABSTRACT;
115         negate=negates;
116         opcode=null;
117     }
118
119     public int getType() {
120         return type;
121     }
122     public Expr getLeftExpr() {
123         return leftexpr;
124     }
125     public int getRightPos() {
126         assert type==Updates.POSITION;
127         return rightposition;
128     }
129     public Expr getRightExpr() {
130         assert type==Updates.EXPR;
131         return rightexpr;
132     }
133 }