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