Adding changes to cvs...
[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         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             rightexpr=rexpr;
59             opcode=Opcode.NE;
60         }
61     }
62
63     boolean isGlobal() {
64         if (leftexpr instanceof VarExpr)
65             return true;
66         else return false;
67     }
68
69
70
71     Descriptor getDescriptor() {
72         if (isGlobal()) {
73             return ((VarExpr)leftexpr).getVar();
74         } else if (isField()) {
75             return ((DotExpr)leftexpr).getField();
76         } else {
77             System.out.println(toString());
78             throw new Error("Unrecognized Update");
79         }
80     }
81
82     boolean isField() {
83         if (leftexpr instanceof DotExpr) {
84             assert ((DotExpr)leftexpr).getIndex()==null;
85             return true;
86         } else
87             return false;
88     }
89     
90     
91     Opcode getOpcode() {
92         return opcode;
93     }
94
95     public Updates(Expr lexpr, Expr rexpr) {
96         leftexpr=lexpr;
97         rightexpr=rexpr;
98         type=Updates.EXPR;
99         opcode=Opcode.EQ;
100     }
101
102     public Updates(Expr lexpr, int rpos) {
103         leftexpr=lexpr;
104         rightposition=rpos;
105         type=Updates.POSITION;
106         opcode=Opcode.EQ;
107     }
108
109     boolean isAbstract() {
110         return type==Updates.ABSTRACT;
111     }
112
113     public Updates(Expr lexpr,boolean negates) {
114         leftexpr=lexpr;
115         type=Updates.ABSTRACT;
116         negate=negates;
117         opcode=null;
118     }
119
120     public int getType() {
121         return type;
122     }
123     public Expr getLeftExpr() {
124         return leftexpr;
125     }
126     public int getRightPos() {
127         assert type==Updates.POSITION;
128         return rightposition;
129     }
130     public Expr getRightExpr() {
131         assert type==Updates.EXPR;
132         return rightexpr;
133     }
134 }