Fixed lot of random bugs. Added code generate strings for expr's.
[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;
13
14     public String toString() {
15         String st="type="+type+"\n";
16         st+="rightposition="+rightposition+"\n";
17         if (rightexpr!=null)
18             st+="rightexpr="+rightexpr.name()+"\n";
19         if (leftexpr!=null)
20             st+="leftexpr="+leftexpr.name()+"\n";
21         st+="opcode="+opcode+"\n";
22         st+="negate="+negate+"\n";
23         return st;
24     }
25
26     public Updates(Expr lexpr, Expr rexpr, Opcode op, boolean negate) {
27         leftexpr=lexpr;
28         type=Updates.EXPR;
29         if (negate) {
30         /* remove negation through opcode translation */
31             if (op==Opcode.GT)
32                 op=Opcode.LE;
33             else if (op==Opcode.GE)
34                 op=Opcode.LT;
35             else if (op==Opcode.EQ)
36                 op=Opcode.NE;
37             else if (op==Opcode.NE)
38                 op=Opcode.EQ;
39             else if (op==Opcode.LT)
40                 op=Opcode.GE;
41             else if (op==Opcode.LE)
42                 op=Opcode.GT;
43         }
44
45         opcode=Opcode.EQ;
46         /* Get rid of everything but NE */
47         if (op==Opcode.GT) {
48             rightexpr=new OpExpr(Opcode.ADD,rexpr,new IntegerLiteralExpr(1));
49         } else if (op==Opcode.GE) {
50             rightexpr=rexpr;
51         } else if (op==Opcode.LT) {
52             rightexpr=new OpExpr(Opcode.SUB,rexpr,new IntegerLiteralExpr(1));
53         } else if (op==Opcode.LE) {
54             rightexpr=rexpr;
55         } else if (op==Opcode.EQ) {
56             rightexpr=rexpr;
57         } else if (op==Opcode.NE) {
58             opcode=Opcode.NE;
59         }
60     }
61
62     boolean isGlobal() {
63         if (leftexpr instanceof VarExpr)
64             return true;
65         else return false;
66     }
67
68
69
70     Descriptor getDescriptor() {
71         if (isGlobal()) {
72             return ((VarExpr)leftexpr).getVar();
73         } else if (isField()) {
74             return ((DotExpr)leftexpr).getField();
75         } else {
76             System.out.println(toString());
77             throw new Error("Unrecognized Update");
78         }
79     }
80
81     boolean isField() {
82         if (leftexpr instanceof DotExpr) {
83             assert ((DotExpr)leftexpr).getIndex()==null;
84             return true;
85         } else
86             return false;
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=null;
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 }