5 public class OpExpr extends Expr {
11 public Expr getUpper() {
12 Expr lupper=left.getUpper();
16 Expr rupper=right.getUpper();
19 OpExpr oe=new OpExpr(this.opcode,lupper,rupper);
20 oe.td = ReservedTypeDescriptor.INT;
25 public Expr getLower() {
26 Expr llower=left.getLower();
30 Expr rlower=right.getLower();
33 OpExpr oe=new OpExpr(this.opcode,llower,rlower);
34 oe.td = ReservedTypeDescriptor.INT;
40 public boolean isSafe() {
43 return left.isSafe()&&right.isSafe();
46 public boolean isInvariant(Set vars) {
47 return left.isInvariant(vars)&&((right==null)||right.isInvariant(vars));
50 public Set findInvariants(Set vars) {
52 /* Don't hoist ints */
54 } else if (isInvariant(vars)) {
59 Set ls=left.findInvariants(vars);
61 ls.addAll(right.findInvariants(vars));
66 public Set getfunctions() {
67 Set leftfunctions=left.getfunctions();
68 Set rightfunctions=null;
69 if (right!=null) rightfunctions=right.getfunctions();
70 if (leftfunctions!=null&&rightfunctions!=null) {
71 HashSet functions=new HashSet();
72 functions.addAll(leftfunctions);
73 functions.addAll(rightfunctions);
76 if (leftfunctions!=null)
78 return rightfunctions;
81 public static boolean isInt(Expr e) {
84 if ((e instanceof IntegerLiteralExpr)||
85 ((e instanceof OpExpr)&&(((OpExpr)e).opcode==Opcode.NOP)&&(((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr)))
90 public static int getInt(Expr e) {
91 if (e instanceof IntegerLiteralExpr)
92 return ((IntegerLiteralExpr)e).getValue();
93 else if ((e instanceof OpExpr) && (((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr))
94 return ((IntegerLiteralExpr)((OpExpr)e).getLeftExpr()).getValue();
95 else throw new Error();
98 public OpExpr(Opcode opcode, Expr left, Expr right) {
99 if ((isInt(left)&&isInt(right))||
100 (isInt(left)&&(opcode==Opcode.NOT))||
101 (isInt(left)&&(opcode==Opcode.RND))) {
102 this.opcode=Opcode.NOP;
104 int lint=isInt(left)?getInt(left):0;
105 int rint=isInt(right)?getInt(right):0;
107 if (opcode==Opcode.ADD) {
109 } else if (opcode==Opcode.SUB) {
111 } else if (opcode==Opcode.SHL) {
113 } else if (opcode==Opcode.SHR) {
115 } else if (opcode==Opcode.MULT) {
117 } else if (opcode==Opcode.DIV) {
119 } else if (opcode==Opcode.GT) {
122 } else if (opcode==Opcode.GE) {
125 } else if (opcode==Opcode.LT) {
128 } else if (opcode==Opcode.LE) {
131 } else if (opcode==Opcode.EQ) {
134 } else if (opcode==Opcode.NE) {
137 } else if (opcode==Opcode.AND) {
138 if ((lint!=0)&&(rint!=0))
140 } else if (opcode==Opcode.OR) {
141 if ((lint!=0)||(rint!=0))
143 } else if (opcode==Opcode.NOT) {
146 } else if (opcode==Opcode.RND) {
147 value=((lint>>3)<<3);
150 } else throw new Error("Unrecognized Opcode");
151 this.left=new IntegerLiteralExpr(value);
152 } else if ((opcode==Opcode.MULT)&&
153 ((isInt(left)&&(getInt(left)==0))
154 ||(isInt(right)&&(getInt(right)==0)))) {
155 this.opcode=Opcode.NOP;
157 this.left=new IntegerLiteralExpr(0);
159 this.opcode = opcode;
162 assert (right == null && (opcode == Opcode.NOT||opcode==Opcode.RND)) || (right != null);
166 public Expr getRightExpr() {
170 public Expr getLeftExpr() {
174 public Set freeVars() {
175 Set lset=left.freeVars();
178 rset=right.freeVars();
186 public String name() {
187 if (opcode==Opcode.NOT)
188 return "!("+left.name()+")";
189 if (opcode==Opcode.NOP)
191 if (opcode==Opcode.RND)
192 return "Round("+left.name()+")";
193 String name=left.name()+opcode.toString();
201 public Opcode getOpcode() {
205 public boolean equals(Map remap, Expr e) {
206 if (e==null||!(e instanceof OpExpr))
209 if (opcode!=oe.opcode)
211 if (!left.equals(remap,oe.left))
213 if ((opcode!=Opcode.NOT)&&(opcode!=Opcode.RND)&&(opcode!=Opcode.NOP))
214 if (!right.equals(remap,oe.right))
219 public DNFRule constructDNF() {
220 if (opcode==Opcode.AND) {
221 DNFRule leftd=left.constructDNF();
222 DNFRule rightd=right.constructDNF();
223 return leftd.and(rightd);
224 } else if (opcode==Opcode.OR) {
225 DNFRule leftd=left.constructDNF();
226 DNFRule rightd=right.constructDNF();
227 return leftd.or(rightd);
228 } else if (opcode==Opcode.NOT) {
229 DNFRule leftd=left.constructDNF();
231 } else return new DNFRule(this);
234 public boolean usesDescriptor(Descriptor d) {
235 return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
238 public void findmatch(Descriptor d, Set s) {
241 right.findmatch(d,s);
244 public Set useDescriptor(Descriptor d) {
245 HashSet newset=new HashSet();
246 newset.addAll(left.useDescriptor(d));
248 newset.addAll(right.useDescriptor(d));
252 public int[] getRepairs(boolean negated, Termination t) {
253 if (left instanceof RelationExpr)
254 return new int[] {AbstractRepair.MODIFYRELATION};
255 if (left instanceof SizeofExpr) {
258 /* remove negation through opcode translation */
261 else if (op==Opcode.GE)
263 else if (op==Opcode.EQ)
265 else if (op==Opcode.NE)
267 else if (op==Opcode.LT)
269 else if (op==Opcode.LE)
273 int maxsize=t.maxsize.getsize(getDescriptor());
274 int size=getInt(right);
277 boolean isRelation=((SizeofExpr)left).setexpr instanceof ImageSetExpr;
281 return new int[] {AbstractRepair.REMOVEFROMRELATION};
283 if ((maxsize!=-1)&&maxsize<=size)
284 return new int[] {AbstractRepair.ADDTORELATION};
285 return new int[] {AbstractRepair.ADDTORELATION,
286 AbstractRepair.REMOVEFROMRELATION};
288 } else if (op==Opcode.GE||op==Opcode.GT) {
289 return new int[]{AbstractRepair.ADDTORELATION};
290 } else if (op==Opcode.LE||op==Opcode.LT) {
291 if ((op==Opcode.LT&&maxsize!=-1&&maxsize<size)||(op==Opcode.LE&&maxsize!=-1&&maxsize<=size))
293 return new int[]{AbstractRepair.REMOVEFROMRELATION};
294 } else if (op==Opcode.NE) {
295 if (maxsize<size&&maxsize!=-1)
297 return new int[]{AbstractRepair.ADDTORELATION};
298 } else throw new Error();
302 return new int[] {AbstractRepair.REMOVEFROMSET};
304 if (maxsize<=size&&maxsize!=-1)
305 return new int[] {AbstractRepair.ADDTOSET};
306 return new int[] {AbstractRepair.ADDTOSET,
307 AbstractRepair.REMOVEFROMSET};
309 } else if (op==Opcode.GE||op==Opcode.GT) {
310 return new int[] {AbstractRepair.ADDTOSET};
311 } else if (op==Opcode.LE||op==Opcode.LT) {
312 if ((op==Opcode.LT&&maxsize<size&&maxsize!=-1)||(op==Opcode.LE&&maxsize<=size&&maxsize!=-1))
314 return new int[] {AbstractRepair.REMOVEFROMSET};
315 } else if (op==Opcode.NE) {
316 if (maxsize<size&&maxsize!=-1)
318 return new int[] {AbstractRepair.ADDTOSET};
319 } else throw new Error();
322 throw new Error("BAD");
325 public Descriptor getDescriptor() {
326 return left.getDescriptor();
329 public boolean inverted() {
330 return left.inverted();
333 public Set getInversedRelations() {
334 Set set = left.getInversedRelations();
336 set.addAll(right.getInversedRelations());
341 public Set getRequiredDescriptors() {
342 Set v = left.getRequiredDescriptors();
345 v.addAll(right.getRequiredDescriptors());
351 public void generate(CodeWriter writer, VarDescriptor dest) {
352 VarDescriptor ld = VarDescriptor.makeNew("leftop");
353 /* Check for loop invariant hoisting. */
354 if (writer.getInvariantValue()!=null&&
355 writer.getInvariantValue().isInvariant(this)) {
356 writer.addDeclaration("int",dest.getSafeSymbol());
357 writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
358 writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
362 left.generate(writer, ld);
363 VarDescriptor rd = null;
364 VarDescriptor lm=VarDescriptor.makeNew("lm");
365 VarDescriptor rm=VarDescriptor.makeNew("rm");
368 if ((opcode==Opcode.OR)||
369 (opcode==Opcode.AND)) {
370 writer.addDeclaration("int",lm.getSafeSymbol());
371 writer.outputline(lm.getSafeSymbol()+"=maybe;");
372 writer.outputline("maybe=0;");
375 rd = VarDescriptor.makeNew("rightop");
376 right.generate(writer, rd);
380 if (opcode == Opcode.RND) {
381 writer.addDeclaration("int",dest.getSafeSymbol());
382 writer.outputline(dest.getSafeSymbol() + " = (" +
383 ld.getSafeSymbol() + ">>3)<<3; ");
384 writer.outputline("if ("+ld.getSafeSymbol()+" % 8) "+dest.getSafeSymbol()+"+=8;");
385 } else if (opcode == Opcode.NOP) {
386 writer.addDeclaration("int", dest.getSafeSymbol());
387 writer.outputline(dest.getSafeSymbol() + " = " +
388 ld.getSafeSymbol() +"; ");
389 } else if (opcode == Opcode.AND) {
390 writer.addDeclaration("int",rm.getSafeSymbol());
391 writer.outputline(rm.getSafeSymbol()+"=maybe;");
392 writer.outputline("maybe = (" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
393 writer.addDeclaration("int",dest.getSafeSymbol());
394 writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";");
395 } else if (opcode == Opcode.OR) {
396 writer.addDeclaration("int",rm.getSafeSymbol());
397 writer.outputline(rm.getSafeSymbol()+"=maybe;");
398 writer.outputline("maybe = (!" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (!" + rd.getSafeSymbol() +
399 " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
400 writer.addDeclaration("int",dest.getSafeSymbol());
401 writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() + ";");
402 } else if (opcode != Opcode.NOT) { /* two operands */
404 writer.addDeclaration("int", dest.getSafeSymbol());
405 writer.outputline(dest.getSafeSymbol() + " = " +
406 ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
407 } else if (opcode == Opcode.NOT) {
408 writer.addDeclaration("int", dest.getSafeSymbol());
409 writer.outputline(dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
413 public void prettyPrint(PrettyPrinter pp) {
415 if (opcode == Opcode.NOT) {
417 left.prettyPrint(pp);
419 } else if (opcode == Opcode.NOP) {
420 left.prettyPrint(pp);
421 } else if (opcode == Opcode.RND) {
423 left.prettyPrint(pp);
425 left.prettyPrint(pp);
426 pp.output(" " + opcode.toString() + " ");
427 assert right != null;
428 right.prettyPrint(pp);
433 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
434 TypeDescriptor lt = left.typecheck(sa);
435 TypeDescriptor rt = right == null ? null : right.typecheck(sa);
439 } else if (right != null && rt == null) {
449 this.td = ReservedTypeDescriptor.INT;