1) Added useDescriptor method to Expr's.
[repair.git] / Repair / RepairCompiler / MCC / IR / OpExpr.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class OpExpr extends Expr {
6
7     Expr left;
8     Expr right;
9     Opcode opcode;
10
11     public boolean isSafe() {
12         if (right==null)
13             return left.isSafe();
14         return left.isSafe()&&right.isSafe();
15     }
16
17     public Set getfunctions() {
18         Set leftfunctions=left.getfunctions();
19         Set rightfunctions=right.getfunctions();
20         if (leftfunctions!=null&&rightfunctions!=null) {
21             HashSet functions=new HashSet();
22             functions.addAll(leftfunctions);
23             functions.addAll(rightfunctions);
24             return functions;
25         }
26         if (leftfunctions!=null)
27             return leftfunctions;
28         return rightfunctions;
29     }
30
31     public void findmatch(Descriptor d, Set  s) {
32         left.findmatch(d,s);
33         if (right!=null)
34             right.findmatch(d,s);
35     }
36
37     public static boolean isInt(Expr e) {
38         if (e==null)
39             return false;
40         if ((e instanceof IntegerLiteralExpr)||
41             ((e instanceof OpExpr)&&(((OpExpr)e).opcode==Opcode.NOP)&&(((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr)))
42             return true;
43         return false;
44     }
45
46     public static int getInt(Expr e) {
47         if (e instanceof IntegerLiteralExpr)
48             return ((IntegerLiteralExpr)e).getValue();
49         else if ((e instanceof OpExpr) && (((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr))
50             return ((IntegerLiteralExpr)((OpExpr)e).getLeftExpr()).getValue();
51         else throw new Error();
52     }
53
54     public OpExpr(Opcode opcode, Expr left, Expr right) {
55         if ((isInt(left)&&isInt(right))||
56             (isInt(left)&&(opcode==Opcode.NOT))||
57             (isInt(left)&&(opcode==Opcode.RND))) {
58             this.opcode=Opcode.NOP;
59             this.right=null;
60             int lint=isInt(left)?getInt(left):0;
61             int rint=isInt(right)?getInt(right):0;
62             int value=0;
63             if (opcode==Opcode.ADD) {
64                 value=lint+rint;
65             } else if (opcode==Opcode.SUB) {
66                 value=lint-rint;
67             } else if (opcode==Opcode.SHL) {
68                 value=lint<<rint;
69             } else if (opcode==Opcode.SHR) {
70                 value=lint>>rint;
71             } else if (opcode==Opcode.MULT) {
72                 value=lint*rint;
73             } else if (opcode==Opcode.DIV) {
74                 value=lint/rint;
75             } else if (opcode==Opcode.GT) {
76                 if (lint>rint)
77                     value=1;
78             } else if (opcode==Opcode.GE) {
79                 if (lint>=rint)
80                     value=1;
81             } else if (opcode==Opcode.LT) {
82                 if (lint<rint)
83                     value=1;
84             } else if (opcode==Opcode.LE) {
85                 if (lint<=rint)
86                     value=1;
87             } else if (opcode==Opcode.EQ) {
88                 if (lint==rint)
89                     value=1;
90             } else if (opcode==Opcode.NE) {
91                 if (lint!=rint)
92                     value=1;
93             } else if (opcode==Opcode.AND) {
94                 if ((lint!=0)&&(rint!=0))
95                     value=1;
96             } else if (opcode==Opcode.OR) {
97                 if ((lint!=0)||(rint!=0))
98                     value=1;
99             } else if (opcode==Opcode.NOT) {
100                 if (lint==0)
101                     value=1;
102             } else if (opcode==Opcode.RND) {
103                 value=((lint>>3)<<3);
104                 if ((lint % 8)!=0)
105                     value+=8;
106             } else throw new Error("Unrecognized Opcode");
107             this.left=new IntegerLiteralExpr(value);
108         } else if ((opcode==Opcode.MULT)&&
109                    ((isInt(left)&&(getInt(left)==0))
110                     ||(isInt(right)&&(getInt(right)==0)))) {
111             this.opcode=Opcode.NOP;
112             this.right=null;
113             this.left=new IntegerLiteralExpr(0);
114         } else {
115             this.opcode = opcode;
116             this.left = left;
117             this.right = right;
118             assert (right == null && (opcode == Opcode.NOT||opcode==Opcode.RND)) || (right != null);
119         }
120     }
121
122     public Expr getRightExpr() {
123         return right;
124     }
125
126     public Expr getLeftExpr() {
127         return left;
128     }
129
130     public Set freeVars() {
131         Set lset=left.freeVars();
132         Set rset=null;
133         if (right!=null)
134             rset=right.freeVars();
135         if (lset==null)
136             return rset;
137         if (rset!=null)
138             lset.addAll(rset);
139         return lset;
140     }
141
142     public String name() {
143         if (opcode==Opcode.NOT)
144             return "!("+left.name()+")";
145         if (opcode==Opcode.NOP)
146             return left.name();
147         if (opcode==Opcode.RND)
148             return "Round("+left.name()+")";
149         String name=left.name()+opcode.toString();
150         if (right!=null)
151             name+=right.name();
152         return name;
153     }
154
155     public Opcode getOpcode() {
156         return opcode;
157     }
158
159
160
161
162     public boolean equals(Map remap, Expr e) {
163         if (e==null||!(e instanceof OpExpr))
164             return false;
165         OpExpr oe=(OpExpr)e;
166         if (opcode!=oe.opcode)
167             return false;
168         if (!left.equals(remap,oe.left))
169             return false;
170         if ((opcode!=Opcode.NOT)&&(opcode!=Opcode.RND)&&(opcode!=Opcode.NOP))
171             if (!right.equals(remap,oe.right))
172                 return false;
173         return true;
174     }
175
176     public DNFRule constructDNF() {
177         if (opcode==Opcode.AND) {
178             DNFRule leftd=left.constructDNF();
179             DNFRule rightd=right.constructDNF();
180             return leftd.and(rightd);
181         } else if (opcode==Opcode.OR) {
182             DNFRule leftd=left.constructDNF();
183             DNFRule rightd=right.constructDNF();
184             return leftd.or(rightd);
185         } else if (opcode==Opcode.NOT) {
186             DNFRule leftd=left.constructDNF();
187             return leftd.not();
188         } else return new DNFRule(this);
189     }
190
191     public boolean usesDescriptor(Descriptor d) {
192         return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
193     }
194
195     public Set useDescriptor(Descriptor d) {
196         HashSet newset=new HashSet();
197         newset.addAll(left.useDescriptor(d));
198         if (right!=null)
199             newset.addAll(right.useDescriptor(d));
200         return newset;
201     }
202     
203     public int[] getRepairs(boolean negated, Termination t) {
204         if (left instanceof RelationExpr)
205             return new int[] {AbstractRepair.MODIFYRELATION};
206         if (left instanceof SizeofExpr) {
207             Opcode op=opcode;
208             if (negated) {
209                 /* remove negation through opcode translation */
210                 if (op==Opcode.GT)
211                     op=Opcode.LE;
212                 else if (op==Opcode.GE)
213                     op=Opcode.LT;
214                 else if (op==Opcode.EQ)
215                     op=Opcode.NE;
216                 else if (op==Opcode.NE)
217                     op=Opcode.EQ;
218                 else if (op==Opcode.LT)
219                     op=Opcode.GE;
220                 else if (op==Opcode.LE)
221                     op=Opcode.GT;
222             }
223
224             int maxsize=t.maxsize.getsize(getDescriptor());
225             int size=getInt(right);
226
227
228             boolean isRelation=((SizeofExpr)left).setexpr instanceof ImageSetExpr;
229             if (isRelation) {
230                 if (op==Opcode.EQ) {
231                     if (size==0)
232                         return new int[] {AbstractRepair.REMOVEFROMRELATION};
233                     else {
234                         if ((maxsize!=-1)&&maxsize<=size)
235                             return new int[] {AbstractRepair.ADDTORELATION};
236                         return new int[] {AbstractRepair.ADDTORELATION,
237                                           AbstractRepair.REMOVEFROMRELATION};
238                     }
239                 } else if (op==Opcode.GE||op==Opcode.GT) {
240                     return new int[]{AbstractRepair.ADDTORELATION}; 
241                 } else if (op==Opcode.LE||op==Opcode.LT) {
242                     if ((op==Opcode.LT&&maxsize!=-1&&maxsize<size)||(op==Opcode.LE&&maxsize!=-1&&maxsize<=size))
243                         return new int[0];
244                     return new int[]{AbstractRepair.REMOVEFROMRELATION};
245                 } else if (op==Opcode.NE) {
246                     if (maxsize<size&&maxsize!=-1)
247                         return new int[0];
248                     return new int[]{AbstractRepair.ADDTORELATION};
249                 } else throw new Error();
250             } else {
251                 if (op==Opcode.EQ) {
252                     if (size==0)
253                         return new int[] {AbstractRepair.REMOVEFROMSET};                        
254                     else {
255                         if (maxsize<=size&&maxsize!=-1)
256                             return new int[] {AbstractRepair.ADDTOSET};
257                         return new int[] {AbstractRepair.ADDTOSET,
258                                               AbstractRepair.REMOVEFROMSET};
259                     }
260                 } else if (op==Opcode.GE||op==Opcode.GT) {
261                     return new int[] {AbstractRepair.ADDTOSET}; 
262                 } else if (op==Opcode.LE||op==Opcode.LT) {
263                     if ((op==Opcode.LT&&maxsize<size&&maxsize!=-1)||(op==Opcode.LE&&maxsize<=size&&maxsize!=-1))
264                         return new int[0];
265                     return new int[] {AbstractRepair.REMOVEFROMSET};
266                 } else if (op==Opcode.NE) {
267                     if (maxsize<size&&maxsize!=-1)
268                         return new int[0];
269                     return new int[] {AbstractRepair.ADDTOSET};
270                 } else throw new Error();
271             }
272         }
273         throw new Error("BAD");
274     }
275     
276     public Descriptor getDescriptor() {
277         return left.getDescriptor();
278     }
279
280     public boolean inverted() {
281         return left.inverted();
282     }
283
284     public Set getInversedRelations() {
285         Set set = left.getInversedRelations();
286         if (right != null) {
287             set.addAll(right.getInversedRelations());
288         }
289         return set;
290     }
291
292     public Set getRequiredDescriptors() {
293         Set v = left.getRequiredDescriptors();
294      
295         if (right != null) {
296             v.addAll(right.getRequiredDescriptors());
297         }
298
299         return v;
300     }   
301
302     public void generate(CodeWriter writer, VarDescriptor dest) {
303         VarDescriptor ld = VarDescriptor.makeNew("leftop");
304         left.generate(writer, ld);        
305         VarDescriptor rd = null;
306         VarDescriptor lm=VarDescriptor.makeNew("lm");
307         VarDescriptor rm=VarDescriptor.makeNew("rm");
308
309         if (right != null) {
310             if ((opcode==Opcode.OR)||
311                 (opcode==Opcode.AND)) {
312                 writer.outputline("int "+lm.getSafeSymbol()+"=maybe;");
313                 writer.outputline("int maybe=0;");
314             }
315
316             rd = VarDescriptor.makeNew("rightop");
317             right.generate(writer, rd);
318         }
319
320         String code;
321         if (opcode == Opcode.RND) {
322             writer.outputline("int " +dest.getSafeSymbol() + " = (" +
323                               ld.getSafeSymbol() + ">>3)<<3; ");
324             writer.outputline("if ("+ld.getSafeSymbol()+" % 8) "+dest.getSafeSymbol()+"+=8;");
325         } else if (opcode == Opcode.NOP) {
326             writer.outputline("int " +dest.getSafeSymbol() + " = " +
327                               ld.getSafeSymbol() +"; ");
328         } else if (opcode == Opcode.AND) {
329             writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
330             writer.outputline("maybe = (" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
331             writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";");
332         } else if (opcode == Opcode.OR) {
333             writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
334             writer.outputline("maybe = (!" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (!" + rd.getSafeSymbol() +
335                               " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
336             writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() +
337                               ";");
338         } else if (opcode != Opcode.NOT) { /* two operands */
339             assert rd != null;
340             writer.outputline("int " + dest.getSafeSymbol() + " = " + 
341                               ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
342         } else if (opcode == Opcode.NOT) {
343             writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
344         }
345     }
346
347     public void prettyPrint(PrettyPrinter pp) {
348         pp.output("(");
349         if (opcode == Opcode.NOT) {
350             pp.output("!");
351             left.prettyPrint(pp);
352         } else if (opcode == Opcode.NOP) {
353             left.prettyPrint(pp);
354         } else if (opcode == Opcode.RND) {
355             pp.output("RND ");
356             left.prettyPrint(pp);
357         } else {           
358             left.prettyPrint(pp);
359             pp.output(" " + opcode.toString() + " ");
360             assert right != null;
361             right.prettyPrint(pp);
362         }
363         pp.output(")");
364     }
365
366     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
367         TypeDescriptor lt = left.typecheck(sa);
368         TypeDescriptor rt = right == null ? null : right.typecheck(sa);
369
370         if (lt == null) {
371             return null;
372         } else if (right != null && rt == null) {
373             return null;
374         }
375
376         boolean ok = true;
377
378         // #ATTN#: if we want node.next != literal(0) to represent a null check than we need to allow ptr arithmetic
379         // either that or we use a isvalid clause to check for null
380
381         /*
382         if (lt != ReservedTypeDescriptor.INT) {
383             sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
384             ok = false;
385         }
386
387         if (right != null) {
388             if (rt != ReservedTypeDescriptor.INT) {
389                 sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
390                 ok = false;
391             }
392         }
393         */
394
395         if (!ok) {
396             return null;
397         }
398
399         this.td = ReservedTypeDescriptor.INT;
400         return this.td;
401     }
402
403 }